killbill-memoizeit

Change Kill Bill lifecycle: - Add new specific events for PLUGIN

6/20/2013 7:20:01 PM

Details

diff --git a/api/src/main/java/com/ning/billing/lifecycle/LifecycleHandlerType.java b/api/src/main/java/com/ning/billing/lifecycle/LifecycleHandlerType.java
index d0b9453..74c2494 100644
--- a/api/src/main/java/com/ning/billing/lifecycle/LifecycleHandlerType.java
+++ b/api/src/main/java/com/ning/billing/lifecycle/LifecycleHandlerType.java
@@ -44,13 +44,17 @@ public @interface LifecycleHandlerType {
          */
         INIT_BUS(Sequence.STARTUP_PRE_EVENT_REGISTRATION),
         /**
+        * Start Felix Framework along with its system bundle
+        */
+        INIT_PLUGIN(Sequence.STARTUP_PRE_EVENT_REGISTRATION),
+        /**
          * Service specific initalization-- service does not start yet
          */
         INIT_SERVICE(Sequence.STARTUP_PRE_EVENT_REGISTRATION),
         /**
-         * Service register their interest in events
+         * Start all the plugins
          */
-        REGISTER_EVENTS(Sequence.STARTUP_PRE_EVENT_REGISTRATION),
+        START_PLUGIN(Sequence.STARTUP_PRE_EVENT_REGISTRATION),
         /**
          * Service start
          * - API call should not work
@@ -63,9 +67,9 @@ public @interface LifecycleHandlerType {
          */
         STOP_SERVICE(Sequence.SHUTDOWN_PRE_EVENT_UNREGISTRATION),
         /**
-         * Unregister interest in events
+         * Stop the plugins
          */
-        UNREGISTER_EVENTS(Sequence.SHUTDOWN_PRE_EVENT_UNREGISTRATION),
+        STOP_PLUGIN(Sequence.SHUTDOWN_PRE_EVENT_UNREGISTRATION),
         /**
          * Stop bus
          */
diff --git a/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java b/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java
index 7b75679..95228cd 100644
--- a/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java
+++ b/beatrix/src/main/java/com/ning/billing/beatrix/DefaultBeatrixService.java
@@ -47,7 +47,7 @@ public class DefaultBeatrixService implements BeatrixService {
     }
 
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
+    @LifecycleHandlerType(LifecycleLevel.INIT_SERVICE)
     public void registerForNotifications() {
         try {
             eventBus.register(beatrixListener);
@@ -56,7 +56,7 @@ public class DefaultBeatrixService implements BeatrixService {
         }
     }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.UNREGISTER_EVENTS)
+    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
     public void unregisterForNotifications() {
         try {
             eventBus.unregister(beatrixListener);
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java b/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java
index 8cb3ba8..694140b 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java
@@ -101,13 +101,13 @@ public class TestLifecycle extends BeatrixTestSuite {
             incrementCount();
         }
 
-        @LifecycleHandlerType(LifecycleLevel.REGISTER_EVENTS)
+        @LifecycleHandlerType(LifecycleLevel.INIT_SERVICE)
         public void registerEvents() {
             log.info("Service2 : got REGISTER_EVENTS");
             incrementCount();
         }
 
-        @LifecycleHandlerType(LifecycleLevel.UNREGISTER_EVENTS)
+        @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
         public void unregisterEvents() {
             log.info("Service2 : got UNREGISTER_EVENTS");
             incrementCount();
diff --git a/invoice/src/main/java/com/ning/billing/invoice/api/DefaultInvoiceService.java b/invoice/src/main/java/com/ning/billing/invoice/api/DefaultInvoiceService.java
index 7cc1c7d..4536b58 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/api/DefaultInvoiceService.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/api/DefaultInvoiceService.java
@@ -50,36 +50,28 @@ public class DefaultInvoiceService implements InvoiceService {
 
     @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.INIT_SERVICE)
     public void initialize() throws NotificationQueueAlreadyExists {
-        dateNotifier.initialize();
-    }
-
-    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
-    public void start() {
-        dateNotifier.start();
-    }
-
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
-    public void registerForNotifications() {
         try {
             eventBus.register(invoiceListener);
             eventBus.register(tagHandler);
         } catch (InternalBus.EventBusException e) {
             throw new RuntimeException("Unable to register to the EventBus!", e);
         }
+        dateNotifier.initialize();
+    }
+
+    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
+    public void start() {
+        dateNotifier.start();
     }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.UNREGISTER_EVENTS)
-    public void unregisterForNotifications() {
+    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
+    public void stop() throws NoSuchNotificationQueue {
         try {
             eventBus.unregister(invoiceListener);
             eventBus.unregister(tagHandler);
         } catch (InternalBus.EventBusException e) {
             throw new RuntimeException("Unable to unregister to the EventBus!", e);
         }
-    }
-
-    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
-    public void stop() throws NoSuchNotificationQueue {
         dateNotifier.stop();
     }
 }
diff --git a/invoice/src/test/java/com/ning/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java b/invoice/src/test/java/com/ning/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java
index ef2d32f..ef6ba5a 100644
--- a/invoice/src/test/java/com/ning/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java
+++ b/invoice/src/test/java/com/ning/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java
@@ -128,12 +128,10 @@ public abstract class InvoiceTestSuiteWithEmbeddedDB extends GuicyKillbillTestSu
 
     private void restartInvoiceService(final InvoiceService invoiceService) throws Exception {
         ((DefaultInvoiceService) invoiceService).initialize();
-        ((DefaultInvoiceService) invoiceService).registerForNotifications();
         ((DefaultInvoiceService) invoiceService).start();
     }
 
     private void stopInvoiceService(final InvoiceService invoiceService) throws Exception {
-        ((DefaultInvoiceService) invoiceService).unregisterForNotifications();
         ((DefaultInvoiceService) invoiceService).stop();
     }
 
diff --git a/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java b/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
index f6ddfa0..81c2b5f 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
@@ -18,12 +18,15 @@ package com.ning.billing.osgi;
 
 import java.io.File;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 import javax.inject.Inject;
 
 import org.apache.felix.framework.Felix;
 import org.apache.felix.framework.util.FelixConstants;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.launch.Framework;
@@ -48,6 +51,7 @@ public class DefaultOSGIService implements OSGIService {
     private final OSGIConfig osgiConfig;
     private final KillbillActivator killbillActivator;
     private final FileInstall fileInstall;
+    private final List<Bundle> installedBundles;
 
     private Framework framework;
 
@@ -58,6 +62,7 @@ public class DefaultOSGIService implements OSGIService {
         this.osgiConfig = osgiConfig;
         this.killbillActivator = killbillActivator;
         this.fileInstall = new FileInstall(osgiBundleFinder, pluginFinder, pluginConfigServiceApi);
+        this.installedBundles = new LinkedList<Bundle>();
         this.framework = null;
     }
 
@@ -66,7 +71,8 @@ public class DefaultOSGIService implements OSGIService {
         return OSGI_SERVICE_NAME;
     }
 
-    @LifecycleHandlerType(LifecycleLevel.INIT_SERVICE)
+
+    @LifecycleHandlerType(LifecycleLevel.INIT_PLUGIN)
     public void initialize() {
         try {
             // We start by deleting existing osi cache; we might optimize later keeping the cache
@@ -76,31 +82,28 @@ public class DefaultOSGIService implements OSGIService {
             this.framework = createAndInitFramework();
             framework.start();
 
-            // This will call the start() method for the bundles
-            fileInstall.installAndStartBundles(framework);
+            installedBundles.addAll(fileInstall.installBundles(framework));
         } catch (BundleException e) {
             logger.error("Failed to initialize Killbill OSGIService", e);
         }
-    }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
-    public void registerForExternalEvents() throws Exception {
     }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.UNREGISTER_EVENTS)
-    public void unregisterForExternalEvents() {
+    @LifecycleHandlerType(LifecycleLevel.START_PLUGIN)
+    public void start() {
+        // This will call the start() method for the bundles
+        fileInstall.startBundles(installedBundles);
     }
 
-    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
-    public void startFramework() {
-    }
 
-    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
+
+    @LifecycleHandlerType(LifecycleLevel.STOP_PLUGIN)
     public void stop() {
         try {
             framework.stop();
             framework.waitForStop(0);
 
+            installedBundles.clear();
         } catch (BundleException e) {
             logger.error("Failed to Stop Killbill OSGIService " + e.getMessage());
         } catch (InterruptedException e) {
diff --git a/osgi/src/main/java/com/ning/billing/osgi/FileInstall.java b/osgi/src/main/java/com/ning/billing/osgi/FileInstall.java
index fe138d4..c318727 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/FileInstall.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/FileInstall.java
@@ -66,27 +66,33 @@ public class FileInstall {
         this.pluginConfigServiceApi = pluginConfigServiceApi;
     }
 
-    public void installAndStartBundles(final Framework framework) {
+
+    public List<Bundle> installBundles(final Framework framework) {
+
+        final List<Bundle> installedBundles = new LinkedList<Bundle>();
         try {
-            final BundleContext context = framework.getBundleContext();
 
+            final BundleContext context = framework.getBundleContext();
             final String jrubyBundlePath = findJrubyBundlePath();
 
             // Install all bundles and create service mapping
-            final List<Bundle> installedBundles = new LinkedList<Bundle>();
             installAllJavaBundles(context, installedBundles, jrubyBundlePath);
             installAllJavaPluginBundles(context, installedBundles);
             installAllJRubyPluginBundles(context, installedBundles, jrubyBundlePath);
 
-            // Start all the bundles
-            for (final Bundle bundle : installedBundles) {
-                startBundle(bundle);
-            }
         } catch (PluginConfigException e) {
             logger.error("Error while parsing plugin configurations", e);
         } catch (BundleException e) {
             logger.error("Error while parsing plugin configurations", e);
         }
+        return installedBundles;
+    }
+
+    public void startBundles(final List<Bundle> installedBundles) {
+        // Start all the bundles
+        for (final Bundle bundle : installedBundles) {
+            startBundle(bundle);
+        }
     }
 
     private void installAllJavaBundles(final BundleContext context, final List<Bundle> installedBundles, @Nullable final String jrubyBundlePath) throws PluginConfigException, BundleException {
diff --git a/overdue/src/main/java/com/ning/billing/overdue/service/DefaultOverdueService.java b/overdue/src/main/java/com/ning/billing/overdue/service/DefaultOverdueService.java
index d638437..dee9f20 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/service/DefaultOverdueService.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/service/DefaultOverdueService.java
@@ -112,16 +112,11 @@ public class DefaultOverdueService implements OverdueService {
 
     @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.INIT_SERVICE)
     public void initialize() {
+        registerForBus();
         notifier.initialize();
     }
 
-    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
-    public void start() {
-        notifier.start();
-    }
-
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
-    public void registerForBus() {
+    private void registerForBus() {
         try {
             busService.getBus().register(listener);
         } catch (final EventBusException e) {
@@ -129,17 +124,18 @@ public class DefaultOverdueService implements OverdueService {
         }
     }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.UNREGISTER_EVENTS)
-    public void unregisterForBus() {
+    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
+    public void start() {
+        notifier.start();
+    }
+
+    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
+    public void stop() {
         try {
             busService.getBus().unregister(listener);
         } catch (final EventBusException e) {
             log.error("Problem encountered registering OverdueListener on the Event Bus", e);
         }
-    }
-
-    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
-    public void stop() {
         notifier.stop();
     }
 }
diff --git a/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteNoDB.java b/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteNoDB.java
index 641b87e..e48240a 100644
--- a/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteNoDB.java
+++ b/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteNoDB.java
@@ -91,9 +91,7 @@ public abstract class OverdueTestSuiteNoDB extends GuicyKillbillTestSuiteNoDB {
     @BeforeMethod(groups = "fast")
     public void beforeMethod() throws Exception {
         bus.start();
-
-        service.registerForBus();
-            service.initialize();
+        service.initialize();
         service.start();
     }
 
diff --git a/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteWithEmbeddedDB.java b/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteWithEmbeddedDB.java
index 9324e71..7b91843 100644
--- a/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteWithEmbeddedDB.java
+++ b/overdue/src/test/java/com/ning/billing/overdue/OverdueTestSuiteWithEmbeddedDB.java
@@ -98,11 +98,8 @@ public abstract class OverdueTestSuiteWithEmbeddedDB extends GuicyKillbillTestSu
     public void beforeMethod() throws Exception {
         super.beforeMethod();
         cacheControllerDispatcher.clearAll();
-        bus.register(listener);
         bus.start();
-
-        // The service will initialize and start the notifier
-        service.registerForBus();
+        bus.register(listener);
         service.initialize();
         service.start();
     }
@@ -110,6 +107,7 @@ public abstract class OverdueTestSuiteWithEmbeddedDB extends GuicyKillbillTestSu
     @AfterMethod(groups = "slow")
     public void afterMethod() throws Exception {
         service.stop();
+        bus.unregister(listener);
         bus.stop();
     }
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/glue/DefaultPaymentService.java b/payment/src/main/java/com/ning/billing/payment/glue/DefaultPaymentService.java
index 335e0f1..be948d2 100644
--- a/payment/src/main/java/com/ning/billing/payment/glue/DefaultPaymentService.java
+++ b/payment/src/main/java/com/ning/billing/payment/glue/DefaultPaymentService.java
@@ -70,19 +70,15 @@ public class DefaultPaymentService implements PaymentService {
 
     @LifecycleHandlerType(LifecycleLevel.INIT_SERVICE)
     public void initialize() throws NotificationQueueAlreadyExists {
-        failedRetryService.initialize(SERVICE_NAME);
-        timedoutRetryService.initialize(SERVICE_NAME);
-        autoPayoffRetryService.initialize(SERVICE_NAME);
-    }
-
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
-    public void registerForNotifications() {
         try {
             eventBus.register(invoiceHandler);
             eventBus.register(tagHandler);
         } catch (InternalBus.EventBusException e) {
             log.error("Unable to register with the EventBus!", e);
         }
+        failedRetryService.initialize(SERVICE_NAME);
+        timedoutRetryService.initialize(SERVICE_NAME);
+        autoPayoffRetryService.initialize(SERVICE_NAME);
     }
 
     @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
@@ -94,6 +90,12 @@ public class DefaultPaymentService implements PaymentService {
 
     @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
     public void stop() throws NoSuchNotificationQueue {
+        try {
+            eventBus.unregister(invoiceHandler);
+            eventBus.unregister(tagHandler);
+        } catch (InternalBus.EventBusException e) {
+            throw new RuntimeException("Unable to unregister to the EventBus!", e);
+        }
         failedRetryService.stop();
         timedoutRetryService.stop();
         autoPayoffRetryService.stop();
diff --git a/server/src/main/java/com/ning/billing/server/DefaultServerService.java b/server/src/main/java/com/ning/billing/server/DefaultServerService.java
index ef06f28..ad68e2b 100644
--- a/server/src/main/java/com/ning/billing/server/DefaultServerService.java
+++ b/server/src/main/java/com/ning/billing/server/DefaultServerService.java
@@ -19,6 +19,7 @@ import javax.inject.Inject;
 
 import com.ning.billing.beatrix.bus.api.ExternalBus;
 import com.ning.billing.lifecycle.LifecycleHandlerType;
+import com.ning.billing.lifecycle.LifecycleHandlerType.LifecycleLevel;
 import com.ning.billing.server.notifications.PushNotificationListener;
 
 public class DefaultServerService implements ServerService {
@@ -40,12 +41,12 @@ public class DefaultServerService implements ServerService {
         return SERVER_SERVICE;
     }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
+    @LifecycleHandlerType(LifecycleLevel.INIT_SERVICE)
     public void registerForNotifications() {
         bus.register(pushNotificationListener);
     }
 
-    @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.UNREGISTER_EVENTS)
+    @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
     public void unregisterForNotifications() {
         bus.unregister(pushNotificationListener);
     }
diff --git a/util/src/main/java/com/ning/billing/util/bus/PersistentInternalBus.java b/util/src/main/java/com/ning/billing/util/bus/PersistentInternalBus.java
index 49b4f89..95039d7 100644
--- a/util/src/main/java/com/ning/billing/util/bus/PersistentInternalBus.java
+++ b/util/src/main/java/com/ning/billing/util/bus/PersistentInternalBus.java
@@ -160,31 +160,47 @@ public class PersistentInternalBus extends PersistentQueueBase implements Intern
 
     @Override
     public void register(final Object handlerInstance) throws EventBusException {
-        eventBusDelegate.register(handlerInstance);
+        if (isStarted) {
+            eventBusDelegate.register(handlerInstance);
+        } else {
+            log.warn("Attempting to register handler " + handlerInstance + " in a non initialized bus");
+        }
     }
 
     @Override
     public void unregister(final Object handlerInstance) throws EventBusException {
-        eventBusDelegate.unregister(handlerInstance);
+        if (isStarted) {
+            eventBusDelegate.unregister(handlerInstance);
+        } else {
+            log.warn("Attempting to unregister handler " + handlerInstance + " in a non initialized bus");
+        }
     }
 
     @Override
     public void post(final BusInternalEvent event, final InternalCallContext context) throws EventBusException {
-        dao.inTransaction(TransactionIsolationLevel.READ_COMMITTED, new Transaction<Void, PersistentBusSqlDao>() {
-            @Override
-            public Void inTransaction(final PersistentBusSqlDao transactional,
-                                      final TransactionStatus status) throws Exception {
-                postFromTransaction(event, context, transactional);
-                return null;
-            }
-        });
+        if (isStarted) {
+            dao.inTransaction(TransactionIsolationLevel.READ_COMMITTED, new Transaction<Void, PersistentBusSqlDao>() {
+                @Override
+                public Void inTransaction(final PersistentBusSqlDao transactional,
+                                          final TransactionStatus status) throws Exception {
+                    postFromTransaction(event, context, transactional);
+                    return null;
+                }
+            });
+        } else {
+            log.warn("Attempting to post event " + event + " in a non initialized bus");
+        }
     }
 
     @Override
     public void postFromTransaction(final BusInternalEvent event, final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory, final InternalCallContext context)
             throws EventBusException {
         final PersistentBusSqlDao transactional = entitySqlDaoWrapperFactory.transmogrify(PersistentBusSqlDao.class);
-        postFromTransaction(event, context, transactional);
+        if (isStarted) {
+            postFromTransaction(event, context, transactional);
+        } else {
+            log.warn("Attempting to post event " + event + " in a non initialized bus");
+        }
     }
 
     private void postFromTransaction(final BusInternalEvent event, final InternalCallContext context, final PersistentBusSqlDao transactional) {