[jboss-osgi-commits] JBoss-OSGI SVN: r87181 - in projects/jboss-osgi/trunk: service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal and 1 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Mon Apr 13 07:08:55 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-04-13 07:08:55 -0400 (Mon, 13 Apr 2009)
New Revision: 87181

Added:
   projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal/
   projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal/RemoteLogEntry.java
Modified:
   projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogActivator.java
   projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogReceiverService.java
   projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogSenderService.java
   projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceRemoteTestCase.java
   projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceTestCase.java
Log:
WIP: Remote Logging

Modified: projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogActivator.java
===================================================================
--- projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogActivator.java	2009-04-13 10:24:04 UTC (rev 87180)
+++ projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogActivator.java	2009-04-13 11:08:55 UTC (rev 87181)
@@ -25,8 +25,6 @@
 
 import java.util.Properties;
 
-import org.jboss.remoting.transport.ServerFactory;
-import org.jboss.remoting.transport.local.TransportServerFactory;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;

Modified: projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogReceiverService.java
===================================================================
--- projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogReceiverService.java	2009-04-13 10:24:04 UTC (rev 87180)
+++ projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogReceiverService.java	2009-04-13 11:08:55 UTC (rev 87181)
@@ -23,11 +23,14 @@
 
 //$Id$
 
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
 import java.util.Properties;
 
 import javax.management.MBeanServer;
 
+import org.jboss.osgi.service.remlog.internal.RemoteLogEntry;
 import org.jboss.remoting.InvocationRequest;
 import org.jboss.remoting.InvokerLocator;
 import org.jboss.remoting.ServerInvocationHandler;
@@ -50,10 +53,9 @@
    private String host;
    private Integer port;
    private Connector connector;
+   
+   private List<LogListener> listeners = new ArrayList<LogListener>();
 
-   // String to be returned from invocation handler upon client invocation calls.
-   private static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
-
    public RemoteLogReceiverService(BundleContext context, Properties props)
    {
       this.host = props.getProperty(RemoteLogActivator.REMOTE_LOG_HOST);
@@ -62,10 +64,12 @@
 
    public void addLogListener(LogListener listener)
    {
+      listeners.add(listener);
    }
 
    public void removeLogListener(LogListener listener)
    {
+      listeners.remove(listener);
    }
 
    public Enumeration<LogEntry> getLog()
@@ -75,26 +79,16 @@
 
    public void start()
    {
-      String locatorURI = "socket://" + host + ":" + port;
       try
       {
-         // create the InvokerLocator based on url string format
-         // to indicate the transport, host, and port to use for the
-         // server invoker.
+         String locatorURI = "socket://" + host + ":" + port;
          InvokerLocator locator = new InvokerLocator(locatorURI);
-         System.out.println("Starting remoting server with locator uri of: " + locatorURI);
          connector = new Connector(locator);
-         // creates all the connector's needed resources, such as the server invoker
          connector.create();
 
-         // create the handler to receive the invocation request from the client for processing
-         SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
-         // first parameter is sub-system name.  can be any String value.
-         connector.addInvocationHandler("sample", invocationHandler);
-
-         // start with a new non daemon thread so
-         // server will wait for request and not exit
-         connector.start();
+         LogEntryInvocationHandler invocationHandler = new LogEntryInvocationHandler();
+         connector.addInvocationHandler("remote-log", invocationHandler);
+         connector.start(true);
       }
       catch (Throwable ex)
       {
@@ -116,64 +110,46 @@
    }
 
    /**
-    * Simple invocation handler implementation. This is the code that will be called with the invocation payload from the client.
+    * Simple invocation handler implementation. 
+    * This is the code that will be called with the invocation payload from the client.
     */
-   public static class SampleInvocationHandler implements ServerInvocationHandler
+   class LogEntryInvocationHandler implements ServerInvocationHandler
    {
-      /**
-       * called to handle a specific invocation
-       * 
-       * @param invocation
-       * @return
-       * @throws Throwable
-       */
       public Object invoke(InvocationRequest invocation) throws Throwable
       {
-         // Print out the invocation request
-         System.out.println("Invocation request is: " + invocation.getParameter());
-         System.out.println("Returning response of: " + RESPONSE_VALUE);
-         // Just going to return static string as this is just simple example code.
-         return RESPONSE_VALUE;
+         RemoteLogEntry remoteEntry = (RemoteLogEntry)invocation.getParameter();
+         for (LogListener listener : listeners)
+         {
+            try
+            {
+               listener.logged(remoteEntry);
+            }
+            catch (RuntimeException rte)
+            {
+               System.out.println(rte.toString());
+            }
+         }
+         return null;
       }
 
-      /**
-       * Adds a callback handler that will listen for callbacks from the server invoker handler.
-       * 
-       * @param callbackHandler
-       */
       public void addListener(InvokerCallbackHandler callbackHandler)
       {
-         // NO OP as do not handling callback listeners in this example
+         // NOOP as do not handling callback listeners
       }
 
-      /**
-       * Removes the callback handler that was listening for callbacks from the server invoker handler.
-       * 
-       * @param callbackHandler
-       */
       public void removeListener(InvokerCallbackHandler callbackHandler)
       {
-         // NO OP as do not handling callback listeners in this example
+         // NOOP as do not handling callback listeners
       }
 
-      /**
-       * set the mbean server that the handler can reference
-       * 
-       * @param server
-       */
       public void setMBeanServer(MBeanServer server)
       {
-         // NO OP as do not need reference to MBeanServer for this handler
+         // NOOP as do not need reference to MBeanServer for this handler
       }
 
-      /**
-       * set the invoker that owns this handler
-       * 
-       * @param invoker
-       */
       public void setInvoker(ServerInvoker invoker)
       {
-         // NO OP as do not need reference back to the server invoker
+         // NOOP as do not need reference back to the server invoker
       }
    }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogSenderService.java
===================================================================
--- projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogSenderService.java	2009-04-13 10:24:04 UTC (rev 87180)
+++ projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/RemoteLogSenderService.java	2009-04-13 11:08:55 UTC (rev 87181)
@@ -25,6 +25,7 @@
 
 import java.util.Properties;
 
+import org.jboss.osgi.service.remlog.internal.RemoteLogEntry;
 import org.jboss.remoting.Client;
 import org.jboss.remoting.InvokerLocator;
 import org.osgi.framework.BundleContext;
@@ -79,30 +80,19 @@
    
    public void logged(LogEntry entry)
    {
-      String locatorURI = "socket://" + host + ":" + port;
+      RemoteLogEntry remoteEntry = new RemoteLogEntry(entry);
       try
       {
-         makeInvocation(locatorURI);
+         String locatorURI = "socket://" + host + ":" + port;
+         InvokerLocator locator = new InvokerLocator(locatorURI);
+         Client remotingClient = new Client(locator);
+         remotingClient.connect();
+         remotingClient.invoke(remoteEntry);
+         remotingClient.disconnect();
       }
       catch (Throwable ex)
       {
          ex.printStackTrace();
       }
    }
-
-   public void makeInvocation(String locatorURI) throws Throwable
-   {
-      // create InvokerLocator with the url type string
-      // indicating the target remoting server to call upon.
-      InvokerLocator locator = new InvokerLocator(locatorURI);
-      System.out.println("Calling remoting server with locator uri of: " + locatorURI);
-
-      Client remotingClient = new Client(locator);
-      remotingClient.connect();
-      String request = "Do something";
-      System.out.println("Invoking server with request of '" + request + "'");
-      Object response = remotingClient.invoke(request);
-      remotingClient.disconnect();
-      System.out.println("Invocation response: " + response);
-   }
 }
\ No newline at end of file

Added: projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal/RemoteLogEntry.java
===================================================================
--- projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal/RemoteLogEntry.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal/RemoteLogEntry.java	2009-04-13 11:08:55 UTC (rev 87181)
@@ -0,0 +1,318 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.service.remlog.internal;
+
+// $Id$
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import org.jboss.osgi.service.remlog.NotImplementedException;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogEntry;
+import org.osgi.service.log.LogService;
+
+/**
+ * A unified implementation of a LogEntry.
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 09-Apr-2009
+ */
+public class RemoteLogEntry implements LogEntry
+{
+   private long time;
+   private Bundle bundle;
+   private ServiceReference sref;
+   private int level;
+   private String message;
+   private Throwable exception;
+
+   public RemoteLogEntry(LogEntry entry)
+   {
+      this.time = entry.getTime();
+      this.level = entry.getLevel();
+      this.message = entry.getMessage();
+      this.exception = entry.getException();
+
+      if (entry.getBundle() != null)
+         bundle = new DummyBundle(entry.getBundle());
+
+      if (entry.getServiceReference() != null)
+         sref = new DummyServiceReference(entry.getServiceReference());
+   }
+
+   public long getTime()
+   {
+      return time;
+   }
+
+   public Bundle getBundle()
+   {
+      return bundle;
+   }
+
+   public ServiceReference getServiceReference()
+   {
+      return sref;
+   }
+
+   public int getLevel()
+   {
+      return level;
+   }
+
+   public String getMessage()
+   {
+      return message;
+   }
+
+   public Throwable getException()
+   {
+      return exception;
+   }
+
+   @Override
+   public String toString()
+   {
+      String t = new SimpleDateFormat("dd-MMM-yyyy HH:mm.ss.SSS").format(new Date(time));
+      String l = " " + logLevel(level);
+      String s = sref != null ? ",sref=" + sref.getBundle().getSymbolicName() : "";
+      String b = ",bnd=" + bundle.getSymbolicName();
+      String m = ",msg=" + message;
+      String e = exception != null ? ",ex=" + exception : "";
+      return "[" + t + l + b + s + m + e + "]";
+   }
+
+   private String logLevel(int level)
+   {
+      String logLevel;
+      switch (level)
+      {
+         case LogService.LOG_DEBUG:
+            logLevel = "DEBUG";
+            break;
+         case LogService.LOG_INFO:
+            logLevel = "INFO";
+            break;
+         case LogService.LOG_WARNING:
+            logLevel = "WARN";
+            break;
+         case LogService.LOG_ERROR:
+            logLevel = "ERROR";
+            break;
+         default:
+            logLevel = "Level=" + level;
+      }
+      return logLevel;
+   }
+
+   /**
+    * A dummy Bundle for remote logging
+    */
+   class DummyBundle implements Bundle
+   {
+      private String symbolicName;
+
+      public DummyBundle(Bundle bundle)
+      {
+         this.symbolicName = bundle.getSymbolicName();
+      }
+
+      @SuppressWarnings("unchecked")
+      public Enumeration findEntries(String path, String filePattern, boolean recurse)
+      {
+         throw new NotImplementedException();
+      }
+
+      public BundleContext getBundleContext()
+      {
+         throw new NotImplementedException();
+      }
+
+      public long getBundleId()
+      {
+         throw new NotImplementedException();
+      }
+
+      public URL getEntry(String path)
+      {
+         throw new NotImplementedException();
+      }
+
+      @SuppressWarnings("unchecked")
+      public Enumeration getEntryPaths(String path)
+      {
+         throw new NotImplementedException();
+      }
+
+      @SuppressWarnings("unchecked")
+      public Dictionary getHeaders()
+      {
+         throw new NotImplementedException();
+      }
+
+      @SuppressWarnings("unchecked")
+      public Dictionary getHeaders(String locale)
+      {
+         throw new NotImplementedException();
+      }
+
+      public long getLastModified()
+      {
+         throw new NotImplementedException();
+      }
+
+      public String getLocation()
+      {
+         throw new NotImplementedException();
+      }
+
+      public ServiceReference[] getRegisteredServices()
+      {
+         throw new NotImplementedException();
+      }
+
+      public URL getResource(String name)
+      {
+         throw new NotImplementedException();
+      }
+
+      @SuppressWarnings("unchecked")
+      public Enumeration getResources(String name) throws IOException
+      {
+         throw new NotImplementedException();
+      }
+
+      public ServiceReference[] getServicesInUse()
+      {
+         throw new NotImplementedException();
+      }
+
+      public int getState()
+      {
+         throw new NotImplementedException();
+      }
+
+      public String getSymbolicName()
+      {
+         return symbolicName;
+      }
+
+      public boolean hasPermission(Object permission)
+      {
+         throw new NotImplementedException();
+      }
+
+      @SuppressWarnings("unchecked")
+      public Class loadClass(String name) throws ClassNotFoundException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void start() throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void start(int options) throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void stop() throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void stop(int options) throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void uninstall() throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void update() throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+
+      public void update(InputStream in) throws BundleException
+      {
+         throw new NotImplementedException();
+      }
+   }
+
+   /**
+    * A dummy ServiceReference for remote logging
+    */
+   class DummyServiceReference implements ServiceReference
+   {
+      private Bundle bundle;
+
+      public DummyServiceReference(ServiceReference sref)
+      {
+         if (sref.getBundle() != null)
+            this.bundle = new DummyBundle(sref.getBundle());
+      }
+
+      public int compareTo(Object reference)
+      {
+         throw new NotImplementedException();
+      }
+
+      public Bundle getBundle()
+      {
+         return bundle;
+      }
+
+      public Object getProperty(String key)
+      {
+         throw new NotImplementedException();
+      }
+
+      public String[] getPropertyKeys()
+      {
+         throw new NotImplementedException();
+      }
+
+      public Bundle[] getUsingBundles()
+      {
+         throw new NotImplementedException();
+      }
+
+      public boolean isAssignableTo(Bundle bundle, String className)
+      {
+         throw new NotImplementedException();
+      }
+   }
+}
\ No newline at end of file


Property changes on: projects/jboss-osgi/trunk/service/remlog/src/main/java/org/jboss/osgi/service/remlog/internal/RemoteLogEntry.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceRemoteTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceRemoteTestCase.java	2009-04-13 10:24:04 UTC (rev 87180)
+++ projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceRemoteTestCase.java	2009-04-13 11:08:55 UTC (rev 87181)
@@ -52,5 +52,7 @@
       logServiceBundle.start();
       
       installBundle(sysContext, "thirdparty/jboss-osgi-service-remlog.jar", true);
+      
+      installBundle(sysContext, "example/example-log-bundleA.jar", true);
    }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceTestCase.java	2009-04-13 10:24:04 UTC (rev 87180)
+++ projects/jboss-osgi/trunk/testsuite/src/test/java/org/jboss/test/osgi/example/log/LogServiceTestCase.java	2009-04-13 11:08:55 UTC (rev 87181)
@@ -47,7 +47,7 @@
  */
 public class LogServiceTestCase extends OSGiTest
 {
-   public void testServiceA() throws Exception
+   public void testLogEntryFilter() throws Exception
    {
       // Bootstrap the Framework and get the system bundle
       OSGiFramework framework = OSGiBootstrap.getBootstrapProvider().getFramework();
@@ -71,7 +71,8 @@
       };
       tracker.open();
 
-      // Install and start the 3rd party LogService.   
+      // Install and start the 3rd party LogService. 
+      // This will register the LogReaderService that we track above.
       URL testURL = getTestArchiveURL("thirdparty/org.apache.felix.log.jar");
       Bundle logServiceBundle = sysContext.installBundle(testURL.toExternalForm());
       logServiceBundle.start();




More information about the jboss-osgi-commits mailing list