Added ConfiguratoinFOrmats singleton to formats module. Moved commons-config related aspects into separate integration 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/bd997069 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/bd997069 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/bd997069 Branch: refs/heads/master Commit: bd9970692dd545f6b526b448f3ae159c3e6786a7 Parents: c15fb1c Author: anatole <anat...@apache.org> Authored: Sat Feb 14 00:49:07 2015 +0100 Committer: anatole <anat...@apache.org> Committed: Sat Feb 14 00:49:07 2015 +0100 ---------------------------------------------------------------------- .gitignore | 2 + dormant/api/PropertyMapSupplier.java | 37 ------ dormant/buildtools/pom.xml | 35 ----- .../src/main/java/old/AggregationPolicy.java | 133 ------------------- .../java/old/EnvPropertiesConfigProvider.java | 54 -------- dormant/core/src/main/java/old/Orderable.java | 34 ----- .../core/src/main/java/old/OrdinalProvider.java | 37 ------ .../java/old/PropertyAdapterProviderSpi.java | 36 ----- .../old/SystemPropertiesConfigProvider.java | 54 -------- .../tamaya/core/internal/format/IniFormat.java | 98 -------------- .../core/internal/format/PropertiesFormat.java | 61 --------- .../internal/format/PropertiesXmlFormat.java | 62 --------- .../resources/AntPathClasspathResolver.java | 60 --------- .../internal/resources/AntPathFileResolver.java | 61 --------- .../resources/DefaultResourceLoader.java | 81 ----------- .../properties/EnvironmentPropertySource.java | 48 ------- .../core/properties/MapBasedPropertySource.java | 98 -------------- .../SystemPropertiesPropertySource.java | 55 -------- .../apache/tamaya/core/spi/PathResolver.java | 45 ------- dormant/pom.xml | 35 +++++ modules/formats/pom.xml | 5 - .../format/CommonsConfigPropertySource.java | 79 ----------- .../tamaya/format/ConfigurationFormat.java | 4 + .../tamaya/format/ConfigurationFormats.java | 88 ++++++++++++ .../format/FlattenedDefaultPropertySource.java | 60 +++++++++ .../tamaya/format/IniConfigurationFormat.java | 57 -------- .../apache/tamaya/format/PropertiesFormat.java | 57 -------- .../tamaya/format/PropertiesXmlFormat.java | 56 -------- .../format/formats/IniConfigurationFormat.java | 84 ++++++++++++ .../tamaya/format/formats/PropertiesFormat.java | 61 +++++++++ .../format/formats/PropertiesXmlFormat.java | 60 +++++++++ modules/integration/commons/pom.xml | 50 +++++++ .../commons/CommonsConfigPropertySource.java | 81 +++++++++++ .../commons/IniConfigurationFormat.java | 60 +++++++++ modules/integration/pom.xml | 3 +- 35 files changed, 587 insertions(+), 1344 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/.gitignore ---------------------------------------------------------------------- diff --git a/.gitignore b/.gitignore index b97b7b5..194682a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ release.properties .project .settings .classpath + +modules/integration/commons-config/commons-configuration.iml \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/api/PropertyMapSupplier.java ---------------------------------------------------------------------- diff --git a/dormant/api/PropertyMapSupplier.java b/dormant/api/PropertyMapSupplier.java deleted file mode 100644 index 69dd308..0000000 --- a/dormant/api/PropertyMapSupplier.java +++ /dev/null @@ -1,37 +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; - -import java.util.Map; - -/** - * Supplier for a property map. - */ -@FunctionalInterface -public interface PropertyMapSupplier { - - /** - * Access the current properties as Map. The resulting Map may not return all items accessible, e.g. - * when the underlying storage does not support iteration of its entries. - * - * @return the a corresponding map, never null. - */ - Map<String,String> getProperties(); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/buildtools/pom.xml ---------------------------------------------------------------------- diff --git a/dormant/buildtools/pom.xml b/dormant/buildtools/pom.xml deleted file mode 100644 index 434563e..0000000 --- a/dormant/buildtools/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ -<?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 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. ---> -<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</groupId> - <artifactId>tamaya-all</artifactId> - <version>0.1-SNAPSHOT</version> - </parent> - - <artifactId>buildtools</artifactId> - <name>Apache Tamaya - Build Tools</name> - - -</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/old/AggregationPolicy.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/old/AggregationPolicy.java b/dormant/core/src/main/java/old/AggregationPolicy.java deleted file mode 100644 index c4c5fda..0000000 --- a/dormant/core/src/main/java/old/AggregationPolicy.java +++ /dev/null @@ -1,133 +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.core.properties; - -import org.apache.tamaya.ConfigException; - -import java.util.logging.Logger; - -/** -* Policy that defines how the different configurations/property sources should be aggregated. -* This is done by a mapping function defined as follows: -* <pre> -* function f(key, value1, value2) -> result -* -* whereas -* -* key = the fully qualified property key, -* value1 = the value from the first configuration/property source (can be null) -* value2 = the value from the second configuration/property source (can be null) -* -* result = the value to be used in the aggregation, or null, which removed the -* key from the result. -* </pre> -* -* Of course, during this evaluation step additional actions can be taken, e.g. refer to #LOG_ERROR, which -* ignores duplicate entries, but also logs the conflict on severe/error level. -*/ -public interface AggregationPolicy { - - /** - * Method which decides how keys/values are aggregated. - * @param key the key current the entry, must not be {@code null}. - * @param currentValue the current keys, or {@code null}. - * @param newValue the new keys, never {@code null}. - * @return the target keys to be used in the resulting property set, or null, to remove the property. - */ - public String aggregate(String key, String currentValue, String newValue); - - /** Ignore overrides, only extend (additive). */ - public static final AggregationPolicy IGNORE_DUPLICATES = (k, v1, v2) -> v1 == null? v2 : v1; - - /** Combine multiple values into a comma separated list. */ - public static final AggregationPolicy COMBINE = (k, v1, v2) -> v1 != null && v2 != null ? v1 + ',' + v2: v2; - - /** - * Interpret later keys as override (additive and override), replacing - * the key loaded earlier/fromMap previous contained - * {@link org.apache.tamaya.spi.PropertySource}. - */ - public static final AggregationPolicy OVERRIDE = (k, v1, v2) -> v2; - - /** - * Throw an exception, when keys are not disjunctive (strictly - * additive). - */ - public static final AggregationPolicy EXCEPTION = - (String key, String value, String newValue) -> { - if(value!=null && newValue!=null && !value.equals(newValue)){ - throw new ConfigException("Conflicting values encountered key="+key+", keys="+value+", newValue="+newValue); - } - return newValue; - }; - - /** - * Ignores any duplicates, but logs the conflict encountered to error/severe level. - */ - public static final AggregationPolicy LOG_ERROR = - (String key, String value, String newValue) -> { - if(value!=null && newValue!=null && !value.equals(newValue)){ - Logger.getLogger(AggregationPolicy.class.getName()) - .severe(() -> "Conflicting values encountered key=" + key + ", keys=" + value + ", newValue=" + newValue); - return value; - } - return newValue; - }; - - /** - * Ignores any duplicates, but logs the conflict encountered to info level. - */ - public static final AggregationPolicy LOG_WARNING = - (String key, String value, String newValue) -> { - if(value!=null && newValue!=null && !value.equals(newValue)){ - Logger.getLogger(AggregationPolicy.class.getName()) - .warning(() -> "Conflicting values encountered key=" + key + ", keys=" + value + ", newValue=" + newValue); - return value; - } - return newValue; - }; - - /** - * Ignores any duplicates, but logs the conflict encountered to info level. - */ - public static final AggregationPolicy LOG_INFO = - (String key, String value, String newValue) -> { - if(value!=null && newValue!=null && !value.equals(newValue)){ - Logger.getLogger(AggregationPolicy.class.getName()) - .info(() -> "Conflicting values encountered key=" + key + ", keys=" + value + ", newValue=" + newValue); - return value; - } - return newValue; - }; - - /** - * Ignores any duplicates, but logs the conflict encountered to debug/finest level. - */ - public static final AggregationPolicy LOG_DEBUG = - (String key, String value, String newValue) -> { - if(value!=null && newValue!=null && !value.equals(newValue)){ - Logger.getLogger(AggregationPolicy.class.getName()) - .finest(() -> "Conflicting values encountered key=" + key + ", keys=" + value + ", newValue=" + newValue); - return value; - } - return newValue; - }; - - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/old/EnvPropertiesConfigProvider.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/old/EnvPropertiesConfigProvider.java b/dormant/core/src/main/java/old/EnvPropertiesConfigProvider.java deleted file mode 100644 index 82306bb..0000000 --- a/dormant/core/src/main/java/old/EnvPropertiesConfigProvider.java +++ /dev/null @@ -1,54 +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 old; - -import org.apache.tamaya.core.properties.PropertySourceBuilder; -import old.ConfigurationProviderSpi; - -import org.apache.tamaya.Configuration; - -/** - * Provides a {@link org.apache.tamaya.Configuration} named 'environment.properties' - * containing the current environment properties. - * - * Created by Anatole on 29.09.2014. - */ -public class EnvPropertiesConfigProvider implements ConfigurationProviderSpi{ - - private Configuration envConfig; - - public EnvPropertiesConfigProvider(){ - envConfig = Configuration.from(PropertySourceBuilder.of("environment.properties").addEnvironmentProperties().build()); - } - - @Override - public String getConfigName(){ - return "environment.properties"; - } - - @Override - public Configuration getConfiguration(){ - return envConfig; - } - - @Override - public void reload() { - // nothing todo here - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/old/Orderable.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/old/Orderable.java b/dormant/core/src/main/java/old/Orderable.java deleted file mode 100644 index 13380a1..0000000 --- a/dormant/core/src/main/java/old/Orderable.java +++ /dev/null @@ -1,34 +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 old; - -/** - * Interface that can be optionally implemented by SPI components to be loaded into - * the Tamaya's ServiceContext. The ordinal provided will be used to determine - * priority and precedence, when multiple components implement the same - * service interface. - */ -@FunctionalInterface -public interface Orderable { - /** - * Get the ordinal keys for the component, by default 0. - * @return the ordinal keys - */ - int order(); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/old/OrdinalProvider.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/old/OrdinalProvider.java b/dormant/core/src/main/java/old/OrdinalProvider.java deleted file mode 100644 index 0152b84..0000000 --- a/dormant/core/src/main/java/old/OrdinalProvider.java +++ /dev/null @@ -1,37 +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 old; - -import java.util.OptionalInt; - -/** - * The ordinal provider is an optional component that provides an abstraction for ordering/prioritizing - * services loaded. This can be used to determine, which SPI should be used, if multiple instances are - * available, or for ordering chain of services. - * @see org.apache.tamaya.spi.ServiceContext - */ -public interface OrdinalProvider { - /** - * Evaluate the ordinal number for the given type. - * @param type the target type, not null. - * @return the ordinal, if not defined, 0 should be returned. - */ - OptionalInt getOrdinal(Class<?> type); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/old/PropertyAdapterProviderSpi.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/old/PropertyAdapterProviderSpi.java b/dormant/core/src/main/java/old/PropertyAdapterProviderSpi.java deleted file mode 100644 index 65c31a7..0000000 --- a/dormant/core/src/main/java/old/PropertyAdapterProviderSpi.java +++ /dev/null @@ -1,36 +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 old; - -import org.apache.tamaya.PropertyAdapter; - -/** - * This service provides different {@link org.apache.tamaya.PropertyAdapter} instances for types. - */ -public interface PropertyAdapterProviderSpi { - - /** - * Called, when a given {@link org.apache.tamaya.Configuration} has to be evaluated. - * - * @return the corresponding {@link java.util.function.Function<String, T>}, or {@code null}, if - * not available for the given target type. - */ - <T> PropertyAdapter<T> getPropertyAdapter(Class<T> type); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/old/SystemPropertiesConfigProvider.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/old/SystemPropertiesConfigProvider.java b/dormant/core/src/main/java/old/SystemPropertiesConfigProvider.java deleted file mode 100644 index 30a0cf0..0000000 --- a/dormant/core/src/main/java/old/SystemPropertiesConfigProvider.java +++ /dev/null @@ -1,54 +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 old; - -import org.apache.tamaya.core.properties.PropertySourceBuilder; -import old.ConfigurationProviderSpi; - -import org.apache.tamaya.Configuration; - -/** - * Provides a {@link org.apache.tamaya.Configuration} named 'system.properties' - * containing the current system properties. - * - * Created by Anatole on 29.09.2014. - */ -public class SystemPropertiesConfigProvider implements ConfigurationProviderSpi{ - - private Configuration systemConfig; - - public SystemPropertiesConfigProvider(){ - systemConfig = Configuration.from(PropertySourceBuilder.of("system.properties").addSystemProperties().build()); - } - - @Override - public String getConfigName(){ - return "system.properties"; - } - - @Override - public Configuration getConfiguration(){ - return systemConfig; - } - - @Override - public void reload() { - // nothing todo here - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/IniFormat.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/IniFormat.java b/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/IniFormat.java deleted file mode 100644 index 85ce778..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/IniFormat.java +++ /dev/null @@ -1,98 +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.core.internal.format; - -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.core.properties.ConfigurationFormat; - - -import org.apache.tamaya.ConfigException; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class IniFormat implements ConfigurationFormat{ - - private static final Logger LOG = Logger.getLogger(IniFormat.class.getName()); - - - @Override - public String getFormatName(){ - return "ini"; - } - - @Override - public boolean isAccepted(Resource resource){ - String path = resource.getFilename(); - return path != null && path.endsWith(".ini"); - } - - @Override - public Map<String,String> readConfiguration(Resource resource){ - Map<String,String> result = new HashMap<>(); - if(isAccepted(resource) && resource.exists()){ - try(InputStream is = resource.getInputStream()){ - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - String line = reader.readLine(); - int lineNum = 0; - String section = null; - while(line != null){ - lineNum++; - line = line.trim(); - if(line.isEmpty()){ - line = reader.readLine(); - continue; - } - if(line.trim().startsWith("#")){ - // comment - } - else if(line.startsWith("[")){ - int end = line.indexOf(']'); - if(end < 0){ - throw new ConfigException( - "Invalid INI-Format, ']' expected, at " + lineNum + " in " + resource); - } - section = line.substring(1, end); - } - else{ - int sep = line.indexOf('='); - String key = line.substring(0,sep); - String value = line.substring(sep+1); - if(section!=null){ - result.put(section + '.' + key, value); - } - else{ - result.put(key, value); - } - } - line = reader.readLine(); - } - } - catch(Exception e){ - LOG.log(Level.SEVERE, e, () -> "Could not read configuration: " + resource); - } - } - return result; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesFormat.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesFormat.java b/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesFormat.java deleted file mode 100644 index 10355ef..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesFormat.java +++ /dev/null @@ -1,61 +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.core.internal.format; - -import java.io.InputStream; -import java.util.Collections; -import java.util.Map; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.core.properties.ConfigurationFormat; - -public class PropertiesFormat implements ConfigurationFormat{ - - private final static Logger LOG = Logger.getLogger(PropertiesFormat.class.getName()); - - @Override - public String getFormatName(){ - return "properties"; - } - - @Override - public boolean isAccepted(Resource resource) { - String path = resource.getFilename(); - return path != null && path.endsWith(".properties"); - } - - @SuppressWarnings("unchecked") - @Override - public Map<String,String> readConfiguration(Resource resource) { - if (isAccepted(resource) && resource.exists()) { - try (InputStream is = resource.getInputStream()) { - Properties p = new Properties(); - p.load(is); - return Map.class.cast(p); - } catch (Exception e) { - LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + resource); - } - } - return Collections.emptyMap(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesXmlFormat.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesXmlFormat.java b/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesXmlFormat.java deleted file mode 100644 index 660c092..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/internal/format/PropertiesXmlFormat.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal.format; - -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.core.properties.ConfigurationFormat; - -import java.io.InputStream; -import java.util.Collections; -import java.util.Map; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - - -public class PropertiesXmlFormat implements ConfigurationFormat{ - - private final static Logger LOG = Logger.getLogger(PropertiesXmlFormat.class.getName()); - - @Override - public String getFormatName(){ - return "xml-properties"; - } - - @Override - public boolean isAccepted(Resource resource){ - String path = resource.getFilename(); - return path != null && path.endsWith(".xml"); - } - - @SuppressWarnings("unchecked") - @Override - public Map<String,String> readConfiguration(Resource resource) { - if (isAccepted(resource) && resource.exists()) { - try (InputStream is = resource.getInputStream()) { - Properties p = new Properties(); - p.loadFromXML(is); - return Map.class.cast(p); - } catch (Exception e) { - LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + resource); - } - } - return Collections.emptyMap(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathClasspathResolver.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathClasspathResolver.java b/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathClasspathResolver.java deleted file mode 100644 index 38a9644..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathClasspathResolver.java +++ /dev/null @@ -1,60 +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.core.internal.resources; - -import org.apache.tamaya.core.internal.resources.io.PathMatchingResourcePatternResolver; -import org.apache.tamaya.core.spi.PathResolver; -import org.apache.tamaya.core.resource.Resource; - -import java.io.IOException; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class AntPathClasspathResolver implements PathResolver { - - private static final Logger LOG = Logger.getLogger(AntPathClasspathResolver.class.getName()); - - @Override - public String getResolverId(){ - return "classpath"; - } - - @Override - public Collection<Resource> resolve(ClassLoader classLoader, Collection<String> expressions){ - PathMatchingResourcePatternResolver resolver = PathMatchingResourcePatternResolver.of(classLoader); - List<Resource> result = new ArrayList<>(); - expressions.forEach((expression) -> { - try { - Resource[] resources = resolver.getResources(expression); - for (Resource res : resources) { - try { - result.add(res); - } catch (Exception e) { - LOG.log(Level.FINEST, "URI could not be extracted from Resource: " + res.toString(), e); - } - } - } - catch(IOException e){ - LOG.log(Level.FINE, "Failed to load resource expression: " + expression, e); - } - }); - return result; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathFileResolver.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathFileResolver.java b/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathFileResolver.java deleted file mode 100644 index 0645d70..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/AntPathFileResolver.java +++ /dev/null @@ -1,61 +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.core.internal.resources; - -import org.apache.tamaya.core.internal.resources.io.PathMatchingResourcePatternResolver; -import org.apache.tamaya.core.spi.PathResolver; -import org.apache.tamaya.core.resource.Resource; - -import java.io.IOException; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - - -public class AntPathFileResolver implements PathResolver { - - private static final Logger LOG = Logger.getLogger(AntPathFileResolver.class.getName()); - - @Override - public String getResolverId(){ - return "file"; - } - - @Override - public Collection<Resource> resolve(ClassLoader classLoader, Collection<String> expressions){ - PathMatchingResourcePatternResolver resolver = PathMatchingResourcePatternResolver.of(classLoader); - List<Resource> result = new ArrayList<>(); - expressions.forEach((expression) -> { - try { - Resource[] resources = resolver.getResources(expression); - for (Resource res : resources) { - try { - result.add(res); - } catch (Exception e) { - LOG.log(Level.FINE, "URI could not be extracted from Resource: " + res.toString(), e); - } - } - } - catch(IOException e){ - LOG.log(Level.FINE, "Failed to load resource expression: " + expression, e); - } - }); - return result; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/DefaultResourceLoader.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/DefaultResourceLoader.java b/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/DefaultResourceLoader.java deleted file mode 100644 index 9f2a8cb..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/internal/resources/DefaultResourceLoader.java +++ /dev/null @@ -1,81 +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.core.internal.resources; - -import org.apache.tamaya.core.spi.PathResolver; -import org.apache.tamaya.core.resource.Resource; -import org.apache.tamaya.core.resource.ResourceLoader; - -import org.apache.tamaya.spi.ServiceContext; - -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -/** - * Singleton accessor to access registered reader mechanism. - */ -public class DefaultResourceLoader implements ResourceLoader{ - - private static final Logger LOG = Logger.getLogger(DefaultResourceLoader.class.getName()); - - @Override - public Collection<String> getResolverIds(){ - return ServiceContext.getInstance().getServices(PathResolver.class).stream().map(PathResolver::getResolverId) - .collect(Collectors.toSet()); - } - - @Override - public List<Resource> getResources(String... expressions){ - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - cl = getClass().getClassLoader(); - } - return getResources(cl, Arrays.asList(expressions)); - } - - @Override - public List<Resource> getResources(Collection<String> expressions){ - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - cl = getClass().getClassLoader(); - } - return getResources(cl, expressions); - } - - @Override - public List<Resource> getResources(ClassLoader classLoader, String... expressions) { - return getResources(classLoader, Arrays.asList(expressions)); - } - - @Override - public List<Resource> getResources(ClassLoader classLoader, Collection<String> expressions){ - List<Resource> resources = new ArrayList<>(); - for(PathResolver resolver : ServiceContext.getInstance().getServices(PathResolver.class)){ - try{ - resources.addAll(resolver.resolve(classLoader, expressions)); - } - catch(Exception e){ - LOG.log(Level.FINEST, e, () -> "Resource not found: " + expressions.toString()); - } - } - return resources; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/properties/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/properties/EnvironmentPropertySource.java b/dormant/core/src/main/java/org/apache/tamaya/core/properties/EnvironmentPropertySource.java deleted file mode 100644 index f74bcb4..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/properties/EnvironmentPropertySource.java +++ /dev/null @@ -1,48 +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.core.properties; - -import java.util.Map; - -public class EnvironmentPropertySource implements PropertySource { - - private static final long serialVersionUID = 4753258482658331010L; - - private int ordinal; - - public Map<String,String> getProperties(){ - return System.getenv(); - } - - @Override - public int getOrdinal(){ - return ordinal; - } - - @Override - public String getName(){ - return "<System.getenv()>"; - } - - @Override - public String toString(){ - return "PropertySource(System Environment)"; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/properties/MapBasedPropertySource.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/properties/MapBasedPropertySource.java b/dormant/core/src/main/java/org/apache/tamaya/core/properties/MapBasedPropertySource.java deleted file mode 100644 index a224509..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/properties/MapBasedPropertySource.java +++ /dev/null @@ -1,98 +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.core.properties; - -import java.lang.Override; -import java.lang.String; -import java.util.*; -import java.util.logging.Logger; - -/** - * Models a {@link org.apache.tamaya.spi.PropertySource} that can be build using a builder pattern. - */ -public class MapBasedPropertySource implements PropertySource { - - private static final long serialVersionUID = 7601389831472839249L; - - private static final Logger LOG = Logger.getLogger(MapBasedPropertySource.class.getName()); - - private int ordinal; - - - private String name; - /** - * The unit's entries. - */ - private Map<String,String> entries = new HashMap<>(); - - /** - * Constructor. - * - * @param entries the config entries, not null. - */ - public MapBasedPropertySource(int ordinal, String name, Map<String, String> entries){ - this.name = Objects.requireNonNull(name); - this.ordinal = ordinal; - this.entries.putAll(Objects.requireNonNull(entries, "entries required.")); - this.entries = Collections.unmodifiableMap(this.entries); - } - - @Override - public Map<String, String> getProperties() { - return this.entries; - } - - @Override - public int getOrdinal(){ - return ordinal; - } - - @Override - public String getName(){ - return name; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - MapBasedPropertySource that = (MapBasedPropertySource) o; - - if (!entries.equals(that.entries)) return false; - if (!name.equals(that.name)) return false; - - return true; - } - - @Override - public int hashCode() { - int result = name.hashCode(); - result = 31 * result + entries.hashCode(); - return result; - } - - @Override - public String toString() { - return "MapBasedPropertySource{" + - "name=" + name + - ", ordinal=" + ordinal + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/properties/SystemPropertiesPropertySource.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/properties/SystemPropertiesPropertySource.java b/dormant/core/src/main/java/org/apache/tamaya/core/properties/SystemPropertiesPropertySource.java deleted file mode 100644 index 1964b16..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/properties/SystemPropertiesPropertySource.java +++ /dev/null @@ -1,55 +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.core.properties; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; - -public class SystemPropertiesPropertySource implements PropertySource { - - - private static final long serialVersionUID = -5935940312707001199L; - - private int ordinal; - - private String name; - - /** - * Constructor. - */ - public SystemPropertiesPropertySource(int ordinal, String name) { - this.ordinal = ordinal; - this.name = Objects.requireNonNull(name); - - } - - @Override - public Map<String,String> getProperties(){ - return Collections.unmodifiableMap(System.getProperties()); - } - - @Override - public String toString(){ - return "PropertySource[System Properties]"; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/core/src/main/java/org/apache/tamaya/core/spi/PathResolver.java ---------------------------------------------------------------------- diff --git a/dormant/core/src/main/java/org/apache/tamaya/core/spi/PathResolver.java b/dormant/core/src/main/java/org/apache/tamaya/core/spi/PathResolver.java deleted file mode 100644 index 04b5b02..0000000 --- a/dormant/core/src/main/java/org/apache/tamaya/core/spi/PathResolver.java +++ /dev/null @@ -1,45 +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.core.spi; - -import org.apache.tamaya.core.resource.Resource; - -import java.util.Collection; - -/** - * Created by Anatole on 16.06.2014. - */ -public interface PathResolver{ - - /** - * Get the (unique) resolver prefix. - * - * @return the resolver prefix, never null. - */ - public String getResolverId(); - - /** - * Resolve the given expression. - * - * @param expressions expressions, never null. - * @return the resolved URIs, never null. - */ - public Collection<Resource> resolve(ClassLoader classLoader, Collection<String> expressions); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/dormant/pom.xml ---------------------------------------------------------------------- diff --git a/dormant/pom.xml b/dormant/pom.xml new file mode 100644 index 0000000..434563e --- /dev/null +++ b/dormant/pom.xml @@ -0,0 +1,35 @@ +<?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 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. +--> +<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</groupId> + <artifactId>tamaya-all</artifactId> + <version>0.1-SNAPSHOT</version> + </parent> + + <artifactId>buildtools</artifactId> + <name>Apache Tamaya - Build Tools</name> + + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/pom.xml ---------------------------------------------------------------------- diff --git a/modules/formats/pom.xml b/modules/formats/pom.xml index d39c845..65b40f4 100644 --- a/modules/formats/pom.xml +++ b/modules/formats/pom.xml @@ -41,10 +41,5 @@ under the License. <artifactId>tamaya-resources</artifactId> <version>${project.version}</version> </dependency> - <dependency> - <groupId>commons-configuration</groupId> - <artifactId>commons-configuration</artifactId> - <version>1.10</version> - </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/CommonsConfigPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/CommonsConfigPropertySource.java b/modules/formats/src/main/java/org/apache/tamaya/format/CommonsConfigPropertySource.java deleted file mode 100644 index 5c9078d..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/CommonsConfigPropertySource.java +++ /dev/null @@ -1,79 +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.format; - -import org.apache.commons.configuration.Configuration; -import org.apache.tamaya.spi.PropertySource; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Objects; - -/** - * PropertySource that wraps {@link org.apache.commons.configuration.Configuration}. - */ -public class CommonsConfigPropertySource implements PropertySource { - - private Configuration commonsConfig; - private int ordinal; - private String name; - - public CommonsConfigPropertySource(int ordinal, String name, Configuration commonsConfig) { - this.commonsConfig = Objects.requireNonNull(commonsConfig); - this.ordinal = ordinal; - this.name = Objects.requireNonNull(name); - } - - public CommonsConfigPropertySource(String name, Configuration commonsConfig) { - commonsConfig = Objects.requireNonNull(commonsConfig); - this.name = Objects.requireNonNull(name); - try { - this.ordinal = commonsConfig.getInt(PropertySource.TAMAYA_ORDINAL); - } catch (Exception e) { - this.ordinal = 0; - } - } - - @Override - public int getOrdinal() { - return ordinal; - } - - @Override - public String getName() { - return name; - } - - @Override - public String get(String key) { - return commonsConfig.getString(key); - } - - @Override - public Map<String, String> getProperties() { - Map<String, String> config = new HashMap<>(); - Iterator<String> keyIter = commonsConfig.getKeys(); - while (keyIter.hasNext()) { - String key = keyIter.next(); - config.put(key, commonsConfig.getString(key)); - } - return config; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java index e35517b..6628286 100644 --- a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java +++ b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java @@ -52,4 +52,8 @@ public interface ConfigurationFormat { */ ConfigurationData readConfiguration(URL url); + + //X TODO Add support to access a default format to see a correct formatting + //X String getFormatExample(); + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java new file mode 100644 index 0000000..0eab24f --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.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.format; + +import org.apache.tamaya.spi.ServiceContext; + +import java.net.URL; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Small accessor and management class dealing with {@link org.apache.tamaya.format.ConfigurationFormat} + * instances. + */ +public class ConfigurationFormats { + /** + * The logger used. + */ + private static final Logger LOG = Logger.getLogger(ConfigurationFormats.class.getName()); + + /** + * Singleton constructor. + */ + private ConfigurationFormats() { + } + + /** + * Get all currently available formats, ordered by priority. + * + * @return the currently available formats, never null. + */ + public static List<ConfigurationFormat> getFormats() { + return ServiceContext.getInstance().getServices(ConfigurationFormat.class); + } + + /** + * Tries to read configuration data from a given URL, hereby trying all formats in order of precedence. + * + * @param url the url from where to read, not null. + * @return the ConfigurationData read, or null. + */ + public static ConfigurationData readConfigurationData(URL url) { + List<ConfigurationFormat> formats = getFormats(); + return readConfigurationData(url, formats.toArray(new ConfigurationFormat[formats.size()])); + } + + /** + * Tries to read configuration data from a given URL, hereby trying all given formats in order. + * + * @param url the url from where to read, not null. + * @param formats the formats to try. + * @return the ConfigurationData read, or null. + */ + public static ConfigurationData readConfigurationData(URL url, ConfigurationFormat... formats) { + ConfigurationData data = null; + String resource = url.toString(); + for (ConfigurationFormat format : formats) { + try { + data = format.readConfiguration(url); + if (data != null) { + return data; + } + } catch (Exception e) { + LOG.log(Level.INFO, e, + () -> "Format "+format.getClass().getName()+" failed to read resource " + resource); + } + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java b/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java new file mode 100644 index 0000000..ba4fd91 --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java @@ -0,0 +1,60 @@ +/* + * 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.format; + +import org.apache.tamaya.spi.PropertySource; + +import java.util.Collections; +import java.util.Map; + +/** + * Flattened default PropertySource that uses the flattened config data read from an URL by a + * ${@link org.apache.tamaya.format.ConfigurationFormat}. + */ +public class FlattenedDefaultPropertySource implements PropertySource { + + private Map<String, String> data; + + /* + * Constructor, uses hereby the flattened config data read from an URL by a + * ${@link org.apache.tamaya.format.ConfigurationFormat}, and if not present falls back to the default section. + */ + public FlattenedDefaultPropertySource(ConfigurationData data) { + this.data = data.getSection(ConfigurationData.FLATTENED_SECTION_NAME); + if (this.data == null) { + this.data = data.getDefaultSection(); + } + this.data = Collections.unmodifiableMap(this.data); + } + + @Override + public int getOrdinal() { + return 0; // TODO read from config! + } + + @Override + public String get(String key) { + return data.get(key); + } + + @Override + public Map<String, String> getProperties() { + return data; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/IniConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/IniConfigurationFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/IniConfigurationFormat.java deleted file mode 100644 index 4e0efa9..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/IniConfigurationFormat.java +++ /dev/null @@ -1,57 +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.format; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.HierarchicalINIConfiguration; -import org.apache.commons.configuration.SubnodeConfiguration; -import org.apache.tamaya.ConfigException; - -import java.net.URL; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * Implements a ini file format based on the APache Commons - * {@link org.apache.commons.configuration.HierarchicalINIConfiguration}. - */ -public class IniConfigurationFormat implements ConfigurationFormat{ - - @Override - public ConfigurationData readConfiguration(URL url) { - ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(url, this); - try { - HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url); - for(String section:commonIniConfiguration.getSections()){ - SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section); - Map<String, String> properties = new HashMap<>(); - Iterator<String> keyIter = sectionConfig.getKeys(); - while(keyIter.hasNext()){ - String key = keyIter.next(); - properties.put(key, sectionConfig.getString(key)); - } - builder.addProperties(section, properties); - } - } catch (ConfigurationException e) { - throw new ConfigException("Failed to parse ini-file format from " + url, e); - } - return builder.build(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesFormat.java deleted file mode 100644 index 05cc1e4..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesFormat.java +++ /dev/null @@ -1,57 +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.format; - -import java.io.InputStream; -import java.net.URL; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link ConfigurationFormat} for -properties files. - * - * @see java.util.Properties#load(java.io.InputStream) - */ -public class PropertiesFormat implements ConfigurationFormat { - /** - * The logger. - */ - private final static Logger LOG = Logger.getLogger(PropertiesFormat.class.getName()); - - - @SuppressWarnings("unchecked") - @Override - public ConfigurationData readConfiguration(URL url) { - Objects.requireNonNull(url); - - try (InputStream is = url.openStream()) { - if (is != null) { - final Properties p = new Properties(); - p.load(is); - return ConfigurationDataBuilder.of(url, this).addProperties( Map.class.cast(p)).build(); - } - } catch (Exception e) { - LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + url); - } - return ConfigurationDataBuilder.of(url, this).build(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesXmlFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesXmlFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesXmlFormat.java deleted file mode 100644 index 893ad80..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesXmlFormat.java +++ /dev/null @@ -1,56 +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.format; - -import java.io.InputStream; -import java.net.URL; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link ConfigurationFormat} for xml property - * files. - * - * @see java.util.Properties#loadFromXML(java.io.InputStream) - */ -public class PropertiesXmlFormat implements ConfigurationFormat { - /** - * The logger. - */ - private final static Logger LOG = Logger.getLogger(PropertiesXmlFormat.class.getName()); - - - @SuppressWarnings("unchecked") - @Override - public ConfigurationData readConfiguration(URL url) { - Objects.requireNonNull(url); - - try (InputStream is = url.openStream()) { - if (is != null) { - final Properties p = new Properties(); - p.load(is); - return ConfigurationDataBuilder.of(url, this).addProperties( Map.class.cast(p)).build(); - } - } catch (Exception e) { - LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + url); - } - return ConfigurationDataBuilder.of(url, this).build(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java new file mode 100644 index 0000000..f8c0261 --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java @@ -0,0 +1,84 @@ +/* + * 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.format.formats; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationDataBuilder; +import org.apache.tamaya.format.ConfigurationFormat; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Implements a ini file format. + */ +public class IniConfigurationFormat implements ConfigurationFormat { + + /** + * The logger. + */ + private final static Logger LOG = Logger.getLogger(IniConfigurationFormat.class.getName()); + + @Override + public ConfigurationData readConfiguration(URL url) { + ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(url, this); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));) { + String line = reader.readLine(); + int lineNum = 0; + String section = null; + while (line != null) { + lineNum++; + line = line.trim(); + if (line.isEmpty()) { + line = reader.readLine(); + continue; + } + if (line.startsWith("[")) { + int end = line.indexOf(']'); + if (end < 0) { + throw new ConfigException( + "Invalid INI-Format, ']' expected, at " + lineNum + " in " + url); + } + section = line.substring(1, end); + } else if (line.trim().startsWith("#")) { + // comment + } else { + int sep = line.indexOf('='); + String key = line.substring(0, sep); + String value = line.substring(sep + 1); + if (section != null) { + builder.addProperty(ConfigurationData.FLATTENED_SECTION_NAME, section + '.' + key, value); + builder.addProperty(section, key, value); + } else { + builder.addProperty(key, value); + } + } + line = reader.readLine(); + } + return builder.build(); + } catch (Exception e) { + LOG.log(Level.SEVERE, e, () -> "Could not read configuration: " + url); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java new file mode 100644 index 0000000..cd1640d --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java @@ -0,0 +1,61 @@ +/* + * 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.format.formats; + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationDataBuilder; +import org.apache.tamaya.format.ConfigurationFormat; + +import java.io.InputStream; +import java.net.URL; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Implementation of a {@link org.apache.tamaya.format.ConfigurationFormat} for -properties files. + * + * @see java.util.Properties#load(java.io.InputStream) + */ +public class PropertiesFormat implements ConfigurationFormat { + /** + * The logger. + */ + private final static Logger LOG = Logger.getLogger(PropertiesFormat.class.getName()); + + + @SuppressWarnings("unchecked") + @Override + public ConfigurationData readConfiguration(URL url) { + Objects.requireNonNull(url); + + try (InputStream is = url.openStream()) { + if (is != null) { + final Properties p = new Properties(); + p.load(is); + return ConfigurationDataBuilder.of(url, this).addProperties( Map.class.cast(p)).build(); + } + } catch (Exception e) { + LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + url); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java new file mode 100644 index 0000000..39b97da --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java @@ -0,0 +1,60 @@ +/* + * 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.format.formats; + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationDataBuilder; +import org.apache.tamaya.format.ConfigurationFormat; + +import java.io.InputStream; +import java.net.URL; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Implementation of a {@link org.apache.tamaya.format.ConfigurationFormat} for xml property + * files. + * + * @see java.util.Properties#loadFromXML(java.io.InputStream) + */ +public class PropertiesXmlFormat implements ConfigurationFormat { + /** + * The logger. + */ + private final static Logger LOG = Logger.getLogger(PropertiesXmlFormat.class.getName()); + + + @SuppressWarnings("unchecked") + @Override + public ConfigurationData readConfiguration(URL url) { + Objects.requireNonNull(url); + + try (InputStream is = url.openStream()) { + if (is != null) { + final Properties p = new Properties(); + p.load(is); + return ConfigurationDataBuilder.of(url, this).addProperties( Map.class.cast(p)).build(); + } + } catch (Exception e) { + LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + url); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/integration/commons/pom.xml ---------------------------------------------------------------------- diff --git a/modules/integration/commons/pom.xml b/modules/integration/commons/pom.xml new file mode 100644 index 0000000..e11a8f6 --- /dev/null +++ b/modules/integration/commons/pom.xml @@ -0,0 +1,50 @@ +<!-- +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.ext.integration</groupId> + <artifactId>tamaya-integrations</artifactId> + <version>0.1-incubating-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <artifactId>commons</artifactId> + <name>Apache Tamaya Commons Configuration Integration</name> + <packaging>jar</packaging> + + <dependencies> + <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>commons-configuration</groupId> + <artifactId>commons-configuration</artifactId> + <version>1.10</version> + </dependency> + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bd997069/modules/integration/commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/integration/commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java b/modules/integration/commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java new file mode 100644 index 0000000..b2f2e82 --- /dev/null +++ b/modules/integration/commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java @@ -0,0 +1,81 @@ +/* + * 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.commons; + +//X TODO Move out into separate commons-config integration module... + +import org.apache.commons.configuration.Configuration; +import org.apache.tamaya.spi.PropertySource; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; + +/** + * PropertySource that wraps {@link org.apache.commons.configuration.Configuration}. + */ +public class CommonsConfigPropertySource implements PropertySource { + + private Configuration commonsConfig; + private int ordinal; + private String name; + + public CommonsConfigPropertySource(int ordinal, String name, Configuration commonsConfig) { + this.commonsConfig = Objects.requireNonNull(commonsConfig); + this.ordinal = ordinal; + this.name = Objects.requireNonNull(name); + } + + public CommonsConfigPropertySource(String name, Configuration commonsConfig) { + commonsConfig = Objects.requireNonNull(commonsConfig); + this.name = Objects.requireNonNull(name); + try { + this.ordinal = commonsConfig.getInt(PropertySource.TAMAYA_ORDINAL); + } catch (Exception e) { + this.ordinal = 0; + } + } + + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return name; + } + + @Override + public String get(String key) { + return commonsConfig.getString(key); + } + + @Override + public Map<String, String> getProperties() { + Map<String, String> config = new HashMap<>(); + Iterator<String> keyIter = commonsConfig.getKeys(); + while (keyIter.hasNext()) { + String key = keyIter.next(); + config.put(key, commonsConfig.getString(key)); + } + return config; + } +}