http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java deleted file mode 100644 index 55399de..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.ConfigQuery; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the - * chain of {@link PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} - * instance to evaluate the current Configuration. - */ -public class DefaultConfiguration implements Configuration { - /** - * The logger. - */ - private static final Logger LOG = Logger.getLogger(DefaultConfiguration.class.getName()); - - /** - * The current {@link ConfigurationContext} of the current instance. - */ - private final ConfigurationContext configurationContext; - - /** - * EvaluationStrategy - */ - private ConfigValueEvaluator configEvaluator = loadConfigValueEvaluator(); - - private ConfigValueEvaluator loadConfigValueEvaluator() { - ConfigValueEvaluator eval = null; - try{ - eval = ServiceContextManager.getServiceContext() - .getService(ConfigValueEvaluator.class); - }catch(Exception e){ - LOG.log(Level.WARNING, "Failed to load ConfigValueEvaluator from ServiceContext, using default.", e); - } - if(eval==null){ - eval = new DefaultConfigValueEvaluator(); - } - return eval; - } - - - /** - * Constructor. - * @param configurationContext The configuration Context to be used. - */ - public DefaultConfiguration(ConfigurationContext configurationContext){ - this.configurationContext = Objects.requireNonNull(configurationContext); - } - - /** - * Get a given value, filtered with the context's filters as needed. - * @param key the property's key, not null. - * @return the filtered value, or null. - */ - @Override - public String get(String key) { - PropertyValue value = configEvaluator.evaluteRawValue(key, configurationContext); - if(value==null || value.getValue()==null){ - return null; - } - value = PropertyFiltering.applyFilter(value, configurationContext); - if(value!=null){ - return value.getValue(); - } - return null; - } - - /** - * Evaluates the raw value using the context's PropertyValueCombinationPolicy. - * @param key the key, not null. - * @return the value, before filtering is applied. - */ - protected PropertyValue evaluteRawValue(String key) { - List<PropertySource> propertySources = configurationContext.getPropertySources(); - PropertyValue filteredValue = null; - PropertyValueCombinationPolicy combinationPolicy = this.configurationContext - .getPropertyValueCombinationPolicy(); - for (PropertySource propertySource : propertySources) { - filteredValue = combinationPolicy.collect(filteredValue, key, propertySource); - } - return filteredValue; - } - - - @Override - public String getOrDefault(String key, String defaultValue) { - String val = get(key); - if(val==null){ - return defaultValue; - } - return val; - } - - @Override - public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { - T val = get(key, type); - if(val==null){ - return defaultValue; - } - return val; - } - - /** - * Get the current properties, composed by the loaded {@link PropertySource} and filtered - * by registered {@link org.apache.tamaya.spi.PropertyFilter}. - * - * @return the final properties. - */ - @Override - public Map<String, String> getProperties() { - Map<String, PropertyValue> filtered = PropertyFiltering.applyFilters( - configEvaluator.evaluateRawValues(configurationContext), - configurationContext); - Map<String,String> result = new HashMap<>(); - for(PropertyValue val:filtered.values()){ - if(val.getValue()!=null) { - result.put(val.getKey(), val.getValue()); - // TODO: Discuss metadata handling... - result.putAll(val.getMetaEntries()); - } - } - return result; - } - - - /** - * Accesses the current String value for the given key and tries to convert it - * using the {@link PropertyConverter} instances provided by the current - * {@link ConfigurationContext}. - * - * @param key the property's absolute, or relative path, e.g. @code - * a/b/c/d.myProperty}. - * @param type The target type required, not null. - * @param <T> the value type - * @return the converted value, never null. - */ - @Override - public <T> T get(String key, Class<T> type) { - return get(key, (TypeLiteral<T>)TypeLiteral.of(type)); - } - - /** - * Accesses the current String value for the given key and tries to convert it - * using the {@link PropertyConverter} instances provided by the current - * {@link ConfigurationContext}. - * - * @param key the property's absolute, or relative path, e.g. @code - * a/b/c/d.myProperty}. - * @param type The target type required, not null. - * @param <T> the value type - * @return the converted value, never null. - */ - @Override - public <T> T get(String key, TypeLiteral<T> type) { - return convertValue(key, get(key), type); - } - - protected <T> T convertValue(String key, String value, TypeLiteral<T> type) { - if (value != null) { - List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); - ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type) - .build(); - for (PropertyConverter<T> converter : converters) { - try { - T t = converter.convert(value, context); - if (t != null) { - return t; - } - } catch (Exception e) { - LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: " + value, e); - } - } - // if the target type is a String, we can return the value, no conversion required. - if(type.equals(TypeLiteral.of(String.class))){ - return (T)value; - } - // unsupported type, throw an exception - throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + - ", supported formats: " + context.getSupportedFormats()); - } - return null; - } - - @Override - public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { - T val = get(key, type); - if(val==null){ - return defaultValue; - } - return val; - } - - @Override - public Configuration with(ConfigOperator operator) { - return operator.operate(this); - } - - @Override - public <T> T query(ConfigQuery<T> query) { - return query.query(this); - } - - @Override - public ConfigurationContext getContext() { - return configurationContext; - } - - @Override - public String toString() { - return "Configuration{\n " + - configurationContext + - '}'; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java deleted file mode 100644 index b26d0af..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; - -import java.util.*; - -/** - * Default implementation of {@link ConfigurationContextBuilder}. - */ -public class DefaultConfigurationBuilder implements ConfigurationBuilder { - - private final ConfigurationContextBuilder contextBuilder; - - /** - * Creates a new builder instance. - */ - public DefaultConfigurationBuilder() { - this.contextBuilder = new DefaultConfigurationContextBuilder(); - } - - /** - * Creates a new builder instance initializing it with the given context. - * @param configuration the configuration to be used, not null. - */ - public DefaultConfigurationBuilder(Configuration configuration) { - this.contextBuilder = new DefaultConfigurationContextBuilder(configuration.getContext()); - } - - /** - * Allows to set configuration context during unit tests. - */ - ConfigurationBuilder setConfiguration(Configuration configuration) { - this.contextBuilder.setContext(configuration.getContext()); - return this; - } - - - @Override - public ConfigurationBuilder setContext(ConfigurationContext context) { - this.contextBuilder.setContext(context); - return this; - } - - @Override - public ConfigurationBuilder addPropertySources(PropertySource... sources){ - this.contextBuilder.addPropertySources(sources); - return this; - } - - @Override - public ConfigurationBuilder addPropertySources(Collection<PropertySource> sources){ - this.contextBuilder.addPropertySources(sources); - return this; - } - - public ConfigurationBuilder addDefaultPropertyFilters() { - this.contextBuilder.addDefaultPropertyFilters(); - return this; - } - - public ConfigurationBuilder addDefaultPropertySources() { - this.contextBuilder.addDefaultPropertySources(); - return this; - } - - public ConfigurationBuilder addDefaultPropertyConverters() { - this.contextBuilder.addDefaultPropertyConverters(); - return this; - } - - @Override - public ConfigurationBuilder removePropertySources(PropertySource... propertySources) { - this.contextBuilder.removePropertySources(propertySources); - return this; - } - - @Override - public ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources) { - this.contextBuilder.removePropertySources(propertySources); - return this; - } - - @Override - public List<PropertySource> getPropertySources() { - return this.contextBuilder.getPropertySources(); - } - - @Override - public ConfigurationBuilder increasePriority(PropertySource propertySource) { - this.contextBuilder.increasePriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder decreasePriority(PropertySource propertySource) { - this.contextBuilder.decreasePriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder highestPriority(PropertySource propertySource) { - this.contextBuilder.highestPriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder lowestPriority(PropertySource propertySource) { - this.contextBuilder.lowestPriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder addPropertyFilters(PropertyFilter... filters){ - this.contextBuilder.addPropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters){ - this.contextBuilder.addPropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyFilters(PropertyFilter... filters) { - this.contextBuilder.removePropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters) { - this.contextBuilder.removePropertyFilters(filters); - return this; - } - - - @Override - public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... converters) { - this.contextBuilder.removePropertyConverters(typeToConvert, converters); - return this; - } - - @Override - public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> converters) { - this.contextBuilder.removePropertyConverters(typeToConvert, converters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert) { - this.contextBuilder.removePropertyConverters(typeToConvert); - return this; - } - - - @Override - public ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy combinationPolicy){ - this.contextBuilder.setPropertyValueCombinationPolicy(combinationPolicy); - return this; - } - - - @Override - public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, PropertyConverter<T>... propertyConverters){ - this.contextBuilder.addPropertyConverters(type, propertyConverters); - return this; - } - - @Override - public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, Collection<PropertyConverter<T>> propertyConverters){ - this.contextBuilder.addPropertyConverters(type, propertyConverters); - return this; - } - - /** - * Builds a new configuration based on the configuration of this builder instance. - * - * @return a new {@link org.apache.tamaya.Configuration configuration instance}, - * never {@code null}. - */ - @Override - public Configuration build() { - return new DefaultConfiguration(this.contextBuilder.build()); - } - - @Override - public ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator) { - this.contextBuilder.sortPropertyFilter(comparator); - return this; - } - - @Override - public ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator) { - this.contextBuilder.sortPropertySources(comparator); - return this; - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - return this.contextBuilder.getPropertyFilters(); - } - - @Override - public Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter() { - return this.contextBuilder.getPropertyConverter(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java deleted file mode 100644 index f5c97d6..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.logging.Logger; - -/** - * Default Implementation of a simple ConfigurationContext. - */ -public class DefaultConfigurationContext implements ConfigurationContext { - /** The logger used. */ - private final static Logger LOG = Logger.getLogger(DefaultConfigurationContext.class.getName()); - /** - * Cubcomponent handling {@link PropertyConverter} instances. - */ - private final PropertyConverterManager propertyConverterManager = new PropertyConverterManager(); - - /** - * The current unmodifiable list of loaded {@link PropertySource} instances. - */ - private List<PropertySource> immutablePropertySources; - - /** - * The current unmodifiable list of loaded {@link PropertyFilter} instances. - */ - private List<PropertyFilter> immutablePropertyFilters; - - /** - * The overriding policy used when combining PropertySources registered to evalute the final configuration - * values. - */ - private PropertyValueCombinationPolicy propertyValueCombinationPolicy; - - /** - * Lock for internal synchronization. - */ - private final ReentrantReadWriteLock propertySourceLock = new ReentrantReadWriteLock(); - /** - * Lock for internal synchronization. - */ - private final ReentrantReadWriteLock propertyFilterLock = new ReentrantReadWriteLock(); - - /** - * Creates an empty Configuration context. - */ - protected DefaultConfigurationContext() { - this(new DefaultConfigurationContextBuilder()); - } - - DefaultConfigurationContext(DefaultConfigurationContextBuilder builder) { - List<PropertySource> propertySources = new ArrayList<>(); - // first we load all PropertySources which got registered via java.util.ServiceLoader - propertySources.addAll(builder.propertySources); - // now sort them according to their ordinal values - immutablePropertySources = Collections.unmodifiableList(propertySources); - LOG.info("Registered " + immutablePropertySources.size() + " property sources: " + - immutablePropertySources); - - // as next step we pick up the PropertyFilters pretty much the same way - List<PropertyFilter> propertyFilters = new ArrayList<>(builder.getPropertyFilters()); - immutablePropertyFilters = Collections.unmodifiableList(propertyFilters); - LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " + - immutablePropertyFilters); - - // Finally add the converters - for(Map.Entry<TypeLiteral<?>, Collection<PropertyConverter<?>>> en:builder.getPropertyConverter().entrySet()) { - for (PropertyConverter converter : en.getValue()) { - this.propertyConverterManager.register(en.getKey(), converter); - } - } - LOG.info("Registered " + propertyConverterManager.getPropertyConverters().size() + " property converters: " + - propertyConverterManager.getPropertyConverters()); - - propertyValueCombinationPolicy = builder.combinationPolicy; - if(propertyValueCombinationPolicy==null){ - propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class); - } - if(propertyValueCombinationPolicy==null){ - propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; - } - LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy); - } - - - @Deprecated - @Override - public void addPropertySources(PropertySource... propertySourcesToAdd) { - Lock writeLock = propertySourceLock.writeLock(); - try { - writeLock.lock(); - List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources); - newPropertySources.addAll(Arrays.asList(propertySourcesToAdd)); - Collections.sort(newPropertySources, PropertySourceComparator.getInstance()); - - this.immutablePropertySources = Collections.unmodifiableList(newPropertySources); - } finally { - writeLock.unlock(); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof DefaultConfigurationContext)){ - return false; - } - - DefaultConfigurationContext that = (DefaultConfigurationContext) o; - - if (!propertyConverterManager.equals(that.propertyConverterManager)) { - return false; - } - if (!immutablePropertySources.equals(that.immutablePropertySources)) { - return false; - } - if (!immutablePropertyFilters.equals(that.immutablePropertyFilters)) { - return false; - } - return getPropertyValueCombinationPolicy().equals(that.getPropertyValueCombinationPolicy()); - - } - - @Override - public int hashCode() { - int result = propertyConverterManager.hashCode(); - result = 31 * result + immutablePropertySources.hashCode(); - result = 31 * result + immutablePropertyFilters.hashCode(); - result = 31 * result + getPropertyValueCombinationPolicy().hashCode(); - return result; - } - - @Override - public String toString() { - StringBuilder b = new StringBuilder("ConfigurationContext{\n"); - b.append(" Property Sources\n"); - b.append(" ----------------\n"); - if(immutablePropertySources.isEmpty()){ - b.append(" No property sources loaded.\n\n"); - }else { - b.append(" CLASS NAME ORDINAL SCANNABLE SIZE STATE ERROR\n\n"); - for (PropertySource ps : immutablePropertySources) { - b.append(" "); - appendFormatted(b, ps.getClass().getSimpleName(), 30); - appendFormatted(b, ps.getName(), 70); - appendFormatted(b, String.valueOf(PropertySourceComparator.getOrdinal(ps)), 8); - appendFormatted(b, String.valueOf(ps.isScannable()), 10); - if (ps.isScannable()) { - appendFormatted(b, String.valueOf(ps.getProperties().size()), 8); - } else { - appendFormatted(b, "-", 8); - } - PropertyValue state = ps.get("_state"); - if(state==null){ - appendFormatted(b, "OK", 10); - }else { - appendFormatted(b, state.getValue(), 10); - if("ERROR".equals(state.getValue())){ - PropertyValue val = ps.get("_exception"); - if(val!=null) { - appendFormatted(b, val.getValue(), 30); - } - } - } - b.append('\n'); - } - b.append("\n"); - } - b.append(" Property Filters\n"); - b.append(" ----------------\n"); - if(immutablePropertyFilters.isEmpty()){ - b.append(" No property filters loaded.\n\n"); - }else { - b.append(" CLASS INFO\n\n"); - for (PropertyFilter filter : getPropertyFilters()) { - b.append(" "); - appendFormatted(b, filter.getClass().getSimpleName(), 30); - b.append(removeNewLines(filter.toString())); - b.append('\n'); - } - b.append("\n\n"); - } - b.append(" Property Converters\n"); - b.append(" -------------------\n"); - b.append(" CLASS TYPE INFO\n\n"); - for(Map.Entry<TypeLiteral<?>, List<PropertyConverter<?>>> converterEntry:getPropertyConverters().entrySet()){ - for(PropertyConverter converter: converterEntry.getValue()){ - b.append(" "); - appendFormatted(b, converter.getClass().getSimpleName(), 30); - appendFormatted(b, converterEntry.getKey().getRawType().getSimpleName(), 30); - b.append(removeNewLines(converter.toString())); - b.append('\n'); - } - } - b.append("\n\n"); - b.append(" PropertyValueCombinationPolicy: " + getPropertyValueCombinationPolicy().getClass().getName()).append('\n'); - b.append('}'); - return b.toString(); - } - - private void appendFormatted(StringBuilder b, String text, int length) { - int padding; - if(text.length() <= (length)){ - b.append(text); - padding = length - text.length(); - }else{ - b.append(text.substring(0, length-1)); - padding = 1; - } - for(int i=0;i<padding;i++){ - b.append(' '); - } - } - - private String removeNewLines(String s) { - return s.replace('\n', ' ').replace('\r', ' '); - } - - @Override - public List<PropertySource> getPropertySources() { - return immutablePropertySources; - } - - @Override - public PropertySource getPropertySource(String name) { - for(PropertySource ps:getPropertySources()){ - if(name.equals(ps.getName())){ - return ps; - } - } - return null; - } - - @Override - public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) { - propertyConverterManager.register(typeToConvert, propertyConverter); - LOG.info("Added PropertyConverter: " + propertyConverter.getClass().getName()); - } - - @Override - public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { - return propertyConverterManager.getPropertyConverters(); - } - - @Override - public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { - return propertyConverterManager.getPropertyConverters(targetType); - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - return immutablePropertyFilters; - } - - @Override - public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy(){ - return propertyValueCombinationPolicy; - } - - @Override - public ConfigurationContextBuilder toBuilder() { - return new DefaultConfigurationContextBuilder(this); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java deleted file mode 100644 index 023faf3..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java +++ /dev/null @@ -1,408 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Logger; - -/** - * Default implementation of {@link ConfigurationContextBuilder}. - */ -public class DefaultConfigurationContextBuilder implements ConfigurationContextBuilder { - - private static final Logger LOG = Logger.getLogger(DefaultConfigurationContextBuilder.class.getName()); - - List<PropertyFilter> propertyFilters = new ArrayList<>(); - List<PropertySource> propertySources = new ArrayList<>(); - PropertyValueCombinationPolicy combinationPolicy = - PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; - Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> propertyConverters = new HashMap<>(); - - /** - * Flag if the config has already been built. - * Configuration can be built only once - */ - private boolean built; - - - - /** - * Creates a new builder instance. - */ - public DefaultConfigurationContextBuilder() { - } - - /** - * Creates a new builder instance initializing it with the given context. - * @param context the context to be used, not null. - */ - public DefaultConfigurationContextBuilder(ConfigurationContext context) { - this.propertyConverters.putAll(context.getPropertyConverters()); - this.propertyFilters.addAll(context.getPropertyFilters()); - for(PropertySource ps:context.getPropertySources()) { - addPropertySources(ps); - } - this.combinationPolicy = context.getPropertyValueCombinationPolicy(); - } - - /** - * Allows to set configuration context during unit tests. - */ - ConfigurationContextBuilder setConfigurationContext(ConfigurationContext configurationContext) { - checkBuilderState(); - //noinspection deprecation - this.propertyFilters.clear(); - this.propertyFilters.addAll(configurationContext.getPropertyFilters()); - this.propertySources.clear(); - for(PropertySource ps:configurationContext.getPropertySources()) { - addPropertySources(ps); - } - this.propertyConverters.clear(); - this.propertyConverters.putAll(configurationContext.getPropertyConverters()); - this.combinationPolicy = configurationContext.getPropertyValueCombinationPolicy(); - return this; - } - - - @Override - public ConfigurationContextBuilder setContext(ConfigurationContext context) { - checkBuilderState(); - this.propertyConverters.putAll(context.getPropertyConverters()); - for(PropertySource ps:context.getPropertySources()){ - this.propertySources.add(ps); - } - this.propertyFilters.addAll(context.getPropertyFilters()); - this.combinationPolicy = context.getPropertyValueCombinationPolicy(); - return this; - } - - @Override - public ConfigurationContextBuilder addPropertySources(PropertySource... sources){ - return addPropertySources(Arrays.asList(sources)); - } - - @Override - public ConfigurationContextBuilder addPropertySources(Collection<PropertySource> sources){ - checkBuilderState(); - for(PropertySource source:sources) { - if (!this.propertySources.contains(source)) { - this.propertySources.add(source); - } - } - return this; - } - - public DefaultConfigurationContextBuilder addDefaultPropertyFilters() { - checkBuilderState(); - for(PropertyFilter pf:ServiceContextManager.getServiceContext().getServices(PropertyFilter.class)){ - addPropertyFilters(pf); - } - return this; - } - - public DefaultConfigurationContextBuilder addDefaultPropertySources() { - checkBuilderState(); - for(PropertySource ps:ServiceContextManager.getServiceContext().getServices(PropertySource.class)){ - addPropertySources(ps); - } - for(PropertySourceProvider pv:ServiceContextManager.getServiceContext().getServices(PropertySourceProvider.class)){ - for(PropertySource ps:pv.getPropertySources()){ - addPropertySources(ps); - } - } - return this; - } - - public DefaultConfigurationContextBuilder addDefaultPropertyConverters() { - checkBuilderState(); - for(Map.Entry<TypeLiteral, Collection<PropertyConverter>> en:getDefaultPropertyConverters().entrySet()){ - for(PropertyConverter pc: en.getValue()) { - addPropertyConverters(en.getKey(), pc); - } - } - return this; - } - - @Override - public ConfigurationContextBuilder removePropertySources(PropertySource... propertySources) { - return removePropertySources(Arrays.asList(propertySources)); - } - - @Override - public ConfigurationContextBuilder removePropertySources(Collection<PropertySource> propertySources) { - checkBuilderState(); - this.propertySources.removeAll(propertySources); - return this; - } - - private PropertySource getPropertySource(String name) { - for(PropertySource ps:propertySources){ - if(ps.getName().equals(name)){ - return ps; - } - } - throw new IllegalArgumentException("No such PropertySource: "+name); - } - - @Override - public List<PropertySource> getPropertySources() { - return this.propertySources; - } - - @Override - public ConfigurationContextBuilder increasePriority(PropertySource propertySource) { - checkBuilderState(); - int index = propertySources.indexOf(propertySource); - if(index<0){ - throw new IllegalArgumentException("No such PropertySource: " + propertySource); - } - if(index<(propertySources.size()-1)){ - propertySources.remove(propertySource); - propertySources.add(index+1, propertySource); - } - return this; - } - - @Override - public ConfigurationContextBuilder decreasePriority(PropertySource propertySource) { - checkBuilderState(); - int index = propertySources.indexOf(propertySource); - if(index<0){ - throw new IllegalArgumentException("No such PropertySource: " + propertySource); - } - if(index>0){ - propertySources.remove(propertySource); - propertySources.add(index-1, propertySource); - } - return this; - } - - @Override - public ConfigurationContextBuilder highestPriority(PropertySource propertySource) { - checkBuilderState(); - int index = propertySources.indexOf(propertySource); - if(index<0){ - throw new IllegalArgumentException("No such PropertySource: " + propertySource); - } - if(index<(propertySources.size()-1)){ - propertySources.remove(propertySource); - propertySources.add(propertySource); - } - return this; - } - - @Override - public ConfigurationContextBuilder lowestPriority(PropertySource propertySource) { - checkBuilderState(); - int index = propertySources.indexOf(propertySource); - if(index<0){ - throw new IllegalArgumentException("No such PropertySource: " + propertySource); - } - if(index>0){ - propertySources.remove(propertySource); - propertySources.add(0, propertySource); - } - return this; - } - - @Override - public ConfigurationContextBuilder addPropertyFilters(PropertyFilter... filters){ - return addPropertyFilters(Arrays.asList(filters)); - } - - @Override - public ConfigurationContextBuilder addPropertyFilters(Collection<PropertyFilter> filters){ - checkBuilderState(); - for(PropertyFilter f:filters) { - if (!this.propertyFilters.contains(f)) { - this.propertyFilters.add(f); - } - } - return this; - } - - @Override - public ConfigurationContextBuilder removePropertyFilters(PropertyFilter... filters) { - return removePropertyFilters(Arrays.asList(filters)); - } - - @Override - public ConfigurationContextBuilder removePropertyFilters(Collection<PropertyFilter> filters) { - checkBuilderState(); - this.propertyFilters.removeAll(filters); - return this; - } - - - @Override - public <T> ConfigurationContextBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... converters) { - return removePropertyConverters(typeToConvert, Arrays.asList(converters)); - } - - @Override - public <T> ConfigurationContextBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> converters) { - Collection<PropertyConverter<?>> subConverters = this.propertyConverters.get(typeToConvert); - if(subConverters!=null) { - subConverters.removeAll(converters); - } - return this; - } - - @Override - public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert) { - this.propertyConverters.remove(typeToConvert); - return this; - } - - - @Override - public ConfigurationContextBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy combinationPolicy){ - checkBuilderState(); - this.combinationPolicy = Objects.requireNonNull(combinationPolicy); - return this; - } - - - @Override - public <T> ConfigurationContextBuilder addPropertyConverters(TypeLiteral<T> type, PropertyConverter<T>... propertyConverters){ - checkBuilderState(); - Objects.requireNonNull(type); - Objects.requireNonNull(propertyConverters); - Collection<PropertyConverter<?>> converters = this.propertyConverters.get(type); - if(converters==null){ - converters = new ArrayList<>(); - this.propertyConverters.put(type, converters); - } - for(PropertyConverter<T> propertyConverter:propertyConverters) { - if (!converters.contains(propertyConverter)) { - converters.add(propertyConverter); - } else { - LOG.warning("Converter ignored, already registered: " + propertyConverter); - } - } - return this; - } - - @Override - public <T> ConfigurationContextBuilder addPropertyConverters(TypeLiteral<T> type, Collection<PropertyConverter<T>> propertyConverters){ - checkBuilderState(); - Objects.requireNonNull(type); - Objects.requireNonNull(propertyConverters); - Collection<PropertyConverter<?>> converters = this.propertyConverters.get(type); - if(converters==null){ - converters = new ArrayList<>(); - this.propertyConverters.put(type, converters); - } - for(PropertyConverter<T> propertyConverter:propertyConverters) { - if (!converters.contains(propertyConverter)) { - converters.add(propertyConverter); - } else { - LOG.warning("Converter ignored, already registered: " + propertyConverter); - } - } - return this; - } - - protected ConfigurationContextBuilder loadDefaults() { - checkBuilderState(); - this.combinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; - addDefaultPropertySources(); - addDefaultPropertyFilters(); - addDefaultPropertyConverters(); - return this; - } - - - private Map<TypeLiteral, Collection<PropertyConverter>> getDefaultPropertyConverters() { - Map<TypeLiteral, Collection<PropertyConverter>> result = new HashMap<>(); - for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices( - PropertyConverter.class)) { - TypeLiteral target = TypeLiteral.of(TypeLiteral.of(conv.getClass()).getType()); - Collection<PropertyConverter> convList = result.get(target); - if (convList == null) { - convList = new ArrayList<>(); - result.put(target, convList); - } - convList.add(conv); - } - return result; - } - - - /** - * Builds a new configuration based on the configuration of this builder instance. - * - * @return a new {@link org.apache.tamaya.Configuration configuration instance}, - * never {@code null}. - */ - @Override - public ConfigurationContext build() { - checkBuilderState(); - built = true; - return new DefaultConfigurationContext(this); - } - - @Override - public ConfigurationContextBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator) { - Collections.sort(propertyFilters, comparator); - return this; - } - - @Override - public ConfigurationContextBuilder sortPropertySources(Comparator<PropertySource> comparator) { - Collections.sort(propertySources, comparator); - return this; - } - - private void checkBuilderState() { - if (built) { - throw new IllegalStateException("Configuration has already been build."); - } - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - return propertyFilters; - } - - @Override - public Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter() { - return Collections.unmodifiableMap(this.propertyConverters); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java deleted file mode 100644 index d003ccb..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Locale; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Converter, converting from String to tge given enum type. - * @param <T> the enum type. - */ -public class EnumConverter<T> implements PropertyConverter<T> { - private final Logger LOG = Logger.getLogger(EnumConverter.class.getName()); - private Class<T> enumType; - private Method factory; - - public EnumConverter(Class<T> enumType) { - if (!Enum.class.isAssignableFrom(enumType)) { - throw new IllegalArgumentException("Not an Enum: " + enumType.getName()); - } - this.enumType = Objects.requireNonNull(enumType); - try { - this.factory = enumType.getMethod("valueOf", String.class); - } catch (NoSuchMethodException e) { - throw new ConfigException("Uncovertible enum type without valueOf method found, please provide a custom " + - "PropertyConverter for: " + enumType.getName()); - } - } - - @Override - public T convert(String value, ConversionContext context) { - try { - return (T) factory.invoke(null, value); - } catch (InvocationTargetException | IllegalAccessException e) { - LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e); - } - try { - return (T) factory.invoke(null, value.toUpperCase(Locale.ENGLISH)); - } catch (InvocationTargetException | IllegalAccessException e) { - LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java deleted file mode 100644 index 200c567..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.spi.PropertyValue; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Logger; - -/** - * This {@link org.apache.tamaya.spi.PropertySource} provides all Properties which are set - * via - * {@code export myprop=myval} on UNIX Systems or - * {@code set myprop=myval} on Windows. You can disable this feature by setting {@code tamaya.envprops.disable} - * or {@code tamaya.defaults.disable}. - */ -public class EnvironmentPropertySource extends BasePropertySource { - - private static final Logger LOG = Logger.getLogger(EnvironmentPropertySource.class.getName()); - - /** - * Default ordinal used. - */ - public static final int DEFAULT_ORDINAL = 300; - - /** - * Prefix that allows environment properties to virtually be mapped on specified sub section. - */ - private String prefix; - - /** - * If true, this property source does not return any properties. This is useful since this - * property source is applied by default, but can be switched off by setting the - * {@code tamaya.envprops.disable} system/environment property to {@code true}. - */ - private boolean disabled = false; - - - /** - * Creates a new instance. Also initializes the {@code prefix} and {@code disabled} properties - * from the system-/ environment properties: - * <pre> - * tamaya.envprops.prefix - * tamaya.envprops.disable - * </pre> - */ - public EnvironmentPropertySource(){ - initFromSystemProperties(); - } - - /** - * Initializes the {@code prefix} and {@code disabled} properties from the system-/ - * environment properties: - * <pre> - * tamaya.envprops.prefix - * tamaya.envprops.disable - * </pre> - */ - private void initFromSystemProperties() { - String value = System.getProperty("tamaya.envprops.prefix"); - if(value==null){ - prefix = System.getenv("tamaya.envprops.prefix"); - } - value = System.getProperty("tamaya.envprops.disable"); - if(value==null){ - value = System.getenv("tamaya.envprops.disable"); - } - if(value==null){ - value = System.getProperty("tamaya.defaults.disable"); - } - if(value==null){ - value = System.getenv("tamaya.defaults.disable"); - } - if(value!=null && !value.isEmpty()) { - this.disabled = Boolean.parseBoolean(value); - } - } - - /** - * Creates a new instance using a fixed ordinal value. - * @param ordinal the ordinal number. - */ - public EnvironmentPropertySource(int ordinal){ - this(null, ordinal); - } - - /** - * Creates a new instance. - * @param prefix the prefix to be used, or null. - * @param ordinal the ordinal to be used. - */ - public EnvironmentPropertySource(String prefix, int ordinal){ - super("environment-properties"); - this.prefix = prefix; - setOrdinal(ordinal); - } - - /** - * Creates a new instance. - * @param prefix the prefix to be used, or null. - */ - public EnvironmentPropertySource(String prefix){ - this.prefix = prefix; - } - - @Override - public int getDefaultOrdinal() { - return DEFAULT_ORDINAL; - } - - @Override - public String getName() { - if(disabled){ - return super.getName() + "(disabled)"; - } - return super.getName(); - } - - @Override - public PropertyValue get(String key) { - if(disabled){ - return null; - } - String prefix = this.prefix; - if(prefix==null) { - return PropertyValue.of(key, System.getenv(key), getName()); - } - return PropertyValue.of(key, System.getenv(key.substring(prefix.length())), getName()); - } - - @Override - public Map<String, PropertyValue> getProperties() { - if(disabled){ - return Collections.emptyMap(); - } - String prefix = this.prefix; - if(prefix==null) { - Map<String, PropertyValue> entries = new HashMap<>(System.getenv().size()); - for (Map.Entry<String, String> entry : System.getenv().entrySet()) { - entries.put(entry.getKey(), PropertyValue.of(entry.getKey(), entry.getValue(), getName())); - } - return entries; - }else{ - Map<String, PropertyValue> entries = new HashMap<>(System.getenv().size()); - for (Map.Entry<String, String> entry : System.getenv().entrySet()) { - entries.put(prefix + entry.getKey(), PropertyValue.of(prefix + entry.getKey(), entry.getValue(), getName())); - } - return entries; - } - } - - @Override - public boolean isScannable() { - return true; - } - - @Override - protected String toStringValues() { - return super.toStringValues() + - " prefix=" + prefix + '\n' + - " disabled=" + disabled + '\n'; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java deleted file mode 100644 index b07231e..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.spi.PropertyValue; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -/** - * Simple PropertySource implementation that just takes a Map and an (optional) priority. - * Optionally the entries passed can be mapped to a different rootContext. - */ -public class MapPropertySource extends BasePropertySource { - - /** - * The current properties. - */ - private final Map<String, PropertyValue> props = new HashMap<>(); - - /** - * Creates a new instance, hereby using the default mechanism for evaluating the property source's - * priority. - * - * @param name unique name of this source. - * @param props the properties - */ - public MapPropertySource(String name, Map<String, String> props) { - this(name, props, null); - } - - /** - * Creates a new instance, hereby using the default mechanism for evaluating the property source's - * priority, but applying a custom mapping {@code prefix} to the entries provided. - * - * @param name unique name of this source. - * @param props the properties - * @param prefix the prefix context mapping, or null (for no mapping). - */ - public MapPropertySource(String name, Map<String, String> props, String prefix) { - super(name); - if (prefix == null) { - for (Map.Entry<String, String> en : props.entrySet()) { - this.props.put(en.getKey(), - PropertyValue.of(en.getKey(), en.getValue(), name)); - } - } else { - for (Map.Entry<String, String> en : props.entrySet()) { - this.props.put(prefix + en.getKey(), - PropertyValue.of(prefix + en.getKey(), en.getValue(), name)); - } - } - } - - /** - * Creates a new instance, hereby using the default mechanism for evaluating the property source's - * priority, but applying a custom mapping {@code rootContext} to the entries provided. - * - * @param name unique name of this source. - * @param props the properties - * @param prefix the prefix context mapping, or null (for no mapping). - */ - public MapPropertySource(String name, Properties props, String prefix) { - this(name, getMap(props), prefix); - } - - /** - * Simple method to convert Properties into a Map instance. - * @param props the properties, not null. - * @return the corresponding Map instance. - */ - public static Map<String, String> getMap(Properties props) { - Map<String, String> result = new HashMap<>(); - for (Map.Entry en : props.entrySet()) { - result.put(en.getKey().toString(), en.getValue().toString()); - } - return result; - } - - - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.unmodifiableMap(this.props); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PriorityServiceComparator.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PriorityServiceComparator.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PriorityServiceComparator.java deleted file mode 100644 index 90a8387..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PriorityServiceComparator.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import javax.annotation.Priority; -import java.io.Serializable; -import java.util.Comparator; - -/** - * Comparator implementation for odering services loaded based on their increasing priority values. - */ -public class PriorityServiceComparator implements Comparator<Object>, Serializable { - - private static final long serialVersionUID = 1L; - - private static final PriorityServiceComparator INSTANCE = new PriorityServiceComparator(); - - private PriorityServiceComparator(){} - - /** - * Get the shared instance of the comparator. - * @return the shared instance, never null. - */ - public static PriorityServiceComparator getInstance(){ - return INSTANCE; - } - - @Override - public int compare(Object o1, Object o2) { - int prio = getPriority(o1) - getPriority(o2); - if (prio < 0) { - return 1; - } else if (prio > 0) { - return -1; - } else { - return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); - } - } - - /** - * Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such - * annotation is present, a default priority is returned (1); - * - * @param o the instance, not null. - * @return a priority, by default 1. - */ - public static int getPriority(Object o) { - int prio = 1; - Priority priority = o.getClass().getAnnotation(Priority.class); - if (priority != null) { - prio = priority.value(); - } - return prio; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2d0ef4b9/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java deleted file mode 100644 index 1dc7586..0000000 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.tamaya.spisupport; - -import org.apache.tamaya.spi.ServiceContextManager; - -import java.io.InputStream; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Simple PropertySource, with a fixed ordinal that reads a .properties file from a given URL. - */ -public class PropertiesResourcePropertySource extends MapPropertySource { - /** The logger used. */ - private static final Logger LOGGER = Logger.getLogger(PropertiesResourcePropertySource.class.getName()); - - /** - * Creates a new instance. - * @param url the resource URL, not null. - */ - public PropertiesResourcePropertySource(URL url){ - this(url, null); - } - - /** - * Creates a new instance. - * @param prefix the (optional) prefix context for mapping (prefixing) the properties loaded. - * @param url the resource URL, not null. - */ - public PropertiesResourcePropertySource(URL url, String prefix){ - super(url.toExternalForm(), loadProps(url), prefix); - } - - /** - * Creates a new instance. - * @param prefix the (optional) prefix context for mapping (prefixing) the properties loaded. - * @param path the resource path, not null. - */ - public PropertiesResourcePropertySource(String path, String prefix){ - super(path, loadProps(path, null), prefix); - } - - /** - * Creates a new instance. - * @param prefix the (optional) prefix context for mapping (prefixing) the properties loaded. - * @param path the resource path, not null. - */ - public PropertiesResourcePropertySource(String path, String prefix, ClassLoader cl){ - super(path, loadProps(path, cl), prefix); - } - - /** - * Loads the properties using the JDK's Property loading mechanism. - * @param path the resource classpath, not null. - * @return the loaded properties. - */ - private static Map<String, String> loadProps(String path, ClassLoader cl) { - if(cl==null){ - cl = PropertiesResourcePropertySource.class.getClassLoader(); - } - URL url = ServiceContextManager.getServiceContext().getResource(path, cl); - return loadProps(url); - } - - /** - * Loads the properties using the JDK's Property loading mechanism. - * @param url the resource URL, not null. - * @return the loaded properties. - */ - private static Map<String, String> loadProps(URL url) { - Map<String,String> result = new HashMap<>(); - if(url!=null) { - try (InputStream is = url.openStream()) { - Properties props = new Properties(); - props.load(is); - for (Map.Entry en : props.entrySet()) { - result.put(en.getKey().toString(), en.getValue().toString()); - } - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to read properties from " + url, e); - } - }else{ - LOGGER.log(Level.WARNING, "No properties found at " + url); - } - return result; - } - -}
