You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2016/07/20 00:59:16 UTC
[6/7] incubator-tamaya git commit: - Minimalized API. - Propagated
change to rest of modules.
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
deleted file mode 100644
index 05acdc3..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
+++ /dev/null
@@ -1,388 +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.builder;
-
-
-import org.apache.tamaya.builder.spi.PropertyConverter;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.builder.spi.ConfigurationContext;
-import org.apache.tamaya.builder.spi.ConfigurationContextBuilder;
-import org.apache.tamaya.builder.spi.PropertyFilter;
-import org.apache.tamaya.builder.spi.PropertySource;
-import org.apache.tamaya.builder.spi.PropertySourceProvider;
-import org.apache.tamaya.builder.spi.PropertyValueCombinationPolicy;
-import org.apache.tamaya.builder.spi.ServiceContextManager;
-import org.apache.tamaya.spisupport.PriorityServiceComparator;
-import org.apache.tamaya.spisupport.PropertyConverterManager;
-import org.apache.tamaya.spisupport.PropertySourceComparator;
-
-import javax.annotation.Priority;
-import java.util.*;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-import java.util.logging.Logger;
-
-/**
- * Implementation of the {@link ConfigurationContext}
- * used by the {@link org.apache.tamaya.builder.ConfigurationBuilder}
- * internally.
- */
-class ProgrammaticConfigurationContext implements ConfigurationContext {
-
- private static final Comparator<PropertySource> PS_COMPARATOR = new PropertySourceComparator();
- private static final Comparator<Object> COMP_COMPARATOR = new PriorityServiceComparator();
- /**
- * The logger used.
- */
- private final static Logger LOG = Logger.getLogger(ProgrammaticConfigurationContext.class.getName());
- /**
- * Cubcomponent handling {@link PropertyConverter} instances.
- */
- private PropertyConverterManager propertyConverterManager = new PropertyConverterManager();
-
- /**
- * The current unmodifiable list of loaded {@link PropertySource} instances.
- */
- private List<PropertySource> immutablePropertySources = new ArrayList<>();
-
- /**
- * The current unmodifiable list of loaded {@link PropertyFilter} instances.
- */
- private List<PropertyFilter> immutablePropertyFilters = new ArrayList<>();
-
- /**
- * The overriding policy used when combining PropertySources registered to evalute the final configuration
- * values.
- */
- private PropertyValueCombinationPolicy propertyValueCombinationPolicy;
-
- /**
- * Lock for internal synchronization.
- */
- private final ReadWriteLock propertySourceLock = new ReentrantReadWriteLock();
-
-
- /**
- * The first time the Configuration system gets invoked we do initialize
- * all our {@link PropertySource}s and
- * {@link PropertyFilter}s which are known at startup.
- */
- @SuppressWarnings("unchecked")
- public ProgrammaticConfigurationContext(Builder builder) {
- propertyConverterManager = new PropertyConverterManager(builder.loadProvidedPropertyConverters);
-
- List<PropertySource> sources = getAllPropertySources(builder);
- Collections.sort(sources, PS_COMPARATOR);
- immutablePropertySources = Collections.unmodifiableList(sources);
-
-
- List<PropertyFilter> filters = getPropertyFilters(builder);
- Collections.sort(filters, COMP_COMPARATOR);
- immutablePropertyFilters = Collections.unmodifiableList(filters);
-
-
- propertyValueCombinationPolicy = builder.propertyValueCombinationPolicy;
- for(Map.Entry<TypeLiteral<?>, List<PropertyConverter<?>>> en: builder.propertyConverters.entrySet()){
- if(en!=null){
- for(PropertyConverter pv:en.getValue()) {
- propertyConverterManager.register(en.getKey(), pv);
- }
- }
- }
-
- LOG.info("Using " + immutablePropertySources.size() + " property sources: " + immutablePropertySources);
- LOG.info("Using " + immutablePropertyFilters.size() + " property filters: " + immutablePropertyFilters);
- LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
- }
-
- private List<PropertyFilter> getPropertyFilters(Builder builder) {
- List<PropertyFilter> provided = new ArrayList<>();
- if(builder.loadProvidedPropertyFilters) {
- provided.addAll(ServiceContextManager.getServiceContext().getServices(PropertyFilter.class));
- }
- for(PropertyFilter pf:builder.propertyFilters) {
- if (pf != null) {
- provided.add(pf);
- }
- }
- return provided;
- }
-
- private List<PropertySource> getAllPropertySources(Builder builder) {
- List<PropertySource> provided = new ArrayList<>();
- if(builder.loadProvidedPropertySources) {
- provided.addAll(ServiceContextManager.getServiceContext().getServices(PropertySource.class));
- }
- for(PropertySource ps:builder.propertySources){
- if(ps!=null){
- provided.add(ps);
- }
- }
- if (builder.loadProvidedPropertySourceProviders) {
- List<PropertySourceProvider> providers = ServiceContextManager.getServiceContext()
- .getServices(PropertySourceProvider.class);
- for (PropertySourceProvider provider : providers) {
- for(PropertySource ps:provider.getPropertySources()) {
- if(ps!=null) {
- provided.addAll(provider.getPropertySources());
- }
- }
- }
- }
- return provided;
- }
-
- public void addPropertySources(PropertySource... propertySourcesToAdd) {
- Lock writeLock = propertySourceLock.writeLock();
- try {
- writeLock.lock();
- List<PropertySource> provided = new ArrayList<>();
- for(PropertySource ps:propertySourcesToAdd){
- if(ps!=null){
- provided.add(ps);
- }
- }
- this.immutablePropertySources = Collections.unmodifiableList(provided);
- } finally {
- writeLock.unlock();
- }
- }
-
- /**
- * Order property source reversely, the most important come first.
- *
- * @param source1 the first PropertySource
- * @param source2 the second PropertySource
- * @return the comparison result.
- */
- private int comparePropertySources(PropertySource source1, PropertySource source2) {
-
- //X TODO this method duplicates DefaultConfigurationContext.PropertySourceComparator.comparePropertySources()
- //X maybe we should extract the Comperator in an own class for real code-reuse (copy paste == bad code reuse)
-
- if (source1.getOrdinal() < source2.getOrdinal()) {
- return -1;
- } else if (source1.getOrdinal() > source2.getOrdinal()) {
- return 1;
- } else {
- return source1.getClass().getName().compareTo(source2.getClass().getName());
- }
- }
-
- /**
- * Compare 2 filters for ordering the filter chain.
- *
- * @param filter1 the first filter
- * @param filter2 the second filter
- * @return the comparison result
- */
- private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
-
- //X TODO this method duplicates DefaultConfigurationContext.PropertySourceComparator.comparePropertyFilters()
- //X maybe we should extract the Comperator in an own class for real code-reuse (copy paste == bad code reuse)
-
- Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
- Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
- int ord1 = prio1 != null ? prio1.value() : 0;
- int ord2 = prio2 != null ? prio2.value() : 0;
-
- if (ord1 < ord2) {
- return -1;
- } else if (ord1 > ord2) {
- return 1;
- } else {
- return filter1.getClass().getName().compareTo(filter2.getClass().getName());
- }
- }
-
- @Override
- public List<PropertySource> getPropertySources() {
- return immutablePropertySources;
- }
-
- 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() {
- // @todo Check if it could be useful to support this method, Oliver B. Fischer
- throw new RuntimeException("This method is currently not supported.");
- }
-
- /**
- * The Builder for {@link ProgrammaticConfigurationContext}
- */
- public final static class Builder {
- /**
- * The current unmodifiable list of loaded {@link PropertySource} instances.
- */
- private final List<PropertySource> propertySources = new ArrayList<>();
-
- /**
- * The current unmodifiable list of loaded {@link PropertyFilter} instances.
- */
- private final List<PropertyFilter> propertyFilters = new ArrayList<>();
-
- private final Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters = new HashMap<>();
-
- /**
- * The overriding policy used when combining PropertySources registered to evalute the final configuration
- * values.
- */
- private PropertyValueCombinationPolicy propertyValueCombinationPolicy =
- PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
-
- private boolean loadProvidedPropertyConverters;
- private boolean loadProvidedPropertySources;
- private boolean loadProvidedPropertySourceProviders;
- private boolean loadProvidedPropertyFilters;
-
- public Builder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) {
- this.propertyValueCombinationPolicy = Objects.requireNonNull(policy);
- return this;
- }
-
- public Builder addPropertySources(PropertySource... propertySources) {
- for (PropertySource ps : propertySources) {
- if (ps != null) {
- this.propertySources.add(ps);
- }
- }
- return this;
- }
-
- public Builder addPropertySources(Collection<PropertySource> propertySources) {
- for (PropertySource ps : propertySources) {
- if (ps != null) {
- this.propertySources.add(ps);
- }
- }
- return this;
- }
-
- public Builder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders) {
- for (PropertySourceProvider ps : propertySourceProviders) {
- if (ps != null) {
- this.propertySources.addAll(ps.getPropertySources());
- }
- }
- return this;
- }
-
- public Builder addPropertySourceProviders(Collection<PropertySourceProvider> propertySourceProviders) {
- for (PropertySourceProvider ps : propertySourceProviders) {
- if (ps != null) {
- this.propertySources.addAll(ps.getPropertySources());
- }
- }
- return this;
- }
-
- public Builder addPropertyFilters(PropertyFilter... propertyFIlter) {
- for (PropertyFilter pf : propertyFIlter) {
- if (pf != null) {
- this.propertyFilters.add(pf);
- }
- }
- return this;
- }
-
- public Builder addPropertyFilters(Collection<PropertyFilter> propertyFIlter) {
- for (PropertyFilter pf : propertyFIlter) {
- if (pf != null) {
- this.propertyFilters.add(pf);
- }
- }
- return this;
- }
-
- /**
- * Should be never used.
- */
- @Deprecated
- public Builder setConfigurationContext(ConfigurationContext configurationContext) {
- this.addPropertySources(configurationContext.getPropertySources());
- this.addPropertyFilters(configurationContext.getPropertyFilters());
- this.propertyValueCombinationPolicy = Objects.requireNonNull(
- configurationContext.getPropertyValueCombinationPolicy());
- return this;
- }
-
- //X TODO think on a functonality/API for using the default PropertyConverters and use the configured ones here
- //X TODO as overrides used first.
-
- public <T> Builder addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) {
- if(!propertyConverters.containsKey(type)){
- List<PropertyConverter<?>> convList = new ArrayList<>();
- convList.add(propertyConverter);
- propertyConverters.put(type, convList);
- }
- return this;
- }
-
- public ConfigurationContext build() {
- return new ProgrammaticConfigurationContext(this);
- }
-
-
- public void loadProvidedPropertyConverters(boolean state) {
- loadProvidedPropertyConverters = state;
- }
-
- public void loadProvidedPropertySources(boolean state) {
- loadProvidedPropertySources = state;
- }
-
- public void loadProvidedPropertySourceProviders(boolean state) {
- loadProvidedPropertySourceProviders = state;
- }
-
- public void loadProvidedPropertyFilters(boolean state) {
- loadProvidedPropertyFilters = state;
- }
-
- }
-
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfiguration.java b/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfiguration.java
new file mode 100644
index 0000000..376966c
--- /dev/null
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfiguration.java
@@ -0,0 +1,76 @@
+/*
+ * 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.builder;
+
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.builder.spi.PropertyFilter;
+import org.apache.tamaya.builder.spi.PropertySource;
+import org.apache.tamaya.builder.spi.PropertyValueCombinationPolicy;
+
+import java.util.List;
+
+/**
+ * Central SPI for programmatically dealing with the setup of the configuration system.
+ * This includes adding and enlisting {@link PropertySource}s,
+ * ConfigFilters, etc.
+ */
+public interface PropertySourceBasedConfiguration extends Configuration {
+
+ /**
+ * This method can be used for programmatically adding {@link PropertySource}s.
+ * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+ *
+ * @param propertySourcesToAdd the PropertySources to add
+ */
+ void addPropertySources(PropertySource... propertySourcesToAdd);
+
+ /**
+ * This method returns the current list of registered PropertySources ordered via their ordinal.
+ * PropertySources with a lower ordinal come last. The PropertySource with the
+ * highest ordinal comes first.
+ * If two PropertySources have the same ordinal number they will get sorted
+ * using their class name just to ensure the user at least gets the same ordering
+ * after a JVM restart, hereby names before are added last.
+ * PropertySources are loaded when this method is called the first time, which basically is
+ * when the first time configuration is accessed.
+ *
+ * @return a sorted list of registered PropertySources. The returned list need not be modifiable
+ */
+ List<PropertySource> getPropertySources();
+
+ /**
+ * Access the current PropertyFilter instances.
+ * @return the list of registered PropertyFilters, never null.
+ */
+ List<PropertyFilter> getPropertyFilters();
+
+ /**
+ * Access the {@link PropertyValueCombinationPolicy} used to evaluate the final
+ * property values.
+ * @return the {@link PropertyValueCombinationPolicy} used, never null.
+ */
+ PropertyValueCombinationPolicy getPropertyValueCombinationPolicy();
+
+ /**
+ * Creates a {@link PropertySourceBasedConfigurationBuilder} preinitialized with the data from this instance.
+ * @return a new builder instance, never null.
+ */
+ PropertySourceBasedConfigurationBuilder toBuilder();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfigurationBuilder.java b/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfigurationBuilder.java
new file mode 100644
index 0000000..c7dd922
--- /dev/null
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBasedConfigurationBuilder.java
@@ -0,0 +1,483 @@
+/*
+ * 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.builder;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.builder.propertysource.SimplePropertySource;
+import org.apache.tamaya.builder.spi.*;
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormats;
+import org.apache.tamaya.format.FlattenedDefaultPropertySource;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static java.lang.String.format;
+
+/**
+ * <p>Builder class used for building a configuration manually without relying
+ * only on the Service Provider Interface API.</p>
+ *
+ * <p><strong>Features of the builder</strong></p>
+ *
+ * <ol>
+ * <li>Adding of property converters manually</li>
+ * <li>Adding of property sources directly</li>
+ * <li>Adding of property sources via URL</li>
+ * <li>Adding of property source providers directly</li>
+ * <li>Enabling and disabling of via SPI mechanism provided resources as converters,
+ * property sources, etc.</li>
+ * </ol>
+ *
+ * <p><strong>Example</strong></p>
+ *
+ * <pre>{@code ConfigurationBuilder builder = new ConfigurationBuilder();
+ * builder.disableProvidedPropertySources() // Do not load provided property
+ * .disableProvidedPropertySourceProviders() // sources and providers automatically
+ * .addPropertySource("file:/etc/conf.properties"); // Load properties from conf.properties
+ *
+ * Configuration config = builder.build();
+ * }</pre>
+ *
+ * <p><strong>Support for configuration formats</strong></p>
+ *
+ * The configuration builder allows you to put property resources
+ * via a URL, as shown in the code example above, without implementing
+ * a {@link PropertySource PropertySource} or providing an
+ * instance of a {@link PropertySource PropertySource}.
+ * If a property resource in
+ * a specific format can be added to configuration builder or not depends
+ * on the available implementations of
+ * {@link org.apache.tamaya.format.ConfigurationFormat} in the classpath.
+ * Which formats are available can be checked via
+ * {@link org.apache.tamaya.format.ConfigurationFormats#getFormats()}.
+ */
+public class PropertySourceBasedConfigurationBuilder {
+ private static final Logger LOG = Logger.getLogger(PropertySourceBasedConfigurationBuilder.class.getName());
+ /**
+ * The current unmodifiable list of loaded {@link PropertySource} instances.
+ */
+ private final List<PropertySource> propertySources = new ArrayList<>();
+
+ /**
+ * The current unmodifiable list of loaded {@link PropertyFilter} instances.
+ */
+ private final List<PropertyFilter> propertyFilters = new ArrayList<>();
+
+ /**
+ * The overriding policy used when combining PropertySources registered to evalute the final configuration
+ * values.
+ */
+ private PropertyValueCombinationPolicy propertyValueCombinationPolicy =
+ PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
+
+ private boolean loadProvidedPropertySources;
+ private boolean loadProvidedPropertySourceProviders;
+ private boolean loadProvidedPropertyFilters;
+
+ /**
+ * Flag if the config has already been built.
+ * Configuration can be built only once
+ */
+ private boolean built;
+
+ public PropertySourceBasedConfigurationBuilder(){
+ }
+
+ public PropertySourceBasedConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) {
+ this.propertyValueCombinationPolicy = Objects.requireNonNull(policy);
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertySources(PropertySource... propertySources) {
+ for (PropertySource ps : propertySources) {
+ if (ps != null) {
+ this.propertySources.add(ps);
+ }
+ }
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertySources(Collection<PropertySource> propertySources) {
+ for (PropertySource ps : propertySources) {
+ if (ps != null) {
+ this.propertySources.add(ps);
+ }
+ }
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders) {
+ for (PropertySourceProvider ps : propertySourceProviders) {
+ if (ps != null) {
+ this.propertySources.addAll(ps.getPropertySources());
+ }
+ }
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertySourceProviders(Collection<PropertySourceProvider> propertySourceProviders) {
+ for (PropertySourceProvider ps : propertySourceProviders) {
+ if (ps != null) {
+ this.propertySources.addAll(ps.getPropertySources());
+ }
+ }
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertyFilters(PropertyFilter... propertyFIlter) {
+ for (PropertyFilter pf : propertyFIlter) {
+ if (pf != null) {
+ this.propertyFilters.add(pf);
+ }
+ }
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> propertyFIlter) {
+ for (PropertyFilter pf : propertyFIlter) {
+ if (pf != null) {
+ this.propertyFilters.add(pf);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * Should be never used.
+ */
+ @Deprecated
+ public PropertySourceBasedConfigurationBuilder usePropertySourceBasedConfiguration(PropertySourceBasedConfiguration configurationContext) {
+ addPropertySources(configurationContext.getPropertySources());
+ addPropertyFilters(configurationContext.getPropertyFilters());
+ this.propertyValueCombinationPolicy = Objects.requireNonNull(
+ configurationContext.getPropertyValueCombinationPolicy());
+ return this;
+ }
+
+ public void loadProvidedPropertySources(boolean state) {
+ loadProvidedPropertySources = state;
+ }
+
+ public void loadProvidedPropertySourceProviders(boolean state) {
+ loadProvidedPropertySourceProviders = state;
+ }
+
+ public void loadProvidedPropertyFilters(boolean state) {
+ loadProvidedPropertyFilters = state;
+ }
+
+ public PropertySourceBasedConfigurationBuilder setConfiguration(PropertySourceBasedConfiguration context) {
+ this.propertySources.clear();
+ for(PropertySource ps:context.getPropertySources()) {
+ addPropertySources(ps);
+ }
+ this.propertyFilters.clear();
+ this.propertyFilters.addAll(context.getPropertyFilters());
+ this.propertyValueCombinationPolicy = context.getPropertyValueCombinationPolicy();
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertySourceURLs(Collection<URL> propertySourceURLsToAdd) {
+ for(URL url:propertySourceURLsToAdd){
+ try {
+ ConfigurationData data = ConfigurationFormats.readConfigurationData(url);
+ addPropertySources(new SimplePropertySource(url.toString(),data.getCombinedProperties()));
+ }catch(Exception e){
+ LOG.log(Level.SEVERE, "Failed to load config from: " + url, e);
+ }
+ }
+ for(URL url:propertySourceURLsToAdd) {
+ try {
+ this.propertySources.add(new SimplePropertySource(url.toString(), getConfigurationDataFromURL(url).getCombinedProperties()));
+ } catch (IOException e) {
+ LOG.log(Level.SEVERE, "Error loading config from: " + url, e);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * Adds one resources with properties in an arbitrary format
+ * to the configuration to be build.
+ *
+ * <p>If a specific format is supported depends on the available
+ * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
+ *
+ * <pre>{@code URL resource = new URL("file:/etc/service/config.json");
+ *
+ * builder.addPropertySources(resource);}
+ * </pre>
+ *
+ * @param url resource with properties for the the configuration to be build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.format.ConfigurationFormat
+ * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
+ */
+ public PropertySourceBasedConfigurationBuilder addPropertySource(URL url) {
+ try {
+ ConfigurationData data = getConfigurationDataFromURL(url);
+ addPropertySources(new SimplePropertySource(url.toString(), data.getCombinedProperties()));
+ } catch (IOException e) {
+ throw new ConfigException("Failed to read " + url.toString(), e);
+ }
+ return this;
+ }
+
+ protected ConfigurationData getConfigurationDataFromURL(URL url) throws IOException {
+ ConfigurationData data = ConfigurationFormats.readConfigurationData(url);
+
+ if (null == data) {
+ String mesg = format("No configuration format found which is able " +
+ "to read properties from %s.", url.toString());
+
+ throw new ConfigException(mesg);
+ }
+
+ return data;
+ }
+
+ public PropertySourceBasedConfigurationBuilder addPropertySourceURLs(URL... propertySourcesToAdd) {
+ return addPropertySourceURLs(Arrays.asList(propertySourcesToAdd));
+ }
+
+ public PropertySourceBasedConfigurationBuilder removePropertySources(Collection<String> propertySourcesToRemove) {
+ for(String key: propertySourcesToRemove){
+ this.propertySources.remove(key);
+ }
+ return this;
+ }
+
+ public PropertySourceBasedConfigurationBuilder removePropertySources(String... propertySourcesToRemove) {
+ return removePropertySources(Arrays.asList(propertySourcesToRemove));
+ }
+
+
+ /**
+ * Adds one or more resources with properties in an arbitrary format
+ * to the configuration to be build.
+ *
+ * <p>If a specific format is supported depends on the available
+ * {@link org.apache.tamaya.format.ConfigurationFormat} implementations.</p>
+ *
+ *<pre>{@code builder.addPropertySources("file:/etc/service/config.json",
+ * "file:/etc/defaults/values.properties");}
+ *</pre>
+ *
+ * @param urls list of resources with properties for the configuration to be
+ * build.
+ *
+ * @return the builder instance currently used
+ *
+ * @see org.apache.tamaya.format.ConfigurationFormat
+ * @see org.apache.tamaya.format.ConfigurationFormats#getFormats()
+ */
+ public PropertySourceBasedConfigurationBuilder addPropertySourceURLs(String... urls) {
+ for(String url:urls) {
+ if (url != null) {
+ try{
+ addPropertySource(new URL(url));
+ } catch(Exception e){
+ throw new ConfigException("Invalid URL: " + url);
+ }
+ }
+ }
+ return this;
+ }
+
+
+ /**
+ * Enables the automatic loading of all {@link PropertySource}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see PropertySource
+ * @see #disableProvidedPropertySources()
+ */
+ public PropertySourceBasedConfigurationBuilder enableProvidedPropertySources() {
+ checkBuilderState();
+
+ loadProvidedPropertySources = true;
+
+ return this;
+ }
+
+ /**
+ * Enables the automatic loading of all {@link PropertySource}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see PropertySource
+ * @see #enableProvidedPropertySources()
+ */
+ public PropertySourceBasedConfigurationBuilder disableProvidedPropertySources() {
+ checkBuilderState();
+
+ loadProvidedPropertySources = false;
+
+ return this;
+ }
+
+ /**
+ * Checks if the automatic loading of all {@link PropertySource
+ * PropertySource} service providers is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ */
+ public boolean isPropertySourcesLoadingEnabled() {
+ return loadProvidedPropertySources;
+ }
+
+
+ /**
+ * Checks if the automatic loading of all {@link PropertyFilter
+ * PropertyFilter} service providers is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ */
+ public boolean isPropertyFilterLoadingEnabled() {
+ return loadProvidedPropertyFilters;
+ }
+
+ /**
+ * Enables the automatic loading of all {@link PropertyFilter}
+ * service providers.
+ *
+ * @return the builder instance currently used
+ *
+ * @see PropertyFilter
+ * @see #disableProvidedPropertyFilters()
+ * @see #addPropertyFilters(PropertyFilter...)
+ */
+ public PropertySourceBasedConfigurationBuilder enabledProvidedPropertyFilters() {
+ checkBuilderState();
+
+ loadProvidedPropertyFilters = true;
+
+ return this;
+ }
+
+ /**
+ * Disables the automatic loading of all {@link PropertyFilter}
+ * service providers.
+ *
+ * @see PropertyFilter
+ * @see #enabledProvidedPropertyFilters()
+ * @see #addPropertyFilters(PropertyFilter...)
+ *
+ * @return the builder instance currently used
+ */
+ public PropertySourceBasedConfigurationBuilder disableProvidedPropertyFilters() {
+ checkBuilderState();
+
+ loadProvidedPropertyFilters = false;
+
+ return this;
+ }
+
+ private void checkBuilderState() {
+ if (built) {
+ throw new IllegalStateException("Configuration has already been build.");
+ }
+ }
+
+ /**
+ * Adds one or more property source provider instances to the configuration to be build.
+ *
+ * <pre>{@code PropertySourceProvider jc = new JavaConfigurationProvider();
+ *
+ * builder.addPropertySources(jc)};
+ * </pre>
+ *
+ * @return the builder instance currently used
+ *
+ * @see PropertySourceProvider
+ */
+ public PropertySourceBasedConfigurationBuilder enableProvidedPropertySourceProviders() {
+ checkBuilderState();
+
+ loadProvidedPropertySourceProviders = true;
+
+ return this;
+ }
+
+ /**
+ * Disables the automatic loading of {@link PropertySourceProvider
+ * property source providers} provided via the SPI API.
+ *
+ * @return the builder instance currently used
+ */
+ public PropertySourceBasedConfigurationBuilder disableProvidedPropertySourceProviders() {
+ checkBuilderState();
+
+ loadProvidedPropertySourceProviders = false;
+
+ return this;
+ }
+
+ /**
+ * Checks if the automatic loading of {@link PropertySourceProvider
+ * PropertySourceProviders} is enabled or disabled.
+ *
+ * @return {@code true} if the automatic loading is enabled,
+ * otherwise {@code false}.
+ */
+ public boolean isPropertySourceProvidersLoadingEnabled() {
+ return loadProvidedPropertySourceProviders;
+ }
+
+ /**
+ * 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}.
+ */
+ public PropertySourceBasedConfiguration build() {
+ checkBuilderState();
+ loadProvidedPropertySources(isPropertySourcesLoadingEnabled());
+ loadProvidedPropertySourceProviders(isPropertySourceProvidersLoadingEnabled());
+ loadProvidedPropertyFilters(isPropertyFilterLoadingEnabled());
+
+ return new DefaultPropertySourceBasedConfiguration(this);
+ }
+
+ /**
+ * Mapper to map a URL given as string to an URL instance.
+ */
+ private static class StringToURLMapper {
+ public URL apply(String u) {
+ try {
+ return new URL(u);
+ } catch (MalformedURLException e) {
+ throw new ConfigException(u + " is not a valid URL", e);
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBuilder.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBuilder.java b/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBuilder.java
index e880fb4..5a69231 100644
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBuilder.java
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/PropertySourceBuilder.java
@@ -18,6 +18,7 @@
*/
package org.apache.tamaya.builder;
+import org.apache.tamaya.builder.propertysource.SimplePropertySource;
import org.apache.tamaya.builder.spi.PropertySource;
import java.util.HashMap;
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/SimplePropertySource.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/SimplePropertySource.java b/modules/builder/src/main/java/org/apache/tamaya/builder/SimplePropertySource.java
deleted file mode 100644
index 8218bc8..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/SimplePropertySource.java
+++ /dev/null
@@ -1,86 +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.builder;
-
-import org.apache.tamaya.builder.spi.PropertySource;
-import org.apache.tamaya.builder.spi.PropertyValue;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
-* Simple property source implementation using a map.
-*/
-public class SimplePropertySource implements PropertySource {
- /** The properties. */
- private final Map<String, String> properties;
- /** The source's name. */
- private final String name;
-
- public SimplePropertySource(String name, Map<String, String> properties){
- this.properties = new HashMap<>(properties);
- this.name = Objects.requireNonNull(name);
- }
-
- @Override
- public int getOrdinal(){
- PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL);
- if(configuredOrdinal!=null){
- try{
- return Integer.parseInt(configuredOrdinal.getValue());
- } catch(Exception e){
- Logger.getLogger(getClass().getName()).log(Level.WARNING,
- "Configured Ordinal is not an int number: " + configuredOrdinal, e);
- }
- }
- return getDefaultOrdinal();
- }
-
- public int getDefaultOrdinal(){
- return 0;
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public PropertyValue get(String key) {
- return null;
- }
-
- @Override
- public Map<String, String> getProperties() {
- return this.properties;
- }
-
- @Override
- public boolean isScannable() {
- return false;
- }
-
- @Override
- public String toString(){
- return "SimplePropertySource(name="+name+", numProps="+properties.size()+")";
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContext.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContext.java
deleted file mode 100644
index cab4a49..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContext.java
+++ /dev/null
@@ -1,278 +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.builder.internal;
-
-import org.apache.tamaya.ConfigurationProvider;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.builder.spi.ConfigurationContext;
-import org.apache.tamaya.builder.spi.ConfigurationContextBuilder;
-import org.apache.tamaya.builder.spi.PropertyConverter;
-import org.apache.tamaya.builder.spi.PropertyFilter;
-import org.apache.tamaya.builder.spi.PropertySource;
-import org.apache.tamaya.builder.spi.PropertySourceProvider;
-import org.apache.tamaya.builder.spi.PropertyValueCombinationPolicy;
-import org.apache.tamaya.builder.spi.ServiceContextManager;
-
-import javax.annotation.Priority;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-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();
-
- /** Comparator used for ordering property sources. */
- private final PropertySourceComparator propertySourceComparator = new PropertySourceComparator();
-
- /** Comparator used for ordering property filters. */
- private final PropertyFilterComparator propertyFilterComparator = new PropertyFilterComparator();
-
-
- /**
- * The first time the Configuration system gets invoked we do initialize
- * all our {@link PropertySource}s and
- * {@link PropertyFilter}s which are known at startup.
- */
- public DefaultConfigurationContext() {
- List<PropertySource> propertySources = new ArrayList<>();
-
- // first we load all PropertySources which got registered via java.util.ServiceLoader
- propertySources.addAll(ServiceContextManager.getServiceContext().getServices(PropertySource.class));
-
- // after that we add all PropertySources which get dynamically registered via their PropertySourceProviders
- propertySources.addAll(evaluatePropertySourcesFromProviders());
-
- // now sort them according to their ordinal values
- Collections.sort(propertySources, new PropertySourceComparator());
-
- 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<>();
- propertyFilters.addAll(ServiceContextManager.getServiceContext().getServices(PropertyFilter.class));
- Collections.sort(propertyFilters, new PropertyFilterComparator());
- immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
- LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
- immutablePropertyFilters);
-
- immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
- LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
- immutablePropertyFilters);
- propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class);
- if(propertyValueCombinationPolicy==null) {
- propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
- }
- LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
- }
-
- DefaultConfigurationContext(DefaultConfigurationContextBuilder builder) {
- List<PropertySource> propertySources = new ArrayList<>();
- // first we load all PropertySources which got registered via java.util.ServiceLoader
- propertySources.addAll(builder.propertySources.values());
- // now sort them according to their ordinal values
- Collections.sort(propertySources, propertySourceComparator);
- 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<>();
- propertyFilters.addAll(ServiceContextManager.getServiceContext().getServices(PropertyFilter.class));
- Collections.sort(propertyFilters, propertyFilterComparator);
- immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
- LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
- immutablePropertyFilters);
-
- 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);
- }
-
- /**
- * Pick up all {@link PropertySourceProvider}s and return all the
- * {@link PropertySource}s they like to register.
- */
- private Collection<? extends PropertySource> evaluatePropertySourcesFromProviders() {
- List<PropertySource> propertySources = new ArrayList<>();
- Collection<PropertySourceProvider> propertySourceProviders = ServiceContextManager.getServiceContext().getServices(PropertySourceProvider.class);
- for (PropertySourceProvider propertySourceProvider : propertySourceProviders) {
- Collection<PropertySource> sources = propertySourceProvider.getPropertySources();
- LOG.finer("PropertySourceProvider " + propertySourceProvider.getClass().getName() +
- " provided the following property sources: " + sources);
- propertySources.addAll(sources);
- }
-
- return propertySources;
- }
-
- @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, new PropertySourceComparator());
-
- this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
- } finally {
- writeLock.unlock();
- }
- }
-
- private static class PropertySourceComparator implements Comparator<PropertySource>, Serializable {
-
- private static final long serialVersionUID = 1L;
-
- /**
- * Order property source reversely, the most important come first.
- *
- * @param source1 the first PropertySource
- * @param source2 the second PropertySource
- * @return the comparison result.
- */
- private int comparePropertySources(PropertySource source1, PropertySource source2) {
- if (source1.getOrdinal() < source2.getOrdinal()) {
- return -1;
- } else if (source1.getOrdinal() > source2.getOrdinal()) {
- return 1;
- } else {
- return source1.getClass().getName().compareTo(source2.getClass().getName());
- }
- }
-
- @Override
- public int compare(PropertySource source1, PropertySource source2) {
- return comparePropertySources(source1, source2);
- }
- }
-
- private static class PropertyFilterComparator implements Comparator<PropertyFilter>, Serializable{
-
- private static final long serialVersionUID = 1L;
-
- /**
- * Compare 2 filters for ordering the filter chain.
- *
- * @param filter1 the first filter
- * @param filter2 the second filter
- * @return the comparison result
- */
- private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
- Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
- Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
- int ord1 = prio1 != null ? prio1.value() : 0;
- int ord2 = prio2 != null ? prio2.value() : 0;
-
- if (ord1 < ord2) {
- return -1;
- } else if (ord1 > ord2) {
- return 1;
- } else {
- return filter1.getClass().getName().compareTo(filter2.getClass().getName());
- }
- }
-
- @Override
- public int compare(PropertyFilter filter1, PropertyFilter filter2) {
- return comparePropertyFilters(filter1, filter2);
- }
- }
-
- @Override
- public List<PropertySource> getPropertySources() {
- return immutablePropertySources;
- }
-
- @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 ConfigurationProvider.getConfigurationContextBuilder().setContext(this);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContextBuilder.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContextBuilder.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContextBuilder.java
deleted file mode 100644
index 56a3379..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationContextBuilder.java
+++ /dev/null
@@ -1,153 +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.builder.internal;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.builder.spi.PropertyConverter;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.builder.spi.ConfigurationContext;
-import org.apache.tamaya.builder.spi.ConfigurationContextBuilder;
-import org.apache.tamaya.builder.spi.PropertyFilter;
-import org.apache.tamaya.builder.spi.PropertySource;
-import org.apache.tamaya.builder.spi.PropertyValueCombinationPolicy;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-
-/**
- * Default implementation of {@link ConfigurationContextBuilder}.
- */
-public class DefaultConfigurationContextBuilder implements ConfigurationContextBuilder {
-
- final Map<String, PropertySource> propertySources = new HashMap<>();
- final List<PropertyFilter> propertyFilters = new ArrayList<>();
- final Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters = new HashMap<>();
- PropertyValueCombinationPolicy combinationPolicy;
-
- public DefaultConfigurationContextBuilder(){
- }
-
- @Override
- public ConfigurationContextBuilder setContext(ConfigurationContext context) {
- this.propertySources.clear();
- for(PropertySource ps:context.getPropertySources()) {
- this.propertySources.put(ps.getName(), ps);
- }
- this.propertyFilters.clear();
- this.propertyFilters.addAll(context.getPropertyFilters());
- this.propertyConverters.clear();
- this.propertyConverters.putAll(context.getPropertyConverters());
- this.combinationPolicy = context.getPropertyValueCombinationPolicy();
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder addPropertySources(Collection<PropertySource> propertySourcesToAdd) {
- for(PropertySource ps:propertySourcesToAdd){
- if(this.propertySources.containsKey(ps.getName())){
- throw new ConfigException("Duplicate PropertySource: " + ps.getName());
- }
- }
- for(PropertySource ps:propertySourcesToAdd) {
- this.propertySources.put(ps.getName(), ps);
- }
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder addPropertySources(PropertySource... propertySourcesToAdd) {
- return addPropertySources(Arrays.asList(propertySourcesToAdd));
- }
-
- @Override
- public ConfigurationContextBuilder removePropertySources(Collection<String> propertySourcesToRemove) {
- for(String key: propertySourcesToRemove){
- this.propertySources.remove(key);
- }
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder removePropertySources(String... propertySourcesToRemove) {
- return removePropertySources(Arrays.asList(propertySourcesToRemove));
- }
-
- @Override
- public ConfigurationContextBuilder addPropertyFilters(Collection<PropertyFilter> filters) {
- this.propertyFilters.addAll(filters);
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder addPropertyFilters(PropertyFilter... filters) {
- return addPropertyFilters(Arrays.asList(filters));
- }
-
- @Override
- public ConfigurationContextBuilder removePropertyFilters(Collection<PropertyFilter> filters) {
- this.propertyFilters.removeAll(filters);
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder removePropertyFilters(PropertyFilter... filters) {
- return removePropertyFilters(Arrays.asList(filters));
- }
-
- @Override
- public <T> ConfigurationContextBuilder addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) {
- List<PropertyConverter<?>> converters = this.propertyConverters.get(typeToConvert);
- if(converters==null){
- converters = new ArrayList<>();
- this.propertyConverters.put(typeToConvert, converters);
- }
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert, PropertyConverter<?>... converters) {
- return removePropertyConverters(typeToConvert, Arrays.asList(converters));
- }
-
- @Override
- public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert, Collection<PropertyConverter<?>> converters) {
- List<PropertyConverter<?>> existing = this.propertyConverters.get(typeToConvert);
- if(existing!=null) {
- existing.removeAll(converters);
- }
- return this;
- }
-
- @Override
- public ConfigurationContextBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) {
- this.combinationPolicy = Objects.requireNonNull(policy);
- return this;
- }
-
- @Override
- public ConfigurationContext build() {
- return new DefaultConfigurationContext(this);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationProvider.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationProvider.java
deleted file mode 100644
index 254edd1..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultConfigurationProvider.java
+++ /dev/null
@@ -1,62 +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.builder.internal;
-
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.builder.spi.*;
-
-/**
- * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the
- * chain of {@link PropertySource} and {@link PropertyFilter}
- * instance to evaluate the current Configuration.
- */
-public class DefaultConfigurationProvider implements ConfigurationProviderSpi {
-
- private ConfigurationContext context = new DefaultConfigurationContext();
- private Configuration config = new DefaultConfiguration(context);
-
- @Override
- public Configuration getConfiguration() {
- return config;
- }
-
- @Override
- public ConfigurationContext getConfigurationContext() {
- return context;
- }
-
- @Override
- public ConfigurationContextBuilder getConfigurationContextBuilder() {
- return ServiceContextManager.getServiceContext().getService(ConfigurationContextBuilder.class);
- }
-
- @Override
- public void setConfigurationContext(ConfigurationContext context){
- // TODO think on a SPI or move event part into API...
- this.config = new DefaultConfiguration(context);
- this.context = context;
- }
-
-
- @Override
- public boolean isConfigurationContextSettable() {
- return true;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultServiceContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultServiceContext.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultServiceContext.java
deleted file mode 100644
index c75f98d..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/DefaultServiceContext.java
+++ /dev/null
@@ -1,156 +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.builder.internal;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.builder.spi.ServiceContext;
-
-import javax.annotation.Priority;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.ServiceLoader;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- * This class implements the (default) {@link ServiceContext} interface and hereby uses the JDK
- * {@link java.util.ServiceLoader} to load the services required.
- */
-public final class DefaultServiceContext implements ServiceContext {
- /**
- * List current services loaded, per class.
- */
- private final ConcurrentHashMap<Class<?>, List<Object>> servicesLoaded = new ConcurrentHashMap<>();
- /**
- * Singletons.
- */
- private final Map<Class<?>, Object> singletons = new ConcurrentHashMap<>();
-
- @Override
- public <T> T getService(Class<T> serviceType) {
- Object cached = singletons.get(serviceType);
- if (cached == null) {
- Collection<T> services = getServices(serviceType);
- if (services.isEmpty()) {
- cached = null;
- } else {
- cached = getServiceWithHighestPriority(services, serviceType);
- }
- if(cached!=null) {
- singletons.put(serviceType, cached);
- }
- }
- return serviceType.cast(cached);
- }
-
- /**
- * 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) {
- 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);
- }
- services = Collections.unmodifiableList(services);
- } catch (Exception e) {
- Logger.getLogger(DefaultServiceContext.class.getName()).log(Level.WARNING,
- "Error loading services current type " + serviceType, e);
- }
- 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 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; //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
- *
- * @return the service with the highest {@link javax.annotation.Priority#value()}
- *
- * @throws ConfigException if there are multiple service implementations with the maximum priority
- */
- private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) {
-
- // we do not need the priority stuff if the list contains only one element
- if (services.size() == 1) {
- return services.iterator().next();
- }
-
- Integer highestPriority = null;
- int highestPriorityServiceCount = 0;
- T highestService = null;
-
- for (T service : services) {
- int prio = getPriority(service);
- if (highestPriority == null || highestPriority < prio) {
- highestService = service;
- highestPriorityServiceCount = 1;
- highestPriority = prio;
- } else if (highestPriority == prio) {
- highestPriorityServiceCount++;
- }
- }
-
- if (highestPriorityServiceCount > 1) {
- throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}",
- highestPriorityServiceCount,
- serviceType.getName(),
- highestPriority,
- services));
- }
-
- return highestService;
- }
-
- @Override
- public int ordinal() {
- return 1;
- }
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIActivator.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIActivator.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIActivator.java
deleted file mode 100644
index df20d3b..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIActivator.java
+++ /dev/null
@@ -1,53 +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.builder.internal;
-
-
-
-import org.apache.tamaya.builder.spi.ServiceContextManager;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-
-import java.util.logging.Logger;
-
-/**
- * A bundle activator that registers the {@link OSGIServiceLoader}.
- */
-public class OSGIActivator implements BundleActivator {
-
- private static final Logger LOG = Logger.getLogger(OSGIActivator.class.getName());
-
- private OSGIServiceLoader serviceLoader;
-
- @Override
- public void start(BundleContext context) {
- // Register marker service
- ServiceContextManager.set(new OSGIServiceContext(context));
- LOG.info("Registered OSGI enabled ServiceContext...");
- serviceLoader = new OSGIServiceLoader();
- context.addBundleListener(serviceLoader);
- }
-
- @Override
- public void stop(BundleContext context) {
- if(serviceLoader!=null) {
- context.removeBundleListener(serviceLoader);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceComparator.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceComparator.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceComparator.java
deleted file mode 100644
index 2ee3ef5..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceComparator.java
+++ /dev/null
@@ -1,69 +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.builder.internal;
-
-import org.osgi.framework.ServiceReference;
-
-import javax.annotation.Priority;
-import java.util.Comparator;
-
-/**
- * Comparator implementation for odering services loaded based on their increasing priority values.
- */
-class OSGIServiceComparator implements Comparator<ServiceReference> {
-
- @Override
- public int compare(ServiceReference o1, ServiceReference o2) {
- int prio = getPriority(o1) - getPriority(o2);
- if (prio < 0) {
- return 1;
- } else if (prio > 0) {
- return -1;
- } else {
- return 0; //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) {
- return getPriority(o.getClass());
- }
-
- /**
- * Checks the given type optionally annotated with a @Priority. If present the annotation's value s evaluated.
- * If no such annotation is present, a default priority is returned (1);
- *
- * @param type the type, not null.
- * @return a priority, by default 1.
- */
- public static int getPriority(Class type) {
- int prio = 1;
- Priority priority = (Priority)type.getAnnotation(Priority.class);
- if (priority != null) {
- prio = priority.value();
- }
- return prio;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceContext.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceContext.java
deleted file mode 100644
index 05c830f..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceContext.java
+++ /dev/null
@@ -1,82 +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.builder.internal;
-
-import org.apache.tamaya.builder.spi.ServiceContext;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
-
-/**
- * ServiceContext implementation based on OSGI Service mechanisms.
- */
-public class OSGIServiceContext implements ServiceContext{
-
- private static final OSGIServiceComparator REF_COMPARATOR = new OSGIServiceComparator();
-
- private final BundleContext bundleContext;
-
- public OSGIServiceContext(BundleContext bundleContext){
- this.bundleContext = Objects.requireNonNull(bundleContext);
- }
-
- public boolean isInitialized(){
- return bundleContext != null;
- }
-
-
- @Override
- public int ordinal() {
- return 10;
- }
-
- @Override
- public <T> T getService(Class<T> serviceType) {
- ServiceReference<T> ref = this.bundleContext.getServiceReference(serviceType);
- if(ref!=null){
- return this.bundleContext.getService(ref);
- }
- return null;
- }
-
- @Override
- public <T> List<T> getServices(Class<T> serviceType) {
- List<ServiceReference<T>> refs = new ArrayList<>();
- try {
- refs.addAll(this.bundleContext.getServiceReferences(serviceType, null));
- Collections.sort(refs, REF_COMPARATOR);
- List<T> services = new ArrayList<>(refs.size());
- for(ServiceReference<T> ref:refs){
- T service = bundleContext.getService(ref);
- if(service!=null) {
- services.add(service);
- }
- }
- return services;
- } catch (InvalidSyntaxException e) {
- e.printStackTrace();
- return Collections.emptyList();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceLoader.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceLoader.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceLoader.java
deleted file mode 100644
index cf307d5..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/OSGIServiceLoader.java
+++ /dev/null
@@ -1,156 +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.builder.internal;
-
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleListener;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceFactory;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.util.tracker.ServiceTracker;
-
-/**
- * An bundle listener that registers services defined in META-INF/services, when a bundle is starting.
- *
- * @author anatole@apache.org
- */
-public class OSGIServiceLoader implements BundleListener {
- // Provide logging
- private static final Logger log = Logger.getLogger(OSGIServiceLoader.class.getName());
-
- private Map<Class, ServiceTracker<Object,Object>> services = new ConcurrentHashMap<>();
-
- @Override
- public void bundleChanged(BundleEvent bundleEvent) {
- // Parse and create metadta on STARTING
- if (bundleEvent.getType() == BundleEvent.STARTED) {
- Bundle bundle = bundleEvent.getBundle();
- if (bundle.getEntry("META-INF/services/") == null) {
- return;
- }
- Enumeration<String> entryPaths = bundle.getEntryPaths("META-INF/services/");
- while (entryPaths.hasMoreElements()) {
- String entryPath = entryPaths.nextElement();
- if(!entryPath.endsWith("/")) {
- processEntryPath(bundle, entryPath);
- }
- }
- }
- }
-
- private void processEntryPath(Bundle bundle, String entryPath) {
- try {
- String serviceName = entryPath.substring("META-INF/services/".length());
- Class<?> serviceClass = bundle.loadClass(serviceName);
-
- URL child = bundle.getEntry(entryPath);
- InputStream inStream = child.openStream();
-
- BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
- String implClassName = br.readLine();
- while (implClassName != null){
- int hashIndex = implClassName.indexOf("#");
- if (hashIndex > 0) {
- implClassName = implClassName.substring(0, hashIndex-1);
- }
- else if (hashIndex == 0) {
- implClassName = "";
- }
- implClassName = implClassName.trim();
- if (implClassName.length() > 0) {
- try {
- // Load the service class
- Class<?> implClass = bundle.loadClass(implClassName);
- if (!serviceClass.isAssignableFrom(implClass)) {
- log.warning("Configured service: " + implClassName + " is not assignble to " +
- serviceClass.getName());
- continue;
- }
- // Provide service properties
- Hashtable<String, String> props = new Hashtable<>();
- props.put(Constants.VERSION_ATTRIBUTE, bundle.getVersion().toString());
- String vendor = bundle.getHeaders().get(Constants.BUNDLE_VENDOR);
- props.put(Constants.SERVICE_VENDOR, (vendor != null ? vendor : "anonymous"));
- // Translate annotated @Priority into a service ranking
- props.put(Constants.SERVICE_RANKING,
- String.valueOf(PriorityServiceComparator.getPriority(implClass)));
-
- // Register the service factory on behalf of the intercepted bundle
- JDKUtilServiceFactory factory = new JDKUtilServiceFactory(implClass);
- BundleContext bundleContext = bundle.getBundleContext();
- bundleContext.registerService(serviceName, factory, props);
- }
- catch(Exception e){
- log.log(Level.SEVERE,
- "Failed to load service class using ServiceLoader logic: " + implClassName, e);
- }
- }
- implClassName = br.readLine();
- }
- br.close();
- }
- catch (RuntimeException rte) {
- throw rte;
- }
- catch (Exception e) {
- log.log(Level.SEVERE, "Failed to read services from: " + entryPath, e);
- }
- }
-
-
- /**
- * Service factory simply instantiating the configured service.
- */
- static class JDKUtilServiceFactory implements ServiceFactory
- {
- private final Class<?> serviceClass;
-
- public JDKUtilServiceFactory(Class<?> serviceClass) {
- this.serviceClass = serviceClass;
- }
-
- @Override
- public Object getService(Bundle bundle, ServiceRegistration registration) {
- try {
- return serviceClass.newInstance();
- }
- catch (Exception ex) {
- ex.printStackTrace();
- throw new IllegalStateException("Cannot instanciate service", ex);
- }
- }
-
- @Override
- public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7144e3f9/modules/builder/src/main/java/org/apache/tamaya/builder/internal/PriorityServiceComparator.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/PriorityServiceComparator.java b/modules/builder/src/main/java/org/apache/tamaya/builder/internal/PriorityServiceComparator.java
deleted file mode 100644
index c7e3656..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/internal/PriorityServiceComparator.java
+++ /dev/null
@@ -1,67 +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.builder.internal;
-
-import javax.annotation.Priority;
-import java.util.Comparator;
-
-/**
- * Comparator implementation for odering services loaded based on their increasing priority values.
- */
-public class PriorityServiceComparator implements Comparator<Object> {
-
- @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) {
- return getPriority(o.getClass());
- }
-
- /**
- * Checks the given type optionally annotated with a @Priority. If present the annotation's value s evaluated.
- * If no such annotation is present, a default priority is returned (1);
- *
- * @param type the type, not null.
- * @return a priority, by default 1.
- */
- public static int getPriority(Class type) {
- int prio = 1;
- Priority priority = (Priority)type.getAnnotation(Priority.class);
- if (priority != null) {
- prio = priority.value();
- }
- return prio;
- }
-}