@SuppressWarnings("unchecked") @Test public void testGetImplementingBundles() { Bootstrap<?> bootstrap = new Bootstrap<>(null); ConfiguredBundle hashSetBundle = (ConfiguredBundle) mock(HashSet.class, withSettings().extraInterfaces(ConfiguredBundle.class).defaultAnswer(RETURNS_DEEP_STUBS)); ConfiguredBundle hashMapBundle = (ConfiguredBundle) mock(HashMap.class, withSettings().extraInterfaces(ConfiguredBundle.class).defaultAnswer(RETURNS_DEEP_STUBS)); Bundle treeSetBundle = (Bundle) mock(TreeSet.class, withSettings().extraInterfaces(Bundle.class).defaultAnswer(RETURNS_DEEP_STUBS)); Bundle treeMapBundle = (Bundle) mock(TreeMap.class, withSettings().extraInterfaces(Bundle.class).defaultAnswer(RETURNS_DEEP_STUBS)); bootstrap.addBundle(hashMapBundle); bootstrap.addBundle(hashSetBundle); bootstrap.addBundle(treeMapBundle); bootstrap.addBundle(treeSetBundle); // List<Set> setBundles = BootstrapExtensions.getImplementingBundles(bootstrap, Set.class); List<Map> mapBundles = BootstrapExtensions.getImplementingBundles(bootstrap, Map.class); // assertThat(setBundles).isNotNull(); assertThat(setBundles).containsExactlyInAnyOrder((Set) hashSetBundle, (Set) treeSetBundle); assertThat(mapBundles).isNotNull(); assertThat(mapBundles).containsExactlyInAnyOrder((Map) hashMapBundle, (Map) treeMapBundle); }
/** * Renders configuration tree report according to provided config. * By default report padded left with one tab. Subtrees are always padded with empty lines for better * visibility. * * @param config tree rendering config * @return rendered tree */ @Override public String renderReport(final ContextTreeConfig config) { final Set<Class<?>> scopes = service.getActiveScopes(); final TreeNode root = new TreeNode("APPLICATION"); renderScopeContent(config, root, Application.class); renderSpecialScope(config, scopes, root, "BUNDLES LOOKUP", GuiceyBundleLookup.class); renderSpecialScope(config, scopes, root, "DROPWIZARD BUNDLES", Bundle.class); renderSpecialScope(config, scopes, root, "CLASSPATH SCAN", ClasspathScanner.class); final StringBuilder res = new StringBuilder().append(Reporter.NEWLINE).append(Reporter.NEWLINE); root.render(res); return res.toString(); }
private void buildBinders() { if (!bindersBuilt) { jerseyBinder = Multibinder.newSetBinder(binder(), Object.class, Graceland.class); managedBinder = Multibinder.newSetBinder(binder(), Managed.class, Graceland.class); managedClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.ManagedClass, Graceland.class); healthCheckBinder = Multibinder.newSetBinder(binder(), HealthCheck.class, Graceland.class); healthCheckClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.HealthCheckClass, Graceland.class); taskBinder = Multibinder.newSetBinder(binder(), Task.class, Graceland.class); taskClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.TaskClass, Graceland.class); bundleBinder = Multibinder.newSetBinder(binder(), Bundle.class, Graceland.class); bundleClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.BundleClass, Graceland.class); commandBinder = Multibinder.newSetBinder(binder(), Command.class, Graceland.class); commandClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.CommandClass, Graceland.class); initializerBinder = Multibinder.newSetBinder(binder(), Initializer.class, Graceland.class); initializerClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.InitializerClass, Graceland.class); configuratorBinder = Multibinder.newSetBinder(binder(), Configurator.class, Graceland.class); configuratorClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.ConfiguratorClass, Graceland.class); bindersBuilt = true; } }
/** * Ran when the Dropwizard service initializes. This method is responsible for setting up the * {@link io.dropwizard.Bundle}s and {@link io.dropwizard.cli.Command}s. * * @param bootstrap Provided by Dropwizard. */ @Override public void initialize(Bootstrap<PlatformConfiguration> bootstrap) { for (Initializer initializer : wrapper.getInitializers()) { initializer.initialize(bootstrap); LOGGER.debug("Registered Initializer: {}", initializer.getClass().getCanonicalName()); } for (Bundle bundle : wrapper.getBundles()) { bootstrap.addBundle(bundle); LOGGER.debug("Registered Bundle: {}", bundle.getClass().getCanonicalName()); } for (Command command : wrapper.getCommands()) { bootstrap.addCommand(command); LOGGER.debug("Registered Command: {}", command.getClass().getCanonicalName()); } }
@Test public void bundle_binds_work() { final Bundle bundle = mock(Bundle.class); final Class<TestBundle> bundleClass = TestBundle.class; Injector injector = Guice.createInjector(new AbstractPlugin() { @Override protected void configure() { bindBundle(bundle); bindBundle(bundleClass); } }); Set<Bundle> bundleSet = injector.getInstance(Keys.Bundles); Set<Class<? extends Bundle>> bundleClassSet = injector.getInstance(Keys.BundleClasses); assertThat(bundleSet, hasSize(1)); assertThat(bundleSet, hasItem(bundle)); assertThat(bundleClassSet, hasSize(1)); assertThat(bundleClassSet, hasItem(TestBundle.class)); }
@Test public void initialize_adds_bundles() { final Bundle bundle = mock(Bundle.class); final Class<TestBundle> bundleClass = TestBundle.class; Application application = buildApplication( new AbstractPlugin() { @Override protected void configure() { bindBundle(bundle); bindBundle(bundleClass); } } ); new Platform(application).initialize(bootstrap); verify(bootstrap).addBundle(eq(bundle)); verify(bootstrap).addBundle(isA(TestBundle.class)); }
@Override public void initialize(final Bootstrap<T> bootstrap) { if (!Strings.isNullOrEmpty(System.getProperty(SINGULARITY_DEFAULT_CONFIGURATION_PROPERTY))) { bootstrap.setConfigurationSourceProvider(new MergingSourceProvider(bootstrap.getConfigurationSourceProvider(), System.getProperty(SINGULARITY_DEFAULT_CONFIGURATION_PROPERTY), bootstrap.getObjectMapper(), new YAMLFactory())); } final Iterable<? extends Module> additionalModules = checkNotNull(getGuiceModules(bootstrap), "getGuiceModules() returned null"); final Iterable<? extends Bundle> additionalBundles = checkNotNull(getDropwizardBundles(bootstrap), "getDropwizardBundles() returned null"); final Iterable<? extends ConfiguredBundle<T>> additionalConfiguredBundles = checkNotNull(getDropwizardConfiguredBundles(bootstrap), "getDropwizardConfiguredBundles() returned null"); final GuiceBundle<SingularityConfiguration> guiceBundle = GuiceBundle.defaultBuilder(SingularityConfiguration.class) .modules(new SingularityServiceModule()) .modules(additionalModules) .build(); bootstrap.addBundle(guiceBundle); bootstrap.addBundle(new CorsBundle()); bootstrap.addBundle(new ViewBundle()); bootstrap.addBundle(new AssetsBundle("/assets/static/", "/static/")); bootstrap.addBundle(new AssetsBundle("/assets/api-docs/", "/api-docs/", "index.html", "api-docs")); bootstrap.addBundle(new MigrationsBundle<SingularityConfiguration>() { @Override public DataSourceFactory getDataSourceFactory(final SingularityConfiguration configuration) { return configuration.getDatabaseConfiguration().get(); } }); for (Bundle bundle : additionalBundles) { bootstrap.addBundle(bundle); } for (ConfiguredBundle<T> configuredBundle : additionalConfiguredBundles) { bootstrap.addBundle(configuredBundle); } bootstrap.getObjectMapper().registerModule(new ProtobufModule()); bootstrap.getObjectMapper().registerModule(new GuavaModule()); bootstrap.getObjectMapper().setSerializationInclusion(Include.NON_NULL); bootstrap.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
@Test public void testAddBundleIfNotExist() { Bundle newBundle = mock(Bundle.class); Bootstrap<?> bootstrap = new Bootstrap<>(null); // Bundle addedBundle = BootstrapExtensions.addBundleIfNotExist(bootstrap, Bundle.class, () -> newBundle); // assertThat(addedBundle).isSameAs(newBundle); assertThat(BootstrapExtensions.getBundles(bootstrap)).containsExactly(newBundle); }
@Test public void testAddBundleIfNotExistPreExisting() { Supplier<Bundle> bundleSupplier = mock(Supplier.class); Bundle oldBundle = mock(Bundle.class); Bootstrap<?> bootstrap = new Bootstrap<>(null); bootstrap.addBundle(oldBundle); // Bundle addedBundle = BootstrapExtensions.addBundleIfNotExist(bootstrap, Bundle.class, bundleSupplier); // assertThat(addedBundle).isSameAs(oldBundle); assertThat(BootstrapExtensions.getBundles(bootstrap)).containsExactly(oldBundle); verifyZeroInteractions(bundleSupplier); }
@Test public void testGetBundles() { Bootstrap<?> bootstrap = new Bootstrap<>(null); Bundle bundle = mock(Bundle.class); bootstrap.addBundle(bundle); // List<Bundle> bundles = BootstrapExtensions.getBundles(bootstrap); // assertThat(bundles).isNotNull(); assertThat(bundles).containsExactly(bundle); }
@Override public void initialize(Bootstrap<?> bootstrap) { this.application = bootstrap.getApplication(); listServices(Bundle.class).forEach(bootstrap::addBundle); listServices(ConfiguredBundle.class).forEach(bootstrap::addBundle); listServices(Command.class).forEach(bootstrap::addCommand); }
@Override protected void configure() { bind(HK2ValidationBundle.class) .to(HK2ValidationBundle.class) .to(Bundle.class) .in(Singleton.class); bind(HK2ConfiguredBundle.class) .to(HK2ConfiguredBundle.class) .to(ConfiguredBundle.class) .in(Singleton.class); }
@Override public void run(Environment environment) { for(Class<? extends Bundle> bundleClass : runtimeBundleClasses) { Bundle runtimeBundle = injector.getInstance(bundleClass); runtimeBundle.run(environment); } //free up memory runtimeBundleClasses.clear(); isInitialized = true; }
/** * Render entire scope subtree. Scope items are rendered first and bundles at the end (because most likely * they will be subtrees). * * @param config tree config * @param root root node * @param scope scope to render */ private void renderScopeContent(final ContextTreeConfig config, final TreeNode root, final Class<?> scope) { renderScopeItems(config, root, scope); final List<Class<Object>> bundles = service.getData() .getItems(Filters.registeredBy(scope).and(Filters.type(ConfigItem.Bundle))); for (Class<Object> bundle : bundles) { renderBundle(config, root, scope, bundle); } }
/** * Register bundles, recognized from dropwizard bundles. {@link Bundle} used as context. * * @param bundles recognized bundles * @see ru.vyarus.dropwizard.guice.GuiceBundle.Builder#configureFromDropwizardBundles() */ public void registerDwBundles(final List<GuiceyBundle> bundles) { setScope(Bundle.class); for (GuiceyBundle bundle : bundles) { register(ConfigItem.Bundle, bundle); } closeScope(); }
/** * Register bundles resolved by lookup mechanism. {@link GuiceyBundleLookup} used as context. * * @param bundles bundles resolved by lookup mechanism * @see GuiceyBundleLookup */ public void registerLookupBundles(final List<GuiceyBundle> bundles) { setScope(GuiceyBundleLookup.class); for (GuiceyBundle bundle : bundles) { register(ConfigItem.Bundle, bundle); } closeScope(); }
@Override public void initialize(Bootstrap<T> bootstrap) { for (ConfiguredBundle<T> configuredBundle : getConfiguredBundles()) { bootstrap.addBundle(configuredBundle); } for (Bundle bundle : getBundles()) { bootstrap.addBundle(bundle); } }
@Override public Iterable<? extends Bundle> getDropwizardBundles(final Bootstrap<SingularityConfiguration> bootstrap) { return ImmutableSet.of(); }
@Test(expected = NullPointerException.class) public void testAddBundleIfNotExistNullBootstrap() { BootstrapExtensions.addBundleIfNotExist(null, Bundle.class, () -> mock(Bundle.class)); }
@Test(expected = NullPointerException.class) public void testAddBundleIfNotExistNullBundleSupplier() { BootstrapExtensions.addBundleIfNotExist(new Bootstrap<>(null), Bundle.class, null); }
@Test(expected = NullPointerException.class) public void testAddBundleIfNotExistNullBundleType() { BootstrapExtensions.addBundleIfNotExist(new Bootstrap<>(null), null, () -> mock(Bundle.class)); }
public BundleSpec(Bundle bundle, Phase phase) { this.bundle = bundle; this.configuredBundle = null; this.phase = phase; }
public Bundle getBundle() { return bundle; }
public void addBundle(Class<? extends Bundle> bundleClass) { if(isInitialized) { throw new IllegalStateException("You cannot add bundles after GuiceBootstrap has been run"); } runtimeBundleClasses.add(bundleClass); }
public BundleItemInfoImpl(final Class<?> type) { super(ConfigItem.Bundle, type); }
@Override public boolean isFromDwBundle() { return getRegisteredBy().contains(Bundle.class); }
/** * @return all configured bundles (without duplicates) */ public List<GuiceyBundle> getBundles() { return getItems(ConfigItem.Bundle); }
protected void bindBundle(Bundle bundle) { Preconditions.checkNotNull(bundle, "Bundle cannot be null."); buildBinders(); bundleBinder.addBinding().toInstance(bundle); }
protected void bindBundle(Class<? extends Bundle> bundleClass) { Preconditions.checkNotNull(bundleClass, "Bundle Class cannot be null."); buildBinders(); bundleClassBinder.addBinding().toInstance(bundleClass); }
public ImmutableSet<Bundle> getBundles() { return getAndBuildInstances(Keys.Bundles, Keys.BundleClasses); }
@Override public void initialize(final Bootstrap<T> bootstrap) { if (!Strings.isNullOrEmpty(System.getProperty(SINGULARITY_DEFAULT_CONFIGURATION_PROPERTY))) { bootstrap.setConfigurationSourceProvider(new MergingSourceProvider(bootstrap.getConfigurationSourceProvider(), System.getProperty(SINGULARITY_DEFAULT_CONFIGURATION_PROPERTY), bootstrap.getObjectMapper(), new YAMLFactory())); } final Iterable<? extends Module> additionalModules = checkNotNull(getGuiceModules(bootstrap), "getGuiceModules() returned null"); final Iterable<? extends Bundle> additionalBundles = checkNotNull(getDropwizardBundles(bootstrap), "getDropwizardBundles() returned null"); final Iterable<? extends ConfiguredBundle<T>> additionalConfiguredBundles = checkNotNull(getDropwizardConfiguredBundles(bootstrap), "getDropwizardConfiguredBundles() returned null"); guiceBundle = GuiceBundle.defaultBuilder(SingularityConfiguration.class) .modules(new SingularityServiceModule()) .modules(new SingularityAuthModule()) .modules(additionalModules) .build(); bootstrap.addBundle(guiceBundle); bootstrap.addBundle(new CorsBundle()); bootstrap.addBundle(new ViewBundle<>()); bootstrap.addBundle(new AssetsBundle("/assets/static/", "/static/")); bootstrap.addBundle(new AssetsBundle("/assets/api-docs/", "/api-docs/", "index.html", "api-docs")); bootstrap.addBundle(new MigrationsBundle<SingularityConfiguration>() { @Override public DataSourceFactory getDataSourceFactory(final SingularityConfiguration configuration) { return configuration.getDatabaseConfiguration().get(); } }); for (Bundle bundle : additionalBundles) { bootstrap.addBundle(bundle); } for (ConfiguredBundle<T> configuredBundle : additionalConfiguredBundles) { bootstrap.addBundle(configuredBundle); } bootstrap.getObjectMapper().registerModule(new ProtobufModule()); bootstrap.getObjectMapper().registerModule(new GuavaModule()); bootstrap.getObjectMapper().setSerializationInclusion(Include.NON_NULL); bootstrap.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
/** * Adds a {@link Bundle} to a {@link Bootstrap bootstrap} in an idempotent manner. Use this when multiple bundles may want a specific * bundle as a dependency, but don't know if another bundle has already added it to the application. * * @param bootstrap * {@code Bootstrap} to which the bundle should be added * @param bundleType * Type of the bundle to look for when verifying that it hasn't already been added to the {@code bootstrap} * @param bundleSupplier * Supplier for the bundle if it has not already been added to the {@code bootstrap} * @param <T> * Type of the bundle * * @return The first bundle of type {@code T} in the {@code bootstrap} */ <T extends Bundle> T addBundleIfNotExist( @NonNull Bootstrap<?> bootstrap, @NonNull Class<T> bundleType, @NonNull Supplier<T> bundleSupplier ) { List<T> implementingBundles = getImplementingBundles(bootstrap, bundleType); if (implementingBundles.isEmpty()) { T bundle = bundleSupplier.get(); bootstrap.addBundle(bundle); return bundle; } return implementingBundles.get(0); }
/** * Dropwizard bundles used in addition to the bundles required by Singularity. This is an extension point when embedding * Singularity into a custom service. */ public Iterable<? extends Bundle> getDropwizardBundles(Bootstrap<T> bootstrap) { return ImmutableList.of(); }
/** * Retrieves the list of {@link Bundle bundles} registered with a {@link Bootstrap}. Note, this does not return * {@link ConfiguredBundle configured bundles}. * * @param bootstrap * {@code Bootstrap} from which to retrieve bundles * * @return The bundles in the {@code Bootstrap} * * @see #getConfiguredBundles(Bootstrap) */ @SuppressWarnings("unchecked") @SneakyThrows List<Bundle> getBundles(@NonNull Bootstrap<?> bootstrap) { return Collections.unmodifiableList((List<Bundle>) BUNDLES_FIELD.get(bootstrap)); }
/** * Anything added here will only have their run() method called. initialize() will not be called * @param bundleClass */ protected void addBundle(Class<? extends Bundle> bundleClass) { bundles.add(bundleClass); }
/** * @param config tree config * @param info item info * @return true if item is bundle and its hidden by config, false otherwise */ private boolean isHiddenBundle(final ContextTreeConfig config, final ItemInfo info) { return ConfigItem.Bundle.equals(info.getItemType()) && !isScopeVisible(config, null, info.getType()); }
/** * Usual bundle registration from {@link ru.vyarus.dropwizard.guice.GuiceBundle.Builder#bundles(GuiceyBundle...)} * or {@link ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBootstrap#bundles(GuiceyBundle...)}. * Context class is set to currently processed bundle. * * @param bundles bundles to register */ public void registerBundles(final GuiceyBundle... bundles) { for (GuiceyBundle bundle : bundles) { register(ConfigItem.Bundle, bundle); } }