You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2016/09/06 17:18:44 UTC

[48/50] [abbrv] incubator-tamaya-sandbox git commit: TAMAYA-145: Added logic for dynamic ordering of PropertySources.

TAMAYA-145: Added logic for dynamic ordering of PropertySources.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/97b5f438
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/97b5f438
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/97b5f438

Branch: refs/heads/master
Commit: 97b5f438249f740b443c6b3cf96f5bd7dff6cb88
Parents: 0b33f92
Author: anatole <an...@apache.org>
Authored: Wed May 11 04:20:26 2016 +0200
Committer: anatole <an...@apache.org>
Committed: Wed May 11 04:20:26 2016 +0200

----------------------------------------------------------------------
 .../tamaya/dsl/WrappedPropertySource.java       | 100 +++++++++++++++++++
 .../DSLLoadingConfigurationProviderSpi.java     |  82 +++++++++++++++
 .../NamedDSLPropertySourceProvider.java         |  53 ++++++++++
 .../ResourceDSLPropertySourceProvider.java      |  82 +++++++++++++++
 ...g.apache.tamaya.spi.ConfigurationProviderSpi |  19 ++++
 ....tamaya.staged.spi.DSLPropertySourceProvider |  20 ++++
 6 files changed, 356 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/97b5f438/metamodels/staged/src/main/java/org/apache/tamaya/dsl/WrappedPropertySource.java
----------------------------------------------------------------------
diff --git a/metamodels/staged/src/main/java/org/apache/tamaya/dsl/WrappedPropertySource.java b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/WrappedPropertySource.java
new file mode 100644
index 0000000..15e6089
--- /dev/null
+++ b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/WrappedPropertySource.java
@@ -0,0 +1,100 @@
+/*
+ * 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.dsl;
+
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertyValue;
+
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Wrapped property source that allows dynamically reassigning the property source's
+ * ordinal value. This is needed for reordering the property sources to
+ * match the DSL configured ordering.
+ */
+final class WrappedPropertySource implements PropertySource {
+
+    private Integer ordinalAssigned;
+    private PropertySource wrapped;
+
+    private WrappedPropertySource(PropertySource wrapped){
+        this.wrapped = Objects.requireNonNull(wrapped);
+    }
+
+    /**
+     * Wraps a given property source.
+     * @param propertySource the property source to be wrapped.
+     * @return a wrapped property source.
+     */
+    public static WrappedPropertySource of(PropertySource propertySource){
+        if(propertySource instanceof WrappedPropertySource){
+            return (WrappedPropertySource)propertySource;
+        }
+        return new WrappedPropertySource(propertySource);
+    }
+
+    @Override
+    public int getOrdinal() {
+        return ordinalAssigned!=null?ordinalAssigned.intValue():wrapped.getOrdinal();
+    }
+
+    /**
+     * Applies the given ordinal to the instance.
+     * @param ordinal the new ordinal.
+     */
+    public void setOrdinal(int ordinal){
+        this.ordinalAssigned = ordinal;
+    }
+
+    /**
+     * Resetting the ordinal to the one of the wrapped property source.
+     */
+    public void resetOrdinal(){
+        this.ordinalAssigned = null;
+    }
+
+    @Override
+    public String getName() {
+        return wrapped.getName();
+    }
+
+    @Override
+    public PropertyValue get(String key) {
+        return wrapped.get(key);
+    }
+
+    @Override
+    public Map<String, String> getProperties() {
+        return wrapped.getProperties();
+    }
+
+    @Override
+    public boolean isScannable() {
+        return wrapped.isScannable();
+    }
+
+    @Override
+    public String toString() {
+        return "WrappedPropertySource{" +
+                "ordinalAssigned=" + ordinalAssigned +
+                ", wrapped=" + wrapped +
+                '}';
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/97b5f438/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/DSLLoadingConfigurationProviderSpi.java
----------------------------------------------------------------------
diff --git a/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/DSLLoadingConfigurationProviderSpi.java b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/DSLLoadingConfigurationProviderSpi.java
new file mode 100644
index 0000000..8538609
--- /dev/null
+++ b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/DSLLoadingConfigurationProviderSpi.java
@@ -0,0 +1,82 @@
+/*
+ * 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.dsl.internal;
+
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.dsl.TamayaConfigurator;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
+import org.apache.tamaya.spi.ConfigurationProviderSpi;
+import org.apache.tamaya.spi.ServiceContextManager;
+import org.apache.tamaya.spisupport.DefaultConfiguration;
+import org.apache.tamaya.spisupport.DefaultConfigurationContext;
+
+import javax.annotation.Priority;
+
+/**
+ * ConfigurationContext that uses {@link TamayaConfigurator} to configure the
+ * Tamaya runtime context.
+ */
+@Priority(10)
+public class DSLLoadingConfigurationProviderSpi implements ConfigurationProviderSpi{
+
+    private boolean configured;
+
+    private ConfigurationContext context = new DefaultConfigurationContext();
+    private Configuration config = new DefaultConfiguration(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;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        checkInitialized();
+        return config;
+    }
+
+    @Override
+    public ConfigurationContext getConfigurationContext() {
+        checkInitialized();
+        return context;
+    }
+
+    private void checkInitialized() {
+        if(!configured){
+            synchronized (context) {
+                TamayaConfigurator.getInstance().configure();
+                configured = true;
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/97b5f438/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/NamedDSLPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/NamedDSLPropertySourceProvider.java b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/NamedDSLPropertySourceProvider.java
new file mode 100644
index 0000000..0bc71da
--- /dev/null
+++ b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/NamedDSLPropertySourceProvider.java
@@ -0,0 +1,53 @@
+/*
+ * 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.dsl.internal;
+
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.staged.spi.DSLPropertySourceProvider;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
+
+/**
+ * DSL provider implementation that allows to use registered {@link PropertySource} instances
+ * by matching {@link PropertySource#getName()} with the configured value and overriding the
+ * {@link PropertySource#getOrdinal()} method.
+ */
+public class NamedDSLPropertySourceProvider implements DSLPropertySourceProvider{
+
+    private static final Logger LOG = Logger.getLogger(NamedDSLPropertySourceProvider.class.getName());
+
+    @Override
+    public List<PropertySource> resolve(String sourceExpression, Map<String, PropertySource> defaultPropertySources) {
+        List<PropertySource> result = new ArrayList<>();
+        PropertySource ps = defaultPropertySources.get(sourceExpression);
+        if(ps==null){
+            LOG.warning("No such property source provider found: " + sourceExpression);
+        }
+        result.add(ps);
+        return result;
+    }
+
+    @Override
+    public String getKey() {
+        return "named:";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/97b5f438/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/ResourceDSLPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/ResourceDSLPropertySourceProvider.java b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/ResourceDSLPropertySourceProvider.java
new file mode 100644
index 0000000..394562d
--- /dev/null
+++ b/metamodels/staged/src/main/java/org/apache/tamaya/dsl/internal/ResourceDSLPropertySourceProvider.java
@@ -0,0 +1,82 @@
+/*
+ * 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.dsl.internal;
+
+import org.apache.tamaya.dsl.DSLFormatManager;
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormat;
+import org.apache.tamaya.resource.ConfigResources;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spisupport.MapPropertySource;
+import org.apache.tamaya.staged.spi.DSLPropertySourceProvider;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * DSL provider implementation that allows to use registered {@link PropertySource} instances
+ * by matching {@link PropertySource#getName()} with the configured value and overriding the
+ * {@link PropertySource#getOrdinal()} method.
+ */
+public class ResourceDSLPropertySourceProvider implements DSLPropertySourceProvider{
+
+    private static final Logger LOG = Logger.getLogger(ResourceDSLPropertySourceProvider.class.getName());
+
+    @Override
+    public List<PropertySource> resolve(String sourceExpression, Map<String, PropertySource> defaultPropertySources) {
+        List<PropertySource> result = new ArrayList<>();
+        List<URL> resources = new ArrayList<>();
+        if(sourceExpression.contains("SUFFIX")) {
+            for (String suffix : DSLFormatManager.getInstance().getSuffixes()) {
+                Collection<URL> locations = ConfigResources.getResourceResolver().getResources(getClass().getClassLoader(),
+                        sourceExpression.replace("SUFFIX", suffix));
+                loadPropertySources(locations, result);
+            }
+        }else {
+            Collection<URL> locations = ConfigResources.getResourceResolver().getResources(getClass().getClassLoader(),
+                    sourceExpression);
+            loadPropertySources(locations, result);
+        }
+        return result;
+    }
+
+    private void loadPropertySources(Collection<URL> locations, List<PropertySource> result) {
+        for(URL url:locations){
+            for(ConfigurationFormat format: DSLFormatManager.getInstance().getFormats()) {
+                try(InputStream is = url.openStream()){
+                    ConfigurationData data = format.readConfiguration(url.toString(),is);
+                    result.add(new MapPropertySource(url.toString(), data.getCombinedProperties()));
+                }catch(Exception e){
+                    LOG.log(Level.FINEST, "Format failed: " + format.getName() + " for " + url, e);
+                }
+            }
+        }
+    }
+
+    @Override
+    public String getKey() {
+        return "resource:";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/97b5f438/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
----------------------------------------------------------------------
diff --git a/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
new file mode 100644
index 0000000..8ef0643
--- /dev/null
+++ b/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
@@ -0,0 +1,19 @@
+#
+# 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 current 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.
+#
+org.apache.tamaya.dsl.internal.DSLLoadingConfigurationProviderSpi
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/97b5f438/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.staged.spi.DSLPropertySourceProvider
----------------------------------------------------------------------
diff --git a/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.staged.spi.DSLPropertySourceProvider b/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.staged.spi.DSLPropertySourceProvider
new file mode 100644
index 0000000..e80367e
--- /dev/null
+++ b/metamodels/staged/src/main/resources/META-INF/services/org.apache.tamaya.staged.spi.DSLPropertySourceProvider
@@ -0,0 +1,20 @@
+#
+# 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 current 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.
+#
+org.apache.tamaya.dsl.internal.NamedDSLPropertySourceProvider
+org.apache.tamaya.dsl.internal.ResourceDSLPropertySourceProvider
\ No newline at end of file