killbill-aplcache

osgi-bundles: track the LogService using a ServiceTracker Don't

2/18/2013 2:35:26 AM

Details

diff --git a/osgi-bundles/jruby/pom.xml b/osgi-bundles/jruby/pom.xml
index 12ba371..a5a02e5 100644
--- a/osgi-bundles/jruby/pom.xml
+++ b/osgi-bundles/jruby/pom.xml
@@ -49,6 +49,10 @@
             <artifactId>org.osgi.core</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>osgi-over-slf4j</artifactId>
         </dependency>
diff --git a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Activator.java b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Activator.java
index f2e4e92..78d4cde 100644
--- a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Activator.java
+++ b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Activator.java
@@ -36,15 +36,16 @@ import com.ning.billing.osgi.api.config.PluginRubyConfig;
 public class Activator implements BundleActivator {
 
     private final List<ServiceReference<?>> serviceReferences = new ArrayList<ServiceReference<?>>();
+    private final Logger logger = new Logger();
 
     private OSGIKillbill osgiKillbill;
-    private LogService logger = null;
     private JRubyPlugin plugin = null;
 
     public void start(final BundleContext context) throws Exception {
-        logger = retrieveApi(context, LogService.class);
+        logger.start(context);
+
         osgiKillbill = retrieveApi(context, OSGIKillbill.class);
-        log(LogService.LOG_INFO, "JRuby bundle activated");
+        logger.log(LogService.LOG_INFO, "JRuby bundle activated");
 
         doMagicToMakeJRubyAndFelixHappy();
 
@@ -66,7 +67,7 @@ public class Activator implements BundleActivator {
         killbillServices.put("logger", logger);
         plugin.instantiatePlugin(killbillServices);
 
-        log(LogService.LOG_INFO, "Starting JRuby plugin " + plugin.getPluginMainClass());
+        logger.log(LogService.LOG_INFO, "Starting JRuby plugin " + plugin.getPluginMainClass());
         plugin.startPlugin(context);
     }
 
@@ -92,12 +93,14 @@ public class Activator implements BundleActivator {
     }
 
     public void stop(final BundleContext context) throws Exception {
-        log(LogService.LOG_INFO, "Stopping JRuby plugin " + plugin.getPluginMainClass());
+        logger.log(LogService.LOG_INFO, "Stopping JRuby plugin " + plugin.getPluginMainClass());
         plugin.stopPlugin(context);
 
         for (final ServiceReference apiReference : serviceReferences) {
             context.ungetService(apiReference);
         }
+
+        logger.close();
     }
 
     private Map<String, Object> retrieveKillbillApis(final BundleContext context) {
@@ -146,10 +149,4 @@ public class Activator implements BundleActivator {
             return null;
         }
     }
-
-    private void log(final int level, final String message) {
-        if (logger != null) {
-            logger.log(level, message);
-        }
-    }
 }
diff --git a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyNotificationPlugin.java b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyNotificationPlugin.java
index 3887a80..134e4b5 100644
--- a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyNotificationPlugin.java
+++ b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyNotificationPlugin.java
@@ -16,8 +16,6 @@
 
 package com.ning.billing.osgi.bundles.jruby;
 
-import javax.annotation.Nullable;
-
 import org.jruby.embed.ScriptingContainer;
 import org.jruby.javasupport.JavaEmbedUtils;
 import org.osgi.framework.BundleContext;
@@ -33,7 +31,7 @@ import com.google.common.eventbus.Subscribe;
 public class JRubyNotificationPlugin extends JRubyPlugin {
 
     public JRubyNotificationPlugin(final PluginRubyConfig config, final ScriptingContainer container,
-                                   final BundleContext bundleContext, @Nullable final LogService logger) {
+                                   final BundleContext bundleContext, final Logger logger) {
         super(config, container, bundleContext, logger);
     }
 
@@ -47,7 +45,7 @@ public class JRubyNotificationPlugin extends JRubyPlugin {
             final ExternalBus externalBus = context.getService(externalBusReference);
             externalBus.register(this);
         } catch (Exception e) {
-            log(LogService.LOG_WARNING, "Error registering notification plugin service", e);
+            logger.log(LogService.LOG_WARNING, "Error registering notification plugin service", e);
         } finally {
             if (externalBusReference != null) {
                 context.ungetService(externalBusReference);
diff --git a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPaymentPlugin.java b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPaymentPlugin.java
index 3002635..01dbf38 100644
--- a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPaymentPlugin.java
+++ b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPaymentPlugin.java
@@ -22,14 +22,11 @@ import java.util.Hashtable;
 import java.util.List;
 import java.util.UUID;
 
-import javax.annotation.Nullable;
-
 import org.jruby.Ruby;
 import org.jruby.embed.ScriptingContainer;
 import org.jruby.javasupport.JavaEmbedUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.log.LogService;
 
 import com.ning.billing.osgi.api.config.PluginRubyConfig;
 import com.ning.billing.payment.api.PaymentMethodPlugin;
@@ -46,7 +43,7 @@ public class JRubyPaymentPlugin extends JRubyPlugin implements PaymentPluginApi 
     private volatile ServiceRegistration<PaymentPluginApi> paymentInfoPluginRegistration;
 
     public JRubyPaymentPlugin(final PluginRubyConfig config, final ScriptingContainer container,
-                              final BundleContext bundleContext, @Nullable final LogService logger) {
+                              final BundleContext bundleContext, final Logger logger) {
         super(config, container, bundleContext, logger);
     }
 
diff --git a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java
index 562b2ea..4aaa263 100644
--- a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java
+++ b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java
@@ -20,7 +20,6 @@ import java.util.Arrays;
 import java.util.Hashtable;
 import java.util.Map;
 
-import javax.annotation.Nullable;
 import javax.servlet.http.HttpServlet;
 
 import org.jruby.Ruby;
@@ -46,7 +45,7 @@ public abstract class JRubyPlugin {
     private static final String KILLBILL_SERVICES = "java_apis";
     private static final String ACTIVE = "@active";
 
-    protected final LogService logger;
+    protected final Logger logger;
     protected final BundleContext bundleContext;
     protected final String pluginGemName;
     protected final String rubyRequire;
@@ -60,7 +59,7 @@ public abstract class JRubyPlugin {
     private String cachedRequireLine = null;
 
     public JRubyPlugin(final PluginRubyConfig config, final ScriptingContainer container,
-                       final BundleContext bundleContext, @Nullable final LogService logger) {
+                       final BundleContext bundleContext, final Logger logger) {
         this.logger = logger;
         this.bundleContext = bundleContext;
         this.pluginGemName = config.getPluginName();
@@ -114,7 +113,7 @@ public abstract class JRubyPlugin {
         // Register the rack handler
         final IRubyObject rackHandler = pluginInstance.callMethod("rack_handler");
         if (!rackHandler.isNil()) {
-            log(LogService.LOG_INFO, String.format("Using %s as rack handler", rackHandler.getMetaClass()));
+            logger.log(LogService.LOG_INFO, String.format("Using %s as rack handler", rackHandler.getMetaClass()));
 
             final JRubyHttpServlet jRubyHttpServlet = new JRubyHttpServlet(rackHandler);
             final Hashtable<String, String> properties = new Hashtable<String, String>();
@@ -217,16 +216,4 @@ public abstract class JRubyPlugin {
     protected Ruby getRuntime() {
         return pluginInstance.getMetaClass().getRuntime();
     }
-
-    protected void log(final int level, final String message) {
-        if (logger != null) {
-            logger.log(level, message);
-        }
-    }
-
-    protected void log(final int level, final String message, final Throwable throwable) {
-        if (logger != null) {
-            logger.log(level, message, throwable);
-        }
-    }
 }
diff --git a/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Logger.java b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Logger.java
new file mode 100644
index 0000000..2b648b0
--- /dev/null
+++ b/osgi-bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/Logger.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2010-2013 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.osgi.bundles.jruby;
+
+import javax.annotation.Nullable;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class Logger {
+
+    // The name of the LogService
+    private static final String LOG_SERVICE_NAME = "org.osgi.service.log.LogService";
+
+    // The ServiceTracker to emit log services
+    private ServiceTracker logTracker;
+
+    public void start(final BundleContext context) {
+        // Track the log service using a ServiceTracker
+        logTracker = new ServiceTracker(context, LOG_SERVICE_NAME, null);
+        logTracker.open();
+    }
+
+    public void close() {
+        if (logTracker != null) {
+            logTracker.close();
+        }
+    }
+
+    public void log(final int level, final String message) {
+        log(level, message, null);
+    }
+
+    public void log(final int level, final String message, @Nullable final Throwable t) {
+        // log using the LogService if available
+        final Object log = logTracker.getService();
+        if (log != null) {
+            if (t == null) {
+                ((LogService) log).log(level, message);
+            } else {
+                ((LogService) log).log(level, message, t);
+            }
+        } else {
+            if (level >= 2) {
+                System.out.println(message);
+            } else {
+                System.err.println(message);
+            }
+
+            if (t != null) {
+                t.printStackTrace(System.err);
+            }
+        }
+    }
+}