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);
}
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);
}
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);
}
});
}
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;
}
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));
}
Aggregations