killbill-aplcache

osgi: remove dependency on fileinstall bundle OSGI bundles

2/21/2013 1:46:34 AM

Details

osgi/pom.xml 4(+0 -4)

diff --git a/osgi/pom.xml b/osgi/pom.xml
index 01e9d7f..c7d393d 100644
--- a/osgi/pom.xml
+++ b/osgi/pom.xml
@@ -34,10 +34,6 @@
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.fileinstall</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.felix</groupId>
             <artifactId>org.osgi.core</artifactId>
         </dependency>
         <dependency>
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 2f2fc65..abe221f 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/DefaultOSGIService.java
@@ -24,7 +24,6 @@ import java.util.Map;
 
 import javax.inject.Inject;
 
-import org.apache.felix.fileinstall.internal.FileInstall;
 import org.apache.felix.framework.Felix;
 import org.apache.felix.framework.util.FelixConstants;
 import org.osgi.framework.Bundle;
@@ -55,6 +54,7 @@ public class DefaultOSGIService implements OSGIService {
     private static final Logger logger = LoggerFactory.getLogger(DefaultOSGIService.class);
 
     private final OSGIConfig osgiConfig;
+    private final PureOSGIBundleFinder osgiBundleFinder;
     private final PluginFinder pluginFinder;
     private final PluginConfigServiceApi pluginConfigServiceApi;
     private final KillbillActivator killbillActivator;
@@ -62,10 +62,11 @@ public class DefaultOSGIService implements OSGIService {
     private Framework framework;
 
     @Inject
-    public DefaultOSGIService(final OSGIConfig osgiConfig, final PluginFinder pluginFinder,
-                              final PluginConfigServiceApi pluginConfigServiceApi,
+    public DefaultOSGIService(final OSGIConfig osgiConfig, final PureOSGIBundleFinder osgiBundleFinder,
+                              final PluginFinder pluginFinder, final PluginConfigServiceApi pluginConfigServiceApi,
                               final KillbillActivator killbillActivator) {
         this.osgiConfig = osgiConfig;
+        this.osgiBundleFinder = osgiBundleFinder;
         this.pluginFinder = pluginFinder;
         this.pluginConfigServiceApi = pluginConfigServiceApi;
         this.killbillActivator = killbillActivator;
@@ -123,11 +124,11 @@ public class DefaultOSGIService implements OSGIService {
             final BundleContext context = framework.getBundleContext();
 
             // Install all bundles and create service mapping
-            // TODO PIERRE Could we leverage Felix fileinstall plugin to manage Killbill plugins?
 
             final List<Bundle> installedBundles = new LinkedList<Bundle>();
             installAllJavaBundles(context, installedBundles);
-            installAllJRubyBundles(context, installedBundles);
+            installAllJavaPluginBundles(context, installedBundles);
+            installAllJRubyPluginBundles(context, installedBundles);
 
             // Start all the bundles
             for (final Bundle bundle : installedBundles) {
@@ -135,6 +136,7 @@ public class DefaultOSGIService implements OSGIService {
                 try {
                     bundle.start();
                 } catch (BundleException e) {
+                    // TODO PIERRE Don't try to start Fragment bundles
                     logger.warn("Unable to start bundle", e);
                 }
             }
@@ -146,6 +148,15 @@ public class DefaultOSGIService implements OSGIService {
     }
 
     private void installAllJavaBundles(final BundleContext context, final List<Bundle> installedBundles) throws PluginConfigException, BundleException {
+        final List<String> bundleJarPaths = osgiBundleFinder.getLatestBundles();
+        for (final String cur : bundleJarPaths) {
+            logger.info("Installing Java OSGI bundle in {}", cur);
+            final Bundle bundle = context.installBundle("file:" + cur);
+            installedBundles.add(bundle);
+        }
+    }
+
+    private void installAllJavaPluginBundles(final BundleContext context, final List<Bundle> installedBundles) throws PluginConfigException, BundleException {
         final List<PluginJavaConfig> pluginJavaConfigs = pluginFinder.getLatestJavaPlugins();
         for (final PluginJavaConfig cur : pluginJavaConfigs) {
             logger.info("Installing Java bundle for plugin {} in {}", cur.getPluginName(), cur.getBundleJarPath());
@@ -155,7 +166,7 @@ public class DefaultOSGIService implements OSGIService {
         }
     }
 
-    private void installAllJRubyBundles(final BundleContext context, final List<Bundle> installedBundles) throws PluginConfigException, BundleException {
+    private void installAllJRubyPluginBundles(final BundleContext context, final List<Bundle> installedBundles) throws PluginConfigException, BundleException {
         final List<PluginRubyConfig> pluginRubyConfigs = pluginFinder.getLatestRubyPlugins();
         for (final PluginRubyConfig cur : pluginRubyConfigs) {
             logger.info("Installing JRuby bundle for plugin {} in {}", cur.getPluginName(), cur.getRubyLoadDir());
@@ -178,12 +189,11 @@ public class DefaultOSGIService implements OSGIService {
         final Map<Object, Object> felixConfig = new HashMap<Object, Object>();
         felixConfig.putAll(config);
 
-        // Install default bundles in the Framework: Killbill bundle and Felix fileinstall bundle
+        // Install default bundles in the Framework: Killbill bundle only for now
         // Note! Think twice before adding a bundle here as it will run inside the System bundle. This means the bundle
         // context that the bundle will see is the System bundle one, which will break e.g. resources lookup
         felixConfig.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP,
-                        ImmutableList.<BundleActivator>of(new FileInstall(),
-                                                          killbillActivator));
+                        ImmutableList.<BundleActivator>of(killbillActivator));
 
         final Framework felix = new Felix(felixConfig);
         felix.init();
diff --git a/osgi/src/main/java/com/ning/billing/osgi/glue/DefaultOSGIModule.java b/osgi/src/main/java/com/ning/billing/osgi/glue/DefaultOSGIModule.java
index 6b9ff07..32d2811 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/glue/DefaultOSGIModule.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/glue/DefaultOSGIModule.java
@@ -26,6 +26,7 @@ import org.skife.config.ConfigurationObjectFactory;
 import com.ning.billing.osgi.DefaultOSGIKillbill;
 import com.ning.billing.osgi.DefaultOSGIService;
 import com.ning.billing.osgi.KillbillActivator;
+import com.ning.billing.osgi.PureOSGIBundleFinder;
 import com.ning.billing.osgi.api.OSGIKillbill;
 import com.ning.billing.osgi.api.OSGIService;
 import com.ning.billing.osgi.api.OSGIServiceRegistration;
@@ -71,6 +72,7 @@ public class DefaultOSGIModule extends AbstractModule {
         bind(OSGIService.class).to(DefaultOSGIService.class).asEagerSingleton();
 
         bind(KillbillActivator.class).asEagerSingleton();
+        bind(PureOSGIBundleFinder.class).asEagerSingleton();
         bind(PluginFinder.class).asEagerSingleton();
         bind(PluginConfigServiceApi.class).to(DefaultPluginConfigServiceApi.class).asEagerSingleton();
         bind(OSGIKillbill.class).to(DefaultOSGIKillbill.class).asEagerSingleton();
diff --git a/osgi/src/main/java/com/ning/billing/osgi/pluginconf/PluginFinder.java b/osgi/src/main/java/com/ning/billing/osgi/pluginconf/PluginFinder.java
index fd4963a..23d67aa 100644
--- a/osgi/src/main/java/com/ning/billing/osgi/pluginconf/PluginFinder.java
+++ b/osgi/src/main/java/com/ning/billing/osgi/pluginconf/PluginFinder.java
@@ -114,7 +114,7 @@ public class PluginFinder {
     }
 
     private <T extends PluginConfig> void loadPluginsForLanguage(final PluginLanguage pluginLanguage) throws PluginConfigException {
-        final String rootDirPath = osgiConfig.getRootInstallationDir() + "/" + pluginLanguage.toString().toLowerCase();
+        final String rootDirPath = osgiConfig.getRootInstallationDir() + "/plugins/" + pluginLanguage.toString().toLowerCase();
         final File rootDir = new File(rootDirPath);
         if (!rootDir.exists() || !rootDir.isDirectory()) {
             logger.warn("Configuration root dir {} is not a valid directory", rootDirPath);
diff --git a/osgi/src/main/java/com/ning/billing/osgi/PureOSGIBundleFinder.java b/osgi/src/main/java/com/ning/billing/osgi/PureOSGIBundleFinder.java
new file mode 100644
index 0000000..0eebfd1
--- /dev/null
+++ b/osgi/src/main/java/com/ning/billing/osgi/PureOSGIBundleFinder.java
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.ning.billing.osgi.pluginconf.PluginConfigException;
+import com.ning.billing.util.config.OSGIConfig;
+
+import com.google.common.collect.ImmutableList;
+
+@Singleton
+public class PureOSGIBundleFinder {
+
+    private final Logger logger = LoggerFactory.getLogger(Logger.class);
+
+    private final OSGIConfig osgiConfig;
+
+    @Inject
+    public PureOSGIBundleFinder(final OSGIConfig osgiConfig) {
+        this.osgiConfig = osgiConfig;
+    }
+
+    public List<String> getLatestBundles() throws PluginConfigException {
+        final String rootDirPath = osgiConfig.getRootInstallationDir() + "/platform/";
+        final File rootDir = new File(rootDirPath);
+        if (!rootDir.exists() || !rootDir.isDirectory()) {
+            logger.warn("Configuration root dir {} is not a valid directory", rootDirPath);
+            return ImmutableList.<String>of();
+        }
+
+        final File[] files = rootDir.listFiles();
+        if (files == null) {
+            return ImmutableList.<String>of();
+        }
+
+        final List<String> bundles = new ArrayList<String>();
+        for (final File bundleJar : files) {
+            if (bundleJar.isFile()) {
+                bundles.add(bundleJar.getAbsolutePath());
+            }
+        }
+
+        return bundles;
+    }
+}