http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/pom.xml ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/pom.xml b/dormant/modules/integration/managed/pom.xml new file mode 100644 index 0000000..3b4f5bc --- /dev/null +++ b/dormant/modules/integration/managed/pom.xml @@ -0,0 +1,71 @@ +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.integration</groupId> + <artifactId>tamaya-integration-all</artifactId> + <version>0.1-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <artifactId>tamaya-integration-managed</artifactId> + <name>Apache Tamaya Modules Integration - Java Management Extensions</name> + <packaging>jar</packaging> + + <properties> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <executions> + <execution> + <id>prepare-agent</id> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfig.java ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfig.java b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfig.java new file mode 100644 index 0000000..b991690 --- /dev/null +++ b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfig.java @@ -0,0 +1,95 @@ +/* + * 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.se; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.AggregationPolicy; + +import java.util.Map; +import java.util.Set; + +/** + * Created by Anatole on 24.11.2014. + */ +public class ManagedConfig implements ManagedConfigMBean{ + @Override + public Set<String> getConfigurationNames() { + return null; + } + + @Override + public String getConfigurationInfo(String configName) { + return null; + } + + @Override + public boolean isConfigurationAvailable(String configName, String envType, String context) { + return false; + } + + @Override + public boolean isConfigurationLoaded(String configName, String envType, String context) { + return false; + } + + @Override + public Map<String, String> getConfiguration(String configName, String envType, String context) throws ConfigException { + return null; + } + + @Override + public Map<String, String> getRecursiveConfigValues(String area, String configName, String envType, String context) throws ConfigException { + return null; + } + + @Override + public Map<String, String> getConfigValues(String area, String configName, String envType, String context) throws ConfigException { + return null; + } + + @Override + public Map<String, String> updateConfiguration(String configName, String envType, String context, Map<String, String> values, AggregationPolicy aggregationPolicy) throws ConfigException { + return null; + } + + @Override + public String getConfigurationInfo(String configName, String envType, String context) { + return null; + } + + @Override + public Set<String> getAreas(String configName, String envType, String context) { + return null; + } + + @Override + public Set<String> getTransitiveAreas(String configName, String envType, String context) { + return null; + } + + @Override + public boolean isAreaExisting(String area, String configName, String envType, String context) { + return false; + } + + @Override + public boolean isAreaEmpty(String area, String configName, String envType, String context) { + return false; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfigMBean.java ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfigMBean.java b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfigMBean.java new file mode 100644 index 0000000..c2ade13 --- /dev/null +++ b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedConfigMBean.java @@ -0,0 +1,176 @@ +/* + * 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.se; + + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.AggregationPolicy; + +import java.util.Map; +import java.util.Set; + +/** + * Managed bean interface for accessing environment data. + */ +public interface ManagedConfigMBean { + /** + * Get the names current the configuration's defined. + * + * @return the names current the configuration's defined. + */ + public Set<String> getConfigurationNames(); + + /** + * Get a general configuration info descriptor in JSON format for a configuration + * type in the following form: + * <pre> + * tbd + * </pre> + * + * @param configName the configuration name, not null. + * @return a JSON formatted meta-information. + */ + public String getConfigurationInfo(String configName); + + /** + * Allows to determine if a configuration current a given type is available (accessible) in the + * given environment context. + * + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return true, if such a configuration is accessible. + */ + public boolean isConfigurationAvailable(String configName, String envType, String envContext); + + /** + * Allows to determine if a configuration current a given type is loaded in the + * given environment context. + * + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return true, if such a configuration is accessible. + */ + public boolean isConfigurationLoaded(String configName, String envType, String envContext); + + /** + * Accesses a configuration current a given type as Map. + * + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return true, if such a configuration is accessible. + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded. + */ + public Map<String, String> getConfiguration(String configName, String envType, String envContext) + throws ConfigException; + + /** + * Accesses a configuration values for current a given config area as Map. + * @param area the target area key, not null. + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return the key/values found, including the recursive child values. + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded. + */ + public Map<String, String> getRecursiveConfigValues(String area, String configName, String envType, String envContext) + throws ConfigException; + + /** + * Accesses a configuration values for current a given config area as Map. + * @param area the target area key, not null. + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return the key/values found, not transitive. + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded. + */ + public Map<String, String> getConfigValues(String area, String configName, String envType, String envContext) + throws ConfigException; + + /** + * Updates a configuration current a given type. + * + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @param values the values to be changed. + * @param aggregationPolicy the aggregation Policy to be used. + * @return the configuration after the changesd have been applied. + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded, or not + * mutable. + */ + public Map<String, String> updateConfiguration(String configName, String envType, String envContext, Map<String, String> values, AggregationPolicy aggregationPolicy) + throws ConfigException; + + /** + * Access a JSON formatted info on a configuration loaded in the form as + * <pre> + * tbd + * </pre> + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return the JSON formatted info, never null. + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded + */ + public String getConfigurationInfo(String configName, String envType, String envContext); + + /** + * Access the defined areas for a given configuration. + * @param configName the configuration name, not null. + * @param envContext the environment context, not null. + * @return the areas defined (only returning the areas that contain properties). + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded + */ + public Set<String> getAreas(String configName, String envType, String envContext); + + /** + * Access the transitive areas for a given configuration. + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return the transitive areas defined. + * @throws org.apache.tamaya.ConfigException If the configuration is not yet loaded + */ + public Set<String> getTransitiveAreas(String configName, String envType, String envContext); + + /** + * Allows to determine if an area is existing. + * @param area the target area key, not null. + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return true, if such an area exists (the area may be empty). + */ + public boolean isAreaExisting(String area, String configName, String envType, String envContext); + + /** + * Allows to determine if an area is empty. + * @param area the target area key, not null. + * @param configName the configuration name, not null. + * @param envType the environment context, not null. + * @param envContext the environment context, not null. + * @return true, if such an area exists and is not empty. + */ + public boolean isAreaEmpty(String area, String configName, String envType, String envContext); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironment.java ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironment.java b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironment.java new file mode 100644 index 0000000..e85b1c1 --- /dev/null +++ b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironment.java @@ -0,0 +1,69 @@ +/* + * 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.se; + +import org.apache.tamaya.Environment; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * MBean implementation for accessing environment data. + * Created by Anatole on 24.11.2014. + */ +public class ManagedEnvironment implements ManagedEnvironmentMBean{ + + @Override + public List<String> getEnvironmentHierarchy() { + return Environment.getEnvironmentHierarchy(); + } + + @Override + public String getEnvironmentInfo(String environmentContext) { + try { + // TODO + return "EnvironmentInfo {}"; + } + catch(Exception e){ + // TODO logging + return "EnvironmentInfo{}"; + } + } + + @Override + public Map<String, String> getEnvironment(String environmentType, String context) { + try { + Optional<Environment> env = Environment.getInstance(environmentType, context); + if (env.isPresent()) { + return env.get().toMap(); + } + } catch (Exception e) { + // TODO logging + } + return Collections.emptyMap(); + } + + @Override + public String getEnvironmentInfo() { + // TODO + return "EnvironmentInfo {}"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironmentMBean.java ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironmentMBean.java b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironmentMBean.java new file mode 100644 index 0000000..ef3b861 --- /dev/null +++ b/dormant/modules/integration/managed/src/main/java/org/apache/tamaya/management/ManagedEnvironmentMBean.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.se; + + +import java.util.List; +import java.util.Map; + +/** + * Managed bean interface for accessing environment data. + */ +public interface ManagedEnvironmentMBean { + + /** + * Returns the current environment hierarchy defined. + * @see org.apache.tamaya.Environment#getEnvironmentType() + * @see org.apache.tamaya.Environment#getEnvironmentHierarchy() + * @return the current environment type hierarchy defined, never null. + */ + public List<String> getEnvironmentHierarchy(); + + /** + * Get the common environment information in JSON format, which has the following form: + * <pre> + * Environment { + * id: "system:VM,domain:test", + * metaInfo { + * a: "aValue", + * b: "bValue" + * } + * entries{ + * val1: "value1", + * val2: "value2", + * } + * } + * </pre> + * @see org.apache.tamaya.Environment + * @param environmentContext the identifier to access the environment instance + * @return the environment JSON info, or null, if no such environment is accessible. + */ + public String getEnvironmentInfo(String environmentContext); + + /** + * Access the given environment as Map. the {@code environmentContext} is added to the + * map using the key {@code __environmentId}. + * @param environmentContext the identifier to access the environment instance + * @param context the context, not null. + * @return a map with the currently defined environment keys and values. + */ + public Map<String,String> getEnvironment(String environmentContext, String context); + + /** + * Get a general JSON info on the currently available environments current the form: + * <pre> + * EnvironmentInfo{ + * host: "hostName", + * ipAddress: "111.112.123.123", + * typeHierarchy: {"system", "domain", "ear", "war", "saas-scope", "tenant"} + * environments { + * Environment { + * id: "system:VM,domain:test", + * ... + * }, + * ... + * } + * } + * </pre> + * @return + */ + public String getEnvironmentInfo(); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/src/main/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/src/main/resources/META-INF/beans.xml b/dormant/modules/integration/managed/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..adee378 --- /dev/null +++ b/dormant/modules/integration/managed/src/main/resources/META-INF/beans.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd"> + +</beans> + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/managed/src/test/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/managed/src/test/resources/META-INF/beans.xml b/dormant/modules/integration/managed/src/test/resources/META-INF/beans.xml new file mode 100644 index 0000000..adee378 --- /dev/null +++ b/dormant/modules/integration/managed/src/test/resources/META-INF/beans.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd"> + +</beans> + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/pom.xml ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/pom.xml b/dormant/modules/integration/pom.xml new file mode 100644 index 0000000..33deee1 --- /dev/null +++ b/dormant/modules/integration/pom.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>tamaya-ext-all</artifactId> + <groupId>org.apache.tamaya.ext</groupId> + <version>0.1-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <packaging>pom</packaging> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.tamaya.integration</groupId> + <artifactId>tamaya-integration-all</artifactId> + + <modules> + <module>managed</module> + <module>cdi</module> + <module>se</module> + </modules> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/se/pom.xml ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/se/pom.xml b/dormant/modules/integration/se/pom.xml new file mode 100644 index 0000000..9958368 --- /dev/null +++ b/dormant/modules/integration/se/pom.xml @@ -0,0 +1,71 @@ +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.integration</groupId> + <artifactId>tamaya-integration-all</artifactId> + <version>0.1-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <artifactId>tamaya-integration-se</artifactId> + <name>Apache Tamaya Modules Integration - Java SE</name> + <packaging>jar</packaging> + + <properties> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <executions> + <execution> + <id>prepare-agent</id> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/integration/se/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java ---------------------------------------------------------------------- diff --git a/dormant/modules/integration/se/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java b/dormant/modules/integration/se/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java new file mode 100644 index 0000000..cdef5be --- /dev/null +++ b/dormant/modules/integration/se/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java @@ -0,0 +1,353 @@ +/* + * 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.integration.se; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.Writer; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.logging.Logger; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.Environment; + +/** + * Properties implementation class that can be applied as current System properties by calling + * {@link ConfiguredSystemProperties#install()}. The system properties will + * then behave contextually depending on the current runtime configuration active. + */ +public class ConfiguredSystemProperties extends Properties { + + private static final long serialVersionUID = 2152870929299226804L; + + private static final Logger LOG = Logger.getLogger(ConfiguredSystemProperties.class.getName()); + private Properties initialProperties; + private static volatile Map<String, Properties> contextualProperties = new ConcurrentHashMap<>(); + private static volatile Supplier<String> contextProvider = () -> + Environment.current().get("context.id").orElse("<system>"); + + + private final Object LOCK = new Object(); + + + private ConfiguredSystemProperties(Properties initialProperties) { + super(initialProperties); + this.initialProperties = initialProperties; + } + + public static void install() { + Properties props = System.getProperties(); + if (props instanceof ConfiguredSystemProperties) { + return; + } + ConfiguredSystemProperties systemProps = new ConfiguredSystemProperties(props); + LOG.finest("Installing enhanced system properties..."); + System.setProperties(systemProps); + LOG.info("Installed enhanced system properties successfully."); + } + + public static void uninstall() { + Properties props = System.getProperties(); + if (props instanceof ConfiguredSystemProperties) { + Properties initialProperties = ((ConfiguredSystemProperties) props).initialProperties; + LOG.finest("Uninstalling enhanced system properties..."); + System.setProperties(initialProperties); + LOG.info("Uninstalled enhanced system properties successfully."); + } + } + + @Override + public String getProperty(String key) { + return getContextualProperties().getProperty(key); + } + + @Override + public String getProperty(String key, String defaultValue) { + return getContextualProperties().getProperty(key, defaultValue); + } + + @Override + public Enumeration<?> propertyNames() { + return getContextualProperties().propertyNames(); + } + + @Override + public Set<String> stringPropertyNames() { + return getContextualProperties().stringPropertyNames(); + } + + @Override + public synchronized int size() { + return getContextualProperties().size(); + } + + @Override + public synchronized Enumeration<Object> keys() { + return getContextualProperties().keys(); + } + + @Override + public synchronized Enumeration<Object> elements() { + return getContextualProperties().elements(); + } + + @Override + public synchronized boolean contains(Object value) { + return getContextualProperties().contains(value); + } + + @Override + public boolean containsValue(Object value) { + return getContextualProperties().containsValue(value); + } + + @Override + public synchronized boolean containsKey(Object key) { + return getContextualProperties().containsKey(key); + } + + @Override + public synchronized Object get(Object key) { + return getContextualProperties().get(key); + } + + @Override + public synchronized Object clone() { + return getContextualProperties().clone(); + } + + @Override + public Set<Object> keySet() { + return getContextualProperties().keySet(); + } + + @Override + public Set<Map.Entry<Object, Object>> entrySet() { + return getContextualProperties().entrySet(); + } + + @Override + public Collection<Object> values() { + return getContextualProperties().values(); + } + + + @Override + public Object getOrDefault(Object key, Object defaultValue) { + return getContextualProperties().getOrDefault(key, defaultValue); + } + + @Override + public void forEach(BiConsumer<? super Object, ? super Object> action) { + getContextualProperties().forEach(action); + } + + + @Override + public Object computeIfAbsent(Object key, Function<? super Object, ?> mappingFunction) { + return getContextualProperties().computeIfAbsent(key, mappingFunction); + } + + @Override + public synchronized Object computeIfPresent(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction) { + return getContextualProperties().computeIfPresent(key, remappingFunction); + } + + @Override + public synchronized Object compute(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction) { + return getContextualProperties().compute(key, remappingFunction); + } + + @Override + public String toString() { + return getContextualProperties().toString(); + } + + @Override + public synchronized Object setProperty(String key, String value) { + return getContextualProperties().setProperty(key, value); + } + + @Override + public synchronized void load(Reader reader) throws IOException { + getContextualProperties().load(reader); + } + + @Override + public synchronized void load(InputStream inStream) throws IOException { + getContextualProperties().load(inStream); + } + + @SuppressWarnings("deprecation") + @Override + public void save(OutputStream out, String comments) { + super.save(out, comments); + } + + @Override + public void store(Writer writer, String comments) throws IOException { + getContextualProperties().store(writer, comments); + } + + @Override + public void store(OutputStream out, String comments) throws IOException { + getContextualProperties().store(out, comments); + } + + @Override + public void loadFromXML(InputStream in) throws IOException { + getContextualProperties().loadFromXML(in); + } + + @Override + public void storeToXML(OutputStream os, String comment) throws IOException { + getContextualProperties().storeToXML(os, comment); + } + + @Override + public void storeToXML(OutputStream os, String comment, String encoding) throws IOException { + getContextualProperties().storeToXML(os, comment, encoding); + } + + @Override + public void list(PrintStream out) { + getContextualProperties().list(out); + } + + @Override + public void list(PrintWriter out) { + getContextualProperties().list(out); + } + + @Override + public boolean isEmpty() { + return getContextualProperties().isEmpty(); + } + + @Override + public Object put(Object key, Object value) { + return getContextualProperties().put(key, value); + } + + @Override + public Object remove(Object key) { + return getContextualProperties().remove(key); + } + + @Override + public void putAll(Map<?, ?> t) { + getContextualProperties().putAll(t); + } + + @Override + public void clear() { + getContextualProperties().clear(); + } + + @Override + public boolean equals(Object o) { + return getContextualProperties().equals(o); + } + + @Override + public int hashCode() { + return getContextualProperties().hashCode(); + } + + @Override + public void replaceAll(BiFunction<? super Object, ? super Object, ?> function) { + getContextualProperties().replaceAll(function); + } + + @Override + public Object putIfAbsent(Object key, Object value) { + return getContextualProperties().putIfAbsent(key, value); + } + + @Override + public boolean remove(Object key, Object value) { + return getContextualProperties().remove(key, value); + } + + @Override + public boolean replace(Object key, Object oldValue, Object newValue) { + return getContextualProperties().replace(key, oldValue, newValue); + } + + @Override + public Object replace(Object key, Object value) { + return getContextualProperties().replace(key, value); + } + + @Override + public Object merge(Object key, Object value, BiFunction<? super Object, ? super Object, ?> remappingFunction) { + return getContextualProperties().merge(key, value, remappingFunction); + } + + public Properties getInitialProperties() { + return initialProperties; + } + + /** + * Uninstalls the contextual system properties for the current context, as determined by the current + * context provider active. + */ + public static void resetProperties() { + String contextId = contextProvider == null ? "" : contextProvider.get(); + contextualProperties.remove(contextId); + } + + protected Properties getContextualProperties() { + String contextId = contextProvider == null ? "" : contextProvider.get(); + Properties props = ConfiguredSystemProperties.contextualProperties.get(contextId); + if (props == null) { + synchronized (LOCK) { + props = ConfiguredSystemProperties.contextualProperties.get(contextId); + if (props == null) { + props = createNewProperties(); + contextualProperties.put(contextId, props); + } + } + } + return props; + } + + protected Properties createNewProperties() { + Properties props = new Properties(initialProperties); + Configuration config = Configuration.current(); + Map<String, String> configMap = config.getProperties(); + for (Map.Entry<String, String> en : configMap.entrySet()) { + props.put(en.getKey(), en.getValue()); + } + return props; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/pom.xml ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/pom.xml b/dormant/modules/metamodels/environment/pom.xml new file mode 100644 index 0000000..cf8fd91 --- /dev/null +++ b/dormant/modules/metamodels/environment/pom.xml @@ -0,0 +1,73 @@ +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.metamodels</groupId> + <artifactId>tamaya-metamodels-all</artifactId> + <version>0.1-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <artifactId>tamaya-metamodel-environment</artifactId> + <name>Apache Tamaya Modules Metamodels - environment</name> + <description>Environment Tamaya Metamodel</description> + <packaging>jar</packaging> + + <properties> + <jdkVersion>1.8</jdkVersion> + <maven.compile.targetLevel>${jdkVersion}</maven.compile.targetLevel> + <maven.compile.sourceLevel>${jdkVersion}</maven.compile.sourceLevel> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <executions> + <execution> + <id>prepare-agent</id> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>${maven.compile.sourceLevel}</source> + <target>${maven.compile.targetLevel}</target> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/BuildableEnvironment.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/BuildableEnvironment.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/BuildableEnvironment.java new file mode 100644 index 0000000..3633c97 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/BuildableEnvironment.java @@ -0,0 +1,109 @@ +/* + * 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 java.util.*; + +/** + * Environment class that is used by the {@link org.apache.tamaya.metamodel.environment.EnvironmentBuilder}. + */ +class BuildableEnvironment implements Environment { + + /** The environment data. */ + private Map<String,String> context = new TreeMap<>(); + + /** + * Constructor. + * @param builder the builder, not null. + */ + BuildableEnvironment(EnvironmentBuilder builder){ + Objects.requireNonNull(builder); + context.putAll(builder.contextData); + } + + @Override + public Map<String, String> toMap() { + return context; + } + + @Override + public Optional<String> get(String key){ + return Optional.ofNullable(context.get(key)); + } + + @Override + public boolean containsKey(String key){ + return context.containsKey(key); + } + + @Override + public Set<String> keySet() { + return context.keySet(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BuildableEnvironment that = (BuildableEnvironment) o; + return context.equals(that.context); + } + + @Override + public int hashCode() { + return context.hashCode(); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString(){ + return "Environment: " + getData(); + } + + /** + * Get the delta. + * @return + */ + private String getData() { + StringBuilder b = new StringBuilder(); + for(Map.Entry<String,String> en: this.context.entrySet()){ + b.append(" ").append(en.getKey()).append('=').append(escape(en.getValue())).append('\n'); + } + if(b.length()>0) + b.setLength(b.length()-1); + return b.toString(); + } + + /** + * Escapes several characters. + * @param value + * @return + */ + private String escape(String value){ + if(value==null) + return null; + return value.replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r").replaceAll("\t", "\\\\t") + .replaceAll("=", "\\\\="); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/Environment.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/Environment.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/Environment.java new file mode 100644 index 0000000..8a8b157 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/Environment.java @@ -0,0 +1,86 @@ +/* + * 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.metamodel.environment.spi.EnvironmentSpi; +import org.apache.tamaya.spi.ServiceContext; + +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Models a runtime environment. Instances current this class are used to + * evaluate the correct configuration artifacts.<br/> + * <h3>Implementation Requirements</h3> + * <p> + * Implementations current this interface must be + * <ul> + * <li>Thread safe. + * <li>Immutable + * <li>serializable + * </ul> + */ +public interface Environment{ + + /** + * Access a property. + * @param key the property's key, not null. + * @return the property's keys. + */ + Optional<String> get(String key); + + /** + * Checks if a property is defined. + * @param key the property's key, not null. + * @return true, if the property is existing. + */ + boolean containsKey(String key); + + /** + * Access the set current property keys, defined by this provider. + * @return the key set, never null. + */ + Set<String> keySet(); + + /** + * Access the environment as Map. + * @return the Map instance containing the environments properties, never null. + */ + Map<String,String> toMap(); + + /** + * Get the current {@link org.apache.tamaya.metamodel.environment.Environment}. 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 Environment current(){ + return ServiceContext.getInstance().getSingleton(EnvironmentSpi.class).getCurrentEnvironment(); + } + + /** + * Get the current {@link Environment}. 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 Environment root(){ + return ServiceContext.getInstance().getSingleton(EnvironmentSpi.class).getRootEnvironment(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/EnvironmentBuilder.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/EnvironmentBuilder.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/EnvironmentBuilder.java new file mode 100644 index 0000000..17ec34b --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/EnvironmentBuilder.java @@ -0,0 +1,97 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** +* Builder to create new {@link Environment instances.} +*/ +public final class EnvironmentBuilder{ + + /** The property name for the stage property. */ + public static final String STAGE_PROP = "stage"; + + /** THe environment data. */ + Map<String,String> contextData = new HashMap<>(); + + /** + * Constructor. + */ + private EnvironmentBuilder() { + } + + /** + * Creates a new buildr instance. + * @return the new builder instance. + */ + public static final EnvironmentBuilder of() { + return new EnvironmentBuilder(); + } + + /** + * Sets a new environment property. + * @param key the key, not null. + * @param value the keys, not null. + * @return the builder for chaining + */ + public EnvironmentBuilder set(String key, String value){ + this.contextData.put(key, value); + return this; + } + + /** + * Sets new environment properties. + * @param values the key/values, not null. + * @return the builder for chaining + */ + public EnvironmentBuilder setAll(Map<String,String> values){ + this.contextData.putAll(values); + return this; + } + + /** + * Sets the stage using the default stage key. + * @param stage The stage, not null. + * @return the builder for chaining. + */ + public EnvironmentBuilder setStage(String stage){ + this.contextData.put(STAGE_PROP, Objects.requireNonNull(stage)); + return this; + } + + /** + * Access a property + * @param key the key, not null. + * @return the builder for chaining. + */ + public String getProperty(String key) { + return this.contextData.get(key); + } + + /** + * Builds a new Environment. + * @return a new Environment, never null. + */ + public Environment build() { + return new BuildableEnvironment(this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java new file mode 100644 index 0000000..db4a7c7 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentApplicationEnvironmentProvider.java @@ -0,0 +1,101 @@ +/* + * 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 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.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.Environment} instance + * is created and attached. + */ +public class ClassLoaderDependentApplicationEnvironmentProvider implements EnvironmentProvider { + + private static final Logger LOG = Logger.getLogger(ClassLoaderDependentApplicationEnvironmentProvider.class.getName()); + + private Map<ClassLoader, Map<String,String>> environments = new ConcurrentHashMap<>(); + private Map<ClassLoader, Boolean> environmentAvailable = new ConcurrentHashMap<>(); + + @Override + public boolean isActive() { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if(cl==null){ + return false; + } + Boolean available = this.environmentAvailable.get(cl); + if(available!=null && !available){ + return false; + } + List<Resource> propertyUris = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, + "classpath:META-INF/env/application.properties", "classpath:META-INF/env/application.xml", "classpath:META-INF/env/application.ini"); + available = !propertyUris.isEmpty(); + this.environmentAvailable.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.environments.get(cl); + if(data!=null){ + return data; + } + List<Resource> propertyUris = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, + "classpath:META-INF/env/application.properties", "classpath:META-INF/env/application.xml", "classpath:META-INF/env/application.ini"); + data = new HashMap<>(); + + for(Resource resource:propertyUris){ + try{ + ConfigurationFormat format = ConfigurationFormats.getFormat(resource); + data.putAll(format.readConfiguration(resource)); + } + catch(Exception e){ + LOG.log(Level.SEVERE, e, () -> "Error reading application environment data fromMap " + resource); + } + } + data.put("classloader.type", cl.getClass().getName()); + data.put("classloader.info", cl.toString()); + Set<Resource> uris = new HashSet<>(); + uris.addAll(propertyUris); + data.put("environment.sources", uris.toString()); + data = Collections.unmodifiableMap(data); + this.environments.put(cl, data); + return data; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java new file mode 100644 index 0000000..2ef88ec --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/ClassLoaderDependentEarEnvironmentProvider.java @@ -0,0 +1,108 @@ +/* + * 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.metamodel.environment.EnvironmentBuilder; +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.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * This class implements a {@link EnvironmentProvider} 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.Environment} + * 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>> environments = new ConcurrentHashMap<>(); + private Map<ClassLoader, Boolean> environmentAvailable = new ConcurrentHashMap<>(); + + @Override + public boolean isActive() { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if(cl==null){ + return false; + } + Boolean available = this.environmentAvailable.get(cl); + if(available!=null && !available){ + return false; + } + List<Resource> propertyUris = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, + "classpath:META-INF/env/ear.properties", "classpath:META-INF/env/ear.xml", "classpath:META-INF/env/ear.ini"); + available = !propertyUris.isEmpty(); + this.environmentAvailable.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.environments.get(cl); + if(data!=null){ + return data; + } + List<Resource> resources = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(cl, + "classpath:META-INF/env/ear.properties", "classpath:META-INF/env/ear.xml", "classpath:META-INF/env/ear.ini"); + data = new HashMap<>(); + for(Resource 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 environment data fromMap " + resource); + } + } +// String earId = data.getOrDefault(EARID_PROP, cl.toString()); + String stageValue = data.get(EnvironmentBuilder.STAGE_PROP); + if (stageValue != null) { + data.put(EnvironmentBuilder.STAGE_PROP,stageValue); + } + data.put("classloader.type", cl.getClass().getName()); + data.put("classloader.info", cl.toString()); + Set<Resource> resourceSet = new HashSet<>(); + resourceSet.addAll(resources); + data.put("environment.sources", resourceSet.toString()); + data = Collections.unmodifiableMap(data); + this.environments.put(cl, data); + return data; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/InitialEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/InitialEnvironmentProvider.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/InitialEnvironmentProvider.java new file mode 100644 index 0000000..9321539 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/InitialEnvironmentProvider.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.metamodel.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.core.env.ConfiguredSystemProperties; +import org.apache.tamaya.metamodel.environment.EnvironmentBuilder; +import org.apache.tamaya.metamodel.environment.spi.EnvironmentProvider; + +/** + * Default {@link org.apache.tamaya.metamodel.environment.Environment}. + */ +public final class InitialEnvironmentProvider implements EnvironmentProvider{ + + private Map<String,String> environmentData = new HashMap<>(); + + public InitialEnvironmentProvider() { + Properties props = System.getProperties(); + if(props instanceof ConfiguredSystemProperties){ + props = ((ConfiguredSystemProperties)props).getInitialProperties(); + } + String stageValue = props.getProperty(EnvironmentBuilder.STAGE_PROP); + environmentData.put(EnvironmentBuilder.STAGE_PROP, stageValue); + environmentData.put("timezone", TimeZone.getDefault().getID()); + environmentData.put("locale", Locale.getDefault().toString()); + try { + environmentData.put("host", InetAddress.getLocalHost().toString()); + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, e, () -> "Failed to evaluate hostname."); + } + // Copy env properties.... + for (Entry<String, String> en : System.getenv().entrySet()) { + environmentData.put(en.getKey(), en.getValue()); + } + environmentData = Collections.unmodifiableMap(environmentData); + } + + @Override + public boolean isActive(){ + return true; + } + + @Override + public Map<String,String> getEnvironmentData() { + return environmentData; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SingleEnvironmentManager.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SingleEnvironmentManager.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SingleEnvironmentManager.java new file mode 100644 index 0000000..cbf177d --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SingleEnvironmentManager.java @@ -0,0 +1,70 @@ +/* + * 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.metamodel.environment.Environment; +import org.apache.tamaya.metamodel.environment.EnvironmentBuilder; +import org.apache.tamaya.metamodel.environment.spi.EnvironmentProvider; +import org.apache.tamaya.metamodel.environment.spi.EnvironmentSpi; +import org.apache.tamaya.spi.ServiceContext; + +import java.util.*; + +/** + * Service for accessing {@link org.apache.tamaya.metamodel.environment.Environment}. Environments are used to + * access/determine configurations.<br/> + * <h3>Implementation PropertyMapSpec</h3> This class is + * <ul> + * <li>thread safe, + * <li>and behaves contextual. + * </ul> + */ +public class SingleEnvironmentManager implements EnvironmentSpi { + + private final List<EnvironmentProvider> environmentProviders = loadEnvironmentProviders(); + private Environment rootEnvironment = getCurrentEnvironment(); + + private List<EnvironmentProvider> loadEnvironmentProviders() { + List<EnvironmentProvider> providerList = new ArrayList<>(); + for(EnvironmentProvider prov: ServiceContext.getInstance().getServices(EnvironmentProvider.class)){ + providerList.add(prov); + } + return providerList; + } + + @Override + public Environment getCurrentEnvironment(){ + EnvironmentBuilder b = EnvironmentBuilder.of(); + for(EnvironmentProvider prov: environmentProviders){ + if(prov.isActive()){ + if(prov.isActive()){ + b.setAll(prov.getEnvironmentData()); + } + } + } + return b.build(); + } + + @Override + public Environment getRootEnvironment(){ + return rootEnvironment; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SystemClassLoaderEnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SystemClassLoaderEnvironmentProvider.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SystemClassLoaderEnvironmentProvider.java new file mode 100644 index 0000000..e943971 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/internal/SystemClassLoaderEnvironmentProvider.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.metamodel.environment.internal; + +import org.apache.tamaya.core.config.ConfigurationFormats; +import org.apache.tamaya.core.resource.Resource; +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.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 EnvironmentProvider { + + private static final Logger LOG = Logger.getLogger(SystemClassLoaderEnvironmentProvider.class.getName()); + + private Map<String,String> data = new HashMap<>(); + + + public SystemClassLoaderEnvironmentProvider(){ + List<Resource> propertyResources = ServiceContext.getInstance().getSingleton(ResourceLoader.class).getResources(ClassLoader.getSystemClassLoader(), + "classpath:META-INF/env/system.properties", "classpath:META-INF/env/system.xml", "classpath:META-INF/env/system.ini"); + for(Resource 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<Resource> resourceSet = new HashSet<>(); + resourceSet.addAll(propertyResources); + data.put("environment.system.sources", resourceSet.toString()); + this.data = Collections.unmodifiableMap(data); + } + @Override + public boolean isActive() { + return true; + } + + @Override + public Map<String,String> getEnvironmentData() { + return data; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentProvider.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentProvider.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentProvider.java new file mode 100644 index 0000000..ef4ff43 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentProvider.java @@ -0,0 +1,44 @@ +/* + * 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.spi; + +import java.util.Map; + +/** + * SPI for components that define a concrete type current {@link org.apache.tamaya.metamodel.environment.Environment}. + * The chain current environment config determine the current {@link org.apache.tamaya.metamodel.environment.Environment} active + * and its parent instances. + * Created by Anatole on 14.10.2014. + */ +public interface EnvironmentProvider { + + /** + * Evaluates if an environment is currently active. + * @return + */ + boolean isActive(); + + /** + * Returns the properties to be added to the environment. + * @return the properties, or an empty map if no properties are to be added (or the provider is not active for the + * current runtime state). + */ + Map<String,String> getEnvironmentData(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentSpi.java ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentSpi.java b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentSpi.java new file mode 100644 index 0000000..dbf8f65 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/java/org/apache/tamaya/metamodel/environment/spi/EnvironmentSpi.java @@ -0,0 +1,49 @@ +/* + * 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.spi; + + +import org.apache.tamaya.metamodel.environment.Environment; + +/** + * Service for accessing {@link org.apache.tamaya.metamodel.environment.Environment}. Environments are used to + * access/determine configurations.<br/> + * <h3>Implementation PropertyMapSpec</h3> This class is + * <ul> + * <li>thread safe, + * <li>and behaves contextual. + * </ul> + */ +public interface EnvironmentSpi { + + /** + * Get the current environment current the given environment type. + * @return the corresponding environment, never null. + * @throws IllegalArgumentException if not such type is present or active. + */ + Environment getCurrentEnvironment(); + + /** + * Get the current environment current the given environment type. + * @return the corresponding environment, never null. + * @throws IllegalArgumentException if not such type is present or active. + */ + Environment getRootEnvironment(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.core.spi.EnvironmentProvider ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.core.spi.EnvironmentProvider b/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.core.spi.EnvironmentProvider new file mode 100644 index 0000000..3592434 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.core.spi.EnvironmentProvider @@ -0,0 +1,22 @@ +# +# 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.core.internal.env.InitialEnvironmentProvider +org.apache.tamaya.core.internal.env.SystemClassLoaderEnvironmentProvider +org.apache.tamaya.core.internal.env.ClassLoaderDependentEarEnvironmentProvider +org.apache.tamaya.core.internal.env.ClassLoaderDependentApplicationEnvironmentProvider http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d9964c64/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.spi.EnvironmentSpi ---------------------------------------------------------------------- diff --git a/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.spi.EnvironmentSpi b/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.spi.EnvironmentSpi new file mode 100644 index 0000000..4a5b828 --- /dev/null +++ b/dormant/modules/metamodels/environment/src/main/resources/META-INF/services/org.apache.tamaya.spi.EnvironmentSpi @@ -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.metamodel.environment.TestEnvironmentManagerSingleton