@Exposed @Provides @Singleton private LanternAssetManager provideAssetManager(@Option({ "asset-repository-config", "asset-repo-config" }) @Nullable Path repoConfig, LanternPluginManager pluginManager) { final Gson gson = new GsonBuilder().registerTypeAdapter(AssetRepository.class, new AssetRepositoryJsonDeserializer(pluginManager)).create(); try { URL url; if (repoConfig != null) { url = PathUtils.toURL(repoConfig); } else { url = getClass().getClassLoader().getResource("assets_repo.json"); checkNotNull(url); } try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) { return new LanternAssetManager(gson.fromJson(reader, AssetRepository.class)); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public Module getModule(Configuration configuration) { return new PrivateModule() { @Override protected void configure() { bind(ObjectMapper.class).toProvider(ObjectMapperProvider.class); bind(HttpClient.class).toInstance(new JettyHttpClient()); } @Inject @Provides @Exposed @Singleton QueryStatsClient getQueryStatsClient(HttpClient httpClient, ObjectMapper objectMapper, @Named("presto_rest.base_uri") String prestoRestInterfaceBaseUri) { return new HttpQueryStatsClient(httpClient, objectMapper, URI.create(prestoRestInterfaceBaseUri)); } }; }
CheckedProviderMethod( Key<T> key, Method method, Object instance, ImmutableSet<Dependency<?>> dependencies, List<Provider<?>> parameterProviders, Class<? extends Annotation> scopeAnnotation, Class<? extends CheckedProvider> checkedProvider, List<TypeLiteral<?>> exceptionTypes, boolean scopeExceptions) { this.key = key; this.scopeAnnotation = scopeAnnotation; this.instance = instance; this.dependencies = dependencies; this.method = method; this.parameterProviders = parameterProviders; this.exposed = method.isAnnotationPresent(Exposed.class); this.checkedProvider = checkedProvider; this.exceptionTypes = exceptionTypes; this.scopeExceptions = scopeExceptions; method.setAccessible(true); }
/** @param method the method to invoke. It's return type must be the same type as {@code key}. */ private ProviderMethod( Key<T> key, Method method, Object instance, ImmutableSet<Dependency<?>> dependencies, Class<? extends Annotation> scopeAnnotation, Annotation annotation) { // We can be safely initialized eagerly since our bindings must exist statically and it is an // error for them not to. super(InitializationTiming.EAGER); this.key = key; this.scopeAnnotation = scopeAnnotation; this.instance = instance; this.dependencies = dependencies; this.method = method; this.exposed = method.isAnnotationPresent(Exposed.class); this.annotation = annotation; }
public void testPrivateModuleInheritScanner_usingPrivateModule() { Injector injector = Guice.createInjector( NamedMunger.module(), new PrivateModule() { @Override protected void configure() {} @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
public void testPrivateModule_skipSourcesWithinPrivateModule() { Injector injector = Guice.createInjector( NamedMunger.module(), new PrivateModule() { @Override protected void configure() { binder() .skipSources(getClass()) .install( new AbstractModule() { @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
public void testPrivateModule_skipSourcesForPrivateModule() { Injector injector = Guice.createInjector( NamedMunger.module(), new AbstractModule() { @Override protected void configure() { binder() .skipSources(getClass()) .install( new PrivateModule() { @Override protected void configure() {} @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
public void testPrivateModuleInheritScanner_usingPrivateBinder() { Injector injector = Guice.createInjector( NamedMunger.module(), new AbstractModule() { @Override protected void configure() { binder() .newPrivateBinder() .install( new AbstractModule() { @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
public void testPrivateModuleInheritScanner_skipSourcesFromPrivateBinder() { Injector injector = Guice.createInjector( NamedMunger.module(), new AbstractModule() { @Override protected void configure() { binder() .newPrivateBinder() .skipSources(getClass()) .install( new AbstractModule() { @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
public void testPrivateModuleInheritScanner_skipSourcesFromPrivateBinder2() { Injector injector = Guice.createInjector( NamedMunger.module(), new AbstractModule() { @Override protected void configure() { binder() .skipSources(getClass()) .newPrivateBinder() .install( new AbstractModule() { @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
public void testPrivateModuleWithinPrivateModule() { Injector injector = Guice.createInjector( NamedMunger.module(), new PrivateModule() { @Override protected void configure() { expose(Key.get(String.class, named("foo-munged"))); install( new PrivateModule() { @Override protected void configure() {} @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
CheckedProviderMethod( Key<T> key, Method method, Object instance, ImmutableSet<Dependency<?>> dependencies, List<Provider<?>> parameterProviders, Class<? extends Annotation> scopeAnnotation, Class<? extends CheckedProvider> checkedProvider, List<TypeLiteral<?>> exceptionTypes) { this.key = key; this.scopeAnnotation = scopeAnnotation; this.instance = instance; this.dependencies = dependencies; this.method = method; this.parameterProviders = parameterProviders; this.exposed = method.isAnnotationPresent(Exposed.class); this.checkedProvider = checkedProvider; this.exceptionTypes = exceptionTypes; method.setAccessible(true); }
@Provides @Singleton @Exposed ServiceGroupMonitor provideServiceGroupMonitor( CuratorFramework client, Codec<ServiceInstance> codec) { PathChildrenCache groupCache = new PathChildrenCache(client, discoveryPath, true /* cacheData */); return new CuratorServiceGroupMonitor(groupCache, MEMBER_SELECTOR, codec); }
@Provides @Singleton @Exposed SingletonService provideSingletonService( ZooKeeperClient client, ServerSetImpl serverSet, @ServiceDiscoveryBindings.ZooKeeper List<ACL> zookeeperAcls) { return new SingletonServiceImpl( serverSet, SingletonServiceImpl.createSingletonCandidate(client, discoveryPath, zookeeperAcls)); }
@Provides @Singleton @Exposed @Named("AdminKey") String provideAdminKey(AuthorizationConfiguration config, ApiKeyEncryption encryption) { return configurationKeyAsPlaintext(config.getAdminApiKey(), encryption, "admin"); }
private EventualProvider<?> providerFor(Invokable<T, ?> method, Errors methodErrors) { Annotation[] annotations = method.getAnnotations(); verifyMethodAccessibility(methodErrors, method, source); @Nullable Annotation bindingAnnotation = Annotations.findBindingAnnotation(methodErrors, method, annotations); verifyAbsenseOfScopeAnnotation(methodErrors, annotations, source); List<Dependency<ListenableFuture<?>>> dependencies = Lists.newArrayListWithCapacity(method.getParameters().size()); for (Parameter parameter : method.getParameters()) { dependencies.add(extractDependency(methodErrors, parameter)); } Key<ListenableFuture<?>> bindingKey; boolean exposedBinding = method.isAnnotationPresent(Exposed.class); if (isVoid(method)) { bindingKey = futureKey(TypeToken.of(Boolean.class), new BlackholedAnnotation()); exposedBinding = false; } else { bindingKey = futureKey(method.getReturnType(), bindingAnnotation); } return new EventualProvider<>( method, exposedBinding, dependencies, bindingKey, scopeAnnotation, source); }
@Exposed @Eventually.Provides @Named("separator") String separator() { tracker.add(0); return ":"; }
@Exposed @Eventually.Provides @Named("output") String output( @Named("first") String first, @Named("second") String second, @Named("separator") String separator) { tracker.add(3); return first + separator + second; }
@Exposed @Named(DirectoryKeys.CONFIG) @Provides @Singleton private Path provideConfigDirectory(@Option({ "config-directory", "config-dir" }) @Nullable Path configDir) { return configDir == null ? DirectoryKeys.DefaultValues.CONFIG : configDir; }
@Exposed @Named(DirectoryKeys.CONFIG) @Provides @Singleton private File provideConfigDirectoryAsFile(@Named(DirectoryKeys.CONFIG) Path configDir) { return configDir.toFile(); }
@Exposed @Named(DirectoryKeys.PLUGINS) @Provides @Singleton private Path providePluginsDirectory(@Option({ "plugins-directory", "plugins-dir" }) @Nullable Path pluginsDir) { return pluginsDir == null ? DirectoryKeys.DefaultValues.PLUGINS : pluginsDir; }
@Exposed @Named(DirectoryKeys.PLUGINS) @Provides @Singleton private File providePluginsDirectoryAsFile(@Named(DirectoryKeys.PLUGINS) Path pluginsDir) { return pluginsDir.toFile(); }
@Exposed @Named(DirectoryKeys.ROOT_WORLD) @Provides @Singleton private Path provideRootWorldDirectory(@Named(DirectoryKeys.ROOT) Path rootDir, GlobalConfig globalConfig) { return rootDir.resolve(globalConfig.getRootWorldFolder()); }
@Exposed @Named(DirectoryKeys.ROOT_WORLD) @Provides @Singleton private File provideRootWorldDirectoryAsFile(@Named(DirectoryKeys.ROOT_WORLD) Path rootWorldDir) { return rootWorldDir.toFile(); }
@Provides @Exposed public Card provideCard(List<Card> cards) { if (cards.isEmpty()) { return null; } return cards.remove(0); }
public void testPrivateModuleScannersDontImpactSiblings_usingPrivateModule() { Injector injector = Guice.createInjector( new PrivateModule() { @Override protected void configure() { install(NamedMunger.module()); } @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }, new PrivateModule() { @Override protected void configure() {} // ignored! (because the scanner doesn't run over this module) @Exposed @TestProvides @Named("foo") String foo() { return "foo"; } }); assertMungedBinding(injector, String.class, "foo", "foo"); }
@Provides @Exposed @Singleton @Named(SecurityGroupModule.SECURITY_GROUP_DATACENTERS_PROPERTY) Function<CassandraInstance, String> provideDataCenterTransform() { return new Function<CassandraInstance, String>() { @Override public String apply(CassandraInstance instance) { // Transform from Cassandra DataCenter/Rack to AWS Region // Assumes the {@link Ec2Snitch} or {@link Ec2MultiRegionSnitch} // For example, DC: "us-east", Rack: "1a" => Region: "us-east-1" return instance.getDataCenter() + "-" + instance.getRack().substring(0, 1); } }; }
/** * @param method the method to invoke. It's return type must be the same type as {@code key}. */ ProviderMethod(Key<T> key, Method method, Object instance, ImmutableSet<Dependency<?>> dependencies, List<Provider<?>> parameterProviders, Class<? extends Annotation> scopeAnnotation) { this.key = key; this.scopeAnnotation = scopeAnnotation; this.instance = instance; this.dependencies = dependencies; this.method = method; this.parameterProviders = parameterProviders; this.exposed = method.isAnnotationPresent(Exposed.class); method.setAccessible(true); }
@Provides @Singleton @Exposed SingletonService provideSingletonService(CuratorFramework client, Codec<ServiceInstance> codec) { return new CuratorSingletonService(client, discoveryPath, MEMBER_TOKEN, codec); }
@Provides @Singleton @Exposed @DatabusClusterInfo Collection<ClusterInfo> provideCassandraClusterInfo(Collection<ClusterInfo> clusterInfos) { return clusterInfos; }
@Exposed @Eventually.Provides Integer integer() { return 1; }
@Exposed @Eventually.Provides String string() { return "a"; }
@Exposed @Eventually.Provides ListenableFuture<Boolean> getInput(@Named("input") String input) { tracker.add(1); return Futures.immediateFuture(Boolean.parseBoolean(input)); }
@Exposed @Provides @Singleton private org.apache.logging.log4j.Logger provideSlf4jAsLog4jLogger(Logger logger) { return LogManager.getLogger(logger.getName()); }
@Exposed @Provides @Singleton private LanternCommandManager provideCommandManager(Logger logger, LanternGame game) { return new LanternCommandManager(logger, new LanternCommandDisambiguator(game)); }
@Provides @Named("orange") @Singleton @Exposed Widget provideOrangeWidget(WidgetFactory factory) { return factory.createWidget("orange"); }
@Provides @Exposed WidgetPair provideWidgetPair(@Named("banana") Widget banana, @Named("orange") Widget orange) { return new WidgetPair(banana, orange); }
@CheckedProvides(RpcProvider.class) @Named("fruit") @Exposed String provideApples() { return "apple"; }