use of io.jooby.ServiceRegistry in project jooby by jooby-project.
the class HikariModule method install.
@Override
public void install(@Nonnull Jooby application) {
if (hikari == null) {
hikari = build(application.getEnvironment(), database);
}
if (metricRegistry != null) {
hikari.setMetricRegistry(metricRegistry);
}
if (healthCheckRegistry != null) {
hikari.setHealthCheckRegistry(healthCheckRegistry);
}
HikariDataSource dataSource = new HikariDataSource(hikari);
ServiceRegistry registry = application.getServices();
ServiceKey<DataSource> key = ServiceKey.key(DataSource.class, database);
/**
* Global default database:
*/
registry.putIfAbsent(KEY, dataSource);
/**
* Specific access:
*/
registry.put(key, dataSource);
application.onStop(dataSource::close);
}
use of io.jooby.ServiceRegistry in project jooby by jooby-project.
the class HandlebarsModule method install.
@Override
public void install(@Nonnull Jooby application) throws Exception {
if (handlebars == null) {
handlebars = create().setTemplatesPath(templatesPathString).setTemplatesPath(templatesPath).build(application.getEnvironment());
}
application.encoder(new HbsTemplateEngine(handlebars, EXT));
ServiceRegistry services = application.getServices();
services.put(Handlebars.class, handlebars);
}
use of io.jooby.ServiceRegistry in project jooby by jooby-project.
the class HibernateModule method install.
@Override
public void install(@Nonnull Jooby application) {
Environment env = application.getEnvironment();
Config config = application.getConfig();
ServiceRegistry registry = application.getServices();
DataSource dataSource = registry.getOrNull(ServiceKey.key(DataSource.class, name));
boolean fallback = false;
if (dataSource == null) {
// TODO: replace with usage exception
dataSource = registry.require(DataSource.class);
fallback = true;
}
BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder();
boolean defaultDdlAuto = env.isActive("dev", "test");
boolean flyway = isFlywayPresent(env, registry, name, fallback);
String ddlAuto = flyway ? "none" : (defaultDdlAuto ? "update" : "none");
configurer.configure(bsrb, config);
BootstrapServiceRegistry bsr = bsrb.build();
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsr);
ssrb.applySetting(AvailableSettings.HBM2DDL_AUTO, ddlAuto);
ssrb.applySetting(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
// apply application.conf
Map<String, String> base = env.getProperties("hibernate");
Map<String, String> custom = env.getProperties(name + ".hibernate", "hibernate");
Map<String, String> settings = new HashMap<>();
settings.putAll(base);
settings.putAll(custom);
ssrb.applySettings(settings);
ssrb.applySetting(AvailableSettings.DATASOURCE, dataSource);
ssrb.applySetting(AvailableSettings.DELAY_CDI_ACCESS, true);
configurer.configure(ssrb, config);
StandardServiceRegistry serviceRegistry = ssrb.build();
if (packages.isEmpty() && classes.isEmpty()) {
packages = Stream.of(application.getBasePackage()).filter(Objects::nonNull).collect(Collectors.toList());
}
MetadataSources sources = new MetadataSources(serviceRegistry);
packages.forEach(sources::addPackage);
classes.forEach(sources::addAnnotatedClass);
configurer.configure(sources, config);
/**
* Scan package?
*/
ClassLoader classLoader = env.getClassLoader();
List<URL> packages = sources.getAnnotatedPackages().stream().map(pkg -> classLoader.getResource(pkg.replace('.', '/'))).collect(Collectors.toList());
MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
if (packages.size() > 0) {
metadataBuilder.applyScanEnvironment(new ScanEnvImpl(packages));
}
configurer.configure(metadataBuilder, config);
Metadata metadata = metadataBuilder.build();
SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
sfb.applyName(name);
sfb.applyNameAsJndiName(false);
configurer.configure(sfb, config);
SessionFactory sf = sfb.build();
/**
* Session and EntityManager.
*/
Provider sessionServiceProvider = new SessionServiceProvider(sf, sessionBuilder);
registry.putIfAbsent(Session.class, sessionServiceProvider);
registry.put(ServiceKey.key(Session.class, name), sessionServiceProvider);
registry.putIfAbsent(EntityManager.class, sessionServiceProvider);
registry.put(ServiceKey.key(EntityManager.class, name), sessionServiceProvider);
/**
* SessionFactory and EntityManagerFactory.
*/
registry.putIfAbsent(SessionFactory.class, sf);
registry.put(ServiceKey.key(SessionFactory.class, name), sf);
registry.putIfAbsent(EntityManagerFactory.class, sf);
registry.put(ServiceKey.key(EntityManagerFactory.class, name), sf);
/**
* Session Provider:
*/
registry.putIfAbsent(SessionProvider.class, sessionBuilder);
registry.put(ServiceKey.key(SessionProvider.class, name), sessionBuilder);
/**
* UnitOfWork Provider:
*/
UnitOfWorkProvider unitOfWorkProvider = new UnitOfWorkProvider(sf, sessionBuilder);
registry.putIfAbsent(UnitOfWork.class, unitOfWorkProvider);
registry.put(ServiceKey.key(UnitOfWork.class, name), unitOfWorkProvider);
application.onStop(sf::close);
}
use of io.jooby.ServiceRegistry in project jooby by jooby-project.
the class CommonsEmailModule method install.
@Override
public void install(@Nonnull Jooby application) {
Config config = mailConfig(application.getConfig(), name);
ServiceRegistry services = application.getServices();
EmailFactory factory = new EmailFactory(config);
register(services, SimpleEmail.class, factory::newSimpleEmail);
register(services, HtmlEmail.class, factory::newHtmlEmail);
register(services, MultiPartEmail.class, factory::newMultiPartEmail);
register(services, ImageHtmlEmail.class, factory::newImageHtmlEmail);
}
use of io.jooby.ServiceRegistry in project jooby by jooby-project.
the class GraphQLModule method install.
@Override
public void install(@Nonnull Jooby application) throws Exception {
String graphqlPath = application.getEnvironment().getProperty("graphql.path", "/graphql");
GraphQLHandler handler = async ? new GraphQLHandler(graphQL) : new BlockingGraphQLHandler(graphQL);
if (supportGetRequest) {
executionMode(application.get(graphqlPath, handler), async);
}
executionMode(application.post(graphqlPath, handler), async);
ServiceRegistry services = application.getServices();
services.put(GraphQL.class, graphQL);
}
Aggregations