use of com.google.inject.spi.Message in project gerrit by GerritCodeReview.
the class WebAppInitializer method init.
private synchronized void init() {
if (manager == null) {
String path = System.getProperty(GERRIT_SITE_PATH);
if (path != null) {
sitePath = Paths.get(path);
} else {
throw new ProvisionException(GERRIT_SITE_PATH + " must be defined");
}
if (System.getProperty("gerrit.init") != null) {
List<String> pluginsToInstall;
String installPlugins = System.getProperty("gerrit.install_plugins");
if (installPlugins == null) {
pluginsToInstall = null;
} else {
pluginsToInstall = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(installPlugins);
}
new SiteInitializer(path, System.getProperty(GERRIT_SITE_PATH), new UnzippedDistribution(servletContext), pluginsToInstall).init();
}
try {
cfgInjector = createCfgInjector();
} catch (CreationException ce) {
final Message first = ce.getErrorMessages().iterator().next();
final StringBuilder buf = new StringBuilder();
buf.append(first.getMessage());
Throwable why = first.getCause();
while (why != null) {
buf.append("\n caused by ");
buf.append(why.toString());
why = why.getCause();
}
if (first.getCause() != null) {
buf.append("\n");
buf.append("\nResolve above errors before continuing.");
buf.append("\nComplete stack trace follows:");
}
logger.atSevere().withCause(first.getCause()).log(buf.toString());
throw new CreationException(Collections.singleton(first));
}
dbInjector = createDbInjector();
initIndexType();
config = cfgInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
sysInjector = createSysInjector();
if (!sshdOff()) {
sshInjector = createSshInjector();
}
webInjector = createWebInjector();
PluginGuiceEnvironment env = sysInjector.getInstance(PluginGuiceEnvironment.class);
env.setDbCfgInjector(dbInjector, cfgInjector);
if (sshInjector != null) {
env.setSshInjector(sshInjector);
}
env.setHttpInjector(webInjector);
// Push the Provider<HttpServletRequest> down into the canonical
// URL provider. Its optional for that provider, but since we can
// supply one we should do so, in case the administrator has not
// setup the canonical URL in the configuration file.
//
// Note we have to do this manually as Guice failed to do the
// injection here because the HTTP environment is not visible
// to the core server modules.
//
sysInjector.getInstance(HttpCanonicalWebUrlProvider.class).setHttpServletRequest(webInjector.getProvider(HttpServletRequest.class));
filter = webInjector.getInstance(GuiceFilter.class);
manager = new LifecycleManager();
manager.add(dbInjector);
manager.add(cfgInjector);
manager.add(sysInjector);
if (sshInjector != null) {
manager.add(sshInjector);
}
manager.add(webInjector);
}
}
use of com.google.inject.spi.Message in project gerrit by GerritCodeReview.
the class SiteProgram method createDbInjector.
/**
* Provides database connectivity and site path.
*/
protected Injector createDbInjector(boolean enableMetrics) {
List<Module> modules = new ArrayList<>();
Module sitePathModule = new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
};
modules.add(sitePathModule);
if (enableMetrics) {
modules.add(new DropWizardMetricMaker.ApiModule());
} else {
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(MetricMaker.class).to(DisabledMetricMaker.class);
}
});
}
modules.add(new LifecycleModule() {
@Override
protected void configure() {
listener().to(SystemReaderInstaller.class);
}
});
Module configModule = new GerritServerConfigModule();
modules.add(configModule);
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(GerritRuntime.class).toInstance(getGerritRuntime());
}
});
Injector cfgInjector = Guice.createInjector(sitePathModule, configModule);
modules.add(new SchemaModule());
modules.add(cfgInjector.getInstance(GitRepositoryManagerModule.class));
// The only implementation of experiments is available in all programs that can use
// gerrit.config
modules.add(new ConfigExperimentFeaturesModule());
try {
return Guice.createInjector(PRODUCTION, ModuleOverloader.override(modules, LibModuleLoader.loadModules(cfgInjector, LibModuleType.DB_MODULE_TYPE)));
} catch (CreationException ce) {
Message first = ce.getErrorMessages().iterator().next();
Throwable why = first.getCause();
StringBuilder buf = new StringBuilder();
if (why != null) {
buf.append(why.getMessage());
why = why.getCause();
} else {
buf.append(first.getMessage());
}
while (why != null) {
buf.append("\n caused by ");
buf.append(why.toString());
why = why.getCause();
}
throw die(buf.toString(), new RuntimeException("DbInjector failed", ce));
}
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class CheckedProvideUtils method findThrowingConstructor.
// safe because it's a constructor of the typeLiteral
@SuppressWarnings("unchecked")
static <T> Constructor<? extends T> findThrowingConstructor(TypeLiteral<? extends T> typeLiteral, Binder binder) {
Class<?> rawType = typeLiteral.getRawType();
Errors errors = new Errors(rawType);
Constructor<?> cxtor = null;
for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
if (constructor.isAnnotationPresent(ThrowingInject.class)) {
if (cxtor != null) {
errors.addMessage("%s has more than one constructor annotated with @ThrowingInject. " + CONSTRUCTOR_RULES, rawType);
}
cxtor = constructor;
Annotation misplacedBindingAnnotation = Annotations.findBindingAnnotation(errors, cxtor, ((AnnotatedElement) cxtor).getAnnotations());
if (misplacedBindingAnnotation != null) {
errors.misplacedBindingAnnotation(cxtor, misplacedBindingAnnotation);
}
}
}
if (cxtor == null) {
errors.addMessage("Could not find a suitable constructor in %s. " + CONSTRUCTOR_RULES, rawType);
}
for (Message msg : errors.getMessages()) {
binder.addError(msg);
}
return (Constructor<? extends T>) cxtor;
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class ProviderMethodsModule method createProviderMethod.
private <T> ProviderMethod<T> createProviderMethod(Binder binder, Method method) {
binder = binder.withSource(method);
Errors errors = new Errors(method);
// prepare the parameter providers
List<Dependency<?>> dependencies = Lists.newArrayList();
List<Provider<?>> parameterProviders = Lists.newArrayList();
List<TypeLiteral<?>> parameterTypes = typeLiteral.getParameterTypes(method);
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterTypes.size(); i++) {
Key<?> key = getKey(errors, parameterTypes.get(i), method, parameterAnnotations[i]);
if (key.equals(LOGGER_KEY)) {
// If it was a Logger, change the key to be unique & bind it to a
// provider that provides a logger with a proper name.
// This solves issue 482 (returning a new anonymous logger on every call exhausts memory)
Key<Logger> loggerKey = Key.get(Logger.class, UniqueAnnotations.create());
binder.bind(loggerKey).toProvider(new LogProvider(method));
key = loggerKey;
}
dependencies.add(Dependency.get(key));
parameterProviders.add(binder.getProvider(key));
}
// Define T as the method's return type.
@SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
for (Message message : errors.getMessages()) {
binder.addError(message);
}
return ProviderMethod.create(key, method, delegate, ImmutableSet.copyOf(dependencies), parameterProviders, scopeAnnotation, skipFastClassGeneration);
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class Errors method getOnlyCause.
/**
* Returns the cause throwable if there is exactly one cause in {@code messages}. If there are
* zero or multiple messages with causes, null is returned.
*/
public static Throwable getOnlyCause(Collection<Message> messages) {
Throwable onlyCause = null;
for (Message message : messages) {
Throwable messageCause = message.getCause();
if (messageCause == null) {
continue;
}
if (onlyCause != null) {
return null;
}
onlyCause = messageCause;
}
return onlyCause;
}
Aggregations