/** * Creates an instance of {@link ProviderMetadata} from the {@link Bundle}. * * @param bundle * @return */ public Iterable<ProviderMetadata> listProviderMetadata(Bundle bundle) { Iterable<String> classNames = stringsForResourceInBundle( "/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle); return instantiateAvailableClasses(bundle, classNames, ProviderMetadata.class); }
/** * 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) { checkNotNull(bundle, "bundle"); checkNotNull(classNames, "classNames"); checkNotNull(type, "type"); return FluentIterable.from(classNames) .transform(loadClassIfAssignableFrom(bundle, type)) .filter(notNull()) .transform(instantiateIfPossible(type)) .filter(notNull()) .toSet(); }
@Test public void testStringsForResourcesInBundleWhenNoResources() throws Exception { Bundle bundle = createMock(Bundle.class); expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(null); replay(bundle); assertEquals(Bundles.stringsForResourceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle), ImmutableSet.of()); verify(bundle); }
@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); replay(bundle); Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( "org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata", "org.jclouds.apis.JcloudsTestComputeApiMetadata", "org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata"), ProviderMetadata.class); assertEquals(providers, ImmutableSet.of(new JcloudsTestBlobStoreProviderMetadata(), new JcloudsTestYetAnotherComputeProviderMetadata())); verify(bundle); }
/** * Reads the resource from a {@link Bundle}. * * @param resourcePath * 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. */ public static ImmutableSet<String> stringsForResourceInBundle(String resourcePath, Bundle bundle) { checkNotNull(resourcePath, "resourcePath"); checkNotNull(bundle, "bundle"); URL resource = bundle.getEntry(resourcePath); if (resource == null) return ImmutableSet.of(); try { return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream())); } catch (IOException e) { } catch (RuntimeException ex) { } return ImmutableSet.of(); }
/** * 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) { checkNotNull(bundle, "bundle"); checkNotNull(classNames, "classNames"); checkNotNull(type, "type"); return FluentIterable.from(classNames) .transform(loadClassIfAssignableFrom(bundle, type)) .filter(notNull()) .transform(instantiateIfPossible(type)) .filter(notNull()) .toSet(); }
@Test 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")); replay(bundle); 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")); verify(bundle); } }
@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); replay(bundle); Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( "org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata", "org.jclouds.providers.JcloudsTestComputeProviderMetadata", "org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata"), ProviderMetadata.class); assertEquals(providers, ImmutableSet.of(new JcloudsTestBlobStoreProviderMetadata(), new JcloudsTestComputeProviderMetadata(), new JcloudsTestYetAnotherComputeProviderMetadata())); verify(bundle); }
/** * Reads the resource from a {@link Bundle}. * * @param resourcePath * 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. */ public static ImmutableSet<String> stringsForResourceInBundle(String resourcePath, Bundle bundle) { checkNotNull(resourcePath, "resourcePath"); checkNotNull(bundle, "bundle"); URL resource = bundle.getEntry(resourcePath); if (resource == null) return ImmutableSet.of(); try { return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream())); } catch (IOException e) { } catch (RuntimeException ex) { } return ImmutableSet.of(); }
/** * Creates an instance of {@link ApiMetadata} from the {@link Bundle}. * * @param bundle * @return */ public Iterable<ApiMetadata> listApiMetadata(Bundle bundle) { Iterable<String> classNames = stringsForResourceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle); return instantiateAvailableClasses(bundle, classNames, ApiMetadata.class); }
/** * 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) { checkNotNull(bundle, "bundle"); checkNotNull(classNames, "classNames"); checkNotNull(type, "type"); return FluentIterable.from(classNames) .transform(loadClassIfAssignableFrom(bundle, type)) .filter(notNull()) .transform(instantiateIfPossible(type)) .filter(notNull()) .toSet(); }
@Test 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")); replay(bundle); 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")); verify(bundle); } }
@SuppressWarnings("rawtypes") @Test public void testInstantiateAvailableClassesWhenNotAllAssignable() throws ClassNotFoundException { Bundle bundle = createMock(Bundle.class); expect((Class) bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( JcloudsTestBlobStoreProviderMetadata.class); expect((Class) bundle.loadClass("org.jclouds.apis.JcloudsTestComputeApiMetadata")).andReturn( JcloudsTestComputeApiMetadata.class); expect((Class) bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( JcloudsTestYetAnotherComputeProviderMetadata.class); replay(bundle); Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( "org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata", "org.jclouds.apis.JcloudsTestComputeApiMetadata", "org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata"), ProviderMetadata.class); assertEquals(providers, ImmutableSet.of(new JcloudsTestBlobStoreProviderMetadata(), new JcloudsTestYetAnotherComputeProviderMetadata())); verify(bundle); }
/** * Reads the resource from a {@link Bundle}. * * @param resourcePath * 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. */ public static ImmutableSet<String> stringsForResourceInBundle(String resourcePath, Bundle bundle) { checkNotNull(resourcePath, "resourcePath"); checkNotNull(bundle, "bundle"); URL resource = bundle.getEntry(resourcePath); if (resource == null) return ImmutableSet.of(); try { return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream())); } catch (IOException e) { } catch (RuntimeException ex) { } return ImmutableSet.of(); }
/** * Creates an instance of {@link ProviderMetadata} from the {@link Bundle}. * * @param bundle * @return */ public Iterable<ProviderMetadata> listProviderMetadata(Bundle bundle) { Iterable<String> classNames = stringsForResourceInBundle( "/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle); return instantiateAvailableClasses(bundle, classNames, ProviderMetadata.class); }
/** * 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) { checkNotNull(bundle, "bundle"); checkNotNull(classNames, "classNames"); checkNotNull(type, "type"); return FluentIterable.from(classNames) .transform(loadClassIfAssignableFrom(bundle, type)) .filter(notNull()) .transform(instantiateIfPossible(type)) .filter(notNull()) .toSet(); }
@Test public void testStringsForResourcesInBundleWhenNoResources() throws Exception { Bundle bundle = createMock(Bundle.class); expect(bundle.getEntry("/META-INF/services/org.jclouds.apis.ApiMetadata")).andReturn(null); replay(bundle); assertEquals(Bundles.stringsForResourceInBundle("/META-INF/services/org.jclouds.apis.ApiMetadata", bundle), ImmutableSet.of()); verify(bundle); }
@SuppressWarnings("rawtypes") @Test public void testInstantiateAvailableClassesWhenAllAssignable() throws ClassNotFoundException { Bundle bundle = createMock(Bundle.class); expect((Class) bundle.loadClass("org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata")).andReturn( JcloudsTestBlobStoreProviderMetadata.class); expect((Class) bundle.loadClass("org.jclouds.providers.JcloudsTestComputeProviderMetadata")).andReturn( JcloudsTestComputeProviderMetadata.class); expect((Class) bundle.loadClass("org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata")).andReturn( JcloudsTestYetAnotherComputeProviderMetadata.class); replay(bundle); Iterable<ProviderMetadata> providers = Bundles.instantiateAvailableClasses(bundle, ImmutableSet.of( "org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata", "org.jclouds.providers.JcloudsTestComputeProviderMetadata", "org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata"), ProviderMetadata.class); assertEquals(providers, ImmutableSet.of(new JcloudsTestBlobStoreProviderMetadata(), new JcloudsTestComputeProviderMetadata(), new JcloudsTestYetAnotherComputeProviderMetadata())); verify(bundle); }
/** * Reads the resource from a {@link Bundle}. * * @param resourcePath * 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. */ public static ImmutableSet<String> stringsForResourceInBundle(String resourcePath, Bundle bundle) { checkNotNull(resourcePath, "resourcePath"); checkNotNull(bundle, "bundle"); URL resource = bundle.getEntry(resourcePath); if (resource == null) return ImmutableSet.of(); try { return ImmutableSet.copyOf(splitOrEmptyAndClose(resource.openStream())); } catch (IOException e) { } catch (RuntimeException ex) { } return ImmutableSet.of(); }
/** * Creates an instance of {@link ProviderMetadata} from the {@link Bundle}. * * @param bundle * @return */ public Iterable<ProviderMetadata> listProviderMetadata(Bundle bundle) { Iterable<String> classNames = stringsForResourceInBundle( "/META-INF/services/org.jclouds.providers.ProviderMetadata", bundle); return instantiateAvailableClasses(bundle, classNames, ProviderMetadata.class); }