felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r931858 - in /felix/trunk/webconsole-plugins/memoryusage: ./ src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/
Date Thu, 08 Apr 2010 10:39:23 GMT
Author: fmeschbe
Date: Thu Apr  8 10:39:22 2010
New Revision: 931858

URL: http://svn.apache.org/viewvc?rev=931858&view=rev
Log:
FELIX-2264 Implement for lazy service registration and dynamic API binding

Modified:
    felix/trunk/webconsole-plugins/memoryusage/pom.xml
    felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
    felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
    felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java

Modified: felix/trunk/webconsole-plugins/memoryusage/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/memoryusage/pom.xml?rev=931858&r1=931857&r2=931858&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/memoryusage/pom.xml (original)
+++ felix/trunk/webconsole-plugins/memoryusage/pom.xml Thu Apr  8 10:39:22 2010
@@ -71,20 +71,24 @@
                         </Bundle-SymbolicName>
                         <Import-Package>
                             javax.management.*,
-                            org.osgi.framework,
-                            org.slf4j.*,
+                            org.osgi.framework
+                        </Import-Package>
+                        <DynamicImport-Package>
+                            <!-- logging -->
+                            org.osgi.service.log;version="[1.3,2)",
                             
-                            <!-- plug into the traditional Felix shell -->
-                            org.apache.felix.shell;
+                            <!-- configuration -->
+                            org.osgi.service.cm;version="[1.2,2)",
+                            org.osgi.service.metatype;version="[1.1,2)",
                             
                             <!-- plug into the web console -->
-                            javax.servlet.*;
-                            org.apache.felix.webconsole;
+                            javax.servlet;
+                            javax.servlet.http;version="[2.3,3)",
+                            org.apache.felix.webconsole;version="[3.0,3.1)",
 
-                            <!-- support configuration -->
-                            org.osgi.service.cm;
-                            org.osgi.service.metatype;resolution:=optional
-                        </Import-Package>
+                            <!-- plug into the traditional Felix shell -->
+                            org.apache.felix.shell;version="[1.0,1.1)"
+                        </DynamicImport-Package>
                         <Private-Package>
                             org.apache.felix.webconsole.plugins.memoryusage.*
                         </Private-Package>
@@ -127,16 +131,5 @@
             <version>2.3</version>
             <scope>provided</scope>
         </dependency>
-        <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-api</artifactId>
-            <version>1.5.2</version>
-            <scope>provided</scope>
-        </dependency>
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <version>3.8.1</version>
-        </dependency>
     </dependencies>
 </project>

Modified: felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java?rev=931858&r1=931857&r2=931858&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
(original)
+++ felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/Activator.java
Thu Apr  8 10:39:22 2010
@@ -21,59 +21,56 @@ package org.apache.felix.webconsole.plug
 import java.util.Dictionary;
 import java.util.Hashtable;
 
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
 
 public class Activator implements BundleActivator
 {
 
     private MemoryUsageSupport support;
 
-    public void start(BundleContext bundleContext)
+    public void start(final BundleContext bundleContext)
     {
-
         support = new MemoryUsageSupport(bundleContext);
 
         // install thread handler shell command
-        try
-        {
-            register(bundleContext, new String[]
-                { "org.apache.felix.shell.Command" }, new MemoryUsageCommand(support), null);
-        }
-        catch (Throwable t)
+        new AbstractServiceFactory(bundleContext, null, "org.apache.felix.shell.Command")
         {
-            // shell service might not be available, don't care
-        }
+            @Override
+            protected Object createObject()
+            {
+                return new MemoryUsageCommand(support);
+            }
+        };
 
         // install Web Console plugin
-        try
-        {
-            Dictionary<String, Object> properties = new Hashtable<String, Object>();
-            properties.put("felix.webconsole.label", MemoryUsageConstants.LABEL);
-
-            register(bundleContext, new String[]
-                { "javax.servlet.Servlet", "org.apache.felix.webconsole.ConfigurationPrinter"
}, new MemoryUsagePanel(
-                bundleContext, support), properties);
-        }
-        catch (Throwable t)
-        {
-            // web console might not be available, don't care
-        }
+        Dictionary<String, Object> pluginProps = new Hashtable<String, Object>();
+        pluginProps.put("felix.webconsole.label", MemoryUsageConstants.LABEL);
+        new AbstractServiceFactory(bundleContext, pluginProps, "javax.servlet.Servlet",
+            "org.apache.felix.webconsole.ConfigurationPrinter")
+        {
+            @Override
+            public Object createObject()
+            {
+                return new MemoryUsagePanel(support);
+            }
+        };
 
         // register for configuration
-        try
-        {
-            Dictionary<String, Object> properties = new Hashtable<String, Object>();
-            properties.put(Constants.SERVICE_PID, MemoryUsageConstants.PID);
-            register(bundleContext, new String[]
-                { "org.osgi.service.cm.ManagedService" }, new MemoryUsageConfigurator(support),
properties);
-        }
-        catch (Throwable t)
-        {
-            // Configuration Admin and Metatype Service API might not be
-            // available, don't care
-        }
+        Dictionary<String, Object> cmProps = new Hashtable<String, Object>();
+        cmProps.put(Constants.SERVICE_PID, MemoryUsageConstants.PID);
+        new AbstractServiceFactory(bundleContext, cmProps, "org.osgi.service.cm.ManagedService")
+        {
+            @Override
+            public Object createObject()
+            {
+                return new MemoryUsageConfigurator(support);
+            }
+        };
     }
 
     public void stop(BundleContext bundleContext)
@@ -85,21 +82,46 @@ public class Activator implements Bundle
         }
     }
 
-    static void register(BundleContext context, String[] serviceNames, Object service,
-        Dictionary<String, Object> properties)
+    private static abstract class AbstractServiceFactory implements ServiceFactory
     {
+        private int counter;
+        private Object service;
+
+        public AbstractServiceFactory(BundleContext context, Dictionary<String, Object>
properties,
+            String... serviceNames)
+        {
+            // ensure properties
+            if (properties == null)
+            {
+                properties = new Hashtable<String, Object>();
+            }
+
+            // default settings
+            properties.put(Constants.SERVICE_DESCRIPTION, "Memory Usage (" + serviceNames[0]
+ ")");
+            properties.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
 
-        // ensure properties
-        if (properties == null)
+            context.registerService(serviceNames, this, properties);
+        }
+
+        public synchronized void ungetService(Bundle bundle, ServiceRegistration registration,
Object service)
         {
-            properties = new Hashtable<String, Object>();
+            counter--;
+            if (counter <= 0)
+            {
+                service = null;
+            }
         }
 
-        // default settings
-        properties.put(Constants.SERVICE_DESCRIPTION, "Memory Usage (" + serviceNames[0]
+ ")");
-        properties.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
+        public synchronized Object getService(Bundle bundle, ServiceRegistration registration)
+        {
+            counter++;
+            if (service == null)
+            {
+                service = createObject();
+            }
+            return service;
+        }
 
-        context.registerService(serviceNames, service, properties);
+        protected abstract Object createObject();
     }
-
 }

Modified: felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java?rev=931858&r1=931857&r2=931858&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
(original)
+++ felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsagePanel.java
Thu Apr  8 10:39:22 2010
@@ -41,22 +41,18 @@ import org.apache.felix.webconsole.Confi
 import org.apache.felix.webconsole.DefaultVariableResolver;
 import org.apache.felix.webconsole.WebConsoleUtil;
 import org.osgi.framework.BundleContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.osgi.service.log.LogService;
 
 @SuppressWarnings("serial")
 class MemoryUsagePanel extends AbstractWebConsolePlugin implements ConfigurationPrinter,
AttachmentProvider
 {
 
-    /** default log */
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
     private final MemoryUsageSupport support;
 
-    MemoryUsagePanel(final BundleContext bundleContext, final MemoryUsageSupport support)
+    MemoryUsagePanel(final MemoryUsageSupport support)
     {
         this.support = support;
-        activate(bundleContext);
+        activate(support.getBundleContext());
     }
 
     // ---------- AbstractWebConsolePlugin
@@ -198,7 +194,7 @@ class MemoryUsagePanel extends AbstractW
                 {
                     resp.getWriter().print(
                         "Failed dumping the heap, JVM does not provide known mechanism to
create a Heap Dump");
-                    log.error("Heap Dump creation failed: JVM has no known Heap Dump API");
+                    support.log(LogService.LOG_ERROR, "Heap Dump creation failed: JVM has
no known Heap Dump API");
                 }
             }
             else if ("gc".equals(command))

Modified: felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java?rev=931858&r1=931857&r2=931858&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java
(original)
+++ felix/trunk/webconsole-plugins/memoryusage/src/main/java/org/apache/felix/webconsole/plugins/memoryusage/internal/MemoryUsageSupport.java
Thu Apr  8 10:39:22 2010
@@ -19,6 +19,7 @@
 package org.apache.felix.webconsole.plugins.memoryusage.internal;
 
 import java.io.File;
+import java.io.PrintStream;
 import java.lang.management.ManagementFactory;
 import java.lang.management.MemoryMXBean;
 import java.lang.management.MemoryNotificationInfo;
@@ -41,16 +42,20 @@ import javax.management.NotificationList
 import javax.management.ObjectName;
 
 import org.osgi.framework.BundleContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
 
-final class MemoryUsageSupport implements NotificationListener
+final class MemoryUsageSupport implements NotificationListener, ServiceListener
 {
 
     // This is the name of the HotSpot Diagnostic MBean
     private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
 
-    private final Logger log = LoggerFactory.getLogger(getClass());
+    // to get the LogService
+    private final BundleContext context;
 
     // the default dump location: the dumps folder in the bundle private data
     // or the current working directory
@@ -65,8 +70,28 @@ final class MemoryUsageSupport implement
     // the actual threshold (configured or dynamically set in the console UI)
     private int threshold;
 
+    // log service
+    private ServiceReference logServiceReference;
+    private Object logService;
+
     MemoryUsageSupport(final BundleContext context)
     {
+        this.context = context;
+
+        // register for the log service
+        try
+        {
+            context.addServiceListener(this, "(objectclass=org.osgi.service.log.LogService)");
+            logServiceReference = context.getServiceReference("org.osgi.service.log.LogService");
+            if (logServiceReference != null)
+            {
+                logService = context.getService(logServiceReference);
+            }
+        }
+        catch (InvalidSyntaxException ise)
+        {
+            // TODO
+        }
 
         // the default dump location
         String propDumps = context.getProperty(MemoryUsageConstants.PROP_DUMP_LOCATION);
@@ -139,6 +164,19 @@ final class MemoryUsageSupport implement
         {
             // don't care
         }
+
+        context.removeServiceListener(this);
+        if (logServiceReference != null)
+        {
+            context.ungetService(logServiceReference);
+            logServiceReference = null;
+            logService = null;
+        }
+    }
+
+    public BundleContext getBundleContext()
+    {
+        return context;
     }
 
     /**
@@ -180,8 +218,9 @@ final class MemoryUsageSupport implement
             }
             this.threshold = percentage;
 
-            log.info("Setting Automatic Memory Dump Threshold to {}% for pools {}", threshold,
thresholdPools);
-            log.info("Automatic Memory Dump cannot be set for pools {}", noThresholdPools);
+            log(LogService.LOG_INFO, "Setting Automatic Memory Dump Threshold to %d%% for
pools %s", threshold,
+                thresholdPools);
+            log(LogService.LOG_INFO, "Automatic Memory Dump cannot be set for pools %s",
noThresholdPools);
         }
         else
         {
@@ -351,7 +390,7 @@ final class MemoryUsageSupport implement
             this.dumpLocation = new File(dumpLocation).getAbsoluteFile();
         }
 
-        log.info("Storing Memory Dumps in {}", this.dumpLocation);
+        log(LogService.LOG_INFO, "Storing Memory Dumps in %s", this.dumpLocation);
     }
 
     final File getDumpLocation()
@@ -463,15 +502,16 @@ final class MemoryUsageSupport implement
         String notifType = notification.getType();
         if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED))
         {
-            log.warn("Received Memory Threshold Exceed Notification, dumping Heap");
+            log(LogService.LOG_WARNING, "Received Memory Threshold Exceed Notification, dumping
Heap");
             try
             {
                 File file = dumpHeap(null, true);
-                log.warn("Heap dumped to " + file);
+                log(LogService.LOG_WARNING, "Heap dumped to " + file);
             }
             catch (NoSuchElementException e)
             {
-                log.error("Failed dumping the heap, JVM does not provide known mechanism
to create a Heap Dump");
+                log(LogService.LOG_ERROR,
+                    "Failed dumping the heap, JVM does not provide known mechanism to create
a Heap Dump");
             }
         }
     }
@@ -507,12 +547,12 @@ final class MemoryUsageSupport implement
                 { tmpFile.getAbsolutePath(), live }, new String[]
                 { String.class.getName(), boolean.class.getName() });
 
-            log.debug("dumpSunMBean: Dumped Heap to {} using Sun HotSpot MBean", tmpFile);
+            log(LogService.LOG_DEBUG, "dumpSunMBean: Dumped Heap to %s using Sun HotSpot
MBean", tmpFile);
             return tmpFile;
         }
         catch (Throwable t)
         {
-            log.debug("dumpSunMBean: Dump by Sun HotSpot MBean not working", t);
+            log(LogService.LOG_DEBUG, "dumpSunMBean: Dump by Sun HotSpot MBean not working",
t);
             tmpFile.delete();
         }
 
@@ -522,9 +562,7 @@ final class MemoryUsageSupport implement
     /**
      * @param name
      * @return
-     * @see http
-     *      ://publib.boulder.ibm.com/infocenter/javasdk/v5r0/index.jsp?topic
-     *      =/com.ibm.java.doc.diagnostics.50/diag/tools/heapdump_enable.html
+     * @see <a href="http://publib.boulder.ibm.com/infocenter/javasdk/v5r0/index.jsp?topic=/com.ibm.java.doc.diagnostics.50/diag/tools/heapdump_enable.html">Getting
Heapdumps</a>
      */
     private File dumpIbmDump(String name)
     {
@@ -554,27 +592,87 @@ final class MemoryUsageSupport implement
                         File target = new File(dumpLocation, name);
                         file.renameTo(target);
 
-                        log.debug("dumpSunMBean: Dumped Heap to {} using IBM Dump.HeapDump()",
target);
+                        log(LogService.LOG_DEBUG, "dumpSunMBean: Dumped Heap to %s using
IBM Dump.HeapDump()", target);
                         return target;
                     }
                 }
 
-                log.debug("dumpIbmDump: None of {} files '{}' is younger than {}", new Object[]
-                    { files.length, dir, minFileTime });
+                log(LogService.LOG_DEBUG, "dumpIbmDump: None of %d files '%s' is younger
than %d", files.length, dir,
+                    minFileTime);
             }
             else
             {
-                log.debug("dumpIbmDump: Hmm '{}' does not seem to be a directory; isdir={}
??", dir, dir.isDirectory());
+                log(LogService.LOG_DEBUG, "dumpIbmDump: Hmm '%s' does not seem to be a directory;
isdir=%b ??", dir,
+                    dir.isDirectory());
             }
 
-            log.warn("dumpIbmDump: Heap Dump has been created but cannot be located");
+            log(LogService.LOG_WARNING, "dumpIbmDump: Heap Dump has been created but cannot
be located");
             return dumpLocation;
         }
         catch (Throwable t)
         {
-            log.debug("dumpIbmDump: Dump by IBM Dump class not working", t);
+            log(LogService.LOG_DEBUG, "dumpIbmDump: Dump by IBM Dump class not working",
t);
         }
 
         return null;
     }
+
+    // ---------- Logging support
+
+    public void serviceChanged(ServiceEvent event)
+    {
+        if (event.getType() == ServiceEvent.REGISTERED && logServiceReference ==
null)
+        {
+            logServiceReference = event.getServiceReference();
+            logService = context.getService(event.getServiceReference());
+        }
+        else if (event.getType() == ServiceEvent.UNREGISTERING && logServiceReference
== event.getServiceReference())
+        {
+            logServiceReference = null;
+            logService = null;
+            context.ungetService(event.getServiceReference());
+        }
+    }
+
+    void log(int level, String format, Object... args)
+    {
+        log(level, null, format, args);
+    }
+
+    void log(int level, Throwable t, String format, Object... args)
+    {
+        Object logService = this.logService;
+        final String message = String.format(format, args);
+        if (logService != null)
+        {
+            ((LogService) logService).log(level, message, t);
+        }
+        else
+        {
+            PrintStream out = (level <= LogService.LOG_ERROR) ? System.err : System.out;
+            out.printf("%s: %s (%d): %s%n", toLevelString(level), context.getBundle().getSymbolicName(),
context
+                .getBundle().getBundleId(), message);
+            if (t != null)
+            {
+                t.printStackTrace(out);
+            }
+        }
+    }
+
+    private String toLevelString(int level)
+    {
+        switch (level)
+        {
+            case LogService.LOG_DEBUG:
+                return "DEBUG";
+            case LogService.LOG_INFO:
+                return "INFO ";
+            case LogService.LOG_WARNING:
+                return "WARN ";
+            case LogService.LOG_ERROR:
+                return "ERROR";
+            default:
+                return "unknown(" + level + ")";
+        }
+    }
 }



Mime
View raw message