Search in sources :

Example 1 with ServiceRegistry

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);
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) ServiceRegistry(io.jooby.ServiceRegistry) DataSource(javax.sql.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource)

Example 2 with ServiceRegistry

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);
}
Also used : ServiceRegistry(io.jooby.ServiceRegistry)

Example 3 with ServiceRegistry

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);
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) Arrays(java.util.Arrays) Provider(javax.inject.Provider) AvailableSettings(org.hibernate.cfg.AvailableSettings) URL(java.net.URL) Extension(io.jooby.Extension) Session(org.hibernate.Session) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) ScanEnvImpl(io.jooby.internal.hibernate.ScanEnvImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) Metadata(org.hibernate.boot.Metadata) UnitOfWorkProvider(io.jooby.internal.hibernate.UnitOfWorkProvider) Map(java.util.Map) Jooby(io.jooby.Jooby) DataSource(javax.sql.DataSource) Nonnull(javax.annotation.Nonnull) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SessionBuilder(org.hibernate.SessionBuilder) Config(com.typesafe.config.Config) Environment(io.jooby.Environment) ServiceKey(io.jooby.ServiceKey) SessionFactory(org.hibernate.SessionFactory) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) EntityManager(javax.persistence.EntityManager) ServiceRegistry(io.jooby.ServiceRegistry) Collectors(java.util.stream.Collectors) MetadataSources(org.hibernate.boot.MetadataSources) Objects(java.util.Objects) SessionServiceProvider(io.jooby.internal.hibernate.SessionServiceProvider) MetadataBuilder(org.hibernate.boot.MetadataBuilder) List(java.util.List) Stream(java.util.stream.Stream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Optional(java.util.Optional) Collections(java.util.Collections) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) Config(com.typesafe.config.Config) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) URL(java.net.URL) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) SessionServiceProvider(io.jooby.internal.hibernate.SessionServiceProvider) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SessionFactory(org.hibernate.SessionFactory) MetadataBuilder(org.hibernate.boot.MetadataBuilder) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) DataSource(javax.sql.DataSource) Provider(javax.inject.Provider) UnitOfWorkProvider(io.jooby.internal.hibernate.UnitOfWorkProvider) SessionServiceProvider(io.jooby.internal.hibernate.SessionServiceProvider) ScanEnvImpl(io.jooby.internal.hibernate.ScanEnvImpl) EntityManager(javax.persistence.EntityManager) UnitOfWorkProvider(io.jooby.internal.hibernate.UnitOfWorkProvider) EntityManagerFactory(javax.persistence.EntityManagerFactory) Objects(java.util.Objects) Environment(io.jooby.Environment) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(io.jooby.ServiceRegistry) Session(org.hibernate.Session)

Example 4 with ServiceRegistry

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);
}
Also used : Config(com.typesafe.config.Config) EmailFactory(io.jooby.internal.email.EmailFactory) ServiceRegistry(io.jooby.ServiceRegistry)

Example 5 with ServiceRegistry

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);
}
Also used : BlockingGraphQLHandler(io.jooby.internal.graphql.BlockingGraphQLHandler) GraphQLHandler(io.jooby.internal.graphql.GraphQLHandler) BlockingGraphQLHandler(io.jooby.internal.graphql.BlockingGraphQLHandler) ServiceRegistry(io.jooby.ServiceRegistry)

Aggregations

ServiceRegistry (io.jooby.ServiceRegistry)27 Config (com.typesafe.config.Config)10 Environment (io.jooby.Environment)8 Map (java.util.Map)7 Jooby (io.jooby.Jooby)5 DataSource (javax.sql.DataSource)5 ServiceKey (io.jooby.ServiceKey)4 Properties (java.util.Properties)4 Nonnull (javax.annotation.Nonnull)4 Extension (io.jooby.Extension)3 HashMap (java.util.HashMap)3 List (java.util.List)3 DatabaseConfig (io.ebean.config.DatabaseConfig)2 JobMethodDetail (io.jooby.internal.quartz.JobMethodDetail)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2 Provider (javax.inject.Provider)2