Search in sources :

Example 61 with Injector

use of com.google.inject.Injector in project nhin-d by DirectProject.

the class DefaultRESTServiceModule_createServiceTest method testCreateServiceFromModule.

@Test
public void testCreateServiceFromModule() throws Exception {
    final Injector configInjector = Guice.createInjector(DefaultRESTServiceModule.create("http://bogus", new OpenServiceSecurityManager()));
    AbstractSecuredService service = (AbstractSecuredService) configInjector.getInstance(AddressService.class);
    assertTrue(service instanceof DefaultAddressService);
    service = (AbstractSecuredService) configInjector.getInstance(AnchorService.class);
    assertTrue(service instanceof DefaultAnchorService);
    service = (AbstractSecuredService) configInjector.getInstance(CertificateService.class);
    assertTrue(service instanceof DefaultCertificateService);
    service = (AbstractSecuredService) configInjector.getInstance(CertPolicyService.class);
    assertTrue(service instanceof DefaultCertPolicyService);
    service = (AbstractSecuredService) configInjector.getInstance(DNSService.class);
    assertTrue(service instanceof DefaultDNSService);
    service = (AbstractSecuredService) configInjector.getInstance(DomainService.class);
    assertTrue(service instanceof DefaultDomainService);
    service = (AbstractSecuredService) configInjector.getInstance(SettingService.class);
    assertTrue(service instanceof DefaultSettingService);
    service = (AbstractSecuredService) configInjector.getInstance(TrustBundleService.class);
    assertTrue(service instanceof DefaultTrustBundleService);
}
Also used : DefaultAddressService(org.nhind.config.rest.impl.DefaultAddressService) AbstractSecuredService(org.nhindirect.common.rest.AbstractSecuredService) DefaultDNSService(org.nhind.config.rest.impl.DefaultDNSService) DefaultAnchorService(org.nhind.config.rest.impl.DefaultAnchorService) DefaultAddressService(org.nhind.config.rest.impl.DefaultAddressService) AddressService(org.nhind.config.rest.AddressService) DefaultDomainService(org.nhind.config.rest.impl.DefaultDomainService) DefaultCertificateService(org.nhind.config.rest.impl.DefaultCertificateService) DefaultSettingService(org.nhind.config.rest.impl.DefaultSettingService) Injector(com.google.inject.Injector) OpenServiceSecurityManager(org.nhindirect.common.rest.OpenServiceSecurityManager) DefaultCertPolicyService(org.nhind.config.rest.impl.DefaultCertPolicyService) DefaultTrustBundleService(org.nhind.config.rest.impl.DefaultTrustBundleService) Test(org.junit.Test)

Example 62 with Injector

use of com.google.inject.Injector in project gerrit by GerritCodeReview.

the class InMemoryDatabase method newDatabase.

public static InMemoryDatabase newDatabase(LifecycleManager lifecycle) {
    Injector injector = Guice.createInjector(new InMemoryModule());
    lifecycle.add(injector);
    return injector.getInstance(InMemoryDatabase.class);
}
Also used : Injector(com.google.inject.Injector)

Example 63 with Injector

use of com.google.inject.Injector in project gerrit by GerritCodeReview.

the class Schema_150_to_151_Test method setUp.

@Before
public void setUp() throws Exception {
    Injector injector = Guice.createInjector(new InMemoryModule());
    injector.injectMembers(this);
    lifecycle = new LifecycleManager();
    lifecycle.add(injector);
    lifecycle.start();
    try (ReviewDb underlyingDb = inMemoryDatabase.getDatabase().open()) {
        schemaCreator.create(underlyingDb);
    }
    db = schemaFactory.open();
    Account.Id userId = accountManager.authenticate(AuthRequest.forUser("user")).getAccountId();
    IdentifiedUser user = userFactory.create(userId);
    requestContext.setContext(new RequestContext() {

        @Override
        public CurrentUser getUser() {
            return user;
        }

        @Override
        public Provider<ReviewDb> getReviewDbProvider() {
            return Providers.of(db);
        }
    });
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) CurrentUser(com.google.gerrit.server.CurrentUser) Injector(com.google.inject.Injector) LifecycleManager(com.google.gerrit.lifecycle.LifecycleManager) RequestContext(com.google.gerrit.server.util.RequestContext) ThreadLocalRequestContext(com.google.gerrit.server.util.ThreadLocalRequestContext) InMemoryModule(com.google.gerrit.testutil.InMemoryModule) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) Provider(com.google.inject.Provider) Before(org.junit.Before)

Example 64 with Injector

use of com.google.inject.Injector in project gerrit by GerritCodeReview.

the class JettyServer method makeContext.

private ContextHandler makeContext(final String contextPath, final JettyEnv env, final Config cfg) {
    final ServletContextHandler app = new ServletContextHandler();
    // This enables the use of sessions in Jetty, feature available
    // for Gerrit plug-ins to enable user-level sessions.
    //
    app.setSessionHandler(new SessionHandler());
    app.setErrorHandler(new HiddenErrorHandler());
    // This is the path we are accessed by clients within our domain.
    //
    app.setContextPath(contextPath);
    // HTTP front-end filters to be used as surrogate of Apache HTTP
    // reverse-proxy filtering.
    // It is meant to be used as simpler tiny deployment of custom-made
    // security enforcement (Security tokens, IP-based security filtering, others)
    String[] filterClassNames = cfg.getStringList("httpd", null, "filterClass");
    for (String filterClassName : filterClassNames) {
        try {
            @SuppressWarnings("unchecked") Class<? extends Filter> filterClass = (Class<? extends Filter>) Class.forName(filterClassName);
            Filter filter = env.webInjector.getInstance(filterClass);
            app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
        } catch (Throwable e) {
            String errorMessage = "Unable to instantiate front-end HTTP Filter " + filterClassName;
            log.error(errorMessage, e);
            throw new IllegalArgumentException(errorMessage, e);
        }
    }
    // Perform the same binding as our web.xml would do, but instead
    // of using the listener to create the injector pass the one we
    // already have built.
    //
    GuiceFilter filter = env.webInjector.getInstance(GuiceFilter.class);
    app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    app.addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return env.webInjector;
        }
    });
    // Jetty requires at least one servlet be bound before it will
    // bother running the filter above. Since the filter has all
    // of our URLs except the static resources, the only servlet
    // we need to bind is the default static resource servlet from
    // the Jetty container.
    //
    final ServletHolder ds = app.addServlet(DefaultServlet.class, "/");
    ds.setInitParameter("dirAllowed", "false");
    ds.setInitParameter("redirectWelcome", "false");
    ds.setInitParameter("useFileMappedBuffer", "false");
    ds.setInitParameter("gzip", "true");
    app.setWelcomeFiles(new String[0]);
    return app;
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GuiceFilter(com.google.inject.servlet.GuiceFilter) Filter(javax.servlet.Filter) GuiceFilter(com.google.inject.servlet.GuiceFilter) Injector(com.google.inject.Injector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener)

Example 65 with Injector

use of com.google.inject.Injector in project gerrit by GerritCodeReview.

the class SiteProgram method getDbType.

private String getDbType(Provider<DataSource> dsProvider) {
    String dbProductName;
    try (Connection conn = dsProvider.get().getConnection()) {
        dbProductName = conn.getMetaData().getDatabaseProductName().toLowerCase();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    List<Module> modules = new ArrayList<>();
    modules.add(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
        }
    });
    modules.add(new GerritServerConfigModule());
    modules.add(new DataSourceModule());
    Injector i = Guice.createInjector(modules);
    List<Binding<DataSourceType>> dsTypeBindings = i.findBindingsByType(new TypeLiteral<DataSourceType>() {
    });
    for (Binding<DataSourceType> binding : dsTypeBindings) {
        Annotation annotation = binding.getKey().getAnnotation();
        if (annotation instanceof Named) {
            if (((Named) annotation).value().toLowerCase().contains(dbProductName)) {
                return ((Named) annotation).value();
            }
        }
    }
    throw new IllegalStateException(String.format("Cannot guess database type from the database product name '%s'", dbProductName));
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) Binding(com.google.inject.Binding) Named(com.google.inject.name.Named) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) Injector(com.google.inject.Injector) DataSourceType(com.google.gerrit.server.schema.DataSourceType) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) Module(com.google.inject.Module) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule) DatabaseModule(com.google.gerrit.server.schema.DatabaseModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SchemaModule(com.google.gerrit.server.schema.SchemaModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) AbstractModule(com.google.inject.AbstractModule) DataSourceModule(com.google.gerrit.server.schema.DataSourceModule)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55