From ff2c2fb3e30366243578dbf195c7301e5334f0f6 Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Wed, 13 Jun 2012 20:39:48 +0300 Subject: [PATCH] Providers and APIs in OSGi can be discovered even if they are installed prior to the installation of jclouds-core. --- .../main/java/org/jclouds/osgi/Activator.java | 2 ++ .../jclouds/osgi/MetadataBundleListener.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/core/src/main/java/org/jclouds/osgi/Activator.java b/core/src/main/java/org/jclouds/osgi/Activator.java index 087585dd85..e6ee8f1398 100644 --- a/core/src/main/java/org/jclouds/osgi/Activator.java +++ b/core/src/main/java/org/jclouds/osgi/Activator.java @@ -44,6 +44,7 @@ public class Activator implements BundleActivator { */ @Override public void start(BundleContext context) throws Exception { + bundleListener.start(context); context.addBundleListener(bundleListener); } @@ -66,6 +67,7 @@ public class Activator implements BundleActivator { */ @Override public void stop(BundleContext context) throws Exception { + bundleListener.stop(context); context.removeBundleListener(bundleListener); ProviderRegistry.clear(); ApiRegistry.clear(); diff --git a/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java b/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java index 9a996c43d6..75d9886172 100644 --- a/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java +++ b/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java @@ -23,6 +23,7 @@ import org.jclouds.apis.ApiRegistry; import org.jclouds.providers.ProviderMetadata; import org.jclouds.providers.ProviderRegistry; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; @@ -43,6 +44,31 @@ public class MetadataBundleListener implements BundleListener { private Map providerMetadataMap = new HashMap(); private Map apiMetadataMap = new HashMap(); + + public void start(BundleContext bundleContext) { + bundleContext.addBundleListener(this); + for (Bundle bundle : bundleContext.getBundles()) { + if (bundle.getState() == Bundle.ACTIVE) { + ProviderMetadata providerMetadata = getProviderMetadata(bundle); + ApiMetadata apiMetadata = getApiMetadata(bundle); + + if (providerMetadata != null) { + ProviderRegistry.registerProvider(providerMetadata); + providerMetadataMap.put(bundle.getBundleId(), providerMetadata); + } + if (apiMetadata != null) { + ApiRegistry.registerApi(apiMetadata); + apiMetadataMap.put(bundle.getBundleId(), apiMetadata); + } + } + } + } + + public void stop(BundleContext bundleContext) { + providerMetadataMap.clear(); + apiMetadataMap.clear(); + } + @Override public void bundleChanged(BundleEvent event) { ProviderMetadata providerMetadata;