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));
}
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;
}
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;
}
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);
}
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()));
}
}
Aggregations