forrest-svn mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bd...@apache.org
Subject svn commit: r1098466 - in /forrest/trunk/whiteboard/forrest-osgi: org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/ org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/ org.apache.forrest.log/src/java/or...
Date Mon, 02 May 2011 03:32:09 GMT
Author: bdube
Date: Mon May  2 03:32:08 2011
New Revision: 1098466

URL: http://svn.apache.org/viewvc?rev=1098466&view=rev
Log:
Harmonize output from console and log servlet to mimic web console
Use LogListener instead of LogReaderService to catch debug and warning levels

Added:
    forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogBuffer.java
  (with props)
Modified:
    forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java
    forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java
    forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log/src/java/org/apache/forrest/log/service/LogWriter.java

Modified: 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=1098466&r1=1098465&r2=1098466&view=diff
==============================================================================
--- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java
(original)
+++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/LogServletPlugin.java
Mon May  2 03:32:08 2011
@@ -21,12 +21,15 @@ import javax.servlet.ServletException;
 
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.http.HttpService;
 import org.osgi.service.http.NamespaceException;
+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.log.servlet.service.LogBuffer;
 import org.apache.forrest.log.servlet.service.LogServlet;
 
 public class LogServletPlugin implements BundleActivator {
@@ -39,6 +42,19 @@ public class LogServletPlugin implements
   public void start(final BundleContext context) throws Exception {
     LOG.debug("Log Servlet plugin starting");
 
+    // register LogListener implemented by LogBuffer
+    ServiceReference readerRef = context.getServiceReference
+      (LogReaderService.class.getName());
+
+    if (null != readerRef) {
+      System.out.println("Registering LogBuffer");
+      LogReaderService readerService = (LogReaderService) context.getService(readerRef);
+      readerService.addLogListener(new LogBuffer());
+      // LOG.debug("Log bundle starting (and self-hosting)");
+    } else {
+      System.out.println("Could not add log listener (LogReaderService is unavailable)");
+    }
+
     // track OSGi HTTP service
     mHttpTracker = new ServiceTracker(context, HttpService.class.getName(), null);
     mHttpTracker.open();

Added: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogBuffer.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/LogBuffer.java?rev=1098466&view=auto
==============================================================================
--- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogBuffer.java
(added)
+++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogBuffer.java
Mon May  2 03:32:08 2011
@@ -0,0 +1,80 @@
+/*
+ * 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.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Vector;
+
+import org.osgi.framework.Bundle;
+import org.osgi.service.log.LogEntry;
+import org.osgi.service.log.LogListener;
+import org.osgi.service.log.LogService;
+
+public class LogBuffer implements LogListener {
+
+  private static final int MAX_LOG_ENTRIES = 1024;
+
+  static Vector<String> sLogBuffer = new Vector<String>(MAX_LOG_ENTRIES);
+
+  // @Override
+  public void logged(LogEntry entry) {
+    int level = entry.getLevel();
+    Bundle bundle = entry.getBundle();
+    String symName = "";
+    String version = "";
+
+    if (null != bundle) {
+      symName = bundle.getSymbolicName();
+      version = bundle.getVersion().toString();
+    }
+
+    // default timezone and locale
+    Date entryTimeStamp = new Date(entry.getTime());
+
+    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss a z");
+
+    StringBuilder msg = new StringBuilder();
+    msg.append(dateFormat.format(entryTimeStamp))
+      .append(" ")
+      .append(logLevelToString(entry.getLevel()))
+      .append(" ")
+      .append(symName)
+      .append(" (")
+      .append(version)
+      .append(") ")
+      .append(entry.getMessage());
+
+    sLogBuffer.add(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";
+    }
+  }
+
+}

Propchange: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogBuffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 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=1098466&r1=1098465&r2=1098466&view=diff
==============================================================================
--- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java
(original)
+++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log.servlet/src/java/org/apache/forrest/log/servlet/service/LogServlet.java
Mon May  2 03:32:08 2011
@@ -22,10 +22,13 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.URI;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -47,8 +50,6 @@ 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 {
@@ -56,14 +57,9 @@ public class LogServlet extends HttpServ
   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
@@ -78,56 +74,19 @@ public class LogServlet extends HttpServ
       throw new IllegalArgumentException("null writer in printLatestEntries()");
     }
 
-    LogReaderService service = (LogReaderService) mTracker.getService();
+    if (!LogBuffer.sLogBuffer.isEmpty()) {
+      Iterator<String> iterator = LogBuffer.sLogBuffer.iterator();
 
-    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());
-        }
+      while (iterator.hasNext()) {
+        writer.println(iterator.next());
       }
-    }
-  }
-
-  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";
+    } else {
+      writer.println("no log entries");
     }
   }
 
   @Override
   public void destroy() {
-    mTracker.close();
     mBundleContext = null;
   }
 

Modified: forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log/src/java/org/apache/forrest/log/service/LogWriter.java
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log/src/java/org/apache/forrest/log/service/LogWriter.java?rev=1098466&r1=1098465&r2=1098466&view=diff
==============================================================================
--- forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log/src/java/org/apache/forrest/log/service/LogWriter.java
(original)
+++ forrest/trunk/whiteboard/forrest-osgi/org.apache.forrest.log/src/java/org/apache/forrest/log/service/LogWriter.java
Mon May  2 03:32:08 2011
@@ -16,6 +16,10 @@
  */
 package org.apache.forrest.log.service;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.osgi.framework.Bundle;
 import org.osgi.service.log.LogEntry;
 import org.osgi.service.log.LogListener;
 import org.osgi.service.log.LogService;
@@ -32,20 +36,44 @@ public class LogWriter implements LogLis
   // @Override
   public void logged(LogEntry entry) {
     int level = entry.getLevel();
+    Bundle bundle = entry.getBundle();
+    String symName = "";
+    String version = "";
+
+    if (null != bundle) {
+      symName = bundle.getSymbolicName();
+      version = bundle.getVersion().toString();
+    }
+
+    // default timezone and locale
+    Date entryTimeStamp = new Date(entry.getTime());
+
+    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss a z");
+
+    StringBuilder msg = new StringBuilder();
+    msg.append(dateFormat.format(entryTimeStamp))
+      .append(" ")
+      // .append(logLevelToString(entry.getLevel()))
+      // .append(" ")
+      .append(symName)
+      .append(" (")
+      .append(version)
+      .append(") ")
+      .append(entry.getMessage());
 
     if (level > -1 && level < LEVEL_STRING.length) {
       switch (level) {
       case LogService.LOG_ERROR:
-        sLogger.error(entry.getMessage());
+        sLogger.error(msg.toString());
         break;
       case LogService.LOG_WARNING:
-        sLogger.warn(entry.getMessage());
+        sLogger.warn(msg.toString());
         break;
       case LogService.LOG_INFO:
-        sLogger.info(entry.getMessage());
+        sLogger.info(msg.toString());
         break;
       case LogService.LOG_DEBUG:
-        sLogger.debug(entry.getMessage());
+        sLogger.debug(msg.toString());
         break;
       default:
         sLogger.warn("LogEntry with unknown log level: " + level);
@@ -53,5 +81,19 @@ public class LogWriter implements LogLis
     }
   }
 
-}
+  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";
+    }
+  }
 
+}



Mime
View raw message