http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java index 9150385..48a958d 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java @@ -22,29 +22,53 @@ import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.*; import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.spisupport.propertysource.CLIPropertySource; +import org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource; +import org.apache.tamaya.spisupport.propertysource.JavaConfigurationPropertySource; +import org.apache.tamaya.spisupport.propertysource.SystemPropertySource; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.*; +import java.util.logging.Logger; /** * Default implementation of {@link ConfigurationBuilder}. */ public class DefaultConfigurationBuilder implements ConfigurationBuilder { - protected final DefaultConfigurationContextBuilder contextBuilder; + private static final Logger LOG = Logger.getLogger(DefaultConfigurationBuilder.class.getName()); + + protected ServiceContext serviceContext = ServiceContextManager.getServiceContext(); + protected List<PropertyFilter> propertyFilters = new ArrayList<>(); + protected List<PropertySource> propertySources = new ArrayList<>(); + protected PropertyValueCombinationPolicy combinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_POLICY; + protected Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> propertyConverters = new HashMap<>(); + + /** + * Flag if the config has already been built. + * Configuration can be built only once + */ + protected boolean built; /** * Creates a new builder instance. */ public DefaultConfigurationBuilder() { - this.contextBuilder = new DefaultConfigurationContextBuilder(); } + /** * Creates a new builder instance. * @param context the configuration context to be used, not null. */ public DefaultConfigurationBuilder(ConfigurationContext context) { - this.contextBuilder = new DefaultConfigurationContextBuilder(context); + this.propertyConverters.putAll(context.getPropertyConverters()); + this.propertyFilters.addAll(context.getPropertyFilters()); + for(PropertySource ps:context.getPropertySources()) { + addPropertySources(ps); + } + this.combinationPolicy = context.getPropertyValueCombinationPolicy(); } /** @@ -52,28 +76,45 @@ public class DefaultConfigurationBuilder implements ConfigurationBuilder { * @param configuration the configuration to be used, not null. */ public DefaultConfigurationBuilder(Configuration configuration) { - this.contextBuilder = new DefaultConfigurationContextBuilder(configuration.getContext()); + this(configuration.getContext()); + } + + @Override + public ConfigurationBuilder setClassLoader(ClassLoader classLoader) { + setServiceContext(ServiceContextManager.getServiceContext(classLoader)); + return this; + } + + @Override + public ConfigurationBuilder setServiceContext(ServiceContext serviceContext) { + checkBuilderState(); + this.serviceContext = Objects.requireNonNull(serviceContext); + return this; } /** - * Allows to set configuration context during unit tests. + * Allows to setCurrent configuration context during unit tests. * @param configuration the configuration to be used, not null. */ public ConfigurationBuilder setConfiguration(Configuration configuration) { - this.contextBuilder.setContext(configuration.getContext()); + 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); + checkBuilderState(); + //noinspection deprecation + this.propertyFilters.clear(); + this.propertyFilters.addAll(context.getPropertyFilters()); + this.propertySources.clear(); + for(PropertySource ps:context.getPropertySources()) { + addPropertySources(ps); + } + this.propertyConverters.clear(); + this.propertyConverters.putAll(context.getPropertyConverters()); + this.combinationPolicy = context.getPropertyValueCombinationPolicy(); return this; } @@ -85,127 +126,177 @@ public class DefaultConfigurationBuilder implements ConfigurationBuilder { */ @Override public ConfigurationBuilder addPropertySources(Collection<PropertySource> sources){ - this.contextBuilder.addPropertySources(sources); + checkBuilderState(); + for(PropertySource source:sources) { + if (!this.propertySources.contains(source)) { + this.propertySources.add(source); + } + } return this; } public ConfigurationBuilder addDefaultPropertyFilters() { - this.contextBuilder.addDefaultPropertyFilters(); + checkBuilderState(); + for(PropertyFilter pf:serviceContext.getServices(PropertyFilter.class)){ + addPropertyFilters(pf); + } return this; } public ConfigurationBuilder addDefaultPropertySources() { - this.contextBuilder.addDefaultPropertySources(); - return this; + checkBuilderState(); + List<PropertySource> propertySources = new ArrayList<>(); + addCorePropertyResources(propertySources); + for(PropertySource ps: serviceContext.getServices(PropertySource.class)) { + if(!propertySources.contains(ps)){ + propertySources.add(ps); + } + } + for(PropertySourceProvider provider: + serviceContext.getServices(PropertySourceProvider.class)){ + propertySources.addAll(provider.getPropertySources()); + } + Collections.sort(propertySources, PropertySourceComparator.getInstance()); + return addPropertySources(propertySources); } public ConfigurationBuilder addDefaultPropertyConverters() { - this.contextBuilder.addDefaultPropertyConverters(); - return this; - } - - @Override - public ConfigurationBuilder removePropertySources(PropertySource... propertySources) { - this.contextBuilder.removePropertySources(propertySources); + checkBuilderState(); + addCorePropertyConverters(); + for(Map.Entry<TypeLiteral, Collection<PropertyConverter>> en:getDefaultPropertyConverters().entrySet()){ + for(PropertyConverter pc: en.getValue()) { + addPropertyConverters(en.getKey(), pc); + } + } return this; } @Override public ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources) { - this.contextBuilder.removePropertySources(propertySources); + checkBuilderState(); + this.propertySources.removeAll(propertySources); return this; } @Override public List<PropertySource> getPropertySources() { - return this.contextBuilder.getPropertySources(); + return Collections.unmodifiableList(this.propertySources); } @Override public ConfigurationBuilder increasePriority(PropertySource propertySource) { - this.contextBuilder.increasePriority(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 ConfigurationBuilder decreasePriority(PropertySource propertySource) { - this.contextBuilder.decreasePriority(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 ConfigurationBuilder highestPriority(PropertySource propertySource) { - this.contextBuilder.highestPriority(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 ConfigurationBuilder lowestPriority(PropertySource propertySource) { - this.contextBuilder.lowestPriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder addPropertyFilters(PropertyFilter... filters){ - this.contextBuilder.addPropertyFilters(filters); + 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 ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters){ - this.contextBuilder.addPropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyFilters(PropertyFilter... filters) { - this.contextBuilder.removePropertyFilters(filters); + checkBuilderState(); + for(PropertyFilter f:filters) { + if (!this.propertyFilters.contains(f)) { + this.propertyFilters.add(f); + } + } 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); + checkBuilderState(); + this.propertyFilters.removeAll(filters); return this; } @Override public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, Collection<PropertyConverter<T>> converters) { - this.contextBuilder.removePropertyConverters(typeToConvert, converters); + Collection<PropertyConverter<?>> subConverters = this.propertyConverters.get(typeToConvert); + if(subConverters!=null) { + subConverters.removeAll(converters); + } return this; } @Override public ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert) { - this.contextBuilder.removePropertyConverters(typeToConvert); + this.propertyConverters.remove(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); + checkBuilderState(); + this.combinationPolicy = Objects.requireNonNull(combinationPolicy); return this; } @Override public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, Collection<PropertyConverter<T>> propertyConverters){ - this.contextBuilder.addPropertyConverters(type, 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; } @@ -217,28 +308,102 @@ public class DefaultConfigurationBuilder implements ConfigurationBuilder { */ @Override public Configuration build() { - return new DefaultConfiguration(this.contextBuilder.build()); + Configuration cfg = new DefaultConfiguration( + new DefaultConfigurationContext( + serviceContext, + this.combinationPolicy, + this.propertyFilters, + this.propertySources, + this.propertyConverters)); + this.built = true; + return cfg; } @Override public ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator) { - this.contextBuilder.sortPropertyFilter(comparator); + Collections.sort(propertyFilters, comparator); return this; } @Override public ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator) { - this.contextBuilder.sortPropertySources(comparator); + Collections.sort(propertySources, comparator); return this; } @Override public List<PropertyFilter> getPropertyFilters() { - return this.contextBuilder.getPropertyFilters(); + return this.propertyFilters; } @Override public Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter() { - return this.contextBuilder.getPropertyConverter(); + return this.propertyConverters; + } + + protected ConfigurationBuilder loadDefaults() { + checkBuilderState(); + this.combinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; + addDefaultPropertySources(); + addDefaultPropertyFilters(); + addDefaultPropertyConverters(); + return this; + } + + protected Map<TypeLiteral, Collection<PropertyConverter>> getDefaultPropertyConverters() { + Map<TypeLiteral, Collection<PropertyConverter>> result = new HashMap<>(); + for (PropertyConverter conv : serviceContext.getServices( + PropertyConverter.class)) { + for(Type type:conv.getClass().getGenericInterfaces()){ + if(type instanceof ParameterizedType){ + ParameterizedType pt = (ParameterizedType)type; + if(PropertyConverter.class.equals(((ParameterizedType) type).getRawType())){ + TypeLiteral target = TypeLiteral.of(pt.getActualTypeArguments()[0]); + Collection<PropertyConverter> convList = result.get(target); + if (convList == null) { + convList = new ArrayList<>(); + result.put(target, convList); + } + convList.add(conv); + } + } + } + } + return result; + } + + protected void addCorePropertyResources(List<PropertySource> propertySources) { + JavaConfigurationPropertySource jps = new JavaConfigurationPropertySource(); + jps.init(serviceContext.getClassLoader()); + for(PropertySource ps: new PropertySource[]{ + new EnvironmentPropertySource(), + jps, + new CLIPropertySource(), + new SystemPropertySource() + }){ + if(!propertySources.contains(ps)){ + propertySources.add(ps); + } + } + } + + @SuppressWarnings("unchecked") + protected void addCorePropertyConverters() { + // should be overridden by subclasses. + } + + protected PropertySource getPropertySource(String name) { + for(PropertySource ps:propertySources){ + if(ps.getName().equals(name)){ + return ps; + } + } + throw new IllegalArgumentException("No such PropertySource: "+name); + } + + private void checkBuilderState() { + if (built) { + throw new IllegalStateException("Configuration has already been build."); + } } }
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java index 5553f09..878d64e 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java @@ -37,7 +37,7 @@ public class DefaultConfigurationContext implements ConfigurationContext { /** * Subcomponent handling {@link PropertyConverter} instances. */ - private final PropertyConverterManager propertyConverterManager = new PropertyConverterManager(); + private PropertyConverterManager propertyConverterManager; /** * The current unmodifiable list of loaded {@link PropertySource} instances. @@ -55,6 +55,9 @@ public class DefaultConfigurationContext implements ConfigurationContext { */ private PropertyValueCombinationPolicy propertyValueCombinationPolicy; + /** The corresponding classLoader for this instance. */ + private ServiceContext serviceContext; + /** * Lock for internal synchronization. */ @@ -62,6 +65,8 @@ public class DefaultConfigurationContext implements ConfigurationContext { @SuppressWarnings("unchecked") protected DefaultConfigurationContext(DefaultConfigurationContextBuilder builder) { + this.serviceContext = builder.getServiceContext(); + propertyConverterManager = new PropertyConverterManager(serviceContext); List<PropertySource> propertySources = new ArrayList<>(); // first we load all PropertySources which got registered via java.util.ServiceLoader propertySources.addAll(builder.propertySources); @@ -81,16 +86,36 @@ public class DefaultConfigurationContext implements ConfigurationContext { LOG.info("Registered " + propertyConverterManager.getPropertyConverters().size() + " property converters: " + propertyConverterManager.getPropertyConverters()); - propertyValueCombinationPolicy = builder.combinationPolicy; - if(propertyValueCombinationPolicy==null){ - propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class); + this.propertyValueCombinationPolicy = builder.combinationPolicy; + if(this.propertyValueCombinationPolicy==null){ + this.propertyValueCombinationPolicy = getServiceContext().getService(PropertyValueCombinationPolicy.class); } - if(propertyValueCombinationPolicy==null){ - propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; + if(this.propertyValueCombinationPolicy==null){ + this.propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; } - LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy); + LOG.info("Using PropertyValueCombinationPolicy: " + this.propertyValueCombinationPolicy); } + public DefaultConfigurationContext(ServiceContext serviceContext, PropertyValueCombinationPolicy combinationPolicy, + List<PropertyFilter> propertyFilters, List<PropertySource> propertySources, + Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> propertyConverters) { + this.propertyValueCombinationPolicy = Objects.requireNonNull(combinationPolicy); + this.serviceContext = Objects.requireNonNull(serviceContext); + this.immutablePropertyFilters = Collections.unmodifiableList(new ArrayList<>(propertyFilters)); + this.immutablePropertySources = Collections.unmodifiableList(new ArrayList<>(propertySources)); + propertyConverterManager = new PropertyConverterManager(serviceContext); + for(Map.Entry<TypeLiteral<?>, Collection<PropertyConverter<?>>> en:propertyConverters.entrySet()) { + for (@SuppressWarnings("rawtypes") PropertyConverter converter : en.getValue()) { + this.propertyConverterManager.register(en.getKey(), converter); + } + } + } + + + @Override + public ServiceContext getServiceContext() { + return serviceContext; + } @Deprecated @Override @@ -162,7 +187,7 @@ public class DefaultConfigurationContext implements ConfigurationContext { appendFormatted(b, "-", 8); } PropertyValue state = ps.get("_state"); - if(state==null){ + if(state==null || state.getValue()==null){ appendFormatted(b, "OK", 10); }else { appendFormatted(b, state.getValue(), 10); @@ -211,6 +236,10 @@ public class DefaultConfigurationContext implements ConfigurationContext { private void appendFormatted(StringBuilder b, String text, int length) { int padding; + if(text==null){ + b.append("<null>"); + return; + } if(text.length() <= (length)){ b.append(text); padding = length - text.length(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java index 80a434d..d9fb558 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilder.java @@ -39,10 +39,12 @@ import java.util.logging.Logger; /** * Default implementation of {@link ConfigurationContextBuilder}. */ +@Deprecated public class DefaultConfigurationContextBuilder implements ConfigurationContextBuilder { private static final Logger LOG = Logger.getLogger(DefaultConfigurationContextBuilder.class.getName()); + protected ServiceContext serviceContext = ServiceContextManager.getServiceContext(); protected List<PropertyFilter> propertyFilters = new ArrayList<>(); protected List<PropertySource> propertySources = new ArrayList<>(); protected PropertyValueCombinationPolicy combinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_POLICY; @@ -95,6 +97,19 @@ public class DefaultConfigurationContextBuilder implements ConfigurationContextB return this; } + @Override + public ConfigurationContextBuilder setServiceContext(ServiceContext serviceContext) { + checkBuilderState(); + this.serviceContext = Objects.requireNonNull(serviceContext); + return this; + } + + @Override + public ConfigurationContextBuilder setClassLoader(ClassLoader classLoader) { + checkBuilderState(); + this.serviceContext = Objects.requireNonNull(serviceContext); + return this; + } @Override public ConfigurationContextBuilder setContext(ConfigurationContext context) { @@ -129,13 +144,13 @@ public class DefaultConfigurationContextBuilder implements ConfigurationContextB checkBuilderState(); List<PropertySource> propertySources = new ArrayList<>(); addCorePropertyResources(propertySources); - for(PropertySource ps: ServiceContextManager.getServiceContext().getServices(PropertySource.class)) { + for(PropertySource ps: serviceContext.getServices(PropertySource.class)) { if(!propertySources.contains(ps)){ propertySources.add(ps); } } for(PropertySourceProvider provider: - ServiceContextManager.getServiceContext().getServices(PropertySourceProvider.class)){ + serviceContext.getServices(PropertySourceProvider.class)){ propertySources.addAll(provider.getPropertySources()); } Collections.sort(propertySources, PropertySourceComparator.getInstance()); @@ -143,9 +158,11 @@ public class DefaultConfigurationContextBuilder implements ConfigurationContextB } protected void addCorePropertyResources(List<PropertySource> propertySources) { + JavaConfigurationPropertySource jps = new JavaConfigurationPropertySource(); + jps.init(getServiceContext().getClassLoader()); for(PropertySource ps: new PropertySource[]{ new EnvironmentPropertySource(), - new JavaConfigurationPropertySource(), + jps, new CLIPropertySource(), new SystemPropertySource() }){ @@ -158,7 +175,7 @@ public class DefaultConfigurationContextBuilder implements ConfigurationContextB @Override public ConfigurationContextBuilder addDefaultPropertyFilters() { checkBuilderState(); - for(PropertyFilter pf:ServiceContextManager.getServiceContext().getServices(PropertyFilter.class)){ + for(PropertyFilter pf:serviceContext.getServices(PropertyFilter.class)){ addPropertyFilters(pf); } return this; @@ -375,7 +392,7 @@ public class DefaultConfigurationContextBuilder implements ConfigurationContextB protected Map<TypeLiteral, Collection<PropertyConverter>> getDefaultPropertyConverters() { Map<TypeLiteral, Collection<PropertyConverter>> result = new HashMap<>(); - for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices( + for (PropertyConverter conv : serviceContext.getServices( PropertyConverter.class)) { for(Type type:conv.getClass().getGenericInterfaces()){ if(type instanceof ParameterizedType){ @@ -395,6 +412,11 @@ public class DefaultConfigurationContextBuilder implements ConfigurationContextB return result; } + @Override + public ServiceContext getServiceContext() { + return serviceContext; + } + /** * Builds a new configuration based on the configuration of this builder instance. http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultServiceContext.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultServiceContext.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultServiceContext.java index 61819f9..f870c46 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultServiceContext.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultServiceContext.java @@ -19,11 +19,10 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.ClassloaderAware; import org.apache.tamaya.spi.ServiceContext; import javax.annotation.Priority; -import java.io.IOException; -import java.net.URL; import java.text.MessageFormat; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -36,6 +35,8 @@ import java.util.logging.Logger; */ public final class DefaultServiceContext implements ServiceContext { private static final Logger LOG = Logger.getLogger(DefaultServiceContext.class.getName()); + + private ClassLoader classLoader; /** * List current services loaded, per class. */ @@ -80,59 +81,6 @@ public final class DefaultServiceContext implements ServiceContext { } /** - * Loads and registers services. - * - * @param <T> the concrete type. - * @param serviceType The service type. - * @return the items found, never {@code null}. - */ - @Override - public <T> List<T> getServices(final Class<T> serviceType) { - @SuppressWarnings("unchecked") - List<T> found = (List<T>) servicesLoaded.get(serviceType); - if (found != null) { - return found; - } - List<T> services = new ArrayList<>(); - try { - for (T t : ServiceLoader.load(serviceType)) { - services.add(t); - } - if(services.isEmpty()) { - for (T t : ServiceLoader.load(serviceType, serviceType.getClassLoader())) { - services.add(t); - } - } - Collections.sort(services, PriorityServiceComparator.getInstance()); - services = Collections.unmodifiableList(services); - } catch (ServiceConfigurationError e) { - LOG.log(Level.WARNING, - "Error loading services current type " + serviceType, e); - if(services==null){ - services = Collections.emptyList(); - } - } - @SuppressWarnings("unchecked") - final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType, (List<Object>) services)); - return previousServices != null ? previousServices : services; - } - - /** - * Checks the given instance for a @Priority annotation. If present the annotation's value is evaluated. If no such - * annotation is present, a default priority of {@code 1} is returned. - * @param o the instance, not {@code null}. - * @return a priority, by default 1. - */ - public static int getPriority(Object o){ - int prio = 1; //X TODO discuss default priority - Priority priority = o.getClass().getAnnotation(Priority.class); - if (priority != null) { - prio = priority.value(); - } - return prio; - } - - /** * @param services to scan * @param <T> type of the service * @@ -153,7 +101,7 @@ public final class DefaultServiceContext implements ServiceContext { int highestPriorityServiceCount = 0; for (T service : services) { - int prio = getPriority(service); + int prio = ServiceContext.getPriority(service); if (highestPriority == null || highestPriority < prio) { highestService = service; highestPriorityServiceCount = 1; @@ -165,40 +113,73 @@ public final class DefaultServiceContext implements ServiceContext { if (highestPriorityServiceCount > 1) { throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", - highestPriorityServiceCount, - serviceType.getName(), - highestPriority, - services)); + highestPriorityServiceCount, + serviceType.getName(), + highestPriority, + services)); } this.factoryTypes.put(serviceType, highestService.getClass()); return highestService; } + /** + * Loads and registers services. + * + * @param <T> the concrete type. + * @param serviceType The service type. + * @return the items found, never {@code null}. + */ @Override - public int ordinal() { - return 1; + public <T> List<T> getServices(final Class<T> serviceType) { + @SuppressWarnings("unchecked") + List<T> found = (List<T>) servicesLoaded.get(serviceType); + if (found != null) { + return found; + } + List<T> services = new ArrayList<>(); + try { + for (T t : ServiceLoader.load(serviceType, classLoader)) { + if(t instanceof ClassloaderAware){ + ((ClassloaderAware)t).init(classLoader); + } + services.add(t); + } + // TODO does this make sense here...? +// if(services.isEmpty()) { +// for (T t : ServiceLoader.load(serviceType, serviceType.getClassLoader())) { +// if(t instanceof ClassloaderAware){ +// ((ClassloaderAware)t).init(classLoader); +// } +// services.add(t); +// } +// } + Collections.sort(services, PriorityServiceComparator.getInstance()); + services = Collections.unmodifiableList(services); + } catch (ServiceConfigurationError e) { + LOG.log(Level.WARNING, + "Error loading services current type " + serviceType, e); + if(services==null){ + services = Collections.emptyList(); + } + } + @SuppressWarnings("unchecked") + final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType, (List<Object>) services)); + return previousServices != null ? previousServices : services; } + @Override - public Enumeration<URL> getResources(String resource, ClassLoader cl) throws IOException { - if(cl==null){ - cl = Thread.currentThread().getContextClassLoader(); - } - if(cl==null){ - cl = getClass().getClassLoader(); - } - return cl.getResources(resource); + public ClassLoader getClassLoader() { + return classLoader; } @Override - public URL getResource(String resource, ClassLoader cl) { - if(cl==null){ - cl = Thread.currentThread().getContextClassLoader(); - } - if(cl==null){ - cl = getClass().getClassLoader(); + public void init(ClassLoader classLoader) { + if(this.classLoader==null){ + this.classLoader = Objects.requireNonNull(classLoader); + }else{ + throw new IllegalStateException("Classloader already setCurrent on this context."); } - return cl.getResource(resource); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java index af22d8c..029c93d 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/EnumConverter.java @@ -21,7 +21,6 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.ConfigException; import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; -import org.osgi.service.component.annotations.Component; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -52,8 +51,9 @@ public class EnumConverter<T> implements PropertyConverter<T> { } @Override - public T convert(String value, ConversionContext context) { - context.addSupportedFormats(getClass(),"<enumValue>"); + public T convert(String value) { + ConversionContext.doOptional(ctx -> + ctx.addSupportedFormats(getClass(),"<enumValue>")); try { return (T) factory.invoke(null, value); } catch (InvocationTargetException | IllegalAccessException e) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyConverterManager.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyConverterManager.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyConverterManager.java index 7f51b23..a672fe7 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyConverterManager.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyConverterManager.java @@ -22,7 +22,7 @@ import org.apache.tamaya.ConfigException; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.ServiceContextManager; +import org.apache.tamaya.spi.ServiceContext; import java.lang.reflect.Constructor; import java.lang.reflect.Method; @@ -37,9 +37,10 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; /** - * Manager that deals with {@link PropertyConverter} instances. + * Manager that deals with {@link org.apache.tamaya.spi.PropertyConverter} instances. * This class is thread-safe. */ public class PropertyConverterManager { @@ -75,14 +76,23 @@ public class PropertyConverterManager { } }; + private final ServiceContext serviceContext; + /** - * Constructor. + * Creates a new instance. + * @param serviceContext the Service context, not null. */ - public PropertyConverterManager() { - this(false); + public PropertyConverterManager(ServiceContext serviceContext) { + this(serviceContext, false); } - public PropertyConverterManager(boolean init) { + /** + * Creates a new instance. + * @param serviceContext the Service context, not null. + * @param init if true, the converters are loaded eagerly. + */ + public PropertyConverterManager(ServiceContext serviceContext, boolean init) { + this.serviceContext = Objects.requireNonNull(serviceContext); if (init) { initConverters(); } @@ -93,12 +103,13 @@ public class PropertyConverterManager { */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected void initConverters() { - for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices(PropertyConverter.class)) { + for (PropertyConverter conv : serviceContext.getServices(PropertyConverter.class)) { Type type = TypeLiteral.getGenericInterfaceTypeParameters(conv.getClass(), PropertyConverter.class)[0]; register(TypeLiteral.of(type), conv); } } + /** * Registers a new converters instance. * @@ -192,6 +203,7 @@ public class PropertyConverterManager { } } + /** * Get the list of all current registered converters for the given target type. * @@ -367,7 +379,7 @@ public class PropertyConverterManager { } converter = new PropertyConverter<T>() { @Override - public T convert(String value, ConversionContext context) { + public T convert(String value) { AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { @@ -431,6 +443,8 @@ public class PropertyConverterManager { return converters.hashCode(); } + + /** * Default converter implementation performing several lookups for String conversion * options. @@ -447,9 +461,11 @@ public class PropertyConverterManager { } @Override - public T convert(String value, ConversionContext context) { - context.addSupportedFormats(getClass(), "<String -> "+factoryMethod.toGenericString()); - + public T convert(String value) { + ConversionContext ctx = ConversionContext.current(); + if(ctx!=null) { + ctx.addSupportedFormats(getClass(), "<String -> " + factoryMethod.toGenericString()); + } if (!Modifier.isStatic(factoryMethod.getModifiers())) { throw new ConfigException(factoryMethod.toGenericString() + " is not a static method. Only static " + @@ -470,4 +486,5 @@ public class PropertyConverterManager { } } } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java index 79463b3..c910a0d 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java @@ -18,14 +18,10 @@ */ package org.apache.tamaya.spisupport; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.FilterContext; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertyValue; +import org.apache.tamaya.spi.*; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.*; +import java.util.logging.Filter; import java.util.logging.Level; import java.util.logging.Logger; @@ -57,7 +53,30 @@ public final class PropertyFiltering{ */ public static PropertyValue applyFilter(PropertyValue value, ConfigurationContext context) { FilterContext filterContext = new FilterContext(value, context); - return filterValue(filterContext); + return filterValue(value, filterContext); + } + + /** + * Filters a single value. + * @param values the full values, not {@code null}. + * @param context the context + * @return the filtered value, including {@code null}. + */ + public static List<PropertyValue> applyFilters(List<PropertyValue> values, ConfigurationContext context) { + List<PropertyValue> result = new ArrayList<>(); + FilterContext filterContext = new FilterContext(values, context); + try { + FilterContext.set(filterContext); + for (PropertyValue val : values) { + PropertyValue filtered = filterValue(val, filterContext); + if(filtered!=null) { + result.add(filtered); + } + } + }finally { + FilterContext.reset(); + } + return result; } /** @@ -71,9 +90,14 @@ public final class PropertyFiltering{ // Apply filters to values, prevent values filtered to null! for (Map.Entry<String, PropertyValue> entry : rawProperties.entrySet()) { FilterContext filterContext = new FilterContext(entry.getValue(), rawProperties, context); - PropertyValue filtered = filterValue(filterContext); - if(filtered!=null){ - result.put(filtered.getKey(), filtered); + try{ + FilterContext.set(filterContext); + PropertyValue filtered = filterValue(filterContext.getProperty(), filterContext); + if(filtered!=null){ + result.put(filtered.getKey(), filtered); + } + }finally{ + FilterContext.reset(); } } return result; @@ -84,39 +108,44 @@ public final class PropertyFiltering{ * @param context the filter context, not {@code null}. * @return the filtered value. */ - private static PropertyValue filterValue(FilterContext context) { - PropertyValue inputValue = context.getProperty(); + private static PropertyValue filterValue(PropertyValue inputValue, FilterContext context) { PropertyValue filteredValue = inputValue; - for (int i = 0; i < MAX_FILTER_LOOPS; i++) { - int changes = 0; - for (PropertyFilter filter : context.getContext().getPropertyFilters()) { - filteredValue = filter.filterProperty(inputValue, context); - if (filteredValue != null && !filteredValue.equals(inputValue)) { - changes++; - LOG.finest("Filter - " + inputValue + " -> " + filteredValue + " by " + filter); + try { + FilterContext.set(context); + for (int i = 0; i < MAX_FILTER_LOOPS; i++) { + int changes = 0; + for (PropertyFilter filter : context.current().getPropertyFilters()) { + String value = filteredValue!=null?filteredValue.getValue():null; + filteredValue = filter.filterProperty(filteredValue); + String newValue = filteredValue!=null?filteredValue.getValue():null; + + if (!Objects.equals(value, newValue)) { + changes++; + LOG.finest("Filter - " + filteredValue + " by " + filter); + } + if (filteredValue == null) { + LOG.finest("Filter removed entry - " + inputValue + ": " + filter); + break; + } } - if(filteredValue==null){ - LOG.finest("Filter removed entry - " + inputValue + ": " + filter); + if (changes == 0) { + LOG.finest("Finishing filter loop, no changes detected."); + break; + } else if (filteredValue == null) { break; - }else{ - inputValue = filteredValue; - } - } - if (changes == 0) { - LOG.finest("Finishing filter loop, no changes detected."); - break; - } else if (filteredValue == null) { - break; - } else { - if (i == (MAX_FILTER_LOOPS - 1)) { - if (LOG.isLoggable(Level.WARNING)) { - LOG.warning("Maximal filter loop count reached, aborting filter evaluation after cycles: " + i); - } } else { - LOG.finest("Repeating filter loop, changes detected: " + changes); + if (i == (MAX_FILTER_LOOPS - 1)) { + if (LOG.isLoggable(Level.WARNING)) { + LOG.warning("Maximal filter loop count reached, aborting filter evaluation after cycles: " + i); + } + } else { + LOG.finest("Repeating filter loop, changes detected: " + changes); + } } } + }finally{ + FilterContext.reset(); } return filteredValue; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java index 1f8cce9..39b6cf2 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.spisupport; -import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; import org.apache.tamaya.spi.PropertyValue; @@ -29,7 +28,7 @@ import java.util.List; * Predicate filtering using a regex expression operating on the key. It allows either * to define the target keys to be selected (includes), or to be excluded (excludes). */ -public final class RegexPropertyFilter implements PropertyFilter{ +public final class RegexPropertyFilter implements PropertyFilter { /** The expression used to include entries that match. */ private List<String> includes; /** The expression used to exclude entries that match. */ @@ -54,10 +53,10 @@ public final class RegexPropertyFilter implements PropertyFilter{ } @Override - public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { + public PropertyValue filterProperty(PropertyValue valueToBeFiltered) { if(includes!=null){ for(String expression:includes){ - if(context.getProperty().getKey().matches(expression)){ + if(valueToBeFiltered.getQualifiedKey().matches(expression)){ return valueToBeFiltered; } } @@ -65,7 +64,7 @@ public final class RegexPropertyFilter implements PropertyFilter{ } if(excludes!=null){ for(String expression:excludes){ - if(context.getProperty().getKey().matches(expression)){ + if(valueToBeFiltered.getQualifiedKey().matches(expression)){ return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/BasePropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/BasePropertySource.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/BasePropertySource.java index e8c35db..c026a11 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/BasePropertySource.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/BasePropertySource.java @@ -33,7 +33,7 @@ import org.apache.tamaya.spi.PropertyValue; public abstract class BasePropertySource implements PropertySource{ /** default ordinal that will be used, if no ordinal is provided with the config. */ private int defaultOrdinal; - /** Used if the ordinal has been set explicitly. */ + /** Used if the ordinal has been setCurrent explicitly. */ private volatile Integer ordinal; /** The name of the property source. */ private String name; @@ -125,7 +125,7 @@ public abstract class BasePropertySource implements PropertySource{ } /** - * Returns the default ordinal used, when no ordinal is set, or the ordinal was not parseable to an int value. + * Returns the default ordinal used, when no ordinal is setCurrent, or the ordinal was not parseable to an int value. * @return the default ordinal used, by default 0. */ public int getDefaultOrdinal(){ http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySource.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySource.java index 570f5d4..df16b76 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySource.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySource.java @@ -26,8 +26,8 @@ import java.util.Map; /** * <p>{@link org.apache.tamaya.spi.PropertySource} to access environment variables via Tamaya - * which are set via {@code export VARIABLE=value} on UNIX systems or - * {@code set VARIABLE=value} on Windows systems.</p> + * which are setCurrent via {@code export VARIABLE=value} on UNIX systems or + * {@code setCurrent VARIABLE=value} on Windows systems.</p> * * <p>Using the {@linkplain EnvironmentPropertySource} without any * additional configuration gives access to all existing environment @@ -46,8 +46,8 @@ import java.util.Map; * * <pre> * PropertySource ps = new EnvironmentPropertySource(); - * PropertyValue opsMode = ps.get("OPS_MODE"); - * PropertyValue color = ps.get("COLOR"); + * PropertyValue opsMode = ps.current("OPS_MODE"); + * PropertyValue color = ps.current("COLOR"); * </pre> * * <h1>Application specific environment variables with prefix</h1> @@ -76,7 +76,7 @@ import java.util.Map; * * <pre> * PropertySource ps = new EnvironmentPropertySource(); - * PropertyValue pv = ps.get("CUSTOMER"); + * PropertyValue pv = ps.current("CUSTOMER"); * System.out.println(pv.getValue()); * </pre> * @@ -197,7 +197,7 @@ public class EnvironmentPropertySource extends BasePropertySource { if (isDisabled()) { return null; } - // Exact match (i.e. com.ACME.size) + // Exact match (i.e. com.ACME.getNumChilds) String effectiveKey = hasPrefix() ? getPrefix() + "." + key : key; String value = getPropertiesProvider().getenv(effectiveKey); @@ -210,6 +210,9 @@ public class EnvironmentPropertySource extends BasePropertySource { value = getPropertiesProvider().getenv(effectiveKey.replaceAll("\\.", "_") .toUpperCase()); } + if(value==null){ + return null; + } return PropertyValue.of(key, value, getName()); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationPropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationPropertySource.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationPropertySource.java index 92f520e..b0348f6 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationPropertySource.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationPropertySource.java @@ -19,6 +19,7 @@ package org.apache.tamaya.spisupport.propertysource; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.ClassloaderAware; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; import org.apache.tamaya.spi.ServiceContextManager; @@ -29,7 +30,6 @@ import java.net.URL; import java.util.*; import static java.lang.String.format; -import static java.lang.Thread.currentThread; /** * Provider which reads all {@value DEFAULT_SIMPLE_PROPERTIES_FILE_NAME} and @@ -38,7 +38,7 @@ import static java.lang.Thread.currentThread; * {@code tamaya.defaultprops.disable} or {@code tamaya.defaults.disable} * as system or environment property this feature can be disabled. */ -public class JavaConfigurationPropertySource extends BasePropertySource { +public class JavaConfigurationPropertySource extends BasePropertySource implements ClassloaderAware { /** * Default location in the classpath, where Tamaya looks for simple line based configuration by default. */ @@ -53,6 +53,11 @@ public class JavaConfigurationPropertySource extends BasePropertySource { private boolean enabled = evaluateEnabled(); + private ClassLoader classLoader; + + private List<PropertySource> propertySources = new ArrayList<>(); + + public JavaConfigurationPropertySource(){ super("resource:META-INF/javaconfiguration.*", DEFAULT_ORDINAL); } @@ -75,19 +80,15 @@ public class JavaConfigurationPropertySource extends BasePropertySource { } private List<PropertySource> getPropertySources() { - List<PropertySource> propertySources = new ArrayList<>(); - propertySources.addAll(loadPropertySourcesByName(DEFAULT_SIMPLE_PROPERTIES_FILE_NAME)); - propertySources.addAll(loadPropertySourcesByName(DEFAULT_XML_PROPERTIES_FILE_NAME)); - Collections.sort(propertySources, PropertySourceComparator.getInstance()); - return propertySources; + return this.propertySources; } - private Collection<? extends PropertySource> loadPropertySourcesByName(String filename) { + private Collection<? extends PropertySource> loadPropertySourcesByName(String filename, ClassLoader classLoader) { List<PropertySource> propertySources = new ArrayList<>(); Enumeration<URL> propertyLocations; try { - propertyLocations = ServiceContextManager.getServiceContext() - .getResources(filename, currentThread().getContextClassLoader()); + propertyLocations = ServiceContextManager.getServiceContext(classLoader) + .getResources(filename); } catch (IOException e) { String msg = format("Error while searching for %s", filename); @@ -131,4 +132,17 @@ public class JavaConfigurationPropertySource extends BasePropertySource { "enabled=" + enabled + '}'; } + + @Override + public void init(ClassLoader classLoader) { + this.classLoader = Objects.requireNonNull(classLoader); + propertySources.addAll(loadPropertySourcesByName(DEFAULT_SIMPLE_PROPERTIES_FILE_NAME, classLoader)); + propertySources.addAll(loadPropertySourcesByName(DEFAULT_XML_PROPERTIES_FILE_NAME, classLoader)); + Collections.sort(propertySources, PropertySourceComparator.getInstance()); + } + + @Override + public ClassLoader getClassLoader() { + return classLoader; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySource.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySource.java index ae800da..e9f70cf 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySource.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySource.java @@ -58,7 +58,7 @@ public class PropertiesResourcePropertySource extends MapPropertySource { * @param path the resource path, not null. */ public PropertiesResourcePropertySource(String path, String prefix){ - super(path, loadProps(path, null), prefix); + super(path, loadProps(path, Thread.currentThread().getContextClassLoader()), prefix); } /** @@ -77,10 +77,7 @@ public class PropertiesResourcePropertySource extends MapPropertySource { * @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); + URL url = ServiceContextManager.getServiceContext(cl).getResource(path); return loadProps(url); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySource.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySource.java index 3eb9d58..335ed23 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySource.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySource.java @@ -185,7 +185,7 @@ public class SimplePropertySource extends BasePropertySource { * Sets the {@code name} and returns a reference to this Builder so that the methods * can be chained together. * - * @param name the {@code name} to set, not null. + * @param name the {@code name} to setCurrent, not null. * @return a reference to this Builder */ public Builder withName(String name) { @@ -197,7 +197,7 @@ public class SimplePropertySource extends BasePropertySource { * Sets the {@code ordinal} and returns a reference to this Builder so that the methods * can be chained together. * - * @param val the {@code ordinal} to set + * @param val the {@code ordinal} to setCurrent * @return a reference to this Builder */ public Builder withOrdinal(int val) { @@ -209,7 +209,7 @@ public class SimplePropertySource extends BasePropertySource { * Sets the {@code defaultOrdinal} and returns a reference to this Builder so that the methods * can be chained together. * - * @param val the {@code defaultOrdinal} to set + * @param val the {@code defaultOrdinal} to setCurrent * @return a reference to this Builder */ public Builder withDefaultOrdinal(int val) { @@ -248,7 +248,7 @@ public class SimplePropertySource extends BasePropertySource { /** * Sets the {@code properties} and returns a reference to this Builder so that the methods can be chained together. * - * @param val the {@code properties} to set + * @param val the {@code properties} to setCurrent * @return a reference to this Builder */ public Builder withProperties(Map<String, String> val) { @@ -261,8 +261,8 @@ public class SimplePropertySource extends BasePropertySource { /** * Sets the {@code properties} and returns a reference to this Builder so that the methods can be chained together. * - * @param key the {@code properties} key to set - * @param val the {@code properties} value to set + * @param key the {@code properties} key to setCurrent + * @param val the {@code properties} value to setCurrent * @return a reference to this Builder */ public Builder withProperty(String key, String val) { @@ -271,7 +271,7 @@ public class SimplePropertySource extends BasePropertySource { } /** - * Returns a {@code SimplePropertySource} built from the parameters previously set. + * Returns a {@code SimplePropertySource} built from the parameters previously setCurrent. * * @return a {@code SimplePropertySource} built with parameters of this {@code SimplePropertySource.Builder} */ http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySource.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySource.java index cfc60bb..7f7fac2 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySource.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySource.java @@ -163,9 +163,14 @@ public class SystemPropertySource extends BasePropertySource { if(disabled){ return null; } + reload(); String prefix = this.prefix; if(prefix==null) { - return PropertyValue.of(key, System.getProperty(key), getName()); + String value = System.getProperty(key); + if(value == null){ + return null; + } + return PropertyValue.of(key, value, getName()); } return PropertyValue.of(key, System.getProperty(key.substring(prefix.length())), getName()); } @@ -175,6 +180,11 @@ public class SystemPropertySource extends BasePropertySource { if(disabled){ return Collections.emptyMap(); } + reload(); + return this.cachedProperties; + } + + public void reload() { // only need to reload and fill our map if something has changed // synchronization was removed, Instance was marked as volatile. In the worst case it // is reloaded twice, but the values will be the same. @@ -182,7 +192,6 @@ public class SystemPropertySource extends BasePropertySource { Map<String, PropertyValue> properties = loadProperties(); this.cachedProperties = Collections.unmodifiableMap(properties); } - return this.cachedProperties; } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/test/java/org/apache/tamaya/spisupport/C.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/C.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/C.java index da581e6..9d51bb0 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/C.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/C.java @@ -38,7 +38,7 @@ public class C extends B implements Readable{ } /** - * Returns the input value, set on creation. Used for test assertion. + * Returns the input value, setCurrent on creation. Used for test assertion. * @return the in value. */ public String getInValue() { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/test/java/org/apache/tamaya/spisupport/CTestConverter.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/CTestConverter.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/CTestConverter.java index dce8121..dc13137 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/CTestConverter.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/CTestConverter.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.spisupport; -import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; /** @@ -26,7 +25,7 @@ import org.apache.tamaya.spi.PropertyConverter; */ public class CTestConverter implements PropertyConverter<C>{ @Override - public C convert(String value, ConversionContext context) { + public C convert(String value) { return new C(value); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java index d14815a..8f13586 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java @@ -20,7 +20,6 @@ package org.apache.tamaya.spisupport; import java.util.Map; import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; import static org.assertj.core.api.Assertions.*; @@ -37,7 +36,7 @@ public class DefaultConfigValueEvaluatorTest { */ @Test public void testEvaluteRawValue() { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); DefaultConfigValueEvaluator instance = new DefaultConfigValueEvaluator(); PropertyValue result = instance.evaluteRawValue("confkey1", config.getContext()); assertThat(result.getValue()).isEqualTo("javaconf-value1"); @@ -50,7 +49,7 @@ public class DefaultConfigValueEvaluatorTest { */ @Test public void testEvaluateRawValues() { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); DefaultConfigValueEvaluator instance = new DefaultConfigValueEvaluator(); Map<String, PropertyValue> result = instance.evaluateRawValues(config.getContext()); assertThat(result.containsKey("confkey1")).isTrue(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java index 9584037..f2f4aef 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java @@ -51,7 +51,7 @@ public class DefaultConfigurationBuilderTest { @Test public void setConfiguration() throws Exception { - Configuration cfg = ConfigurationProvider.getConfiguration(); + Configuration cfg = Configuration.current(); ConfigurationBuilder b = new DefaultConfigurationBuilder() .setConfiguration(cfg); assertThat(b.build()).isEqualTo(cfg); @@ -99,8 +99,8 @@ public class DefaultConfigurationBuilderTest { @Test public void addRemovePropertyFilters_Array() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; + PropertyFilter filter1 = (value) -> value; + PropertyFilter filter2 = (value) -> value; DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); Configuration cfg = b.addPropertyFilters(filter1, filter2).build(); ConfigurationContext ctx = cfg.getContext(); @@ -126,8 +126,8 @@ public class DefaultConfigurationBuilderTest { @Test public void addRemovePropertyFilters_Collection() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; + PropertyFilter filter1 = (value) -> value; + PropertyFilter filter2 = (value) -> value; DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); Configuration cfg = b.addPropertyFilters(Arrays.asList(filter1, filter2)).build(); ConfigurationContext ctx = cfg.getContext(); @@ -291,7 +291,7 @@ public class DefaultConfigurationBuilderTest { DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); PropertyFilter[] propertyFilters = new PropertyFilter[10]; for (int i = 0; i < propertyFilters.length; i++) { - propertyFilters[i] = (value, context) -> value.toBuilder().setValue(toString() + " - ").build(); + propertyFilters[i] = (value) -> value.setValue(toString() + " - "); } b.addPropertyFilters(propertyFilters); @@ -306,8 +306,8 @@ public class DefaultConfigurationBuilderTest { @Test public void addRemovePropertyConverter_Array() throws Exception { - PropertyConverter converter1 = (value, context) -> value.toLowerCase(); - PropertyConverter converter2 = (value, context) -> value.toUpperCase(); + PropertyConverter converter1 = (value) -> value.toLowerCase(); + PropertyConverter converter2 = (value) -> value.toUpperCase(); ConfigurationBuilder b = new DefaultConfigurationBuilder() .addPropertyConverters(TypeLiteral.of(String.class), converter1, converter2); Configuration cfg = b.build(); @@ -340,8 +340,8 @@ public class DefaultConfigurationBuilderTest { @Test public void addRemovePropertyConverter_Collection() throws Exception { - PropertyConverter converter1 = (value, context) -> value.toLowerCase(); - PropertyConverter converter2 = (value, context) -> value.toUpperCase(); + PropertyConverter converter1 = (value) -> value.toLowerCase(); + PropertyConverter converter2 = (value) -> value.toUpperCase(); ConfigurationBuilder b = new DefaultConfigurationBuilder() .addPropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1, converter2)); Configuration cfg = b.build(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java index 326484f..0869e82 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java @@ -18,12 +18,8 @@ */ package org.apache.tamaya.spisupport; -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.PropertyValueCombinationPolicy; +import org.apache.tamaya.spi.*; + import static org.assertj.core.api.Assertions.*; import org.apache.tamaya.ConfigurationProvider; @@ -168,8 +164,8 @@ public class DefaultConfigurationContextBuilderTest { @Test public void addPropertyFilters_Array() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; + PropertyFilter filter1 = (value) -> value; + PropertyFilter filter2 = (value) -> value; ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); b.addPropertyFilters(filter1, filter2); ConfigurationContext ctx = b.build(); @@ -191,8 +187,8 @@ public class DefaultConfigurationContextBuilderTest { @Test public void addPropertyFilters_Collection() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; + PropertyFilter filter1 = (value) -> value; + PropertyFilter filter2 = (value) -> value; ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); ConfigurationContext ctx = b.build(); @@ -214,8 +210,8 @@ public class DefaultConfigurationContextBuilderTest { @Test public void removePropertyFilters_Array() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; + PropertyFilter filter1 = (value) -> value; + PropertyFilter filter2 = (value) -> value; ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyFilters(filter1, filter2); ConfigurationContext ctx = b.build(); @@ -239,8 +235,8 @@ public class DefaultConfigurationContextBuilderTest { @Test public void removePropertyFilters_Collection() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; + PropertyFilter filter1 = (value) -> value; + PropertyFilter filter2 = (value) -> value; ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); ConfigurationContext ctx = b.build(); @@ -265,7 +261,7 @@ public class DefaultConfigurationContextBuilderTest { @Test @SuppressWarnings({"rawtypes", "unchecked"}) public void addPropertyConverters_Array() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + PropertyConverter converter = (value) -> value.toLowerCase(); ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyConverters(TypeLiteral.of(String.class), converter); ConfigurationContext ctx = b.build(); @@ -287,7 +283,7 @@ public class DefaultConfigurationContextBuilderTest { @Test @SuppressWarnings({"rawtypes", "unchecked"}) public void addPropertyConverters_Collection() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + PropertyConverter converter = (value) -> value.toLowerCase(); ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); @@ -312,7 +308,7 @@ public class DefaultConfigurationContextBuilderTest { @Test @SuppressWarnings({"rawtypes", "unchecked"}) public void removePropertyConverters_Type() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + PropertyConverter converter = (value) -> value.toLowerCase(); ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyConverters(TypeLiteral.of(String.class), converter); ConfigurationContext ctx = b.build(); @@ -328,7 +324,7 @@ public class DefaultConfigurationContextBuilderTest { @Test @SuppressWarnings({"rawtypes", "unchecked"}) public void removePropertyConverters_Array() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + PropertyConverter converter = (value) -> value.toLowerCase(); ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyConverters(TypeLiteral.of(String.class), converter); ConfigurationContext ctx = b.build(); @@ -344,7 +340,7 @@ public class DefaultConfigurationContextBuilderTest { @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void removePropertyConverters_Collection() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + PropertyConverter converter = (value) -> value.toLowerCase(); ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); ConfigurationContext ctx = b.build(); @@ -513,7 +509,7 @@ public class DefaultConfigurationContextBuilderTest { ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); PropertyFilter[] propertyFilters = new PropertyFilter[10]; for (int i = 0; i < propertyFilters.length; i++) { - propertyFilters[i] = (value, context) -> value.toBuilder().setValue(toString() + " - ").build(); + propertyFilters[i] = (value) -> value.setValue(toString() + " - "); } b.addPropertyFilters(propertyFilters); @@ -545,7 +541,7 @@ public class DefaultConfigurationContextBuilderTest { @Test public void testRemoveAllFilters() throws Exception { ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); - b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + b.addPropertyFilters((value) -> value.setValue(toString() + " - ")); assertThat(b.getPropertyFilters().isEmpty()).isFalse(); b.removePropertyFilters(b.getPropertyFilters()); assertThat(b.getPropertyFilters().isEmpty()).isTrue(); @@ -562,12 +558,12 @@ public class DefaultConfigurationContextBuilderTest { @Test public void testResetContext() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + PropertyConverter converter = (value) -> value.toLowerCase(); DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); ConfigurationContext empty = b.build(); b = new DefaultConfigurationContextBuilder(); - b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + b.addPropertyFilters((value) -> value.setValue(toString() + " - ")); b.addPropertySources(new MockedPropertySource()); b.addPropertyConverters(TypeLiteral.of(String.class), converter); ConfigurationContext full = b.build(); @@ -581,7 +577,7 @@ public class DefaultConfigurationContextBuilderTest { assertThat(caughtAlreadyBuilt).isTrue(); b = new DefaultConfigurationContextBuilder(); - b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + b.addPropertyFilters((value) -> value.setValue(toString() + " - ")); b.addPropertySources(new MockedPropertySource()); b.addPropertyConverters(TypeLiteral.of(String.class), converter); b.resetWithConfigurationContext(empty); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java index 9232ca6..afe5db6 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java @@ -18,11 +18,16 @@ */ package org.apache.tamaya.spisupport; +import org.apache.tamaya.ConfigOperator; +import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.*; import org.junit.Test; +import java.util.Collections; import java.util.Map; +import java.util.function.Function; +import java.util.function.UnaryOperator; import static org.assertj.core.api.Assertions.*; import org.assertj.core.internal.cglib.core.Predicate; @@ -30,7 +35,7 @@ import org.assertj.core.internal.cglib.core.Predicate; public class DefaultConfigurationTest { /** - * Tests for get(String) + * Tests for current(String) */ @Test(expected = NullPointerException.class) public void getDoesNotAcceptNull() { @@ -40,7 +45,7 @@ public class DefaultConfigurationTest { } /** - * Tests for get(String, Class) + * Tests for current(String, Class) */ @SuppressWarnings({"rawtypes", "unchecked"}) @Test(expected = NullPointerException.class) @@ -51,7 +56,7 @@ public class DefaultConfigurationTest { } /** - * Tests for get(String, TypeLiteral) + * Tests for current(String, TypeLiteral) */ @Test(expected = NullPointerException.class) public void getDoesNotAcceptNullForTypeLiteralTargetType() { @@ -65,7 +70,7 @@ public class DefaultConfigurationTest { DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); assertThat(c.get("valueOfValid")).isNotNull(); assertThat(c.get("valueOfNull")).isNull(); - assertThat(c.get("Filternull")).isNull(); //get does apply filtering + assertThat(c.get("Filternull")).isNull(); //current does apply filtering } /** @@ -164,7 +169,9 @@ public class DefaultConfigurationTest { @Test public void testConvertValue() { DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); - assertThat(100 == (Integer) c.convertValue("aHundred", "100", TypeLiteral.of(Integer.class))).isTrue(); + assertThat(100 == (Integer) c.convertValue("aHundred", + Collections.singletonList(PropertyValue.of("aHundred", "100", null)), + TypeLiteral.of(Integer.class))).isTrue(); } @Test(expected = NullPointerException.class) @@ -175,12 +182,26 @@ public class DefaultConfigurationTest { } @Test(expected = NullPointerException.class) + public void map_Null() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + + c.map(null); + } + + @Test(expected = NullPointerException.class) public void query_Null() { DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.query(null); } + @Test(expected = NullPointerException.class) + public void adapt_Null() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + + c.adapt(null); + } + @Test public void with() { DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); @@ -188,10 +209,22 @@ public class DefaultConfigurationTest { } @Test + public void map() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertThat(c).isEqualTo(c.map(config -> config)); + } + + @Test public void query() { DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); assertThat("testQ").isEqualTo(c.query(config -> "testQ")); } + + @Test + public void adapt() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertThat("testQ").isEqualTo(c.adapt(config -> "testQ")); + } @Test public void testEqualsAndHashAndToStringValues() {
