Repository: incubator-tamaya Updated Branches: refs/heads/master 1b6cf9647 -> 4f8d167be
Simplified and fixed bugs in environment module. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/cf7980e3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/cf7980e3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/cf7980e3 Branch: refs/heads/master Commit: cf7980e399d4aa7a6570b3acd6d209b2fc80f07e Parents: 1b6cf96 Author: anatole <anat...@apache.org> Authored: Thu May 21 18:44:19 2015 +0200 Committer: anatole <anat...@apache.org> Committed: Thu May 21 18:44:19 2015 +0200 ---------------------------------------------------------------------- sandbox/environment/pom.xml | 20 ++++ .../environment/BuildableRuntimeContext.java | 16 --- .../tamaya/environment/RuntimeContext.java | 14 --- .../environment/RuntimeContextBuilder.java | 23 +--- .../environment/RuntimeContextProvider.java | 11 +- ...ctClassLoaderDependentRuntimeContextSpi.java | 115 +++++++++++++++++++ ...ssLoaderDependentAppEnvironmentProvider.java | 40 +++++++ ...DependentApplicationEnvironmentProvider.java | 103 ----------------- ...ssLoaderDependentEarEnvironmentProvider.java | 83 ++----------- .../internal/InitialEnvironmentProvider.java | 73 ------------ .../internal/InitialEnvironmentProviderSpi.java | 74 ++++++++++++ .../internal/SingleEnvironmentManager.java | 35 +++--- .../SystemClassLoaderEnvironmentProvider.java | 72 ------------ ...SystemClassLoaderEnvironmentProviderSpi.java | 88 ++++++++++++++ .../environment/spi/ContextDataProvider.java | 42 ------- .../environment/spi/ContextProviderSpi.java | 45 ++++++++ .../tamaya/environment/spi/ContextSpi.java | 6 - .../environment/EnvironmentManagerTest.java | 66 ----------- .../RuntimeContextProviderSpiTest.java | 57 +++++++++ .../TestEnvironmentManagerSingleton.java | 12 +- .../environment/TestEnvironmentProvider.java | 17 ++- ...he.tamaya.environment.spi.ContextProviderSpi | 23 ++++ ...org.apache.tamaya.environment.spi.ContextSpi | 2 +- 23 files changed, 506 insertions(+), 531 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/pom.xml ---------------------------------------------------------------------- diff --git a/sandbox/environment/pom.xml b/sandbox/environment/pom.xml index 7773c43..666140d 100644 --- a/sandbox/environment/pom.xml +++ b/sandbox/environment/pom.xml @@ -34,9 +34,29 @@ under the License. <dependencies> <dependency> <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-api</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-formats</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-resource</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/BuildableRuntimeContext.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/BuildableRuntimeContext.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/BuildableRuntimeContext.java index 94d0125..f7675bb 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/BuildableRuntimeContext.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/BuildableRuntimeContext.java @@ -28,8 +28,6 @@ import java.util.TreeMap; class BuildableRuntimeContext implements RuntimeContext { /** The context id, never null or empty. */ private String contextId; - /** The parent context. */ - private RuntimeContext parentContext; /** * The environment data. @@ -44,7 +42,6 @@ class BuildableRuntimeContext implements RuntimeContext { BuildableRuntimeContext(RuntimeContextBuilder builder) { Objects.requireNonNull(builder); this.contextId = builder.contextId; - this.parentContext = builder.parentContext; context.putAll(builder.contextData); } @@ -59,19 +56,6 @@ class BuildableRuntimeContext implements RuntimeContext { } @Override - public String getQualifiedContextId() { - if(parentContext!=null){ - return parentContext.getQualifiedContextId()+"/"+contextId; - } - return contextId; - } - - @Override - public RuntimeContext getParentContext() { - return parentContext; - } - - @Override public String get(String key) { return context.get(key); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContext.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContext.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContext.java index b782667..ca98432 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContext.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContext.java @@ -44,20 +44,6 @@ public interface RuntimeContext { String getContextId(); /** - * Returns a full (and unique) context id that identifies the current context. Depending on the environment isolation this - * can be always the same key (e.g. in a SE use case) or a varying key depending on the current classloader - * visible (OSGI, EE environment). - * @return the context id, never null. - */ - String getQualifiedContextId(); - - /** - * Access the parent context. - * @return the parent context for this instance, or null, if this is a root context. - */ - RuntimeContext getParentContext(); - - /** * Access a runtime context variable. * @param key the key * @return the corresponding value. http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextBuilder.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextBuilder.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextBuilder.java index e5ceb29..4fb3f02 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextBuilder.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextBuilder.java @@ -29,8 +29,6 @@ public final class RuntimeContextBuilder { /** The context id, never null or empty. */ String contextId; - /** The parent context. */ - RuntimeContext parentContext; /** THe environment data. */ Map<String,String> contextData = new HashMap<>(); @@ -38,9 +36,8 @@ public final class RuntimeContextBuilder { /** * Constructor. */ - private RuntimeContextBuilder(String contextId, RuntimeContext parentContext) { + private RuntimeContextBuilder(String contextId) { this.contextId = Objects.requireNonNull(contextId); - this.parentContext = parentContext; } /** @@ -48,24 +45,16 @@ public final class RuntimeContextBuilder { * @return the new builder instance. */ public static RuntimeContextBuilder of(String contextId) { - return new RuntimeContextBuilder(contextId, null); + return new RuntimeContextBuilder(contextId); } /** - * Creates a new buildr instance. - * @return the new builder instance. - */ - public static RuntimeContextBuilder of(String contextId, RuntimeContext parentContext) { - return new RuntimeContextBuilder(contextId, parentContext); - } - - /** - * Sets a new parent context. - * @param parentContext the parent context (can be null). + * Sets the environment contextId. + * @param contextId the contextId, not null. * @return the builder for chaining */ - public RuntimeContextBuilder withParentContext(RuntimeContext parentContext){ - this.parentContext = parentContext; + public RuntimeContextBuilder setContextId(String contextId){ + this.contextId = Objects.requireNonNull(contextId); return this; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextProvider.java index 9b37467..565b255 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextProvider.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/RuntimeContextProvider.java @@ -29,7 +29,7 @@ public final class RuntimeContextProvider { private static final ContextSpi contextSpi = loadSpi(); private static ContextSpi loadSpi(){ - return ServiceContext.getInstance().getService(ContextSpi.class); + return ServiceContext.getInstance().getService(ContextSpi.class).get(); } /** @@ -46,13 +46,4 @@ public final class RuntimeContextProvider { return contextSpi.getCurrentContext(); } - /** - * Get the current {@link org.apache.tamaya.environment.RuntimeContextProvider}. The environment is used to determine the current runtime state, which - * is important for returning the correct configuration. - * @return the current Environment, never null. - */ - public static RuntimeContext root(){ - return contextSpi.getRootContext(); - } - } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/AbstractClassLoaderDependentRuntimeContextSpi.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/AbstractClassLoaderDependentRuntimeContextSpi.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/AbstractClassLoaderDependentRuntimeContextSpi.java new file mode 100644 index 0000000..de92e78 --- /dev/null +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/AbstractClassLoaderDependentRuntimeContextSpi.java @@ -0,0 +1,115 @@ +/* + * 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.environment.internal; + +import org.apache.tamaya.environment.RuntimeContext; +import org.apache.tamaya.environment.RuntimeContextBuilder; +import org.apache.tamaya.environment.spi.ContextProviderSpi; +import org.apache.tamaya.format.ConfigurationFormat; +import org.apache.tamaya.format.ConfigurationFormats; +import org.apache.tamaya.resource.ConfigResources; + +import java.io.InputStream; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Application environment provider that is dependent on the current context classloader and tries to + * evaluate {@code META-INF/env/application.properties, META-INF/env/application.xml and META-INF/env/application.ini}. + * Only if a property named {@code org.apache.tamaya.env.applicationId} is found, it will + * be used as the {@code environmentId} and a corresponding {@link org.apache.tamaya.environment.RuntimeContext} instance + * is created and attached. + */ +public abstract class AbstractClassLoaderDependentRuntimeContextSpi implements ContextProviderSpi { + + private static final Logger LOG = Logger.getLogger(AbstractClassLoaderDependentRuntimeContextSpi.class.getName()); + + private String contextId; + private Map<ClassLoader, Map<String, String>> contexts = new ConcurrentHashMap<>(); + private Map<ClassLoader, Boolean> contextsAvailable = new ConcurrentHashMap<>(); + + protected AbstractClassLoaderDependentRuntimeContextSpi(String contextId) { + this.contextId = Objects.requireNonNull(contextId); + } + + private boolean isActive() { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (cl == null) { + return false; + } + Boolean available = this.contextsAvailable.get(cl); + if (available != null) { + return available; + } + return true; + } + + protected Collection<URL> getConfigLocations() { + return ConfigResources.getResourceResolver().getResources(Thread.currentThread().getContextClassLoader(), + "classpath:META-INF/context/" + contextId + ".properties", "classpath:META-INF/context/\"+contextId+\".xml", "classpath:META-INF/context/\"+contextId+\".ini"); + } + + protected List<ConfigurationFormat> getConfigFormats(URL url) { + return ConfigurationFormats.getFormats(url); + } + + @Override + public void setupContext(RuntimeContextBuilder contextBuilder) { + if (isActive()) { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (cl == null) { + return; + } + Map<String, String> data = this.contexts.get(cl); + if (data == null) { + Collection<URL> propertyUris = getConfigLocations(); + data = new HashMap<>(); + for (URL resource : propertyUris) { + for (ConfigurationFormat format : getConfigFormats(resource)) { + try (InputStream is = resource.openStream()) { + data.putAll(format.readConfiguration(resource.toExternalForm(), is).getDefaultSection()); + } catch (Exception e) { + LOG.log(Level.SEVERE, e, () -> "Error reading application context data fromMap " + resource); + } + } + } + data.put("classloader.type", cl.getClass().getName()); + data.put("classloader.info", cl.toString()); + Set<URL> uris = new HashSet<>(); + uris.addAll(propertyUris); + data.put("context.sources", uris.toString()); + data = Collections.unmodifiableMap(data); + contextBuilder.setContextId(contextId); + this.contexts.put(cl, data); + } + contextBuilder.setAll(data); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentAppEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentAppEnvironmentProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentAppEnvironmentProvider.java new file mode 100644 index 0000000..7268f5e --- /dev/null +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentAppEnvironmentProvider.java @@ -0,0 +1,40 @@ +/* + * 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.environment.internal; + + +import javax.annotation.Priority; + +/** + * This class implements a {@link org.apache.tamaya.environment.RuntimeContextProvider} that tries + * to read configuration for an ear deployment located under {@code META-INF/env/ear.properties, + * META-INF/env/ear.xml or META-INF/env/ear.ini}. The environment id hereby is defined by a + * configuration entry named {@code org.apache.tamaya.core.env.earId}. + * + * Only if such a configuration with such an {@code earId} is found an {@link org.apache.tamaya.environment.RuntimeContext} + * is created and attached to the corresponding ear classloader. + */ +@Priority(3000) +public class ClassLoaderDependentAppEnvironmentProvider extends AbstractClassLoaderDependentRuntimeContextSpi{ + + public ClassLoaderDependentAppEnvironmentProvider(){ + super("app"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java deleted file mode 100644 index e130644..0000000 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java +++ /dev/null @@ -1,103 +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.environment.internal; - -import java.net.URL; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.core.config.ConfigurationFormats; -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.core.resource.ResourceLoader; -import org.apache.tamaya.core.properties.ConfigurationFormat; -import org.apache.tamaya.environment.spi.ContextDataProvider; -import org.apache.tamaya.metamodel.environment.spi.EnvironmentProvider; -import org.apache.tamaya.spi.ServiceContext; - -/** - * Application environment provider that is dependent on the current context classloader and tries to - * evaluate {@code META-INF/env/application.properties, META-INF/env/application.xml and META-INF/env/application.ini}. - * Only if a property named {@code org.apache.tamaya.env.applicationId} is found, it will - * be used as the {@code environmentId} and a corresponding {@link org.apache.tamaya.metamodel.environment.RuntimeContext} instance - * is created and attached. - */ -public class ClassLoaderDependentApplicationEnvironmentProvider implements ContextDataProvider { - - private static final Logger LOG = Logger.getLogger(ClassLoaderDependentApplicationEnvironmentProvider.class.getName()); - - private Map<ClassLoader, Map<String,String>> contexts = new ConcurrentHashMap<>(); - private Map<ClassLoader, Boolean> contextsAvailable = new ConcurrentHashMap<>(); - - @Override - public boolean isActive() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - return false; - } - Boolean available = this.contextsAvailable.get(cl); - if(available!=null && !available){ - return false; - } - List<URL> propertyUris = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, - "classpath:META-INF/context/application.properties", "classpath:META-INF/context/application.xml", "classpath:META-INF/context/application.ini"); - available = !propertyUris.isEmpty(); - this.contextsAvailable.put(cl, available); - return available; - } - - @Override - public Map<String,String> getEnvironmentData() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - return null; - } - Map<String,String> data = this.contexts.get(cl); - if(data!=null){ - return data; - } - List<URL> propertyUris = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, - "classpath:META-INF/context/application.properties", "classpath:META-INF/context/application.xml", "classpath:META-INF/context/application.ini"); - data = new HashMap<>(); - - for(URL resource:propertyUris){ - try{ - ConfigurationFormat format = ConfigurationFormats.getFormat(resource); - data.putAll(format.readConfiguration(resource)); - } - catch(Exception e){ - LOG.log(Level.SEVERE, e, () -> "Error reading application context data fromMap " + resource); - } - } - data.put("classloader.type", cl.getClass().getName()); - data.put("classloader.info", cl.toString()); - Set<URL> uris = new HashSet<>(); - uris.addAll(propertyUris); - data.put("context.sources", uris.toString()); - data = Collections.unmodifiableMap(data); - this.contexts.put(cl, data); - return data; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java index b4160e2..688fc8c 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java @@ -18,92 +18,23 @@ */ package org.apache.tamaya.environment.internal; -import org.apache.tamaya.core.config.ConfigurationFormats; -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.metamodel.environment.RuntimeContextBuilder; -import org.apache.tamaya.metamodel.environment.spi.EnvironmentProvider; -import org.apache.tamaya.spi.ServiceContext; -import org.apache.tamaya.core.properties.ConfigurationFormat; -import org.apache.tamaya.core.resource.ResourceLoader; - -import java.net.URL; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; +import javax.annotation.Priority; /** - * This class implements a {@link EnvironmentProvider} that tries + * This class implements a {@link org.apache.tamaya.environment.RuntimeContextProvider} that tries * to read configuration for an ear deployment located under {@code META-INF/env/ear.properties, * META-INF/env/ear.xml or META-INF/env/ear.ini}. The environment id hereby is defined by a * configuration entry named {@code org.apache.tamaya.core.env.earId}. * - * Only if such a configuration with such an {@code earId} is found an {@link org.apache.tamaya.metamodel.environment.RuntimeContext} + * Only if such a configuration with such an {@code earId} is found an {@link org.apache.tamaya.environment.RuntimeContext} * is created and attached to the corresponding ear classloader. */ -public class ClassLoaderDependentEarEnvironmentProvider implements EnvironmentProvider { - - private static final Logger LOG = Logger.getLogger(ClassLoaderDependentEarEnvironmentProvider.class.getName()); - -// private static final String EARID_PROP = "environment.earId"; - - private Map<ClassLoader, Map<String,String>> contexts = new ConcurrentHashMap<>(); - private Map<ClassLoader, Boolean> contextsAvailable = new ConcurrentHashMap<>(); - - @Override - public boolean isActive() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - return false; - } - Boolean available = this.contextsAvailable.get(cl); - if(available!=null && !available){ - return false; - } - List<URL> propertyUris = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, - "classpath:META-INF/context/ear.properties", "classpath:META-INF/context/ear.xml", "classpath:META-INF/context/ear.ini"); - available = !propertyUris.isEmpty(); - this.contextsAvailable.put(cl, available); - return available; - } +@Priority(2000) +public class ClassLoaderDependentEarEnvironmentProvider extends AbstractClassLoaderDependentRuntimeContextSpi{ - @Override - public Map<String,String> getEnvironmentData() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - return null; - } - Map<String,String> data = this.contexts.get(cl); - if(data!=null){ - return data; - } - List<URL> resources = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, - "classpath:META-INF/context/ear.properties", "classpath:META-INF/context/ear.xml", "classpath:META-INF/context/ear.ini"); - data = new HashMap<>(); - for(URL resource:resources){ - try{ - ConfigurationFormat format = ConfigurationFormats.getFormat(resource); - Map<String,String> read = format.readConfiguration(resource); - data.putAll(read); - } - catch(Exception e){ - LOG.log(Level.SEVERE, e, () -> "Error reading ear context data fromMap " + resource); - } - } -// String earId = data.getOrDefault(EARID_PROP, cl.toString()); - String stageValue = data.get(RuntimeContextBuilder.STAGE_PROP); - if (stageValue != null) { - data.put(RuntimeContextBuilder.STAGE_PROP,stageValue); - } - data.put("classloader.type", cl.getClass().getName()); - data.put("classloader.info", cl.toString()); - Set<URL> resourceSet = new HashSet<>(); - resourceSet.addAll(resources); - data.put("context.sources", resourceSet.toString()); - data = Collections.unmodifiableMap(data); - this.contexts.put(cl, data); - return data; + public ClassLoaderDependentEarEnvironmentProvider(){ + super("ear"); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProvider.java deleted file mode 100644 index 062becc..0000000 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProvider.java +++ /dev/null @@ -1,73 +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.environment.internal; - -import java.net.InetAddress; -import java.util.Collections; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.TimeZone; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.environment.RuntimeContext; -import org.apache.tamaya.environment.spi.ContextDataProvider; -import org.apache.tamaya.environment.RuntimeContextBuilder; - -/** - * Default {@link org.apache.tamaya.environment.RuntimeContext}. - */ -public final class InitialEnvironmentProvider implements ContextDataProvider{ - - private static final String STAGE_PROP = "env.STAGE"; - private Map<String,String> contextData = new HashMap<>(); - - public InitialEnvironmentProvider() { - try { - contextData.put("host", InetAddress.getLocalHost().toString()); - } catch (Exception e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, e, () -> "Failed to evaluate hostname."); - } - contextData.put("timezone", TimeZone.getDefault().getID()); - contextData.put("locale", Locale.getDefault().toString()); - // Copy all env properties.... - for (Entry<String, String> en : System.getenv().entrySet()) { - contextData.put(en.getKey(), en.getValue()); - } - String value = System.getProperty(STAGE_PROP); - if(value==null) { - value = System.getenv(STAGE_PROP); - } - if(value==null){ - value = "DEVELOPMENT"; - } - contextData.put(STAGE_PROP, value); - contextData = Collections.unmodifiableMap(contextData); - } - - - @Override - public RuntimeContext getContext(RuntimeContext currentContext) { - return RuntimeContextBuilder.of("root").withParentContext(currentContext) - .setAll(contextData).build(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProviderSpi.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProviderSpi.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProviderSpi.java new file mode 100644 index 0000000..c1b1001 --- /dev/null +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/InitialEnvironmentProviderSpi.java @@ -0,0 +1,74 @@ +/* + * 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.environment.internal; + +import java.net.InetAddress; +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TimeZone; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.tamaya.environment.RuntimeContext; +import org.apache.tamaya.environment.spi.ContextProviderSpi; +import org.apache.tamaya.environment.RuntimeContextBuilder; + +import javax.annotation.Priority; + +/** + * Default {@link org.apache.tamaya.environment.RuntimeContext}. + */ +@Priority(0) +public final class InitialEnvironmentProviderSpi implements ContextProviderSpi { + + private static final String STAGE_PROP = "env.STAGE"; + private Map<String,String> contextData = new HashMap<>(); + + public InitialEnvironmentProviderSpi() { + try { + contextData.put("host", InetAddress.getLocalHost().toString()); + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, e, () -> "Failed to evaluate hostname."); + } + contextData.put("timezone", TimeZone.getDefault().getID()); + contextData.put("locale", Locale.getDefault().toString()); + // Copy all env properties.... + for (Entry<String, String> en : System.getenv().entrySet()) { + contextData.put(en.getKey(), en.getValue()); + } + String value = System.getProperty(STAGE_PROP); + if(value==null) { + value = System.getenv(STAGE_PROP); + } + if(value==null){ + value = "DEVELOPMENT"; + } + contextData.put(STAGE_PROP, value); + contextData = Collections.unmodifiableMap(contextData); + } + + @Override + public void setupContext(RuntimeContextBuilder contextBuilder) { + contextBuilder.setAll(contextData); + contextBuilder.setContextId("root"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SingleEnvironmentManager.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SingleEnvironmentManager.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SingleEnvironmentManager.java index 7ed63b8..d81b8bb 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SingleEnvironmentManager.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SingleEnvironmentManager.java @@ -19,11 +19,14 @@ package org.apache.tamaya.environment.internal; +import org.apache.tamaya.ConfigException; import org.apache.tamaya.environment.RuntimeContext; import org.apache.tamaya.environment.RuntimeContextBuilder; +import org.apache.tamaya.environment.spi.ContextProviderSpi; import org.apache.tamaya.spi.ServiceContext; -import java.util.*; +import java.util.ArrayList; +import java.util.List; /** * Service for accessing {@link org.apache.tamaya.environment.RuntimeContext}. Environments are used to @@ -34,35 +37,25 @@ import java.util.*; * <li>and behaves contextual. * </ul> */ -public class SingleEnvironmentManager implements org.apache.tamaya.environment.spi.ContextSpi{ +public class SingleEnvironmentManager implements org.apache.tamaya.environment.spi.ContextSpi { - private final List<EnvironmentProvider> environmentProviders = loadEnvironmentProviders(); - private RuntimeContext rootEnvironment = getCurrentEnvironment(); + private final List<ContextProviderSpi> environmentProviders = loadEnvironmentProviders(); - private List<EnvironmentProvider> loadEnvironmentProviders() { - List<EnvironmentProvider> providerList = new ArrayList<>(); - for(EnvironmentProvider prov: ServiceContext.getInstance().getServices(EnvironmentProvider.class)){ + private List<ContextProviderSpi> loadEnvironmentProviders() { + List<ContextProviderSpi> providerList = new ArrayList<>(); + for (ContextProviderSpi prov : ServiceContext.getInstance().getServices(ContextProviderSpi.class)) { providerList.add(prov); } return providerList; } @Override - public RuntimeContext getCurrentEnvironment(){ - RuntimeContextBuilder b = RuntimeContextBuilder.of(); - for(EnvironmentProvider prov: environmentProviders){ - if(prov.isActive()){ - if(prov.isActive()){ - b.setAll(prov.getEnvironmentData()); - } - } + public RuntimeContext getCurrentContext() { + RuntimeContextBuilder builder = RuntimeContextBuilder.of("unknown"); + for (ContextProviderSpi prov : environmentProviders) { + prov.setupContext(builder); } - return b.build(); - } - - @Override - public RuntimeContext getRootEnvironment(){ - return rootEnvironment; + return builder.build(); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProvider.java deleted file mode 100644 index f692b95..0000000 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProvider.java +++ /dev/null @@ -1,72 +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.metamodel.environment.internal; - -import org.apache.tamaya.core.config.ConfigurationFormats; -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.environment.spi.ContextDataProvider; -import org.apache.tamaya.metamodel.environment.spi.EnvironmentProvider; -import org.apache.tamaya.spi.ServiceContext; -import org.apache.tamaya.core.properties.ConfigurationFormat; -import org.apache.tamaya.core.resource.ResourceLoader; - - -import java.net.URL; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * System environment provider (loaded only once using the system class loader) that loads additional environment properties fromMap the classpath evaluating - * {@code META-INF/env/system.properties, META-INF/env/system.xml and META-INF/env/system.ini}. - */ -public class SystemClassLoaderEnvironmentProvider implements ContextDataProvider { - - private static final Logger LOG = Logger.getLogger(SystemClassLoaderEnvironmentProvider.class.getName()); - - private Map<String,String> data = new HashMap<>(); - - - public SystemClassLoaderEnvironmentProvider(){ - List<URL> propertyResources = Resource.getResources(ClassLoader.getSystemClassLoader(), - "classpath:META-INF/env/system.properties", "classpath:META-INF/env/system.xml", "classpath:META-INF/env/system.ini"); - for(URL resource:propertyResources){ - try{ - ConfigurationFormat format = ConfigurationFormats.getFormat(resource); - Map<String,String> data = format.readConfiguration(resource); - data.putAll(data); - } - catch(Exception e){ - LOG.log(Level.INFO, e, () -> "Could not read environment data from " + resource); - } - } - data.put("classloader.type", ClassLoader.getSystemClassLoader().getClass().getName()); - data.put("classloader.info", ClassLoader.getSystemClassLoader().toString()); - Set<URL> resourceSet = new HashSet<>(); - resourceSet.addAll(propertyResources); - data.put("environment.system.sources", resourceSet.toString()); - this.data = Collections.unmodifiableMap(data); - } - - @Override - public Map<String,String> getContextData() { - return data; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProviderSpi.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProviderSpi.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProviderSpi.java new file mode 100644 index 0000000..7eb9cd1 --- /dev/null +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/internal/SystemClassLoaderEnvironmentProviderSpi.java @@ -0,0 +1,88 @@ +/* + * 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.environment.internal; + +import org.apache.tamaya.environment.RuntimeContextBuilder; +import org.apache.tamaya.environment.spi.ContextProviderSpi; +import org.apache.tamaya.format.ConfigurationFormat; +import org.apache.tamaya.format.ConfigurationFormats; +import org.apache.tamaya.resource.ConfigResources; + +import javax.annotation.Priority; +import java.io.InputStream; +import java.net.URL; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * System environment provider (loaded only once using the system class loader) that loads additional environment properties fromMap the classpath evaluating + * {@code META-INF/env/system.properties, META-INF/env/system.xml and META-INF/env/system.ini}. + */ +@Priority(1000) +public class SystemClassLoaderEnvironmentProviderSpi implements ContextProviderSpi { + + private static final Logger LOG = Logger.getLogger(SystemClassLoaderEnvironmentProviderSpi.class.getName()); + + + private Map<String,String> data = new HashMap<>(); + + public SystemClassLoaderEnvironmentProviderSpi(){ + if (data == null) { + Collection<URL> propertyUris = getConfigLocations(); + data = new HashMap<>(); + for (URL resource : propertyUris) { + for (ConfigurationFormat format : getConfigFormats(resource)) { + try (InputStream is = resource.openStream()) { + data.putAll(format.readConfiguration(resource.toExternalForm(), is).getDefaultSection()); + } catch (Exception e) { + LOG.log(Level.SEVERE, e, () -> "Error reading application context data from " + resource); + } + } + } + data.put("classloader.type", ClassLoader.getSystemClassLoader().getClass().getName()); + data.put("classloader.info", "System-Classloader"); + Set<URL> uris = new HashSet<>(); + uris.addAll(propertyUris); + data.put("context.sources", uris.toString()); + data = Collections.unmodifiableMap(data); + this.data = Collections.unmodifiableMap(data); + } + } + + protected List<ConfigurationFormat> getConfigFormats(URL url) { + return ConfigurationFormats.getFormats(url); + } + + protected Collection<URL> getConfigLocations() { + return ConfigResources.getResourceResolver().getResources(ClassLoader.getSystemClassLoader(), + "classpath:META-INF/context/system.properties", "classpath:META-INF/context/system.xml", + "classpath:META-INF/context/system.ini"); + } + + + @Override + public void setupContext(RuntimeContextBuilder contextBuilder) { + if (!data.isEmpty()) { + contextBuilder.setAll(data).build(); + contextBuilder.setContextId("system"); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextDataProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextDataProvider.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextDataProvider.java deleted file mode 100644 index 99e2c03..0000000 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextDataProvider.java +++ /dev/null @@ -1,42 +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.environment.spi; - - -import org.apache.tamaya.environment.RuntimeContext; - - -/** - * SPI component for evaluating the current runtime context. All registered providers hereby are - * organized by default depending on their (optional) {@code @Priority} annotation's value. (the - * effective ordering depends on the current {@link org.apache.tamaya.spi.ServiceContext} implementation - * active). - */ -public interface ContextDataProvider { - - /** - * If a data providers identifies a new runtime context level, it should build a new - * {@link org.apache.tamaya.environment.RuntimeContext} with all the related data to be added to this - * context, otherwise it should simply return null. - * - * @param currentContext the current context, or null for the root context. - * @return the new current context for the current runtime state, or null. - */ - public RuntimeContext getContext(RuntimeContext currentContext); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextProviderSpi.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextProviderSpi.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextProviderSpi.java new file mode 100644 index 0000000..d0a8b3e --- /dev/null +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextProviderSpi.java @@ -0,0 +1,45 @@ +/* + * 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.environment.spi; + + +import org.apache.tamaya.environment.RuntimeContext; +import org.apache.tamaya.environment.RuntimeContextBuilder; + + +/** + * SPI component for evaluating the current runtime context. All registered providers hereby are + * organized by default depending on their (optional) {@code @Priority} annotation's value. (the + * effective ordering depends on the current {@link org.apache.tamaya.spi.ServiceContext} implementation + * active). + */ +public interface ContextProviderSpi { + + public static final String ENVIRONMENT_TYPE = "environment-type"; + + /** + * If a data providers identifies a new runtime context level, it should build a new + * {@link org.apache.tamaya.environment.RuntimeContext} with all the related data to be added to this + * context, otherwise it should simply return null. + * + * @param contextBuilder the current context builder. + * @return the new current context for the current runtime state, or null. + */ + void setupContext(RuntimeContextBuilder contextBuilder); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextSpi.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextSpi.java b/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextSpi.java index 1651aaa..edc5c54 100644 --- a/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextSpi.java +++ b/sandbox/environment/src/main/java/org/apache/tamaya/environment/spi/ContextSpi.java @@ -38,10 +38,4 @@ public interface ContextSpi { */ RuntimeContext getCurrentContext(); - /** - * Get the current root environment id. - * @return the corresponding root environment id, never null. - */ - RuntimeContext getRootContext(); - } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/EnvironmentManagerTest.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/EnvironmentManagerTest.java b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/EnvironmentManagerTest.java deleted file mode 100644 index 958ace6..0000000 --- a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/EnvironmentManagerTest.java +++ /dev/null @@ -1,66 +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.metamodel.environment; - -import org.apache.tamaya.Environment; -import org.junit.Test; - - -import static org.junit.Assert.*; - -/** - * Tests for basic {@link org.apache.tamaya.EnvironmentManager} functionality. - * Created by Anatole on 17.10.2014. - */ -public class EnvironmentManagerTest { - - @Test - public void testGetEnvironment(){ - RuntimeContext env = RuntimeContext.current(); - assertNotNull(env); - RuntimeContext env2 = RuntimeContext.current(); - assertNotNull(env2); - assertFalse("Current Environments requested in same context are not the same!", env==env2); - } - - @Test - public void testGetRootEnvironment(){ - RuntimeContext env = RuntimeContext.root(); - assertNotNull(env); - RuntimeContext env2 = RuntimeContext.root(); - assertNotNull(env2); - assertTrue("Root Environments requested in same context are not the same!", env==env2); - } - - @Test - public void testRootIsNotCurrentEnvironment(){ - RuntimeContext env1 = RuntimeContext.root(); - RuntimeContext env2 = RuntimeContext.current(); - assertNotNull(env1); - assertNotNull(env2); - // within this testdata environment these are always the same - assertEquals(env1, env2); - } - - @Test - public void testEnvironmentOverride(){ - assertEquals(RuntimeContext.root().get("user.country").get(),System.getProperty("user.country")); - assertEquals(RuntimeContext.current().get("user.country").get(), System.getProperty("user.country")); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/RuntimeContextProviderSpiTest.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/RuntimeContextProviderSpiTest.java b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/RuntimeContextProviderSpiTest.java new file mode 100644 index 0000000..88b1692 --- /dev/null +++ b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/RuntimeContextProviderSpiTest.java @@ -0,0 +1,57 @@ +/* + * 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.metamodel.environment; + +import org.apache.tamaya.environment.RuntimeContext; +import org.apache.tamaya.environment.RuntimeContextProvider; +import org.junit.Test; + + +import static org.junit.Assert.*; + +/** + * Tests for basic {@link RuntimeContextProvider} functionality. + * Created by Anatole on 17.10.2014. + */ +public class RuntimeContextProviderSpiTest { + + @Test + public void testGetEnvironment(){ + RuntimeContext env = RuntimeContextProvider.current(); + assertNotNull(env); + RuntimeContext env2 = RuntimeContextProvider.current(); + assertNotNull(env2); + assertFalse("Current Environments requested in same context are not the same!", env==env2); + } + + @Test + public void testRootIsNotCurrentEnvironment(){ + RuntimeContext env1 = RuntimeContextProvider.current(); + assertNotNull(env1); + RuntimeContext env2 = RuntimeContextProvider.current(); + assertNotNull(env2); + // within this testdata environment these are always the same + assertEquals(env1, env2); + } + + @Test + public void testEnvironmentOverride(){ + assertEquals(RuntimeContextProvider.current().get("env.STAGE"), "MyStage"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentManagerSingleton.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentManagerSingleton.java b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentManagerSingleton.java index 52227c9..393775f 100644 --- a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentManagerSingleton.java +++ b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentManagerSingleton.java @@ -18,17 +18,19 @@ */ package org.apache.tamaya.metamodel.environment; +import org.apache.tamaya.environment.RuntimeContext; +import org.apache.tamaya.environment.spi.ContextSpi; + /** * Created by Anatole on 12.09.2014. */ -public class TestEnvironmentManagerSingleton implements org.apache.tamaya.metamodel.environment.spi.ContextSpi { - @Override - public RuntimeContext getCurrentEnvironment(){ +public class TestEnvironmentManagerSingleton implements ContextSpi { + + public RuntimeContext getCurrentContext(){ return null; } - @Override - public RuntimeContext getRootEnvironment(){ + public RuntimeContext getRootContext(){ return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentProvider.java b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentProvider.java index 7524a80..11760b9 100644 --- a/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentProvider.java +++ b/sandbox/environment/src/test/java/org/apache/tamaya/metamodel/environment/TestEnvironmentProvider.java @@ -18,8 +18,10 @@ */ package org.apache.tamaya.metamodel.environment; -import org.apache.tamaya.core.spi.EnvironmentProvider; +import org.apache.tamaya.environment.RuntimeContextBuilder; +import org.apache.tamaya.environment.spi.ContextProviderSpi; +import javax.annotation.Priority; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -27,22 +29,19 @@ import java.util.Map; /** * Environment provider used by some tests. */ -public class TestEnvironmentProvider implements EnvironmentProvider { +@Priority(2000) +public class TestEnvironmentProvider implements ContextProviderSpi { private Map<String, String> data = new HashMap<>(); public TestEnvironmentProvider(){ - data.put("user.country", System.getProperty("user.country")); + data.put("env.STAGE", "MyStage"); data.put("java.version", System.getProperty("java.version")); data = Collections.unmodifiableMap(data); } - @Override - public boolean isActive() { - return true; - } @Override - public Map<String, String> getEnvironmentData() { - return data; + public void setupContext(RuntimeContextBuilder contextBuilder){ + contextBuilder.setAll(data); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextProviderSpi ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextProviderSpi b/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextProviderSpi new file mode 100644 index 0000000..7f71c15 --- /dev/null +++ b/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextProviderSpi @@ -0,0 +1,23 @@ +# +# 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.environment.internal.ClassLoaderDependentAppEnvironmentProvider +org.apache.tamaya.environment.internal.ClassLoaderDependentEarEnvironmentProvider +org.apache.tamaya.environment.internal.InitialEnvironmentProviderSpi +org.apache.tamaya.environment.internal.SystemClassLoaderEnvironmentProviderSpi +org.apache.tamaya.metamodel.environment.TestEnvironmentProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/cf7980e3/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextSpi ---------------------------------------------------------------------- diff --git a/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextSpi b/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextSpi index 8915399..166dd67 100644 --- a/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextSpi +++ b/sandbox/environment/src/test/resources/META-INF/services/org.apache.tamaya.environment.spi.ContextSpi @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.environment.SingleEnvironmentManager +org.apache.tamaya.environment.internal.SingleEnvironmentManager