killbill-aplcache

Merge branch 'new-paymentpluginapi' of github.com:killbill/killbill

2/7/2013 10:12:00 PM

Details

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 8091eff..bdf7ff6 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
@@ -30,6 +30,8 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.launch.Framework;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -44,9 +46,12 @@ import com.ning.billing.osgi.api.config.PluginRubyConfig;
 import com.ning.billing.osgi.pluginconf.DefaultPluginConfigServiceApi;
 import com.ning.billing.osgi.pluginconf.PluginConfigException;
 import com.ning.billing.osgi.pluginconf.PluginFinder;
+import com.ning.billing.payment.plugin.api.PaymentPluginApi;
+import com.ning.billing.payment.provider.PaymentProviderPluginRegistry;
 import com.ning.billing.util.config.OSGIConfig;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 
 public class DefaultOSGIService implements OSGIService {
 
@@ -54,20 +59,27 @@ public class DefaultOSGIService implements OSGIService {
 
     private static final Logger logger = LoggerFactory.getLogger(DefaultOSGIService.class);
 
-    private Framework framework;
 
     private final OSGIConfig osgiConfig;
     private final PluginFinder pluginFinder;
     private final PluginConfigServiceApi pluginConfigServiceApi;
     private final KillbillActivator killbillActivator;
+    private final PaymentProviderPluginRegistry paymentProviderPluginRegistry;
+
+    private Framework framework;
+    private volatile ServiceReference<PaymentPluginApi>[] paymentApiReferences;
+    private Map<String, PaymentPluginApi> paymentPluginApis;
 
     @Inject
     public DefaultOSGIService(final OSGIConfig osgiConfig, final PluginFinder pluginFinder,
-                              final PluginConfigServiceApi pluginConfigServiceApi, final KillbillActivator killbillActivator) {
+                              final PluginConfigServiceApi pluginConfigServiceApi,
+                              final KillbillActivator killbillActivator,
+                              final PaymentProviderPluginRegistry paymentProviderPluginRegistry) {
         this.osgiConfig = osgiConfig;
         this.pluginFinder = pluginFinder;
         this.pluginConfigServiceApi = pluginConfigServiceApi;
         this.killbillActivator = killbillActivator;
+        this.paymentProviderPluginRegistry = paymentProviderPluginRegistry;
         this.framework = null;
     }
 
@@ -93,18 +105,25 @@ public class DefaultOSGIService implements OSGIService {
         }
     }
 
-    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
-    public void startFramework() {
-    }
-
     @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.REGISTER_EVENTS)
-    public void registerForExternalEvents() {
+    public void registerForExternalEvents() throws Exception {
+        // We use that level which comes before START to register the paymentPluginApis  -- before Payment system starts
+        fetchPaymentPluginApis();
+        for (String pluginName : paymentPluginApis.keySet()) {
+            paymentProviderPluginRegistry.register(paymentPluginApis.get(pluginName), pluginName);
+        }
     }
 
     @LifecycleHandlerType(LifecycleHandlerType.LifecycleLevel.UNREGISTER_EVENTS)
     public void unregisterForExternalEvents() {
+        releasePaymentPluginApis();
+    }
+
+    @LifecycleHandlerType(LifecycleLevel.START_SERVICE)
+    public void startFramework() {
     }
 
+
     @LifecycleHandlerType(LifecycleLevel.STOP_SERVICE)
     public void stop() {
         try {
@@ -177,6 +196,28 @@ public class DefaultOSGIService implements OSGIService {
         return felix;
     }
 
+    private void fetchPaymentPluginApis() throws InvalidSyntaxException {
+
+        final BundleContext context = framework.getBundleContext();
+
+        paymentApiReferences = (ServiceReference<PaymentPluginApi>[]) context.getServiceReferences(PaymentPluginApi.class.getName(), null);
+        final ImmutableMap.Builder paymentPluginApisBuilder = ImmutableMap.builder();
+        for (ServiceReference<PaymentPluginApi> ref : paymentApiReferences) {
+            // TODO 'name' STEPH needs to be in API
+            paymentPluginApisBuilder.put(ref.getProperty("name"), context.getService(ref));
+        }
+        paymentPluginApis = paymentPluginApisBuilder.build();
+    }
+
+
+    private void releasePaymentPluginApis() {
+        for (ServiceReference<PaymentPluginApi> ref : paymentApiReferences) {
+            final BundleContext context = framework.getBundleContext();
+            context.ungetService(ref);
+        }
+        paymentApiReferences = null;
+    }
+
     private void pruneOSGICache() {
         final String path = osgiConfig.getOSGIBundleRootDir() + "/" + osgiConfig.getOSGIBundleCacheName();
         deleteUnderDirectory(new File(path));
diff --git a/osgi/src/main/java/com/ning/billing/osgi/KillbillActivator.java b/osgi/src/main/java/com/ning/billing/osgi/KillbillActivator.java
index 586aa12..5b83eeb 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/KillbillActivator.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/KillbillActivator.java
@@ -16,20 +16,27 @@
 
 package com.ning.billing.osgi;
 
+import java.util.List;
+
 import javax.inject.Inject;
 
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
 import com.ning.billing.osgi.api.OSGIKillbill;
+import com.ning.billing.payment.plugin.api.PaymentPluginApi;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
 
 public class KillbillActivator implements BundleActivator {
 
     private final OSGIKillbill osgiKillbill;
 
     private volatile ServiceRegistration osgiKillbillRegistration;
-
     @Inject
     public KillbillActivator(final OSGIKillbill osgiKillbill) {
         this.osgiKillbill = osgiKillbill;
@@ -52,17 +59,4 @@ public class KillbillActivator implements BundleActivator {
         }
     }
 
-    //    public PaymentPluginApi getPaymentPluginApiForPlugin(final String pluginName) {
-    //        try {
-    //            final ServiceReference<PaymentPluginApi>[] paymentApiReferences = (ServiceReference<PaymentPluginApi>[]) context.getServiceReferences(PaymentPluginApi.class.getName(), "(name=hello)");
-    //            final PaymentPluginApi pluginApi = context.getService(paymentApiReferences[0]);
-    //            return pluginApi;
-    //        } catch (InvalidSyntaxException e) {
-    //            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
-    //        } finally {
-    //            //context.ungetService(paymentApiReferences[0]);
-    //            // STEPH TODO leak reference here
-    //        }
-    //        return null;
-    //    }
 }