killbill-aplcache

osgi-bundles: fix NPE in test bundle Now that the LogService

2/20/2013 8:12:57 PM

Details

diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestBasicOSGIWithTestBundle.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestBasicOSGIWithTestBundle.java
index df39b19..bac5205 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestBasicOSGIWithTestBundle.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestBasicOSGIWithTestBundle.java
@@ -78,7 +78,7 @@ public class TestBasicOSGIWithTestBundle extends TestOSGIBase {
         // OSGIDataSourceConfig
         super.setup();
 
-        // This is extracted from surefire system configuration-- needs to be added explicitely in IntelliJ for correct running
+        // This is extracted from surefire system configuration-- needs to be added explicitly in IntelliJ for correct running
         final String killbillVersion = System.getProperty("killbill.version");
         SetupBundleWithAssertion setupTest = new SetupBundleWithAssertion(BUNDLE_TEST_RESOURCE, osgiConfig, killbillVersion);
         setupTest.setupBundle();
diff --git a/osgi-bundles/test/pom.xml b/osgi-bundles/test/pom.xml
index 66a0668..5bddb19 100644
--- a/osgi-bundles/test/pom.xml
+++ b/osgi-bundles/test/pom.xml
@@ -41,6 +41,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/test/src/test/java/com/ning/billing/osgi/bundles/test/Logger.java b/osgi-bundles/test/src/test/java/com/ning/billing/osgi/bundles/test/Logger.java
new file mode 100644
index 0000000..4b85926
--- /dev/null
+++ b/osgi-bundles/test/src/test/java/com/ning/billing/osgi/bundles/test/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.test;
+
+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);
+            }
+        }
+    }
+}
diff --git a/osgi-bundles/test/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java b/osgi-bundles/test/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java
index 83576ca..06f1308 100644
--- a/osgi-bundles/test/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java
+++ b/osgi-bundles/test/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java
@@ -53,9 +53,7 @@ public class TestActivator implements BundleActivator {
     private OSGIKillbill osgiKillbill;
     private volatile ServiceReference<OSGIKillbill> osgiKillbillReference;
 
-    private LogService logService;
-    private volatile ServiceReference<LogService> logServiceReference;
-
+    private final Logger logger = new Logger();
 
     private volatile boolean isRunning;
     private volatile ServiceRegistration paymentInfoPluginRegistration;
@@ -69,7 +67,7 @@ public class TestActivator implements BundleActivator {
         System.out.println("TestActivator starting bundle = " + bundleName);
 
         fetchOSGIKIllbill(context);
-        fetchLogService(context);
+        logger.start(context);
 
         final IDBI dbi = new DBI(osgiKillbill.getDataSource());
         testDao = new TestDao(dbi);
@@ -89,16 +87,15 @@ public class TestActivator implements BundleActivator {
         this.isRunning = false;
         releaseOSGIKIllbill(context);
         this.osgiKillbill = null;
-        releaseLogService(context);
-        this.logService = null;
         unregisterPlaymentPluginApi(context);
+        logger.close();
         System.out.println("Good bye world from TestActivator!");
     }
 
     @Subscribe
     public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
 
-        logService.log(LogService.LOG_INFO, "Received external event " + killbillEvent.toString());
+        logger.log(LogService.LOG_INFO, "Received external event " + killbillEvent.toString());
 
         // Only looking at account creation
         if (killbillEvent.getEventType() != ExtBusEventType.ACCOUNT_CREATION) {
@@ -118,7 +115,7 @@ public class TestActivator implements BundleActivator {
             testDao.insertAccountExternalKey(account.getExternalKey());
 
         } catch (AccountApiException e) {
-            logService.log(LogService.LOG_ERROR, e.getMessage());
+            logger.log(LogService.LOG_ERROR, e.getMessage());
         }
     }
 
@@ -148,22 +145,6 @@ public class TestActivator implements BundleActivator {
         }
     }
 
-
-    private void fetchLogService(final BundleContext context) {
-        this.logServiceReference = (ServiceReference<LogService>) context.getServiceReference(LogService.class.getName());
-        try {
-            this.logService = context.getService(logServiceReference);
-        } catch (Exception e) {
-            System.err.println("Error in TestActivator: " + e.getLocalizedMessage());
-        }
-    }
-
-    private void releaseLogService(final BundleContext context) {
-        if (logServiceReference != null) {
-            context.ungetService(logServiceReference);
-        }
-    }
-
     private void registerPaymentApi(final BundleContext context, final TestDao dao) {
 
         final Dictionary props = new Hashtable();