mirror of https://github.com/apache/jclouds.git
osgi code formatting and cleanup
This commit is contained in:
parent
6cdec6aad0
commit
a30ff81c15
|
@ -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<ApiMetadata> fromServiceLoader() {
|
||||
return Iterable.class.cast(ServiceLoader.load(ApiMetadata.class));
|
||||
return ServiceLoader.load(ApiMetadata.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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.
|
||||
* <p/>
|
||||
* <p/>
|
||||
* 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 <code>BundleActivator.start</code>
|
||||
* 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 <code>BundleActivator.start</code> 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.
|
||||
* <p/>
|
||||
* <p/>
|
||||
* 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 {
|
||||
|
|
|
@ -22,8 +22,7 @@ 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 {
|
||||
|
@ -31,16 +30,20 @@ public interface ApiListener {
|
|||
/**
|
||||
* Method to be called when an api gets added.
|
||||
*
|
||||
* @param api The api that was added.
|
||||
* @param <A> The {@link org.jclouds.apis.ApiMetadata}.
|
||||
* @param api
|
||||
* The api that was added.
|
||||
* @param <A>
|
||||
* The {@link org.jclouds.apis.ApiMetadata}.
|
||||
*/
|
||||
<A extends ApiMetadata> void added(A api);
|
||||
|
||||
/**
|
||||
* Method to be called when an api gets removed.
|
||||
*
|
||||
* @param api The api that was added.
|
||||
* @param <A> The {@link ApiMetadata}.
|
||||
* @param api
|
||||
* The api that was added.
|
||||
* @param <A>
|
||||
* The {@link ApiMetadata}.
|
||||
*/
|
||||
<A extends ApiMetadata> void removed(A api);
|
||||
}
|
||||
|
|
|
@ -16,15 +16,19 @@
|
|||
* 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<ApiMetadata> apis = Sets.newHashSet();
|
||||
|
@ -38,7 +42,7 @@ public class ApiRegistry {
|
|||
}
|
||||
|
||||
public static Iterable<ApiMetadata> fromRegistry() {
|
||||
return Iterable.class.cast(apis);
|
||||
return apis;
|
||||
}
|
||||
|
||||
public static void clear() {
|
|
@ -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 <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(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<String> stringsForResorceInBundle(String resourcePath, Bundle bundle) {
|
||||
public static ImmutableSet<String> 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<String> splitOrEmptyAndClose(InputStream in) throws IOException {
|
||||
return Splitter.on('\n').omitEmptyStrings().split(Strings2.toStringAndClose(in));
|
||||
return Splitter.on('\n').omitEmptyStrings().split(toStringAndClose(in));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ProviderListener> providerListeners = Lists.newArrayList();
|
||||
private final List<ApiListener> 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:
|
||||
case STARTED:
|
||||
addBundle(event.getBundle());
|
||||
break;
|
||||
case BundleEvent.STOPPING:
|
||||
case BundleEvent.STOPPED:
|
||||
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) {
|
||||
|
@ -151,7 +152,8 @@ public class MetadataBundleListener implements BundleListener {
|
|||
* @return
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -162,14 +164,15 @@ public class MetadataBundleListener implements BundleListener {
|
|||
* @return
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
@ -181,7 +184,8 @@ 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);
|
||||
|
|
|
@ -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 <P> The {@link ProviderMetadata}.
|
||||
* @param provider
|
||||
* The provider that was added.
|
||||
* @param <P>
|
||||
* The {@link ProviderMetadata}.
|
||||
*/
|
||||
<P extends ProviderMetadata> void added(P provider);
|
||||
|
||||
/**
|
||||
* Method to be called when a Provider gets removed.
|
||||
*
|
||||
* @param provider The provider that was added.
|
||||
* @param <P> The {@link ProviderMetadata}.
|
||||
* @param provider
|
||||
* The provider that was added.
|
||||
* @param <P>
|
||||
* The {@link ProviderMetadata}.
|
||||
*/
|
||||
<P extends ProviderMetadata> void removed(P provider);
|
||||
|
||||
|
|
|
@ -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<ProviderMetadata> providers = Sets.newHashSet();
|
||||
|
@ -38,7 +42,7 @@ public class ProviderRegistry {
|
|||
}
|
||||
|
||||
public static Iterable<ProviderMetadata> fromRegistry() {
|
||||
return Iterable.class.cast(providers);
|
||||
return providers;
|
||||
}
|
||||
|
||||
public static void clear() {
|
|
@ -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<ProviderMetadata> fromServiceLoader() {
|
||||
return Iterable.class.cast(ServiceLoader.load(ProviderMetadata.class));
|
||||
return ServiceLoader.load(ProviderMetadata.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<ProviderMetadata> 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<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);
|
||||
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"));
|
||||
|
||||
|
|
|
@ -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,21 +46,7 @@ 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;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -56,20 +57,25 @@ 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<ProviderMetadata> 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<ProviderMetadata> 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<ApiMetadata> 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,7 +220,6 @@ public class MetadataBundleListenerTest {
|
|||
verify(bundle);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a different {@link ClassLoader}.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue