osgi code formatting and cleanup

This commit is contained in:
Adrian Cole 2012-12-31 20:07:19 -08:00
parent 6cdec6aad0
commit a30ff81c15
11 changed files with 215 additions and 181 deletions

View File

@ -26,6 +26,7 @@ import java.util.NoSuchElementException;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import org.jclouds.View; import org.jclouds.View;
import org.jclouds.osgi.ApiRegistry;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -59,9 +60,8 @@ public class Apis {
* *
* @return all available apis loaded from classpath via ServiceLoader * @return all available apis loaded from classpath via ServiceLoader
*/ */
@SuppressWarnings("unchecked")
private static Iterable<ApiMetadata> fromServiceLoader() { private static Iterable<ApiMetadata> fromServiceLoader() {
return Iterable.class.cast(ServiceLoader.load(ApiMetadata.class)); return ServiceLoader.load(ApiMetadata.class);
} }
/** /**

View File

@ -18,8 +18,6 @@
*/ */
package org.jclouds.osgi; package org.jclouds.osgi;
import org.jclouds.apis.ApiRegistry;
import org.jclouds.providers.ProviderRegistry;
import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceReference;
@ -32,19 +30,18 @@ public class Activator implements BundleActivator {
private MetadataBundleListener bundleListener = new MetadataBundleListener(); private MetadataBundleListener bundleListener = new MetadataBundleListener();
/** /**
* Called when this bundle is started so the Framework can perform the * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
* bundle-specific activities necessary to start this bundle. This method * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
* can be used to register services or to allocate any resources that this
* bundle needs.
* <p/> * <p/>
* <p/> * <p/>
* This method must complete and return to its caller in a timely manner. * This method must complete and return to its caller in a timely manner.
* *
* @param context The execution context of the bundle being started. * @param context
* @throws Exception If this method throws an exception, this * The execution context of the bundle being started.
* bundle is marked as stopped and the Framework will remove this * @throws Exception
* bundle's listeners, unregister all services registered by this * If this method throws an exception, this bundle is marked as stopped and the Framework will remove this
* bundle, and release all services used by this bundle. * bundle's listeners, unregister all services registered by this bundle, and release all services used by
* this bundle.
*/ */
@Override @Override
public void start(BundleContext context) throws Exception { 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 * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
* bundle-specific activities necessary to stop the bundle. In general, this * the bundle. In general, this method should undo the work that the <code>BundleActivator.start</code> method
* method should undo the work that the <code>BundleActivator.start</code> * started. There should be no active threads that were started by this bundle when this bundle returns. A stopped
* method started. There should be no active threads that were started by * bundle must not call any Framework objects.
* this bundle when this bundle returns. A stopped bundle must not call any
* Framework objects.
* <p/> * <p/>
* <p/> * <p/>
* This method must complete and return to its caller in a timely manner. * This method must complete and return to its caller in a timely manner.
* *
* @param context The execution context of the bundle being stopped. * @param context
* @throws Exception If this method throws an exception, the * The execution context of the bundle being stopped.
* bundle is still marked as stopped, and the Framework will remove * @throws Exception
* the bundle's listeners, unregister all services registered by the * If this method throws an exception, the bundle is still marked as stopped, and the Framework will
* bundle, and release all services used by the bundle. * remove the bundle's listeners, unregister all services registered by the bundle, and release all
* services used by the bundle.
*/ */
@Override @Override
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {

View File

@ -22,25 +22,28 @@ package org.jclouds.osgi;
import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiMetadata;
/** /**
* A listener interface for {@link org.jclouds.apis.ApiMetadata}. * A listener interface for {@link org.jclouds.apis.ApiMetadata}. In OSGi a api can be added or removed dynamically.
* In OSGi a api can be added or removed dynamically.
* OSGi services using this interface will receive a notification whenever this happens. * OSGi services using this interface will receive a notification whenever this happens.
*/ */
public interface ApiListener { public interface ApiListener {
/** /**
* Method to be called when an api gets added. * Method to be called when an api gets added.
* *
* @param api The api that was added. * @param api
* @param <A> The {@link org.jclouds.apis.ApiMetadata}. * The api that was added.
* @param <A>
* The {@link org.jclouds.apis.ApiMetadata}.
*/ */
<A extends ApiMetadata> void added(A api); <A extends ApiMetadata> void added(A api);
/** /**
* Method to be called when an api gets removed. * Method to be called when an api gets removed.
* *
* @param api The api that was added. * @param api
* @param <A> The {@link ApiMetadata}. * The api that was added.
* @param <A>
* The {@link ApiMetadata}.
*/ */
<A extends ApiMetadata> void removed(A api); <A extends ApiMetadata> void removed(A api);
} }

View File

@ -16,32 +16,36 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.jclouds.apis; package org.jclouds.osgi;
import java.util.Set; import java.util.Set;
import org.jclouds.apis.ApiMetadata;
import com.google.common.annotations.Beta;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
/** /**
* A registry for holding {@link org.jclouds.apis.ApiMetadata}. * A registry for holding {@link org.jclouds.apis.ApiMetadata}.
*/ */
@Beta
public class ApiRegistry { public class ApiRegistry {
private static final Set<ApiMetadata> apis = Sets.newHashSet(); private static final Set<ApiMetadata> apis = Sets.newHashSet();
public static void registerApi(ApiMetadata api) { public static void registerApi(ApiMetadata api) {
apis.add(api); apis.add(api);
} }
public static void unRegisterApi(ApiMetadata api) { public static void unRegisterApi(ApiMetadata api) {
apis.remove(api); apis.remove(api);
} }
public static Iterable<ApiMetadata> fromRegistry() { public static Iterable<ApiMetadata> fromRegistry() {
return Iterable.class.cast(apis); return apis;
} }
public static void clear() { public static void clear() {
apis.clear(); apis.clear();
} }
} }

View File

@ -19,16 +19,16 @@
package org.jclouds.osgi; package org.jclouds.osgi;
import static com.google.common.base.Preconditions.checkNotNull; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import org.jclouds.util.Strings2;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -40,30 +40,30 @@ import com.google.common.collect.ImmutableSet;
*/ */
public final class Bundles { public final class Bundles {
private Bundles() { private Bundles() {
} }
/** /**
* instantiates the supplied classnames using the bundle classloader, and * instantiates the supplied classnames using the bundle classloader, and casts to the supplied type. Any errors are
* casts to the supplied type. Any errors are silently ignored. * silently ignored.
* *
* @return instances that could be instantiated without error. * @return instances that could be instantiated without error.
*/ */
public static <T> ImmutableSet<T> instantiateAvailableClasses(Bundle bundle, Iterable<String> classNames, Class<T> type) { public static <T> ImmutableSet<T> instantiateAvailableClasses(Bundle bundle, Iterable<String> classNames,
Class<T> type) {
checkNotNull(bundle, "bundle"); checkNotNull(bundle, "bundle");
checkNotNull(classNames, "classNames"); checkNotNull(classNames, "classNames");
checkNotNull(type, "type"); checkNotNull(type, "type");
return FluentIterable.from(classNames) return FluentIterable.from(classNames)
.transform(loadClassIfAssignableFrom(bundle, type)) .transform(loadClassIfAssignableFrom(bundle, type))
.filter(Predicates.notNull()) .filter(notNull())
.transform(instantiateIfPossible(type)) .transform(instantiateIfPossible(type))
.filter(Predicates.notNull()) .filter(notNull())
.toSet(); .toSet();
} }
/** /**
* A function that loads classes from the bundle, or returns null if the * A function that loads classes from the bundle, or returns null if the class isn't found or assignable by the input
* class isn't found or assignable by the input parameter * parameter
* *
* @param bundle * @param bundle
* where to find classes * where to find classes
@ -90,8 +90,7 @@ public final class Bundles {
} }
/** /**
* A function that instantiates classes or returns null, if it encounters any * A function that instantiates classes or returns null, if it encounters any problems.
* problems.
* *
* @param clazz * @param clazz
* superclass to cast as * superclass to cast as
@ -118,10 +117,9 @@ public final class Bundles {
* The path to the resource. * The path to the resource.
* @param bundle * @param bundle
* The bundle to read from. * The bundle to read from.
* @return strings delimited by newline in the stream or empty set, on any * @return strings delimited by newline in the stream or empty set, on any exception.
* exception.
*/ */
public static ImmutableSet<String> stringsForResorceInBundle(String resourcePath, Bundle bundle) { public static ImmutableSet<String> stringsForResourceInBundle(String resourcePath, Bundle bundle) {
checkNotNull(resourcePath, "resourcePath"); checkNotNull(resourcePath, "resourcePath");
checkNotNull(bundle, "bundle"); checkNotNull(bundle, "bundle");
@ -131,13 +129,12 @@ public final class Bundles {
try { try {
return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream())); return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream()));
} catch (IOException e) { } catch (IOException e) {
return ImmutableSet.of();
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
return ImmutableSet.of();
} }
return ImmutableSet.of();
} }
private static Iterable<String> splitOrEmptyAndClose(InputStream in) throws IOException { private static Iterable<String> splitOrEmptyAndClose(InputStream in) throws IOException {
return Splitter.on('\n').omitEmptyStrings().split(Strings2.toStringAndClose(in)); return Splitter.on('\n').omitEmptyStrings().split(toStringAndClose(in));
} }
} }

View File

@ -19,14 +19,15 @@
package org.jclouds.osgi; package org.jclouds.osgi;
import static org.jclouds.osgi.Bundles.instantiateAvailableClasses; 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 java.util.List;
import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiMetadata;
import org.jclouds.apis.ApiRegistry;
import org.jclouds.providers.ProviderMetadata; import org.jclouds.providers.ProviderMetadata;
import org.jclouds.providers.ProviderRegistry;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleEvent;
@ -37,9 +38,9 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Multimap; 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 * A {@link BundleListener} that listens for {@link BundleEvent} and searches for
* installed Bundles. This is used as a workaround for OSGi environments where the ServiceLoader cannot cross bundle * {@link org.jclouds.providers.ProviderMetadata} and {@link org.jclouds.apis.ApiMetadata} in newly installed Bundles.
* boundaries. * This is used as a workaround for OSGi environments where the ServiceLoader cannot cross bundle boundaries.
* *
* @author iocanel * @author iocanel
*/ */
@ -51,11 +52,10 @@ public class MetadataBundleListener implements BundleListener {
private final List<ProviderListener> providerListeners = Lists.newArrayList(); private final List<ProviderListener> providerListeners = Lists.newArrayList();
private final List<ApiListener> apiListeners = Lists.newArrayList(); private final List<ApiListener> apiListeners = Lists.newArrayList();
/** /**
* Starts the listener. * Starts the listener. Checks the bundles that are already active and registers {@link ProviderMetadata} and
* Checks the bundles that are already active and registers {@link ProviderMetadata} and {@link ApiMetadata} found. * {@link ApiMetadata} found. Registers the itself as a {@link BundleListener}.
* Registers the itself as a {@link BundleListener}. *
* @param bundleContext * @param bundleContext
*/ */
public synchronized void start(BundleContext bundleContext) { public synchronized void start(BundleContext bundleContext) {
@ -69,9 +69,8 @@ public class MetadataBundleListener implements BundleListener {
} }
/** /**
* Stops the listener. * Stops the listener. Removes itself from the {@link BundleListener}s. Clears metadata maps and listeners lists.
* Removes itself from the {@link BundleListener}s. *
* Clears metadata maps and listeners lists.
* @param bundleContext * @param bundleContext
*/ */
public void stop(BundleContext bundleContext) { public void stop(BundleContext bundleContext) {
@ -85,20 +84,21 @@ public class MetadataBundleListener implements BundleListener {
@Override @Override
public synchronized void bundleChanged(BundleEvent event) { public synchronized void bundleChanged(BundleEvent event) {
switch (event.getType()) { switch (event.getType()) {
case BundleEvent.STARTED: case STARTED:
addBundle(event.getBundle()); addBundle(event.getBundle());
break; break;
case BundleEvent.STOPPING: case STOPPING:
case BundleEvent.STOPPED: case STOPPED:
removeBundle(event.getBundle()); removeBundle(event.getBundle());
break; break;
} }
} }
/** /**
* Searches for {@link ProviderMetadata} and {@link ApiMetadata} inside the {@link Bundle}. * Searches for {@link ProviderMetadata} and {@link ApiMetadata} inside the {@link Bundle}. If metadata are found
* If metadata are found they are registered in the {@link ProviderRegistry} and {@link ApiRegistry}. * they are registered in the {@link ProviderRegistry} and {@link ApiRegistry}. Also the {@link ProviderListener} and
* Also the {@link ProviderListener} and {@link ApiListener} are notified. * {@link ApiListener} are notified.
*
* @param bundle * @param bundle
*/ */
private synchronized void addBundle(Bundle 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. * Searches for {@link ProviderMetadata} and {@link ApiMetadata} registered under the {@link Bundle} id. If metadata
* If metadata are found they are removed the {@link ProviderRegistry} and {@link ApiRegistry}. * are found they are removed the {@link ProviderRegistry} and {@link ApiRegistry}. Also the {@link ProviderListener}
* Also the {@link ProviderListener} and {@link ApiListener} are notified. * and {@link ApiListener} are notified.
*
* @param bundle * @param bundle
*/ */
private synchronized void removeBundle(Bundle 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}. * Creates an instance of {@link ProviderMetadata} from the {@link Bundle}.
* *
* @param bundle * @param bundle
* @return * @return
*/ */
public Iterable<ProviderMetadata> listProviderMetadata(Bundle bundle) { public Iterable<ProviderMetadata> listProviderMetadata(Bundle bundle) {
Iterable<String> classNames = stringsForResorceInBundle("/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle); Iterable<String> classNames = stringsForResourceInBundle(
"/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle);
return instantiateAvailableClasses(bundle, classNames, ProviderMetadata.class); return instantiateAvailableClasses(bundle, classNames, ProviderMetadata.class);
} }
/** /**
* Creates an instance of {@link ApiMetadata} from the {@link Bundle}. * Creates an instance of {@link ApiMetadata} from the {@link Bundle}.
* *
* @param bundle * @param bundle
* @return * @return
*/ */
public Iterable<ApiMetadata> listApiMetadata(Bundle bundle) { public Iterable<ApiMetadata> listApiMetadata(Bundle bundle) {
Iterable<String> classNames = stringsForResorceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle); Iterable<String> classNames = stringsForResourceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle);
return instantiateAvailableClasses(bundle, classNames, ApiMetadata.class); return instantiateAvailableClasses(bundle, classNames, ApiMetadata.class);
} }
/** /**
* Adds a {@link ProviderListener} and notifies it of existing {@link ProviderMetadata}. * 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) { public synchronized void addProviderListener(ProviderListener listener) {
providerListeners.add(listener); providerListeners.add(listener);
@ -180,8 +183,9 @@ public class MetadataBundleListener implements BundleListener {
/** /**
* Removes the {@link ProviderListener} * Removes the {@link ProviderListener}
* *
* @param listener The listener * @param listener
* The listener
*/ */
public synchronized void removeProviderListener(ProviderListener listener) { public synchronized void removeProviderListener(ProviderListener listener) {
providerListeners.remove(listener); providerListeners.remove(listener);
@ -189,7 +193,7 @@ public class MetadataBundleListener implements BundleListener {
/** /**
* Adds a {@link ApiListener} and notifies it of existing {@link ApiMetadata}. * Adds a {@link ApiListener} and notifies it of existing {@link ApiMetadata}.
* *
* @param listener * @param listener
*/ */
public synchronized void addApiListenerListener(ApiListener listener) { public synchronized void addApiListenerListener(ApiListener listener) {
@ -201,7 +205,7 @@ public class MetadataBundleListener implements BundleListener {
/** /**
* Removes the {@link ApiListener} * Removes the {@link ApiListener}
* *
* @param listener * @param listener
*/ */
public synchronized void removeApiListenerListener(ApiListener listener) { public synchronized void removeApiListenerListener(ApiListener listener) {

View File

@ -22,25 +22,28 @@ package org.jclouds.osgi;
import org.jclouds.providers.ProviderMetadata; import org.jclouds.providers.ProviderMetadata;
/** /**
* A listener interface for {@link ProviderMetadata}. * A listener interface for {@link ProviderMetadata}. In OSGi a provider can be added or removed dynamically. OSGi
* In OSGi a provider can be added or removed dynamically. * services using this interface will receive a notification whenever this happens.
* OSGi services using this interface will receive a notification whenever this happens.
*/ */
public interface ProviderListener { public interface ProviderListener {
/** /**
* Method to be called when a Provider gets added. * Method to be called when a Provider gets added.
* *
* @param provider The provider that was added. * @param provider
* @param <P> The {@link ProviderMetadata}. * The provider that was added.
* @param <P>
* The {@link ProviderMetadata}.
*/ */
<P extends ProviderMetadata> void added(P provider); <P extends ProviderMetadata> void added(P provider);
/** /**
* Method to be called when a Provider gets removed. * Method to be called when a Provider gets removed.
* *
* @param provider The provider that was added. * @param provider
* @param <P> The {@link ProviderMetadata}. * The provider that was added.
* @param <P>
* The {@link ProviderMetadata}.
*/ */
<P extends ProviderMetadata> void removed(P provider); <P extends ProviderMetadata> void removed(P provider);

View File

@ -16,15 +16,19 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.jclouds.providers; package org.jclouds.osgi;
import java.util.Set; import java.util.Set;
import org.jclouds.providers.ProviderMetadata;
import com.google.common.annotations.Beta;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
/** /**
* A registry for holding {@link org.jclouds.providers.ProviderMetadata}. * A registry for holding {@link org.jclouds.providers.ProviderMetadata}.
*/ */
@Beta
public class ProviderRegistry { public class ProviderRegistry {
private static final Set<ProviderMetadata> providers = Sets.newHashSet(); private static final Set<ProviderMetadata> providers = Sets.newHashSet();
@ -38,7 +42,7 @@ public class ProviderRegistry {
} }
public static Iterable<ProviderMetadata> fromRegistry() { public static Iterable<ProviderMetadata> fromRegistry() {
return Iterable.class.cast(providers); return providers;
} }
public static void clear() { public static void clear() {

View File

@ -24,14 +24,15 @@ import static com.google.common.collect.Iterables.find;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import com.google.common.collect.ImmutableSet;
import org.jclouds.Context; import org.jclouds.Context;
import org.jclouds.View; import org.jclouds.View;
import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiMetadata;
import org.jclouds.osgi.ProviderRegistry;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
/** /**
@ -72,9 +73,8 @@ public class Providers {
* *
* @return all available providers loaded from classpath via ServiceLoader * @return all available providers loaded from classpath via ServiceLoader
*/ */
@SuppressWarnings("unchecked")
public static Iterable<ProviderMetadata> fromServiceLoader() { public static Iterable<ProviderMetadata> fromServiceLoader() {
return Iterable.class.cast(ServiceLoader.load(ProviderMetadata.class)); return ServiceLoader.load(ProviderMetadata.class);
} }
/** /**

View File

@ -44,12 +44,12 @@ public class BundlesTest {
@Test @Test
public void testInstantiateAvailableClassesWhenAllAssignable() throws ClassNotFoundException { public void testInstantiateAvailableClassesWhenAllAssignable() throws ClassNotFoundException {
Bundle bundle = createMock(Bundle.class); Bundle bundle = createMock(Bundle.class);
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")) expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(
.andReturn(JcloudsTestBlobStoreProviderMetadata.class); JcloudsTestBlobStoreProviderMetadata.class);
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")) expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(
.andReturn(JcloudsTestComputeProviderMetadata.class); JcloudsTestComputeProviderMetadata.class);
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")) expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(
.andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class); JcloudsTestYetAnotherComputeProviderMetadata.class);
replay(bundle); replay(bundle);
Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of(
@ -65,12 +65,12 @@ public class BundlesTest {
@Test @Test
public void testInstantiateAvailableClassesWhenNotAllAssignable() throws ClassNotFoundException { public void testInstantiateAvailableClassesWhenNotAllAssignable() throws ClassNotFoundException {
Bundle bundle = createMock(Bundle.class); Bundle bundle = createMock(Bundle.class);
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")) expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(
.andReturn(JcloudsTestBlobStoreProviderMetadata.class); JcloudsTestBlobStoreProviderMetadata.class);
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")) expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(
.andReturn(JcloudsTestComputeApiMetadata.class); JcloudsTestComputeApiMetadata.class);
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")) expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(
.andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class); JcloudsTestYetAnotherComputeProviderMetadata.class);
replay(bundle); replay(bundle);
Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( Iterable<ProviderMetadata> 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); expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(null);
replay(bundle); 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()); ImmutableSet.of());
verify(bundle); verify(bundle);
@ -100,13 +100,12 @@ public class BundlesTest {
public void testStringsForResourcesInBundleWhenResourcePresent() throws Exception { public void testStringsForResourcesInBundleWhenResourcePresent() throws Exception {
Bundle bundle = createMock(Bundle.class); Bundle bundle = createMock(Bundle.class);
expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")) expect(bundle.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(
.andReturn(getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")); getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata"));
replay(bundle); replay(bundle);
assertEquals(Bundles.stringsForResorceInBundle( assertEquals(Bundles.stringsForResourceInBundle("/META-INF/services/org.jclouds.providers.ProviderMetadata",
"/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle), ImmutableSet.of( bundle), ImmutableSet.of("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata",
"org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata",
"org.jclouds.providers.JcloudsTestComputeProviderMetadata", "org.jclouds.providers.JcloudsTestComputeProviderMetadata",
"org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")); "org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata"));

View File

@ -18,7 +18,22 @@
*/ */
package org.jclouds.osgi; 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.ApiMetadata;
import org.jclouds.apis.JcloudsTestBlobStoreApiMetadata; import org.jclouds.apis.JcloudsTestBlobStoreApiMetadata;
import org.jclouds.apis.JcloudsTestComputeApiMetadata; import org.jclouds.apis.JcloudsTestComputeApiMetadata;
@ -31,45 +46,36 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleEvent;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.net.MalformedURLException; import com.google.common.collect.Lists;
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;
/** /**
* *
* @author iocanel * @author iocanel
* *
*/ */
public class MetadataBundleListenerTest { public class MetadataBundleListenerTest {
@Test @Test
public void testSanity() throws MalformedURLException, ClassNotFoundException { 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(); ClassLoader loader = createIsolatedClassLoader();
assertFalse(ProviderMetadata.class.isAssignableFrom(loader.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata"))); assertFalse(ProviderMetadata.class.isAssignableFrom(loader
.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")));
} }
@Test @Test
public void testGetProviderMetadata() throws Exception { public void testGetProviderMetadata() throws Exception {
MetadataBundleListener listener = new MetadataBundleListener(); MetadataBundleListener listener = new MetadataBundleListener();
Bundle bundle = createMock(Bundle.class); 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.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(JcloudsTestBlobStoreProviderMetadata.class).anyTimes(); getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes();
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(JcloudsTestComputeProviderMetadata.class).anyTimes(); expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); 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); replay(bundle);
List<ProviderMetadata> providerMetadataList = Lists.newArrayList(listener.listProviderMetadata(bundle)); List<ProviderMetadata> providerMetadataList = Lists.newArrayList(listener.listProviderMetadata(bundle));
assertNotNull(providerMetadataList); assertNotNull(providerMetadataList);
@ -88,11 +94,15 @@ public class MetadataBundleListenerTest {
Bundle bundle = createMock(Bundle.class); Bundle bundle = createMock(Bundle.class);
expect(bundle.getBundleId()).andReturn(10L).anyTimes(); 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.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.JcloudsTestBlobStoreProviderMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(JcloudsTestComputeProviderMetadata.class).anyTimes(); JcloudsTestBlobStoreProviderMetadata.class).anyTimes();
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(JcloudsTestYetAnotherComputeProviderMetadata.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)); providerListener.added(anyObject(JcloudsTestBlobStoreProviderMetadata.class));
expectLastCall().times(1); expectLastCall().times(1);
@ -112,10 +122,14 @@ public class MetadataBundleListenerTest {
ClassLoader isolatedClassLoader = createIsolatedClassLoader(); ClassLoader isolatedClassLoader = createIsolatedClassLoader();
MetadataBundleListener listener = new MetadataBundleListener(); MetadataBundleListener listener = new MetadataBundleListener();
Bundle bundle = createMock(Bundle.class); 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.getEntry("/META-INF/services/org.jclouds.providers.ProviderMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(isolatedClassLoader.loadClass(JcloudsTestBlobStoreProviderMetadata.class.getName())).anyTimes(); getClass().getResource("/META-INF/services/org.jclouds.providers.ProviderMetadata")).anyTimes();
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn(JcloudsTestComputeProviderMetadata.class).anyTimes(); expect(bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn(JcloudsTestYetAnotherComputeProviderMetadata.class).anyTimes(); 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); replay(bundle);
List<ProviderMetadata> providerMetadataList = Lists.newArrayList(listener.listProviderMetadata(bundle)); List<ProviderMetadata> providerMetadataList = Lists.newArrayList(listener.listProviderMetadata(bundle));
@ -131,10 +145,14 @@ public class MetadataBundleListenerTest {
public void testGetApiMetadata() throws Exception { public void testGetApiMetadata() throws Exception {
MetadataBundleListener listener = new MetadataBundleListener(); MetadataBundleListener listener = new MetadataBundleListener();
Bundle bundle = createMock(Bundle.class); 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.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(JcloudsTestBlobStoreApiMetadata.class).anyTimes(); getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes();
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(JcloudsTestComputeApiMetadata.class).anyTimes(); expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn(JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); 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); replay(bundle);
List<ApiMetadata> apiMetadataList = Lists.newArrayList(listener.listApiMetadata(bundle)); List<ApiMetadata> apiMetadataList = Lists.newArrayList(listener.listApiMetadata(bundle));
@ -155,11 +173,14 @@ public class MetadataBundleListenerTest {
Bundle bundle = createMock(Bundle.class); Bundle bundle = createMock(Bundle.class);
expect(bundle.getBundleId()).andReturn(10L).anyTimes(); 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.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.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(JcloudsTestBlobStoreApiMetadata.class).anyTimes(); getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes();
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(JcloudsTestComputeApiMetadata.class).anyTimes(); expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn(JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); 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)); apiListener.added(anyObject(JcloudsTestBlobStoreApiMetadata.class));
expectLastCall().times(1); expectLastCall().times(1);
@ -179,10 +200,14 @@ public class MetadataBundleListenerTest {
ClassLoader isolatedClassLoader = createIsolatedClassLoader(); ClassLoader isolatedClassLoader = createIsolatedClassLoader();
MetadataBundleListener listener = new MetadataBundleListener(); MetadataBundleListener listener = new MetadataBundleListener();
Bundle bundle = createMock(Bundle.class); 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.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(isolatedClassLoader.loadClass(JcloudsTestBlobStoreApiMetadata.class.getName())).anyTimes(); getClass().getResource("/META-INF/services/org.jclouds.apis.ApiMetadata")).anyTimes();
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn(JcloudsTestComputeApiMetadata.class).anyTimes(); expect(bundle.loadClass("org.jclouds.apis.JcloudsTestBlobStoreApiMetadata")).andReturn(
expect(bundle.loadClass("org.jclouds.apis.JcloudsTestYetAnotherComputeApiMetadata")).andReturn(JcloudsTestYetAnotherComputeApiMetadata.class).anyTimes(); 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); replay(bundle);
@ -195,10 +220,9 @@ public class MetadataBundleListenerTest {
verify(bundle); verify(bundle);
} }
/** /**
* Creates a different {@link ClassLoader}. * Creates a different {@link ClassLoader}.
* *
* @return * @return
*/ */
private ClassLoader createIsolatedClassLoader() throws MalformedURLException { private ClassLoader createIsolatedClassLoader() throws MalformedURLException {