diff --git a/core/src/main/java/org/jclouds/apis/Apis.java b/core/src/main/java/org/jclouds/apis/Apis.java index 7631018613..08eb3be905 100644 --- a/core/src/main/java/org/jclouds/apis/Apis.java +++ b/core/src/main/java/org/jclouds/apis/Apis.java @@ -26,6 +26,7 @@ import java.util.NoSuchElementException; import java.util.ServiceLoader; import org.jclouds.View; +import org.jclouds.osgi.ApiRegistry; import com.google.common.base.Function; import com.google.common.base.Predicate; @@ -59,9 +60,8 @@ public class Apis { * * @return all available apis loaded from classpath via ServiceLoader */ - @SuppressWarnings("unchecked") private static Iterable fromServiceLoader() { - return Iterable.class.cast(ServiceLoader.load(ApiMetadata.class)); + return ServiceLoader.load(ApiMetadata.class); } /** diff --git a/core/src/main/java/org/jclouds/osgi/Activator.java b/core/src/main/java/org/jclouds/osgi/Activator.java index f8a5cc9b6a..8f58deee99 100644 --- a/core/src/main/java/org/jclouds/osgi/Activator.java +++ b/core/src/main/java/org/jclouds/osgi/Activator.java @@ -18,8 +18,6 @@ */ package org.jclouds.osgi; -import org.jclouds.apis.ApiRegistry; -import org.jclouds.providers.ProviderRegistry; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -32,19 +30,18 @@ public class Activator implements BundleActivator { private MetadataBundleListener bundleListener = new MetadataBundleListener(); /** - * Called when this bundle is started so the Framework can perform the - * bundle-specific activities necessary to start this bundle. This method - * can be used to register services or to allocate any resources that this - * bundle needs. + * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start + * this bundle. This method can be used to register services or to allocate any resources that this bundle needs. *

*

* This method must complete and return to its caller in a timely manner. - * - * @param context The execution context of the bundle being started. - * @throws Exception If this method throws an exception, this - * bundle is marked as stopped and the Framework will remove this - * bundle's listeners, unregister all services registered by this - * bundle, and release all services used by this bundle. + * + * @param context + * The execution context of the bundle being started. + * @throws Exception + * If this method throws an exception, this bundle is marked as stopped and the Framework will remove this + * bundle's listeners, unregister all services registered by this bundle, and release all services used by + * this bundle. */ @Override public void start(BundleContext context) throws Exception { @@ -92,21 +89,20 @@ public class Activator implements BundleActivator { } /** - * Called when this bundle is stopped so the Framework can perform the - * bundle-specific activities necessary to stop the bundle. In general, this - * method should undo the work that the BundleActivator.start - * method started. There should be no active threads that were started by - * this bundle when this bundle returns. A stopped bundle must not call any - * Framework objects. + * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop + * the bundle. In general, this method should undo the work that the BundleActivator.start method + * started. There should be no active threads that were started by this bundle when this bundle returns. A stopped + * bundle must not call any Framework objects. *

*

* This method must complete and return to its caller in a timely manner. - * - * @param context The execution context of the bundle being stopped. - * @throws Exception If this method throws an exception, the - * bundle is still marked as stopped, and the Framework will remove - * the bundle's listeners, unregister all services registered by the - * bundle, and release all services used by the bundle. + * + * @param context + * The execution context of the bundle being stopped. + * @throws Exception + * If this method throws an exception, the bundle is still marked as stopped, and the Framework will + * remove the bundle's listeners, unregister all services registered by the bundle, and release all + * services used by the bundle. */ @Override public void stop(BundleContext context) throws Exception { diff --git a/core/src/main/java/org/jclouds/osgi/ApiListener.java b/core/src/main/java/org/jclouds/osgi/ApiListener.java index 4ff0a73e5a..ddd4474021 100644 --- a/core/src/main/java/org/jclouds/osgi/ApiListener.java +++ b/core/src/main/java/org/jclouds/osgi/ApiListener.java @@ -22,25 +22,28 @@ package org.jclouds.osgi; import org.jclouds.apis.ApiMetadata; /** - * A listener interface for {@link org.jclouds.apis.ApiMetadata}. - * In OSGi a api can be added or removed dynamically. + * A listener interface for {@link org.jclouds.apis.ApiMetadata}. In OSGi a api can be added or removed dynamically. * OSGi services using this interface will receive a notification whenever this happens. */ public interface ApiListener { /** * Method to be called when an api gets added. - * - * @param api The api that was added. - * @param The {@link org.jclouds.apis.ApiMetadata}. + * + * @param api + * The api that was added. + * @param + * The {@link org.jclouds.apis.ApiMetadata}. */ void added(A api); /** * Method to be called when an api gets removed. - * - * @param api The api that was added. - * @param The {@link ApiMetadata}. + * + * @param api + * The api that was added. + * @param + * The {@link ApiMetadata}. */ void removed(A api); } diff --git a/core/src/main/java/org/jclouds/apis/ApiRegistry.java b/core/src/main/java/org/jclouds/osgi/ApiRegistry.java similarity index 66% rename from core/src/main/java/org/jclouds/apis/ApiRegistry.java rename to core/src/main/java/org/jclouds/osgi/ApiRegistry.java index 409677330a..74f54737a1 100644 --- a/core/src/main/java/org/jclouds/apis/ApiRegistry.java +++ b/core/src/main/java/org/jclouds/osgi/ApiRegistry.java @@ -16,32 +16,36 @@ * specific language governing permissions and limitations * under the License. */ -package org.jclouds.apis; +package org.jclouds.osgi; import java.util.Set; +import org.jclouds.apis.ApiMetadata; + +import com.google.common.annotations.Beta; import com.google.common.collect.Sets; /** * A registry for holding {@link org.jclouds.apis.ApiMetadata}. */ +@Beta public class ApiRegistry { - private static final Set apis = Sets.newHashSet(); + private static final Set apis = Sets.newHashSet(); - public static void registerApi(ApiMetadata api) { - apis.add(api); - } + public static void registerApi(ApiMetadata api) { + apis.add(api); + } - public static void unRegisterApi(ApiMetadata api) { - apis.remove(api); - } + public static void unRegisterApi(ApiMetadata api) { + apis.remove(api); + } - public static Iterable fromRegistry() { - return Iterable.class.cast(apis); - } + public static Iterable fromRegistry() { + return apis; + } - public static void clear() { - apis.clear(); - } + public static void clear() { + apis.clear(); + } } diff --git a/core/src/main/java/org/jclouds/osgi/Bundles.java b/core/src/main/java/org/jclouds/osgi/Bundles.java index a37f8ec18c..d15be1a087 100644 --- a/core/src/main/java/org/jclouds/osgi/Bundles.java +++ b/core/src/main/java/org/jclouds/osgi/Bundles.java @@ -19,16 +19,16 @@ package org.jclouds.osgi; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Predicates.notNull; +import static org.jclouds.util.Strings2.toStringAndClose; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import org.jclouds.util.Strings2; import org.osgi.framework.Bundle; import com.google.common.base.Function; -import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableSet; @@ -40,30 +40,30 @@ import com.google.common.collect.ImmutableSet; */ public final class Bundles { private Bundles() { - } /** - * instantiates the supplied classnames using the bundle classloader, and - * casts to the supplied type. Any errors are silently ignored. + * instantiates the supplied classnames using the bundle classloader, and casts to the supplied type. Any errors are + * silently ignored. * * @return instances that could be instantiated without error. */ - public static ImmutableSet instantiateAvailableClasses(Bundle bundle, Iterable classNames, Class type) { + public static ImmutableSet instantiateAvailableClasses(Bundle bundle, Iterable classNames, + Class type) { checkNotNull(bundle, "bundle"); checkNotNull(classNames, "classNames"); checkNotNull(type, "type"); return FluentIterable.from(classNames) .transform(loadClassIfAssignableFrom(bundle, type)) - .filter(Predicates.notNull()) + .filter(notNull()) .transform(instantiateIfPossible(type)) - .filter(Predicates.notNull()) + .filter(notNull()) .toSet(); } /** - * A function that loads classes from the bundle, or returns null if the - * class isn't found or assignable by the input parameter + * A function that loads classes from the bundle, or returns null if the class isn't found or assignable by the input + * parameter * * @param bundle * where to find classes @@ -90,8 +90,7 @@ public final class Bundles { } /** - * A function that instantiates classes or returns null, if it encounters any - * problems. + * A function that instantiates classes or returns null, if it encounters any problems. * * @param clazz * superclass to cast as @@ -118,10 +117,9 @@ public final class Bundles { * The path to the resource. * @param bundle * The bundle to read from. - * @return strings delimited by newline in the stream or empty set, on any - * exception. + * @return strings delimited by newline in the stream or empty set, on any exception. */ - public static ImmutableSet stringsForResorceInBundle(String resourcePath, Bundle bundle) { + public static ImmutableSet stringsForResourceInBundle(String resourcePath, Bundle bundle) { checkNotNull(resourcePath, "resourcePath"); checkNotNull(bundle, "bundle"); @@ -131,13 +129,12 @@ public final class Bundles { try { return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream())); } catch (IOException e) { - return ImmutableSet.of(); } catch (RuntimeException ex) { - return ImmutableSet.of(); } + return ImmutableSet.of(); } private static Iterable splitOrEmptyAndClose(InputStream in) throws IOException { - return Splitter.on('\n').omitEmptyStrings().split(Strings2.toStringAndClose(in)); + return Splitter.on('\n').omitEmptyStrings().split(toStringAndClose(in)); } } diff --git a/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java b/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java index bf5e222cfb..c59cf10973 100644 --- a/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java +++ b/core/src/main/java/org/jclouds/osgi/MetadataBundleListener.java @@ -19,14 +19,15 @@ package org.jclouds.osgi; import static org.jclouds.osgi.Bundles.instantiateAvailableClasses; -import static org.jclouds.osgi.Bundles.stringsForResorceInBundle; +import static org.jclouds.osgi.Bundles.stringsForResourceInBundle; +import static org.osgi.framework.BundleEvent.STARTED; +import static org.osgi.framework.BundleEvent.STOPPED; +import static org.osgi.framework.BundleEvent.STOPPING; import java.util.List; import org.jclouds.apis.ApiMetadata; -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; @@ -37,9 +38,9 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; /** - * A {@link BundleListener} that listens for {@link BundleEvent} and searches for {@link org.jclouds.providers.ProviderMetadata} and {@link org.jclouds.apis.ApiMetadata} in newly - * installed Bundles. This is used as a workaround for OSGi environments where the ServiceLoader cannot cross bundle - * boundaries. + * A {@link BundleListener} that listens for {@link BundleEvent} and searches for + * {@link org.jclouds.providers.ProviderMetadata} and {@link org.jclouds.apis.ApiMetadata} in newly installed Bundles. + * This is used as a workaround for OSGi environments where the ServiceLoader cannot cross bundle boundaries. * * @author iocanel */ @@ -51,11 +52,10 @@ public class MetadataBundleListener implements BundleListener { private final List providerListeners = Lists.newArrayList(); private final List apiListeners = Lists.newArrayList(); - /** - * Starts the listener. - * Checks the bundles that are already active and registers {@link ProviderMetadata} and {@link ApiMetadata} found. - * Registers the itself as a {@link BundleListener}. + * Starts the listener. Checks the bundles that are already active and registers {@link ProviderMetadata} and + * {@link ApiMetadata} found. Registers the itself as a {@link BundleListener}. + * * @param bundleContext */ public synchronized void start(BundleContext bundleContext) { @@ -69,9 +69,8 @@ public class MetadataBundleListener implements BundleListener { } /** - * Stops the listener. - * Removes itself from the {@link BundleListener}s. - * Clears metadata maps and listeners lists. + * Stops the listener. Removes itself from the {@link BundleListener}s. Clears metadata maps and listeners lists. + * * @param bundleContext */ public void stop(BundleContext bundleContext) { @@ -85,20 +84,21 @@ public class MetadataBundleListener implements BundleListener { @Override public synchronized void bundleChanged(BundleEvent event) { switch (event.getType()) { - case BundleEvent.STARTED: - addBundle(event.getBundle()); - break; - case BundleEvent.STOPPING: - case BundleEvent.STOPPED: - removeBundle(event.getBundle()); - break; + case STARTED: + addBundle(event.getBundle()); + break; + case STOPPING: + case STOPPED: + removeBundle(event.getBundle()); + break; } } /** - * Searches for {@link ProviderMetadata} and {@link ApiMetadata} inside the {@link Bundle}. - * If metadata are found they are registered in the {@link ProviderRegistry} and {@link ApiRegistry}. - * Also the {@link ProviderListener} and {@link ApiListener} are notified. + * Searches for {@link ProviderMetadata} and {@link ApiMetadata} inside the {@link Bundle}. If metadata are found + * they are registered in the {@link ProviderRegistry} and {@link ApiRegistry}. Also the {@link ProviderListener} and + * {@link ApiListener} are notified. + * * @param bundle */ private synchronized void addBundle(Bundle bundle) { @@ -124,9 +124,10 @@ public class MetadataBundleListener implements BundleListener { } /** - * Searches for {@link ProviderMetadata} and {@link ApiMetadata} registered under the {@link Bundle} id. - * If metadata are found they are removed the {@link ProviderRegistry} and {@link ApiRegistry}. - * Also the {@link ProviderListener} and {@link ApiListener} are notified. + * Searches for {@link ProviderMetadata} and {@link ApiMetadata} registered under the {@link Bundle} id. If metadata + * are found they are removed the {@link ProviderRegistry} and {@link ApiRegistry}. Also the {@link ProviderListener} + * and {@link ApiListener} are notified. + * * @param bundle */ private synchronized void removeBundle(Bundle bundle) { @@ -146,30 +147,32 @@ public class MetadataBundleListener implements BundleListener { /** * Creates an instance of {@link ProviderMetadata} from the {@link Bundle}. - * + * * @param bundle * @return */ public Iterable listProviderMetadata(Bundle bundle) { - Iterable classNames = stringsForResorceInBundle("/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle); + Iterable classNames = stringsForResourceInBundle( + "/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle); return instantiateAvailableClasses(bundle, classNames, ProviderMetadata.class); } /** * Creates an instance of {@link ApiMetadata} from the {@link Bundle}. - * + * * @param bundle * @return */ public Iterable listApiMetadata(Bundle bundle) { - Iterable classNames = stringsForResorceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle); + Iterable classNames = stringsForResourceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle); return instantiateAvailableClasses(bundle, classNames, ApiMetadata.class); } /** * Adds a {@link ProviderListener} and notifies it of existing {@link ProviderMetadata}. - * - * @param listener The listener. + * + * @param listener + * The listener. */ public synchronized void addProviderListener(ProviderListener listener) { providerListeners.add(listener); @@ -180,8 +183,9 @@ public class MetadataBundleListener implements BundleListener { /** * Removes the {@link ProviderListener} - * - * @param listener The listener + * + * @param listener + * The listener */ public synchronized void removeProviderListener(ProviderListener listener) { providerListeners.remove(listener); @@ -189,7 +193,7 @@ public class MetadataBundleListener implements BundleListener { /** * Adds a {@link ApiListener} and notifies it of existing {@link ApiMetadata}. - * + * * @param listener */ public synchronized void addApiListenerListener(ApiListener listener) { @@ -201,7 +205,7 @@ public class MetadataBundleListener implements BundleListener { /** * Removes the {@link ApiListener} - * + * * @param listener */ public synchronized void removeApiListenerListener(ApiListener listener) { diff --git a/core/src/main/java/org/jclouds/osgi/ProviderListener.java b/core/src/main/java/org/jclouds/osgi/ProviderListener.java index 39e38755df..507dd4ed02 100644 --- a/core/src/main/java/org/jclouds/osgi/ProviderListener.java +++ b/core/src/main/java/org/jclouds/osgi/ProviderListener.java @@ -22,25 +22,28 @@ package org.jclouds.osgi; import org.jclouds.providers.ProviderMetadata; /** - * A listener interface for {@link ProviderMetadata}. - * In OSGi a provider can be added or removed dynamically. - * OSGi services using this interface will receive a notification whenever this happens. + * A listener interface for {@link ProviderMetadata}. In OSGi a provider can be added or removed dynamically. OSGi + * services using this interface will receive a notification whenever this happens. */ public interface ProviderListener { /** * Method to be called when a Provider gets added. - * - * @param provider The provider that was added. - * @param

The {@link ProviderMetadata}. + * + * @param provider + * The provider that was added. + * @param

+ * The {@link ProviderMetadata}. */

void added(P provider); /** * Method to be called when a Provider gets removed. - * - * @param provider The provider that was added. - * @param

The {@link ProviderMetadata}. + * + * @param provider + * The provider that was added. + * @param

+ * The {@link ProviderMetadata}. */

void removed(P provider); diff --git a/core/src/main/java/org/jclouds/providers/ProviderRegistry.java b/core/src/main/java/org/jclouds/osgi/ProviderRegistry.java similarity index 90% rename from core/src/main/java/org/jclouds/providers/ProviderRegistry.java rename to core/src/main/java/org/jclouds/osgi/ProviderRegistry.java index ece39834e9..1ab8bcf10a 100644 --- a/core/src/main/java/org/jclouds/providers/ProviderRegistry.java +++ b/core/src/main/java/org/jclouds/osgi/ProviderRegistry.java @@ -16,15 +16,19 @@ * specific language governing permissions and limitations * under the License. */ -package org.jclouds.providers; +package org.jclouds.osgi; import java.util.Set; +import org.jclouds.providers.ProviderMetadata; + +import com.google.common.annotations.Beta; import com.google.common.collect.Sets; /** * A registry for holding {@link org.jclouds.providers.ProviderMetadata}. */ +@Beta public class ProviderRegistry { private static final Set providers = Sets.newHashSet(); @@ -38,7 +42,7 @@ public class ProviderRegistry { } public static Iterable fromRegistry() { - return Iterable.class.cast(providers); + return providers; } public static void clear() { diff --git a/core/src/main/java/org/jclouds/providers/Providers.java b/core/src/main/java/org/jclouds/providers/Providers.java index c4b79e54ea..36d57255ba 100644 --- a/core/src/main/java/org/jclouds/providers/Providers.java +++ b/core/src/main/java/org/jclouds/providers/Providers.java @@ -24,14 +24,15 @@ import static com.google.common.collect.Iterables.find; import java.util.NoSuchElementException; import java.util.ServiceLoader; -import com.google.common.collect.ImmutableSet; import org.jclouds.Context; import org.jclouds.View; import org.jclouds.apis.ApiMetadata; +import org.jclouds.osgi.ProviderRegistry; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableSet; import com.google.common.reflect.TypeToken; /** @@ -72,9 +73,8 @@ public class Providers { * * @return all available providers loaded from classpath via ServiceLoader */ - @SuppressWarnings("unchecked") public static Iterable fromServiceLoader() { - return Iterable.class.cast(ServiceLoader.load(ProviderMetadata.class)); + return ServiceLoader.load(ProviderMetadata.class); } /** diff --git a/core/src/test/java/org/jclouds/osgi/BundlesTest.java b/core/src/test/java/org/jclouds/osgi/BundlesTest.java index fc4c319295..173bfbb6d1 100644 --- a/core/src/test/java/org/jclouds/osgi/BundlesTest.java +++ b/core/src/test/java/org/jclouds/osgi/BundlesTest.java @@ -44,12 +44,12 @@ public class BundlesTest { @Test public void testInstantiateAvailableClassesWhenAllAssignable() throws ClassNotFoundException { Bundle bundle = createMock(Bundle.class); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")) - .andReturn(JcloudsTestBlobStoreProviderMetadata.class); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")) - .andReturn(JcloudsTestComputeProviderMetadata.class); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")) - .andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( + JcloudsTestBlobStoreProviderMetadata.class); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn( + JcloudsTestComputeProviderMetadata.class); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( + JcloudsTestYetAnotherComputeProviderMetadata.class); replay(bundle); Iterable providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( @@ -65,12 +65,12 @@ public class BundlesTest { @Test public void testInstantiateAvailableClassesWhenNotAllAssignable() throws ClassNotFoundException { Bundle bundle = createMock(Bundle.class); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")) - .andReturn(JcloudsTestBlobStoreProviderMetadata.class); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")) - .andReturn(JcloudsTestComputeApiMetadata.class); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")) - .andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( + JcloudsTestBlobStoreProviderMetadata.class); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn( + JcloudsTestComputeApiMetadata.class); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( + JcloudsTestYetAnotherComputeProviderMetadata.class); replay(bundle); Iterable providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( @@ -90,7 +90,7 @@ public class BundlesTest { expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(null); replay(bundle); - assertEquals(Bundles.stringsForResorceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle), + assertEquals(Bundles.stringsForResourceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle), ImmutableSet.of()); verify(bundle); @@ -100,13 +100,12 @@ public class BundlesTest { public void testStringsForResourcesInBundleWhenResourcePresent() throws Exception { Bundle bundle = createMock(Bundle.class); - expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")) - .andReturn(getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")); + expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")); replay(bundle); - assertEquals(Bundles.stringsForResorceInBundle( - "/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle), ImmutableSet.of( - "org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata", + assertEquals(Bundles.stringsForResourceInBundle("/META-INF/services/org.jclouds.providers.ProviderMetadata", + bundle), ImmutableSet.of("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata", "org.jclouds.providers.JcloudsTestComputeProviderMetadata", "org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")); diff --git a/core/src/test/java/org/jclouds/osgi/MetadataBundleListenerTest.java b/core/src/test/java/org/jclouds/osgi/MetadataBundleListenerTest.java index 7ea1b43621..d659be9c1f 100644 --- a/core/src/test/java/org/jclouds/osgi/MetadataBundleListenerTest.java +++ b/core/src/test/java/org/jclouds/osgi/MetadataBundleListenerTest.java @@ -18,7 +18,22 @@ */ package org.jclouds.osgi; -import com.google.common.collect.Lists; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; + import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.JcloudsTestBlobStoreApiMetadata; import org.jclouds.apis.JcloudsTestComputeApiMetadata; @@ -31,45 +46,36 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleEvent; import org.testng.annotations.Test; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.List; - -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; +import com.google.common.collect.Lists; /** * * @author iocanel - * + * */ public class MetadataBundleListenerTest { @Test public void testSanity() throws MalformedURLException, ClassNotFoundException { - //We are checking here that the class loader we create and use in this test series is indeed different and isolated from our tests classloader. + // We are checking here that the class loader we create and use in this test series is indeed different and + // isolated from our tests classloader. ClassLoader loader = createIsolatedClassLoader(); - assertFalse(ProviderMetadata.class.isAssignableFrom(loader.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata"))); + assertFalse(ProviderMetadata.class.isAssignableFrom(loader + .loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata"))); } - @Test public void testGetProviderMetadata() throws Exception { MetadataBundleListener listener = new MetadataBundleListener(); Bundle bundle = createMock(Bundle.class); - expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(JcloudsTestBlobStoreProviderMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(JcloudsTestComputeProviderMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); + expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( + JcloudsTestBlobStoreProviderMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn( + JcloudsTestComputeProviderMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( + JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); replay(bundle); List providerMetadataList = Lists.newArrayList(listener.listProviderMetadata(bundle)); assertNotNull(providerMetadataList); @@ -88,11 +94,15 @@ public class MetadataBundleListenerTest { Bundle bundle = createMock(Bundle.class); expect(bundle.getBundleId()).andReturn(10L).anyTimes(); - expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes(); + expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes(); expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(null).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(JcloudsTestBlobStoreProviderMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(JcloudsTestComputeProviderMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( + JcloudsTestBlobStoreProviderMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn( + JcloudsTestComputeProviderMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( + JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); providerListener.added(anyObject(JcloudsTestBlobStoreProviderMetadata.class)); expectLastCall().times(1); @@ -112,10 +122,14 @@ public class MetadataBundleListenerTest { ClassLoader isolatedClassLoader = createIsolatedClassLoader(); MetadataBundleListener listener = new MetadataBundleListener(); Bundle bundle = createMock(Bundle.class); - expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(isolatedClassLoader.loadClass(JcloudsTestBlobStoreProviderMetadata.class.getName())).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(JcloudsTestComputeProviderMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); + expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( + isolatedClassLoader.loadClass(JcloudsTestBlobStoreProviderMetadata.class.getName())).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn( + JcloudsTestComputeProviderMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( + JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); replay(bundle); List providerMetadataList = Lists.newArrayList(listener.listProviderMetadata(bundle)); @@ -131,10 +145,14 @@ public class MetadataBundleListenerTest { public void testGetApiMetadata() throws Exception { MetadataBundleListener listener = new MetadataBundleListener(); Bundle bundle = createMock(Bundle.class); - expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(JcloudsTestBlobStoreApiMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(JcloudsTestComputeApiMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn(JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); + expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn( + JcloudsTestBlobStoreApiMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn( + JcloudsTestComputeApiMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn( + JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); replay(bundle); List apiMetadataList = Lists.newArrayList(listener.listApiMetadata(bundle)); @@ -155,11 +173,14 @@ public class MetadataBundleListenerTest { Bundle bundle = createMock(Bundle.class); expect(bundle.getBundleId()).andReturn(10L).anyTimes(); expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(null).anyTimes(); - expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(JcloudsTestBlobStoreApiMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(JcloudsTestComputeApiMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn(JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); - + expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn( + JcloudsTestBlobStoreApiMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn( + JcloudsTestComputeApiMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn( + JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); apiListener.added(anyObject(JcloudsTestBlobStoreApiMetadata.class)); expectLastCall().times(1); @@ -179,10 +200,14 @@ public class MetadataBundleListenerTest { ClassLoader isolatedClassLoader = createIsolatedClassLoader(); MetadataBundleListener listener = new MetadataBundleListener(); Bundle bundle = createMock(Bundle.class); - expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(isolatedClassLoader.loadClass(JcloudsTestBlobStoreApiMetadata.class.getName())).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(JcloudsTestComputeApiMetadata.class).anyTimes(); - expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn(JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); + expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn( + getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn( + isolatedClassLoader.loadClass(JcloudsTestBlobStoreApiMetadata.class.getName())).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn( + JcloudsTestComputeApiMetadata.class).anyTimes(); + expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn( + JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); replay(bundle); @@ -195,10 +220,9 @@ public class MetadataBundleListenerTest { verify(bundle); } - /** * Creates a different {@link ClassLoader}. - * + * * @return */ private ClassLoader createIsolatedClassLoader() throws MalformedURLException {