Providers and APIs in OSGi can be discovered even if they are installed prior to the installation of jclouds-core.

This commit is contained in:
Ioannis Canellos 2012-06-13 20:39:48 +03:00
parent c0e23d4460
commit ff2c2fb3e3
2 changed files with 28 additions and 0 deletions

View File

@ -44,6 +44,7 @@ public class Activator implements BundleActivator {
*/ */
@Override @Override
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
bundleListener.start(context);
context.addBundleListener(bundleListener); context.addBundleListener(bundleListener);
} }
@ -66,6 +67,7 @@ public class Activator implements BundleActivator {
*/ */
@Override @Override
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {
bundleListener.stop(context);
context.removeBundleListener(bundleListener); context.removeBundleListener(bundleListener);
ProviderRegistry.clear(); ProviderRegistry.clear();
ApiRegistry.clear(); ApiRegistry.clear();

View File

@ -23,6 +23,7 @@ import org.jclouds.apis.ApiRegistry;
import org.jclouds.providers.ProviderMetadata; import org.jclouds.providers.ProviderMetadata;
import org.jclouds.providers.ProviderRegistry; import org.jclouds.providers.ProviderRegistry;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener; import org.osgi.framework.BundleListener;
@ -43,6 +44,31 @@ public class MetadataBundleListener implements BundleListener {
private Map<Long, ProviderMetadata> providerMetadataMap = new HashMap<Long, ProviderMetadata>(); private Map<Long, ProviderMetadata> providerMetadataMap = new HashMap<Long, ProviderMetadata>();
private Map<Long, ApiMetadata> apiMetadataMap = new HashMap<Long, ApiMetadata>(); private Map<Long, ApiMetadata> apiMetadataMap = new HashMap<Long, ApiMetadata>();
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 @Override
public void bundleChanged(BundleEvent event) { public void bundleChanged(BundleEvent event) {
ProviderMetadata providerMetadata; ProviderMetadata providerMetadata;