use of com.google.inject.Binder in project roboguice by roboguice.
the class FactoryProvider2 method getBindingFromNewInjector.
/**
* Creates a child injector that binds the args, and returns the binding for the method's result.
*/
public Binding<?> getBindingFromNewInjector(final Method method, final Object[] args, final AssistData data) {
checkState(injector != null, "Factories.create() factories cannot be used until they're initialized by Guice.");
final Key<?> returnType = data.returnType;
// We ignore any pre-existing binding annotation.
final Key<?> returnKey = Key.get(returnType.getTypeLiteral(), RETURN_ANNOTATION);
Module assistedModule = new AbstractModule() {
@Override
@SuppressWarnings({ "unchecked", // raw keys are necessary for the args array and return value
"rawtypes" })
protected void configure() {
Binder binder = binder().withSource(method);
int p = 0;
if (!data.optimized) {
for (Key<?> paramKey : data.paramTypes) {
// Wrap in a Provider to cover null, and to prevent Guice from injecting the parameter
binder.bind((Key) paramKey).toProvider(Providers.of(args[p++]));
}
} else {
for (Key<?> paramKey : data.paramTypes) {
// Bind to our ThreadLocalProviders.
binder.bind((Key) paramKey).toProvider(data.providers.get(p++));
}
}
Constructor constructor = data.constructor;
// message for the user.
if (constructor != null) {
binder.bind(returnKey).toConstructor(constructor, (TypeLiteral) data.implementationType).in(// make sure we erase any scope on the implementation type
Scopes.NO_SCOPE);
}
}
};
Injector forCreate = injector.createChildInjector(assistedModule);
Binding<?> binding = forCreate.getBinding(returnKey);
// If we have providers cached in data, cache the binding for future optimizations.
if (data.optimized) {
data.cachedBinding = binding;
}
return binding;
}
use of com.google.inject.Binder in project graylog2-server by Graylog2.
the class CmdLineTool method run.
@Override
public void run() {
final Level logLevel = setupLogger();
final PluginBindings pluginBindings = installPluginConfigAndBindings(getPluginPath(configFile), chainingClassLoader);
if (isDumpDefaultConfig()) {
dumpDefaultConfigAndExit();
}
final NamedConfigParametersModule configModule = readConfiguration(configFile);
if (isDumpConfig()) {
dumpCurrentConfigAndExit();
}
if (!validateConfiguration()) {
LOG.error("Validating configuration file failed - exiting.");
System.exit(1);
}
beforeStart();
final List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
LOG.info("Running with JVM arguments: {}", Joiner.on(' ').join(arguments));
injector = setupInjector(configModule, pluginBindings, binder -> binder.bind(ChainingClassLoader.class).toInstance(chainingClassLoader));
if (injector == null) {
LOG.error("Injector could not be created, exiting! (Please include the previous error messages in bug reports.)");
System.exit(1);
}
// This is holding all our metrics.
final MetricRegistry metrics = injector.getInstance(MetricRegistry.class);
addInstrumentedAppender(metrics, logLevel);
// Report metrics via JMX.
final JmxReporter reporter = JmxReporter.forRegistry(metrics).build();
reporter.start();
startCommand();
}
Aggregations