@Override public void run(Environment environment) { ServiceLocatorUtilities.bind(serviceLocator, new EnvBinder(application, environment)); LifecycleEnvironment lifecycle = environment.lifecycle(); AdminEnvironment admin = environment.admin(); listServices(HealthCheck.class).forEach(healthCheck -> { String name = healthCheck.getClass().getSimpleName(); environment.healthChecks().register(name, healthCheck); }); listServices(Managed.class).forEach(lifecycle::manage); listServices(LifeCycle.class).forEach(lifecycle::manage); listServices(LifeCycle.Listener.class).forEach(lifecycle::addLifeCycleListener); listServices(ServerLifecycleListener.class).forEach(lifecycle::addServerLifecycleListener); listServices(Task.class).forEach(admin::addTask); environment.jersey().register(HK2LifecycleListener.class); //Set service locator as parent for Jersey's service locator environment.getApplicationContext().setAttribute(ServletProperties.SERVICE_LOCATOR, serviceLocator); environment.getAdminContext().setAttribute(ServletProperties.SERVICE_LOCATOR, serviceLocator); serviceLocator.inject(application); }
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; } }
@Test public void task_binds_work() { final Task task = mock(Task.class); final Class<TestTask> taskClass = TestTask.class; Injector injector = Guice.createInjector(new AbstractPlugin() { @Override protected void configure() { bindTask(task); bindTask(taskClass); } }); Set<Task> taskSet = injector.getInstance(Keys.Tasks); Set<Class<? extends Task>> taskClassSet = injector.getInstance(Keys.TaskClasses); assertThat(taskSet, hasSize(1)); assertThat(taskSet, hasItem(task)); assertThat(taskClassSet, hasSize(1)); assertThat(taskClassSet, hasItem(TestTask.class)); }
@Test public void run_adds_tasks() throws Exception { final Task task = mock(Task.class); final Class<TestTask> taskClass = TestTask.class; Application application = buildApplication( new AbstractPlugin() { @Override protected void configure() { bindTask(task); bindTask(taskClass); } } ); new Platform(application).run(configuration, environment); verify(adminEnvironment).addTask(eq(task)); verify(adminEnvironment).addTask(isA(TestTask.class)); }
public static Task task() { return new Task(NAME) { @Override public void execute(final ImmutableMultimap<String, String> unused, final PrintWriter printWriter) throws Exception { printWriter.println(new ActivateJobExecutor().start()); } }; }
@Override public void scanAndAdd(Environment environment, Injector injector, Reflections reflections) { Set<Class<? extends Task>> taskClasses = reflections.getSubTypesOf(Task.class); for (Class<? extends Task> task : taskClasses) { environment.admin().addTask(injector.getInstance(task)); LOGGER.info("Added task: " + task); } }
/** * Register Task classes into Dropwizard context. * * @param environment */ private void registerTasks(Environment environment) { final Map<String, Task> beansOfType = applicationContext.getBeansOfType(Task.class); for (String beanName : beansOfType.keySet()) { Task task = beansOfType.get(beanName); environment.admin().addTask(task); logger.info("Registering task: " + task.getClass().getName()); } }
/** * Ran when the Dropwizard service starts up. This method is responsible for setting up the * {@link io.dropwizard.setup.Environment} using the bindings from the loaded * {@link io.graceland.plugin.Plugin}s. * * @param configuration Provided by Dropwizard. * @param environment Provided by Dropwizard. * @throws Exception Thrown by Dropwizard. */ @Override public void run(PlatformConfiguration configuration, Environment environment) throws Exception { dropwizardModule.setup(configuration, environment); for (Configurator configurator : wrapper.getConfigurators()) { configurator.configure(configuration, environment); LOGGER.debug("Registered Configurator: {}", configurator.getClass().getCanonicalName()); } for (Object jerseyComponent : wrapper.getJerseyComponents()) { environment.jersey().register(jerseyComponent); LOGGER.debug("Registered Jersey Component: {}", jerseyComponent.getClass().getCanonicalName()); } for (Managed managed : wrapper.getManaged()) { environment.lifecycle().manage(managed); LOGGER.debug("Registered Managed Object: {}", managed.getClass().getCanonicalName()); } for (HealthCheck healthCheck : wrapper.getHealthChecks()) { environment.healthChecks().register(healthCheck.toString(), healthCheck); LOGGER.debug("Registered Health Check: {}", healthCheck.getClass().getCanonicalName()); } for (Task task : wrapper.getTasks()) { environment.admin().addTask(task); LOGGER.debug("Registered Task: {}", task.getClass().getCanonicalName()); } for (FilterSpec filterSpec : wrapper.getFilterSpecs()) { registerFilterSpec(environment, filterSpec); LOGGER.debug("Registered Filter {}: {}", filterSpec.getName(), filterSpec.getFilter().getClass().getCanonicalName()); } }
private <T extends Task> void registerTask(final Environment environment, Class<T> type) { LOGGER.info("Registering task with class {}", type.getName()); T task = injector.getInstance(type); environment.admin().addTask(task); }
private void registerTasks(Environment environment, Injector injector) { reflections.getSubTypesOf(Task.class).forEach(c -> { environment.admin().addTask(injector.getInstance(c)); }); }
@Override public void addTask(Task task) { _environment.admin().addTask(task); }
@Override public void addTask(Task task) { // ignore }
@Override public boolean matches(final Class<?> type) { return FeatureUtils.is(type, Task.class); }
@Override public void install(final Environment environment, final Task instance) { environment.admin().addTask(instance); }
protected void bindTask(Task task) { Preconditions.checkNotNull(task, "Task cannot be null."); buildBinders(); taskBinder.addBinding().toInstance(task); }
protected void bindTask(Class<? extends Task> taskClass) { Preconditions.checkNotNull(taskClass, "Task Class cannot be null."); buildBinders(); taskClassBinder.addBinding().toInstance(taskClass); }
public ImmutableSet<Task> getTasks() { return getAndBuildInstances(Keys.Tasks, Keys.TaskClasses); }
void addTask(Task task);