Search in sources :

Example 11 with Module

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

the class MockMemcachedClient method testBasicInjection.

@Test
public void testBasicInjection() throws Exception {
    final MemcachedCacheConfig config = new MemcachedCacheConfig() {

        @Override
        public String getHosts() {
            return "127.0.0.1:22";
        }
    };
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/memcached");
            binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
            binder.bind(MemcachedCacheConfig.class).toInstance(config);
            binder.bind(Cache.class).toProvider(MemcachedProviderWithConfig.class).in(ManageLifecycle.class);
        }
    }));
    Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    try {
        Cache cache = injector.getInstance(Cache.class);
        Assert.assertEquals(MemcachedCache.class, cache.getClass());
    } finally {
        lifecycle.stop();
    }
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Module(com.google.inject.Module) Test(org.junit.Test)

Example 12 with Module

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

the class JsonConfigTesterBase method setup.

@Before
public void setup() throws IllegalAccessException {
    assertions = 0;
    T fakeValues = EasyMock.createNiceMock(clazz);
    propertyValues.clear();
    testProperties.clear();
    for (Field field : clazz.getDeclaredFields()) {
        final String propertyKey = getPropertyKey(field);
        if (null != propertyKey) {
            field.setAccessible(true);
            Class<?> fieldType = field.getType();
            if (String.class.isAssignableFrom(fieldType)) {
                propertyValues.put(propertyKey, UUID.randomUUID().toString());
            } else if (Collection.class.isAssignableFrom(fieldType)) {
                propertyValues.put(propertyKey, "[]");
            } else if (Map.class.isAssignableFrom(fieldType)) {
                propertyValues.put(propertyKey, "{}");
            } else {
                propertyValues.put(propertyKey, String.valueOf(field.get(fakeValues)));
            }
        }
    }
    testProperties.putAll(System.getProperties());
    testProperties.putAll(propertyValues);
    injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(simpleJsonConfigModule));
    configurator = injector.getBinding(JsonConfigurator.class).getProvider().get();
    configProvider = JsonConfigProvider.of(configPrefix, clazz);
}
Also used : Field(java.lang.reflect.Field) Collection(java.util.Collection) Module(com.google.inject.Module) Before(org.junit.Before)

Example 13 with Module

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

the class ComposingEmitterModuleTest method testGetEmitterViaRealGuice.

@Test
public void testGetEmitterViaRealGuice() {
    Injector injector = Guice.createInjector(new DruidGuiceExtensions(), new LifecycleModule(), new Module() {

        @Override
        public void configure(Binder binder) {
            Properties props = new Properties();
            props.put("druid.emitter.composing.emitters", "[\"" + testEmitterType + "\"]");
            binder.bind(Properties.class).toInstance(props);
            binder.bind(Validator.class).toInstance(Validation.buildDefaultValidatorFactory().getValidator());
            binder.bind(Emitter.class).annotatedWith(Names.named(testEmitterType)).toInstance(emitter);
        }
    }, new ComposingEmitterModule());
    injector.getInstance(Key.get(Emitter.class, Names.named("composing"))).start();
    EasyMock.verify(emitter);
}
Also used : Binder(com.google.inject.Binder) Emitter(com.metamx.emitter.core.Emitter) DruidGuiceExtensions(io.druid.guice.DruidGuiceExtensions) Injector(com.google.inject.Injector) Module(com.google.inject.Module) LifecycleModule(io.druid.guice.LifecycleModule) ComposingEmitterModule(io.druid.server.initialization.ComposingEmitterModule) Properties(java.util.Properties) ComposingEmitterModule(io.druid.server.initialization.ComposingEmitterModule) LifecycleModule(io.druid.guice.LifecycleModule) Test(org.junit.Test)

Example 14 with Module

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

the class ZkCoordinatorTest method testInjector.

@Test
public void testInjector() throws Exception {
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bind(ObjectMapper.class).toInstance(jsonMapper);
            binder.bind(SegmentLoaderConfig.class).toInstance(new SegmentLoaderConfig() {

                @Override
                public File getInfoDir() {
                    return infoDir;
                }

                @Override
                public int getNumLoadingThreads() {
                    return 5;
                }

                @Override
                public int getAnnounceIntervalMillis() {
                    return 50;
                }
            });
            binder.bind(ZkPathsConfig.class).toInstance(new ZkPathsConfig() {

                @Override
                public String getBase() {
                    return "/druid";
                }
            });
            binder.bind(DruidServerMetadata.class).toInstance(new DruidServerMetadata("dummyServer", "dummyHost", 0, "dummyType", "normal", 0));
            binder.bind(DataSegmentAnnouncer.class).toInstance(announcer);
            binder.bind(CuratorFramework.class).toInstance(curator);
            binder.bind(ServerManager.class).toInstance(serverManager);
            binder.bind(ScheduledExecutorFactory.class).toInstance(ScheduledExecutors.createFactory(new Lifecycle()));
        }
    });
    ZkCoordinator zkCoordinator = injector.getInstance(ZkCoordinator.class);
    List<DataSegment> segments = Lists.newLinkedList();
    for (int i = 0; i < COUNT; ++i) {
        segments.add(makeSegment("test" + i, "1", new Interval("P1d/2011-04-01")));
        segments.add(makeSegment("test" + i, "1", new Interval("P1d/2011-04-02")));
        segments.add(makeSegment("test" + i, "2", new Interval("P1d/2011-04-02")));
        segments.add(makeSegment("test_two" + i, "1", new Interval("P1d/2011-04-01")));
        segments.add(makeSegment("test_two" + i, "1", new Interval("P1d/2011-04-02")));
    }
    Collections.sort(segments);
    for (DataSegment segment : segments) {
        writeSegmentToCache(segment);
    }
    checkCache(segments);
    Assert.assertTrue(serverManager.getDataSourceCounts().isEmpty());
    zkCoordinator.start();
    Assert.assertTrue(!serverManager.getDataSourceCounts().isEmpty());
    for (int i = 0; i < COUNT; ++i) {
        Assert.assertEquals(3L, serverManager.getDataSourceCounts().get("test" + i).longValue());
        Assert.assertEquals(2L, serverManager.getDataSourceCounts().get("test_two" + i).longValue());
    }
    Assert.assertEquals(5 * COUNT, announceCount.get());
    zkCoordinator.stop();
    for (DataSegment segment : segments) {
        deleteSegmentFromCache(segment);
    }
    Assert.assertEquals(0, infoDir.listFiles().length);
    Assert.assertTrue(infoDir.delete());
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) DataSegment(io.druid.timeline.DataSegment) Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) ZkPathsConfig(io.druid.server.initialization.ZkPathsConfig) SegmentLoaderConfig(io.druid.segment.loading.SegmentLoaderConfig) Module(com.google.inject.Module) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 15 with Module

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

the class ResourceFilterTestHelper method getRequestPaths.

// Feeds in an array of [ PathName, MethodName, ResourceFilter , Injector]
public static Collection<Object[]> getRequestPaths(final Class clazz, final Iterable<Class<?>> mockableInjections, final Iterable<Key<?>> mockableKeys, final Iterable<?> injectedObjs) {
    final Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            for (Class clazz : mockableInjections) {
                binder.bind(clazz).toInstance(EasyMock.createNiceMock(clazz));
            }
            for (Object obj : injectedObjs) {
                binder.bind((Class) obj.getClass()).toInstance(obj);
            }
            for (Key<?> key : mockableKeys) {
                binder.bind((Key<Object>) key).toInstance(EasyMock.createNiceMock(key.getTypeLiteral().getRawType()));
            }
            binder.bind(AuthConfig.class).toInstance(new AuthConfig(true));
        }
    });
    //Ignore the first "/"
    final String basepath = ((Path) clazz.getAnnotation(Path.class)).value().substring(1);
    final List<Class<? extends ResourceFilter>> baseResourceFilters = clazz.getAnnotation(ResourceFilters.class) == null ? Collections.<Class<? extends ResourceFilter>>emptyList() : ImmutableList.copyOf(((ResourceFilters) clazz.getAnnotation(ResourceFilters.class)).value());
    return ImmutableList.copyOf(Iterables.concat(// Step 3 - Merge all the Objects arrays for each endpoints
    Iterables.transform(//  - Resource Filter instance for the endpoint
    Iterables.filter(// ResourceFilters applied to them
    ImmutableList.copyOf(clazz.getDeclaredMethods()), new Predicate<Method>() {

        @Override
        public boolean apply(Method input) {
            return input.getAnnotation(GET.class) != null || input.getAnnotation(POST.class) != null || input.getAnnotation(DELETE.class) != null && (input.getAnnotation(ResourceFilters.class) != null || !baseResourceFilters.isEmpty());
        }
    }), new Function<Method, Collection<Object[]>>() {

        @Override
        public Collection<Object[]> apply(final Method method) {
            final List<Class<? extends ResourceFilter>> resourceFilters = method.getAnnotation(ResourceFilters.class) == null ? baseResourceFilters : ImmutableList.copyOf(method.getAnnotation(ResourceFilters.class).value());
            return Collections2.transform(resourceFilters, new Function<Class<? extends ResourceFilter>, Object[]>() {

                @Override
                public Object[] apply(Class<? extends ResourceFilter> input) {
                    if (method.getAnnotation(Path.class) != null) {
                        return new Object[] { String.format("%s%s", basepath, method.getAnnotation(Path.class).value()), input.getAnnotation(GET.class) == null ? (method.getAnnotation(DELETE.class) == null ? "POST" : "DELETE") : "GET", injector.getInstance(input), injector };
                    } else {
                        return new Object[] { basepath, input.getAnnotation(GET.class) == null ? (method.getAnnotation(DELETE.class) == null ? "POST" : "DELETE") : "GET", injector.getInstance(input), injector };
                    }
                }
            });
        }
    })));
}
Also used : Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) AuthConfig(io.druid.server.security.AuthConfig) Method(java.lang.reflect.Method) Predicate(com.google.common.base.Predicate) Binder(com.google.inject.Binder) ResourceFilters(com.sun.jersey.spi.container.ResourceFilters) Function(com.google.common.base.Function) ResourceFilter(com.sun.jersey.spi.container.ResourceFilter) DELETE(javax.ws.rs.DELETE) Injector(com.google.inject.Injector) GET(javax.ws.rs.GET) Collection(java.util.Collection) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Module(com.google.inject.Module) Key(com.google.inject.Key)

Aggregations

Module (com.google.inject.Module)386 Injector (com.google.inject.Injector)253 AbstractModule (com.google.inject.AbstractModule)252 Binder (com.google.inject.Binder)63 Test (org.junit.Test)51 PrivateModule (com.google.inject.PrivateModule)46 Provider (com.google.inject.Provider)44 ArrayList (java.util.ArrayList)30 CreationException (com.google.inject.CreationException)26 Guice.createInjector (com.google.inject.Guice.createInjector)25 Map (java.util.Map)24 Properties (java.util.Properties)22 ImmutableList (com.google.common.collect.ImmutableList)20 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)20 Key (com.google.inject.Key)19 Set (java.util.Set)19 ImmutableSet (com.google.common.collect.ImmutableSet)18 Binding (com.google.inject.Binding)17 List (java.util.List)17