Search in sources :

Example 66 with Provides

use of com.google.inject.Provides in project camel by apache.

the class CamelModuleWithRouteTypes method routes.

@Provides
Set<RoutesBuilder> routes(Injector injector) {
    Set<RoutesBuilder> answer = Sets.newHashSet();
    for (Class<? extends RoutesBuilder> type : routes) {
        RoutesBuilder route = injector.getInstance(type);
        answer.add(route);
    }
    return answer;
}
Also used : RoutesBuilder(org.apache.camel.RoutesBuilder) Provides(com.google.inject.Provides)

Example 67 with Provides

use of com.google.inject.Provides in project cdap by caskdata.

the class DefaultPreviewManager method createPreviewInjector.

/**
   * Create injector for the given application id.
   */
@VisibleForTesting
Injector createPreviewInjector(ApplicationId applicationId) throws IOException {
    CConfiguration previewCConf = CConfiguration.copy(cConf);
    java.nio.file.Path previewDirPath = Paths.get(cConf.get(Constants.CFG_LOCAL_DATA_DIR), "preview").toAbsolutePath();
    Files.createDirectories(previewDirPath);
    java.nio.file.Path previewDir = Files.createDirectories(Paths.get(previewDirPath.toAbsolutePath().toString(), applicationId.getApplication()));
    previewCConf.set(Constants.CFG_LOCAL_DATA_DIR, previewDir.toString());
    Configuration previewHConf = new Configuration(hConf);
    previewHConf.set(Constants.CFG_LOCAL_DATA_DIR, previewDir.toString());
    previewCConf.setIfUnset(Constants.CFG_DATA_LEVELDB_DIR, previewDir.toString());
    previewCConf.setBoolean(Constants.Explore.EXPLORE_ENABLED, false);
    return Guice.createInjector(new ConfigModule(previewCConf, previewHConf), new IOModule(), new AuthenticationContextModules().getMasterModule(), new SecurityModules().getStandaloneModules(), new PreviewSecureStoreModule(secureStore), new PreviewStreamAdminModule(streamAdmin), new PreviewDiscoveryRuntimeModule(discoveryService), new LocationRuntimeModule().getStandaloneModules(), new ConfigStoreModule().getStandaloneModule(), new PreviewRunnerModule(artifactRepository, artifactStore, authorizerInstantiator, authorizationEnforcer, privilegesManager, streamCoordinatorClient, preferencesStore), new ProgramRunnerRuntimeModule().getStandaloneModules(), new PreviewDataModules().getDataFabricModule(transactionManager), new PreviewDataModules().getDataSetsModule(datasetFramework), new DataSetServiceModules().getStandaloneModules(), new MetricsClientRuntimeModule().getStandaloneModules(), new LoggingModules().getStandaloneModules(), new NamespaceStoreModule().getStandaloneModules(), new MessagingServerRuntimeModule().getInMemoryModules(), new AbstractModule() {

        @Override
        protected void configure() {
            // Bind system datasets defined in App-fabric.
            // Have to do it here as public binding, instead of inside PreviewRunnerModule due to Guice 3
            // doesn't support exporting multi-binder from private module
            MapBinder<String, DatasetModule> datasetModuleBinder = MapBinder.newMapBinder(binder(), String.class, DatasetModule.class, Constants.Dataset.Manager.DefaultDatasetModules.class);
            datasetModuleBinder.addBinding("app-fabric").toInstance(new AppFabricDatasetModule());
        }

        @Provides
        @Named(Constants.Service.MASTER_SERVICES_BIND_ADDRESS)
        @SuppressWarnings("unused")
        public InetAddress providesHostname(CConfiguration cConf) {
            String address = cConf.get(Constants.Preview.ADDRESS);
            return Networks.resolve(address, new InetSocketAddress("localhost", 0).getAddress());
        }
    });
}
Also used : IOModule(co.cask.cdap.common.guice.IOModule) DataSetServiceModules(co.cask.cdap.data.runtime.DataSetServiceModules) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Configuration(org.apache.hadoop.conf.Configuration) PreviewDataModules(co.cask.cdap.data.runtime.preview.PreviewDataModules) PreviewSecureStoreModule(co.cask.cdap.security.guice.preview.PreviewSecureStoreModule) PreviewDiscoveryRuntimeModule(co.cask.cdap.common.guice.preview.PreviewDiscoveryRuntimeModule) ConfigModule(co.cask.cdap.common.guice.ConfigModule) InetSocketAddress(java.net.InetSocketAddress) NamespaceStoreModule(co.cask.cdap.store.guice.NamespaceStoreModule) PreviewRunnerModule(co.cask.cdap.app.preview.PreviewRunnerModule) MessagingServerRuntimeModule(co.cask.cdap.messaging.guice.MessagingServerRuntimeModule) MetricsClientRuntimeModule(co.cask.cdap.metrics.guice.MetricsClientRuntimeModule) SecurityModules(co.cask.cdap.security.guice.SecurityModules) AppFabricDatasetModule(co.cask.cdap.internal.app.AppFabricDatasetModule) DatasetModule(co.cask.cdap.api.dataset.module.DatasetModule) ConfigStoreModule(co.cask.cdap.config.guice.ConfigStoreModule) Named(com.google.inject.name.Named) AuthenticationContextModules(co.cask.cdap.security.auth.context.AuthenticationContextModules) LocationRuntimeModule(co.cask.cdap.common.guice.LocationRuntimeModule) AppFabricDatasetModule(co.cask.cdap.internal.app.AppFabricDatasetModule) Provides(com.google.inject.Provides) CConfiguration(co.cask.cdap.common.conf.CConfiguration) LoggingModules(co.cask.cdap.logging.guice.LoggingModules) AbstractModule(com.google.inject.AbstractModule) ProgramRunnerRuntimeModule(co.cask.cdap.app.guice.ProgramRunnerRuntimeModule) MapBinder(com.google.inject.multibindings.MapBinder) PreviewStreamAdminModule(co.cask.cdap.data.stream.preview.PreviewStreamAdminModule) InetAddress(java.net.InetAddress) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 68 with Provides

use of com.google.inject.Provides in project graphhopper by graphhopper.

the class GraphHopperServletModule method createObjectMapper.

@Provides
@Singleton
ObjectMapper createObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new ISO8601DateFormat());
    objectMapper.registerModule(new JtsModule());
    SimpleModule pathDetailModule = new SimpleModule();
    pathDetailModule.addSerializer(PathDetail.class, new PathDetailSerializer());
    pathDetailModule.addDeserializer(PathDetail.class, new PathDetailDeserializer());
    objectMapper.registerModule(pathDetailModule);
    // Because VirtualEdgeIteratorState has getters which throw Exceptions.
    // http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions
    objectMapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {

        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {

                @Override
                public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
                    try {
                        super.serializeAsField(bean, gen, prov);
                    } catch (Exception e) {
                    // Ignoring expected exception, see above.
                    }
                }
            }).collect(Collectors.toList());
        }
    }));
    return objectMapper;
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) CmdArgs(com.graphhopper.util.CmdArgs) IOException(java.io.IOException) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ServletModule(com.google.inject.servlet.ServletModule) Collectors(java.util.stream.Collectors) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) List(java.util.List) Provides(com.google.inject.Provides) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) Map(java.util.Map) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) com.fasterxml.jackson.databind(com.fasterxml.jackson.databind) PathDetail(com.graphhopper.util.details.PathDetail) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) List(java.util.List) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 69 with Provides

use of com.google.inject.Provides in project druid by druid-io.

the class SpnegoFilterConfigTest method testserde.

@Test
public void testserde() {
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.install(new PropertiesModule(Arrays.asList("test.runtime.properties")));
            binder.install(new ConfigModule());
            binder.install(new DruidGuiceExtensions());
            JsonConfigProvider.bind(binder, "druid.hadoop.security.spnego", SpnegoFilterConfig.class);
        }

        @Provides
        @LazySingleton
        public ObjectMapper jsonMapper() {
            return new DefaultObjectMapper();
        }
    });
    Properties props = injector.getInstance(Properties.class);
    SpnegoFilterConfig config = injector.getInstance(SpnegoFilterConfig.class);
    Assert.assertEquals(props.getProperty("druid.hadoop.security.spnego.principal"), config.getPrincipal());
    Assert.assertEquals(props.getProperty("druid.hadoop.security.spnego.keytab"), config.getKeytab());
    Assert.assertEquals(props.getProperty("druid.hadoop.security.spnego.authToLocal"), config.getAuthToLocal());
}
Also used : DruidGuiceExtensions(io.druid.guice.DruidGuiceExtensions) ConfigModule(io.druid.guice.ConfigModule) Provides(com.google.inject.Provides) Properties(java.util.Properties) Binder(com.google.inject.Binder) LazySingleton(io.druid.guice.LazySingleton) Injector(com.google.inject.Injector) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Module(com.google.inject.Module) PropertiesModule(io.druid.guice.PropertiesModule) ConfigModule(io.druid.guice.ConfigModule) PropertiesModule(io.druid.guice.PropertiesModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 70 with Provides

use of com.google.inject.Provides in project druid by druid-io.

the class EmitterModule method getServiceEmitter.

@Provides
@ManageLifecycle
public ServiceEmitter getServiceEmitter(@Self Supplier<DruidNode> configSupplier, Emitter emitter) {
    final DruidNode config = configSupplier.get();
    String version = getClass().getPackage().getImplementationVersion();
    final ImmutableMap<String, String> otherServiceDimensions = ImmutableMap.of("version", // Version is null during `mvn test`.
    Strings.nullToEmpty(version));
    final ServiceEmitter retVal = new ServiceEmitter(config.getServiceName(), config.getHostAndPort(), emitter, otherServiceDimensions);
    EmittingLogger.registerEmitter(retVal);
    return retVal;
}
Also used : ServiceEmitter(com.metamx.emitter.service.ServiceEmitter) DruidNode(io.druid.server.DruidNode) ManageLifecycle(io.druid.guice.ManageLifecycle) Provides(com.google.inject.Provides)

Aggregations

Provides (com.google.inject.Provides)126 Singleton (javax.inject.Singleton)36 AbstractModule (com.google.inject.AbstractModule)26 Singleton (com.google.inject.Singleton)24 Injector (com.google.inject.Injector)22 Named (javax.inject.Named)17 LazySingleton (io.druid.guice.LazySingleton)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Named (com.google.inject.name.Named)8 IOException (java.io.IOException)7 File (java.io.File)6 ArrayList (java.util.ArrayList)6 Inject (javax.inject.Inject)6 Properties (java.util.Properties)5 Client (javax.ws.rs.client.Client)5 Test (org.junit.Test)5 Key (com.google.inject.Key)4 ExecutorService (java.util.concurrent.ExecutorService)4 ConfigModule (co.cask.cdap.common.guice.ConfigModule)3 Binder (com.google.inject.Binder)3