killbill-aplcache

Details

diff --git a/osgi-bundles/libs/killbill/src/main/java/com/ning/killbill/osgi/libs/killbill/KillbillActivatorBase.java b/osgi-bundles/libs/killbill/src/main/java/com/ning/killbill/osgi/libs/killbill/KillbillActivatorBase.java
new file mode 100644
index 0000000..ec729ca
--- /dev/null
+++ b/osgi-bundles/libs/killbill/src/main/java/com/ning/killbill/osgi/libs/killbill/KillbillActivatorBase.java
@@ -0,0 +1,78 @@
+/*
+ * 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.killbill.osgi.libs.killbill;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+import com.ning.killbill.osgi.libs.killbill.OSGIKillbillEventDispatcher.OSGIKillbillEventHandler;
+
+public abstract class KillbillActivatorBase implements BundleActivator, OSGIKillbillEventHandler {
+
+
+    protected OSGIKillbillAPI api;
+    protected OSGIKillbillLogService logService;
+    protected OSGIKillbillRegistrar registrar;
+    protected OSGIKillbillDataSource dataSource;
+    protected OSGIKillbillEventDispatcher dispatcher;
+
+    @Override
+    public void start(final BundleContext context) {
+
+        // Tracked resource
+        api = new OSGIKillbillAPI(context);
+        logService = new OSGIKillbillLogService(context);
+        dataSource = new OSGIKillbillDataSource(context);
+        dispatcher = new OSGIKillbillEventDispatcher(context);
+
+        // Registrar for bundle
+        registrar = new OSGIKillbillRegistrar();
+
+        // Killbill events
+        final OSGIKillbillEventHandler handler = getOSGIKillbillEventHandler();
+        if (handler != null) {
+            dispatcher.registerEventHandler(this);
+        }
+    }
+
+    @Override
+    public void stop(final BundleContext context) {
+
+        // Close trackers
+        api.close();
+        dispatcher.close();
+        dataSource.close();
+        logService.close();
+
+        try {
+            // Remove Killbill event handler
+            final OSGIKillbillEventHandler handler = getOSGIKillbillEventHandler();
+            if (handler != null) {
+                dispatcher.unregisterEventHandler(handler);
+            }
+        } catch (OSGIServiceNotAvailable ignore) {
+            // If the system bundle shut down prior to that bundle, we can' unregister our Observer, which is fine.
+        }
+
+        // Unregistaer all servies from that bundle
+        registrar.unregisterAll();
+        System.out.println("Good bye world from TestActivator!");
+    }
+
+
+    public abstract OSGIKillbillEventHandler getOSGIKillbillEventHandler();
+}
diff --git a/osgi-bundles/tests/beatrix/pom.xml b/osgi-bundles/tests/beatrix/pom.xml
index 4f06c99..2de7c56 100644
--- a/osgi-bundles/tests/beatrix/pom.xml
+++ b/osgi-bundles/tests/beatrix/pom.xml
@@ -92,6 +92,7 @@
                     <instructions>
                         <Bundle-Activator>com.ning.billing.osgi.bundles.test.TestActivator</Bundle-Activator>
                         <Import-Package>
+                            <!-- maven-bundle-plugin does not seem to detect that the library is using OSGIKillbill, this is annoying... -->
                             *;resolution:=optional,com.ning.billing.osgi.api
                         </Import-Package>
                     </instructions>
diff --git a/osgi-bundles/tests/beatrix/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java b/osgi-bundles/tests/beatrix/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java
index 7b3a6bf..78559e6 100644
--- a/osgi-bundles/tests/beatrix/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java
+++ b/osgi-bundles/tests/beatrix/src/test/java/com/ning/billing/osgi/bundles/test/TestActivator.java
@@ -18,11 +18,8 @@ package com.ning.billing.osgi.bundles.test;
 
 import java.util.Dictionary;
 import java.util.Hashtable;
-import java.util.Observable;
-import java.util.Observer;
 import java.util.UUID;
 
-import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.log.LogService;
 import org.skife.jdbi.v2.DBI;
@@ -36,12 +33,8 @@ import com.ning.billing.osgi.api.OSGIPluginProperties;
 import com.ning.billing.osgi.bundles.test.dao.TestDao;
 import com.ning.billing.payment.plugin.api.PaymentPluginApi;
 import com.ning.billing.util.callcontext.TenantContext;
-import com.ning.killbill.osgi.libs.killbill.OSGIKillbillAPI;
-import com.ning.killbill.osgi.libs.killbill.OSGIKillbillDataSource;
-import com.ning.killbill.osgi.libs.killbill.OSGIKillbillEventDispatcher;
+import com.ning.killbill.osgi.libs.killbill.KillbillActivatorBase;
 import com.ning.killbill.osgi.libs.killbill.OSGIKillbillEventDispatcher.OSGIKillbillEventHandler;
-import com.ning.killbill.osgi.libs.killbill.OSGIKillbillLogService;
-import com.ning.killbill.osgi.libs.killbill.OSGIKillbillRegistrar;
 
 /**
  * Test class used by Beatrix OSGI test to verify that:
@@ -50,13 +43,7 @@ import com.ning.killbill.osgi.libs.killbill.OSGIKillbillRegistrar;
  * - test bundle is able to register a fake PaymentApi service
  * - test bundle can use the DataSource from Killbill and write on disk
  */
-public class TestActivator implements BundleActivator, OSGIKillbillEventHandler {
-
-    private OSGIKillbillAPI api;
-    private OSGIKillbillLogService logService;
-    private OSGIKillbillRegistrar registrar;
-    private OSGIKillbillDataSource dataSource;
-    private OSGIKillbillEventDispatcher dispatcher;
+public class TestActivator extends KillbillActivatorBase implements OSGIKillbillEventHandler {
 
     private TestDao testDao;
 
@@ -66,32 +53,26 @@ public class TestActivator implements BundleActivator, OSGIKillbillEventHandler 
         final String bundleName = context.getBundle().getSymbolicName();
         System.out.println("TestActivator starting bundle = " + bundleName);
 
-        api = new OSGIKillbillAPI(context);
-        logService = new OSGIKillbillLogService(context);
-        dataSource = new OSGIKillbillDataSource(context);
-        dispatcher = new OSGIKillbillEventDispatcher(context);
-        dispatcher.registerEventHandler(this);
-        registrar = new OSGIKillbillRegistrar();
+        super.start(context);
+
 
         final IDBI dbi = new DBI(dataSource.getDataSource());
         testDao = new TestDao(dbi);
-        registerPaymentApi(context, testDao);
         testDao.createTable();
         testDao.insertStarted();
+        registerPaymentApi(context, testDao);
     }
 
     @Override
     public void stop(final BundleContext context) {
-        api.close();
-        logService.close();
-        dispatcher.unregisterEventHandler(this);
-        dispatcher.close();
-        dataSource.close();
-
-        registrar.unregisterAll();
+        super.stop(context);
         System.out.println("Good bye world from TestActivator!");
     }
 
+    @Override
+    public OSGIKillbillEventHandler getOSGIKillbillEventHandler() {
+        return this;
+    }
 
     private void registerPaymentApi(final BundleContext context, final TestDao dao) {