Search in sources :

Example 16 with Message

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);
    }
}
Also used : PluginGuiceEnvironment(com.google.gerrit.server.plugins.PluginGuiceEnvironment) Message(com.google.inject.spi.Message) LifecycleManager(com.google.gerrit.lifecycle.LifecycleManager) CreationException(com.google.inject.CreationException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpCanonicalWebUrlProvider(com.google.gerrit.httpd.HttpCanonicalWebUrlProvider) ProvisionException(com.google.inject.ProvisionException) GuiceFilter(com.google.inject.servlet.GuiceFilter)

Example 17 with Message

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));
    }
}
Also used : SchemaModule(com.google.gerrit.server.schema.SchemaModule) Message(com.google.inject.spi.Message) GerritServerConfigModule.getSecureStoreClassName(com.google.gerrit.server.config.GerritServerConfigModule.getSecureStoreClassName) SecureStoreClassName(com.google.gerrit.server.securestore.SecureStoreClassName) ArrayList(java.util.ArrayList) CreationException(com.google.inject.CreationException) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) Injector(com.google.inject.Injector) ConfigExperimentFeaturesModule(com.google.gerrit.server.experiments.ConfigExperimentFeatures.ConfigExperimentFeaturesModule) DropWizardMetricMaker(com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker) SitePath(com.google.gerrit.server.config.SitePath) GitRepositoryManagerModule(com.google.gerrit.server.git.GitRepositoryManagerModule) Module(com.google.inject.Module) ConfigExperimentFeaturesModule(com.google.gerrit.server.experiments.ConfigExperimentFeatures.ConfigExperimentFeaturesModule) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SchemaModule(com.google.gerrit.server.schema.SchemaModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) AbstractModule(com.google.inject.AbstractModule) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker) LifecycleModule(com.google.gerrit.lifecycle.LifecycleModule) SystemReaderInstaller(com.google.gerrit.server.git.SystemReaderInstaller)

Example 18 with Message

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;
}
Also used : Errors(com.google.inject.internal.Errors) Message(com.google.inject.spi.Message) Constructor(java.lang.reflect.Constructor) Annotation(java.lang.annotation.Annotation)

Example 19 with Message

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);
}
Also used : Message(com.google.inject.spi.Message) Dependency(com.google.inject.spi.Dependency) Logger(java.util.logging.Logger) Provider(com.google.inject.Provider) TypeLiteral(com.google.inject.TypeLiteral)

Example 20 with Message

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;
}
Also used : Message(com.google.inject.spi.Message)

Aggregations

Message (com.google.inject.spi.Message)49 ProvisionException (com.google.inject.ProvisionException)9 CreationException (com.google.inject.CreationException)7 ArrayList (java.util.ArrayList)7 AbstractModule (com.google.inject.AbstractModule)6 Provider (com.google.inject.Provider)5 TypeLiteral (com.google.inject.TypeLiteral)5 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 Errors (com.google.inject.internal.Errors)4 InjectionPoint (com.google.inject.spi.InjectionPoint)4 DisabledMetricMaker (com.google.gerrit.metrics.DisabledMetricMaker)3 GerritServerConfigModule (com.google.gerrit.server.config.GerritServerConfigModule)3 SitePath (com.google.gerrit.server.config.SitePath)3 Test (org.junit.Test)3 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)2 Function (com.google.common.base.Function)2 ImmutableList (com.google.common.collect.ImmutableList)2