Search in sources :

Example 31 with Message

use of com.google.inject.spi.Message in project camel by apache.

the class CloseErrorsImpl method closeError.

public void closeError(Object key, Object object, Exception cause) {
    String message = Errors.format("Failed to close object %s with key %s", object, key);
    errors.addMessage(new Message(errors.getSources(), message, cause));
}
Also used : Message(com.google.inject.spi.Message)

Example 32 with Message

use of com.google.inject.spi.Message in project airlift by airlift.

the class ConfigurationFactory method build.

/**
 * This is used by the configuration provider
 */
<T> T build(ConfigurationProvider<T> configurationProvider) {
    requireNonNull(configurationProvider, "configurationProvider");
    registerConfigurationProvider(configurationProvider, Optional.empty());
    // check for a prebuilt instance
    T instance = getCachedInstance(configurationProvider);
    if (instance != null) {
        return instance;
    }
    ConfigurationBinding<T> configurationBinding = configurationProvider.getConfigurationBinding();
    ConfigurationHolder<T> holder = build(configurationBinding.getConfigClass(), configurationBinding.getPrefix(), getConfigDefaults(configurationBinding.getKey()));
    instance = holder.getInstance();
    // inform caller about warnings
    if (warningsMonitor != null) {
        for (Message message : holder.getProblems().getWarnings()) {
            warningsMonitor.onWarning(message.toString());
        }
    }
    // add to instance cache
    T existingValue = putCachedInstance(configurationProvider, instance);
    // creation race and we lost. Just use the winners' instance;
    if (existingValue != null) {
        return existingValue;
    }
    return instance;
}
Also used : Message(com.google.inject.spi.Message)

Example 33 with Message

use of com.google.inject.spi.Message in project airlift by airlift.

the class Bootstrap method initialize.

public Injector initialize() {
    checkState(!initialized, "Already initialized");
    initialized = true;
    Logging logging = null;
    if (initializeLogging) {
        logging = Logging.initialize();
    }
    Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> log.error(throwable, "Uncaught exception in thread %s", thread.getName()));
    Map<String, String> requiredProperties;
    if (requiredConfigurationProperties == null) {
        // initialize configuration
        log.info("Loading configuration");
        requiredProperties = Collections.emptyMap();
        String configFile = System.getProperty("config");
        if (configFile != null) {
            try {
                requiredProperties = loadPropertiesFrom(configFile);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    } else {
        requiredProperties = requiredConfigurationProperties;
    }
    Map<String, String> unusedProperties = new TreeMap<>(requiredProperties);
    // combine property sources
    Map<String, String> properties = new HashMap<>();
    if (optionalConfigurationProperties != null) {
        properties.putAll(optionalConfigurationProperties);
    }
    properties.putAll(requiredProperties);
    properties.putAll(getSystemProperties());
    // replace environment variables in property values
    List<Message> errors = new ArrayList<>();
    properties = replaceEnvironmentVariables(properties, System.getenv(), (key, error) -> {
        unusedProperties.remove(key);
        errors.add(new Message(error));
    });
    // create configuration factory
    properties = ImmutableSortedMap.copyOf(properties);
    List<Message> warnings = new ArrayList<>();
    ConfigurationFactory configurationFactory = new ConfigurationFactory(properties, warning -> warnings.add(new Message(warning)));
    Boolean quietConfig = configurationFactory.build(BootstrapConfig.class).getQuiet();
    // initialize logging
    if (logging != null) {
        log.info("Initializing logging");
        LoggingConfiguration configuration = configurationFactory.build(LoggingConfiguration.class);
        logging.configure(configuration);
    }
    // Register configuration classes defined in the modules
    configurationFactory.registerConfigurationClasses(modules);
    // Validate configuration classes
    errors.addAll(configurationFactory.validateRegisteredConfigurationProvider());
    // at this point all config file properties should be used
    // so we can calculate the unused properties
    unusedProperties.keySet().removeAll(configurationFactory.getUsedProperties());
    for (String key : unusedProperties.keySet()) {
        Message message = new Message(format("Configuration property '%s' was not used", key));
        (strictConfig ? errors : warnings).add(message);
    }
    // If there are configuration errors, fail-fast to keep output clean
    if (!errors.isEmpty()) {
        throw new ApplicationConfigurationException(errors, warnings);
    }
    // Log effective configuration
    if (!((quietConfig == null) ? quiet : quietConfig)) {
        logConfiguration(configurationFactory);
    }
    // Log any warnings
    if (!warnings.isEmpty()) {
        StringBuilder message = new StringBuilder();
        message.append("Configuration warnings\n");
        message.append("==========\n\n");
        message.append("Configuration should be updated:\n\n");
        for (int index = 0; index < warnings.size(); index++) {
            message.append(format("%s) %s\n", index + 1, warnings.get(index)));
        }
        message.append("\n");
        message.append("==========");
        log.warn(message.toString());
    }
    // system modules
    Builder<Module> moduleList = ImmutableList.builder();
    moduleList.add(new LifeCycleModule());
    moduleList.add(new ConfigurationModule(configurationFactory));
    moduleList.add(binder -> binder.bind(WarningsMonitor.class).toInstance(log::warn));
    // disable broken Guice "features"
    moduleList.add(Binder::disableCircularProxies);
    moduleList.add(Binder::requireExplicitBindings);
    moduleList.add(Binder::requireExactBindingAnnotations);
    moduleList.addAll(modules);
    // create the injector
    Injector injector = Guice.createInjector(Stage.PRODUCTION, moduleList.build());
    // Create the life-cycle manager
    LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    // Start services
    lifeCycleManager.start();
    return injector;
}
Also used : Module(com.google.inject.Module) Logger(io.airlift.log.Logger) ConfigRecord(io.airlift.configuration.ConfigurationInspector.ConfigRecord) HashMap(java.util.HashMap) ConfigurationLoader.getSystemProperties(io.airlift.configuration.ConfigurationLoader.getSystemProperties) ArrayList(java.util.ArrayList) ConfigurationLoader.loadPropertiesFrom(io.airlift.configuration.ConfigurationLoader.loadPropertiesFrom) ImmutableList(com.google.common.collect.ImmutableList) Binder(com.google.inject.Binder) Message(com.google.inject.spi.Message) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) Map(java.util.Map) Logging(io.airlift.log.Logging) ConfigurationUtils.replaceEnvironmentVariables(io.airlift.configuration.ConfigurationUtils.replaceEnvironmentVariables) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ConfigurationModule(io.airlift.configuration.ConfigurationModule) WarningsMonitor(io.airlift.configuration.WarningsMonitor) IOException(java.io.IOException) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Injector(com.google.inject.Injector) Stage(com.google.inject.Stage) UncheckedIOException(java.io.UncheckedIOException) LoggingConfiguration(io.airlift.log.LoggingConfiguration) List(java.util.List) ConfigAttribute(io.airlift.configuration.ConfigurationInspector.ConfigAttribute) ConfigurationInspector(io.airlift.configuration.ConfigurationInspector) TreeMap(java.util.TreeMap) Guice(com.google.inject.Guice) Builder(com.google.common.collect.ImmutableList.Builder) Collections(java.util.Collections) Message(com.google.inject.spi.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) LoggingConfiguration(io.airlift.log.LoggingConfiguration) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) Logging(io.airlift.log.Logging) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TreeMap(java.util.TreeMap) Module(com.google.inject.Module) ConfigurationModule(io.airlift.configuration.ConfigurationModule)

Example 34 with Message

use of com.google.inject.spi.Message in project airlift by airlift.

the class Problems method addWarning.

public void addWarning(String format, Object... params) {
    Message message = new Message(format(format, params));
    warnings.add(message);
    monitor.onWarning(message);
}
Also used : Message(com.google.inject.spi.Message)

Example 35 with Message

use of com.google.inject.spi.Message in project roboguice by roboguice.

the class BinderTest method testUserReportedError.

public void testUserReportedError() {
    final Message message = new Message(getClass(), "Whoops!");
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                addError(message);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertSame(message, Iterables.getOnlyElement(expected.getErrorMessages()));
    }
}
Also used : Message(com.google.inject.spi.Message) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

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