Java Code Examples for org.glassfish.hk2.utilities.ServiceLocatorUtilities#bind()

The following examples show how to use org.glassfish.hk2.utilities.ServiceLocatorUtilities#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Jersey2BackstopperConfigHelperTest.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void backstopperOnlyExceptionMapperFactory_removes_all_exception_mappers_except_Jersey2ApiExceptionHandler()
    throws NoSuchFieldException, IllegalAccessException {
    // given
    AbstractBinder lotsOfExceptionMappersBinder = new AbstractBinder() {
        @Override
        protected void configure() {
            bind(JsonMappingExceptionMapper.class).to(ExceptionMapper.class).in(Singleton.class);
            bind(JsonParseExceptionMapper.class).to(ExceptionMapper.class).in(Singleton.class);
            bind(generateJerseyApiExceptionHandler(projectApiErrors, utils)).to(ExceptionMapper.class);
        }
    };

    ServiceLocator locator = ServiceLocatorUtilities.bind(lotsOfExceptionMappersBinder);

    // when
    BackstopperOnlyExceptionMapperFactory overrideExceptionMapper = new BackstopperOnlyExceptionMapperFactory(locator);

    // then
    Set<Object> emTypesLeft = overrideExceptionMapper.getFieldObj(
        ExceptionMapperFactory.class, overrideExceptionMapper, "exceptionMapperTypes"
    );
    assertThat(emTypesLeft).hasSize(1);
    ServiceHandle serviceHandle = overrideExceptionMapper.getFieldObj(emTypesLeft.iterator().next(), "mapper");
    assertThat(serviceHandle.getService()).isInstanceOf(Jersey2ApiExceptionHandler.class);
}
 
Example 2
Source File: Jersey2BackstopperConfigHelperTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void exceptionMapperFactoryOverrideBinder_configures_ExceptionMappers_override() {
    // given
    AbstractBinder defaultJersey2ExceptionMapperBinder = new ExceptionMapperFactory.Binder();
    ExceptionMapperFactoryOverrideBinder overrideBinder =  new ExceptionMapperFactoryOverrideBinder();
    ServiceLocator locator = ServiceLocatorUtilities.bind(defaultJersey2ExceptionMapperBinder, overrideBinder);

    // when
    ExceptionMappers result = locator.getService(ExceptionMappers.class);

    // then
    assertThat(result).isInstanceOf(BackstopperOnlyExceptionMapperFactory.class);
}
 
Example 3
Source File: MigrationFeature.java    From ameba with MIT License 5 votes vote down vote up
private void bindFlyway(final String name, final Flyway flyway) {
    ServiceLocatorUtilities.bind(locator, new AbstractBinder() {
        @Override
        protected void configure() {
            bind(flyway).to(Flyway.class).named(name).proxy(false);
            if (name.equals(DataSourceManager.getDefaultDataSourceName())) {
                bind(flyway).to(Flyway.class).proxy(false);
            }
        }
    });
}
 
Example 4
Source File: SubmarineServer.java    From submarine with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException,
    IOException {
  PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));

  final SubmarineConfiguration conf = SubmarineConfiguration.getInstance();
  LOG.info("Submarine server Host: " + conf.getServerAddress());
  if (conf.useSsl() == false) {
    LOG.info("Submarine server Port: " + conf.getServerPort());
  } else {
    LOG.info("Submarine server SSL Port: " + conf.getServerSslPort());
  }

  jettyWebServer = setupJettyServer(conf);

  // Web UI
  HandlerList handlers = new HandlerList();
  webApp = setupWebAppContext(handlers, conf);
  jettyWebServer.setHandler(handlers);

  // Add
  sharedServiceLocator = ServiceLocatorFactory.getInstance().create("shared-locator");
  ServiceLocatorUtilities.enableImmediateScope(sharedServiceLocator);
  ServiceLocatorUtilities.bind(
      sharedServiceLocator,
      new AbstractBinder() {
        @Override
        protected void configure() {
          bindAsContract(NotebookServer.class)
              .to(WebSocketServlet.class)
              .in(Singleton.class);
        }
      });

  setupRestApiContextHandler(webApp, conf);

  // Notebook server
  setupNotebookServer(webApp, conf, sharedServiceLocator);

  // Cluster Server
  setupClusterServer();

  rpcServer = SubmarineRpcServer.startRpcServer();
  startServer();
}
 
Example 5
Source File: NiPingMonitorApplication.java    From SAPNetworkMonitor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run(ServerConfiguration configuration, Environment environment) throws Exception {

    final DBIFactory factory = new DBIFactory();
    final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "sapData");

    ObjectMapper objectMapper = environment.getObjectMapper();
    SapConfiguration sapConfiguration = configuration.getSapConfig();
    JobConfiguration jobConfiguration = configuration.getJobConfig();
    NiPingServiceBinder niPingServiceBinder = new NiPingServiceBinder(jdbi, objectMapper, sapConfiguration, jobConfiguration);

    ServiceLocator serviceLocator = ServiceLocatorUtilities.bind(niPingServiceBinder);
    SapBasicAuthenticator sapBasicAuthenticator = ServiceLocatorUtilities.getService(serviceLocator, SapBasicAuthenticator.class
            .getName());
    SapOAuthenticator sapOAuthenticator = ServiceLocatorUtilities.getService(serviceLocator, SapOAuthenticator.class.getName());

    final BasicCredentialAuthFilter basicAuthFilter = new BasicCredentialAuthFilter.Builder<BasicAuthUser>()
            .setAuthenticator(sapBasicAuthenticator)
            .buildAuthFilter();
    final AuthFilter oAuthFilter = new OAuthCredentialAuthFilter.Builder<OAuthUser>()
            .setAuthenticator(sapOAuthenticator)
            .setPrefix("Bearer")
            .buildAuthFilter();

    final PolymorphicAuthDynamicFeature feature = new PolymorphicAuthDynamicFeature<UserPrincipal>(ImmutableMap.of(BasicAuthUser
            .class, basicAuthFilter, OAuthUser.class, oAuthFilter));
    final AbstractBinder binder = new PolymorphicAuthValueFactoryProvider.Binder<>(ImmutableSet.of(BasicAuthUser.class, OAuthUser
            .class));
    environment.jersey().register(new AuthFilterDynamicBinding());
    environment.jersey().register(feature);
    environment.jersey().register(binder);

    environment.jersey().register(niPingServiceBinder);
    environment.jersey().packages("com.cloudwise.sap.niping.auth");
    environment.jersey().packages("com.cloudwise.sap.niping.service");
    environment.jersey().packages("com.cloudwise.sap.niping.dao");
    environment.jersey().packages("com.cloudwise.sap.niping.common.vo.converter");
    environment.jersey().packages("com.cloudwise.sap.niping.resource");

    environment.jersey().register(SessionFactoryProvider.class);
    environment.servlets().setSessionHandler(new SessionHandler());
}