
com.proofpoint.configuration.ConfigurationFactoryBuilder Maven / Gradle / Ivy
/*
* Copyright 2010 Proofpoint, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.proofpoint.configuration;
import com.google.common.collect.ImmutableMap;
import com.proofpoint.configuration.Problems.Monitor;
import javax.annotation.Nullable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import static com.google.common.base.Charsets.UTF_8;
import static java.lang.String.format;
public final class ConfigurationFactoryBuilder
{
private final Map properties = new HashMap<>();
private final Set expectToUse = new HashSet<>();
private Monitor monitor = Problems.NULL_MONITOR;
private final List errors = new ArrayList<>();
private Map applicationDefaults = ImmutableMap.of();
private Map moduleDefaults = ImmutableMap.of();
private Map moduleDefaultSource = ImmutableMap.of();
/**
* Loads properties from the given file
*
* @param path file path
* @return self
* @throws java.io.IOException errors
*/
public ConfigurationFactoryBuilder withFile(@Nullable final String path)
throws IOException
{
if (path == null) {
return this;
}
final Properties properties = new Properties() {
@SuppressWarnings("UseOfPropertiesAsHashtable")
@Override
public synchronized Object put(Object key, Object value) {
final Object old = super.put(key, value);
if (old != null) {
errors.add(format("Duplicate configuration property '%s' in file %s", key, path));
}
return old;
}
};
try (Reader reader = new InputStreamReader(new FileInputStream(path), UTF_8)) {
properties.load(reader);
}
mergeProperties(properties);
expectToUse.addAll(properties.stringPropertyNames());
return this;
}
public ConfigurationFactoryBuilder withSystemProperties()
{
mergeProperties(System.getProperties());
return this;
}
public ConfigurationFactoryBuilder withRequiredProperties(Map requiredConfigurationProperties)
{
properties.putAll(requiredConfigurationProperties);
expectToUse.addAll(requiredConfigurationProperties.keySet());
return this;
}
public ConfigurationFactoryBuilder withApplicationDefaults(Map applicationDefaults)
{
this.applicationDefaults = ImmutableMap.copyOf(applicationDefaults);
expectToUse.addAll(applicationDefaults.keySet());
return this;
}
public ConfigurationFactoryBuilder withModuleDefaults(Map moduleDefaults, Map moduleDefaultSource)
{
this.moduleDefaults = ImmutableMap.copyOf(moduleDefaults);
this.moduleDefaultSource = ImmutableMap.copyOf(moduleDefaultSource);
expectToUse.addAll(moduleDefaults.keySet());
return this;
}
public ConfigurationFactoryBuilder withMonitor(Monitor monitor)
{
this.monitor = monitor;
return this;
}
public ConfigurationFactory build()
{
return new ConfigurationFactory(properties, applicationDefaults, moduleDefaults, moduleDefaultSource, expectToUse, errors, monitor);
}
private void mergeProperties(Properties properties)
{
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy