Author: bdube Date: Sun May 1 19:04:46 2011 New Revision: 1098386 URL: http://svn.apache.org/viewvc?rev=1098386&view=rev Log: Add a crude log viewer servlet and register at /logs
The same information is also displayed in a nicer format by the web console at /system/console/logs. This just makes the information available without the console. Added: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build.xml (with props) forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/logServlet.bnd forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java (with props) forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java (with props) Modified: forrest/trunk/.gitignore forrest/trunk/whiteboard/forrest-osgi/build.xml Modified: forrest/trunk/.gitignore URL: http://svn.apache.org/viewvc/forrest/trunk/.gitignore?rev=1098386&r1=1098385&r2=1098386&view=diff ============================================================================== --- forrest/trunk/.gitignore (original) +++ forrest/trunk/.gitignore Sun May 1 19:04:46 2011 @@ -157,6 +157,9 @@ # /whiteboard/forrest-osgi/org.apache.forrest.log/ /whiteboard/forrest-osgi/org.apache.forrest.log/build +# /whiteboard/forrest-osgi/org.apache.forrest.log.servlet/ +/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build + # /whiteboard/forrest-osgi/org.apache.forrest.log4j.conf/ /whiteboard/forrest-osgi/org.apache.forrest.log4j.conf/build Modified: forrest/trunk/whiteboard/forrest-osgi/build.xml URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest-osgi/build.xml?rev=1098386&r1=1098385&r2=1098386&view=diff ============================================================================== --- forrest/trunk/whiteboard/forrest-osgi/build.xml (original) +++ forrest/trunk/whiteboard/forrest-osgi/build.xml Sun May 1 19:04:46 2011 @@ -57,6 +57,8 @@ <ant dir="org.apache.forrest.log" /> + <ant dir="org.apache.forrest.log.servlet" /> + <ant dir="org.apache.forrest.log4j.conf" /> <ant dir="org.apache.forrest.plugin.api" /> @@ -88,6 +90,8 @@ <ant dir="org.apache.forrest.log" target="clean" /> + <ant dir="org.apache.forrest.log.servlet" /> + <ant dir="org.apache.forrest.log4j.conf" target="clean" /> <ant dir="org.apache.forrest.plugin.api" target="clean" /> Added: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build.xml URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build.xml?rev=1098386&view=auto ============================================================================== --- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build.xml (added) +++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build.xml Sun May 1 19:04:46 2011 @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project name="Apache Forrest Logging Servlet" default="bundle"> + +<!-- + Bundle properties + Override master build.xml where necessary +--> + <property name="bundle.name" value="Apache Forrest Logging Servlet Bundle" /> + <property name="bundle.symbolic.name" value="org.apache.forrest.log.servlet" /> + <property name="bundle.activator" value="org.apache.forrest.log.servlet.LogServletPlugin" /> + <property name="bundle.bnd.file" location="logServlet.bnd" /> + + <import file="../master.xml" /> + +</project> Propchange: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/build.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/logServlet.bnd URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/logServlet.bnd?rev=1098386&view=auto ============================================================================== --- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/logServlet.bnd (added) +++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/logServlet.bnd Sun May 1 19:04:46 2011 @@ -0,0 +1,8 @@ +Bundle-Name: ${bundle.name} +Bundle-SymbolicName: ${bundle.symbolic.name} +Bundle-Version: ${bundle.version} +Bundle-Activator: ${bundle.activator} +Bundle-Vendor: ${bundle.vendor} +Export-Package: org.apache.forrest.log.servlet;version="0.10.1" +Import-Package: org.osgi.framework;version="[1.5,1.6)", org.osgi.util.tracker;version="[1.4,1.5)", org.osgi.service.http;version="[1.2,1.3)", javax.servlet, javax.servlet.http, org.apache.forrest.plugin.api;version="[0.10,0.11)", org.apache.forrest.http;version="[0.10,0.11)", org.apache.forrest.util;version="[0.10,0.11)", org.osgi.service.log;version="[1.3,2)", org.apache.forrest.log;version="[0.10,0.11)" +Private-Package: * Added: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java?rev=1098386&view=auto ============================================================================== --- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java (added) +++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java Sun May 1 19:04:46 2011 @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.forrest.log.servlet; + +import java.util.Properties; +import javax.servlet.ServletException; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.service.http.HttpService; +import org.osgi.service.http.NamespaceException; +import org.osgi.service.log.LogService; +import org.osgi.util.tracker.ServiceTracker; + +import org.apache.forrest.log.LogPlugin.LOG; +import org.apache.forrest.log.servlet.service.LogServlet; + +public class LogServletPlugin implements BundleActivator { + + private static final String SERVLET_ALIAS = "/logs"; + + private ServiceTracker mHttpTracker; + private LogServlet mServlet; + + public void start(final BundleContext context) throws Exception { + LOG.debug("Log Servlet plugin starting"); + + // track OSGi HTTP service + mHttpTracker = new ServiceTracker(context, HttpService.class.getName(), null); + mHttpTracker.open(); + + // create servlet instance + mServlet = new LogServlet(context); + + /* + * ServiceTracker.waitForService(long timeout) is not supposed + * to be called from BundleActivator methods because activator + * methods are expected to return quickly. The point of this + * activator is to register the servlet with the HTTP service, + * so I'm willing to wait. + */ + try { + mHttpTracker.waitForService(1000); + HttpService service = (HttpService) mHttpTracker.getService(); + + if (null != service) { + try { + service.registerServlet(SERVLET_ALIAS, mServlet, null, null); + } catch (ServletException e) { + e.printStackTrace(); + } catch (NamespaceException e) { + e.printStackTrace(); + } + } + } catch (InterruptedException ie) { + ; + } + } + + public void stop(BundleContext context) throws Exception { + LOG.debug("Log Servlet plugin stopping"); + + /* + * Clean up our servlet registration + * Without this, the framework would still unregister for us + * but this is the only way to have destroy() called on our + * servlet + */ + HttpService service = (HttpService) mHttpTracker.getService(); + + if (null != service) { + service.unregister(SERVLET_ALIAS); + } + + mHttpTracker.close(); + } + +} Propchange: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java ------------------------------------------------------------------------------ svn:eol-style = native Added: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java?rev=1098386&view=auto ============================================================================== --- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java (added) +++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java Sun May 1 19:04:46 2011 @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.forrest.log.servlet.service; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URI; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.Filter; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.osgi.service.log.LogEntry; +import org.osgi.service.log.LogReaderService; +import org.osgi.service.log.LogService; +import org.osgi.util.tracker.ServiceTracker; + +import org.apache.forrest.log.LogPlugin.LOG; +import org.apache.forrest.plugin.api.ForrestPlugin; +import org.apache.forrest.plugin.api.ForrestResult; +import org.apache.forrest.util.ContentType; + +public class LogServlet extends HttpServlet { + + private static final long serialVersionUID = 3575916939233594893L; + + private BundleContext mBundleContext; + private ServiceTracker mTracker; + + public LogServlet(final BundleContext context) { + mBundleContext = context; + mTracker = new ServiceTracker(mBundleContext, + LogReaderService.class.getName(), + null); + mTracker.open(); + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + resp.setContentType("text/plain"); + printLatestEntries(resp.getWriter()); + } + + private void printLatestEntries(PrintWriter writer) { + if (null == writer) { + throw new IllegalArgumentException("null writer in printLatestEntries()"); + } + + LogReaderService service = (LogReaderService) mTracker.getService(); + + if (null != service) { + Enumeration<?> logEntries = service.getLog(); + + if (null != logEntries) { + while (logEntries.hasMoreElements()) { + LogEntry entry = (LogEntry) logEntries.nextElement(); + Bundle bundle = entry.getBundle(); + String symName = ""; + String version = ""; + + if (null != bundle) { + symName = bundle.getSymbolicName(); + version = bundle.getVersion().toString(); + } + + StringBuilder msg = new StringBuilder(); + msg.append(logLevelToString(entry.getLevel())) + .append(": ") + .append(symName) + .append(" (") + .append(version) + .append(") ") + .append(entry.getMessage()); + + writer.println(msg.toString()); + } + } + } + } + + private String logLevelToString(int level) { + switch (level) { + case LogService.LOG_ERROR: + return "ERROR"; + case LogService.LOG_WARNING: + return "WARN"; + case LogService.LOG_INFO: + return "INFO"; + case LogService.LOG_DEBUG: + return "DEBUG"; + default: + return "UNKNOWN"; + } + } + + @Override + public void destroy() { + mTracker.close(); + mBundleContext = null; + } + +} Propchange: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java ------------------------------------------------------------------------------ svn:eol-style = native