Removed non core functionality, adaptedd tests.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b5731e09 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b5731e09 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b5731e09 Branch: refs/heads/master Commit: b5731e091ff1f7923dcbfe6fa9000b9db3d26308 Parents: 64a6af7 Author: anatole <anat...@apache.org> Authored: Sun Jan 4 19:12:42 2015 +0100 Committer: anatole <anat...@apache.org> Committed: Sun Jan 4 19:12:42 2015 +0100 ---------------------------------------------------------------------- .../core/PathBasedPropertySourceProvider.java | 93 ---------- .../core/ResourcePropertySourceProvider.java | 102 ----------- .../core/formats/ConfigurationFormat.java | 53 ------ .../tamaya/core/formats/PropertiesFormat.java | 114 ------------ .../core/formats/PropertiesXmlFormat.java | 115 ------------ .../internal/resource/ClassPathResource.java | 178 ------------------- .../resource/DefaultResourceLoader.java | 93 ---------- .../core/internal/resource/FileResource.java | 162 ----------------- .../internal/resource/InputStreamResource.java | 117 ------------ .../core/internal/resource/UrlResource.java | 135 -------------- .../core/propertysource/BasePropertySource.java | 6 +- .../core/propertysource/DefaultOrdinal.java | 2 + .../EnvironmentPropertySource.java | 2 +- .../propertysource/SystemPropertySource.java | 6 +- .../core/resources/InputStreamSupplier.java | 37 ---- .../apache/tamaya/core/resources/Resource.java | 114 ------------ .../tamaya/core/resources/ResourceLoader.java | 90 ---------- ....apache.tamaya.core.resources.ResourceLoader | 2 +- .../propertysource/BasePropertySourceTest.java | 4 +- .../testdata/TestPropertyDefaultSource.java | 51 ++++++ .../TestPropertyDefaultSourceProvider.java | 32 ---- .../testdata/TestPropertySourceProvider.java | 51 +++++- .../org.apache.tamaya.spi.PropertySource | 19 ++ ...org.apache.tamaya.spi.PropertySourceProvider | 1 - .../resources/cfg/defaults/test1.properties | 20 --- .../cfg/final/anotherTestFile.properties | 22 --- .../tamaya/format/ConfigurationFormat.java | 53 ++++++ .../apache/tamaya/format/PropertiesFormat.java | 114 ++++++++++++ .../tamaya/format/PropertiesXmlFormat.java | 115 ++++++++++++ .../tamaya/resource/InputStreamSupplier.java | 37 ++++ .../PathBasedPropertySourceProvider.java | 93 ++++++++++ .../org/apache/tamaya/resource/Resource.java | 114 ++++++++++++ .../apache/tamaya/resource/ResourceLoader.java | 90 ++++++++++ .../ResourcePropertySourceProvider.java | 102 +++++++++++ .../resource/internal/ClassPathResource.java | 178 +++++++++++++++++++ .../internal/DefaultResourceLoader.java | 93 ++++++++++ .../tamaya/resource/internal/FileResource.java | 162 +++++++++++++++++ .../resource/internal/InputStreamResource.java | 117 ++++++++++++ .../tamaya/resource/internal/UrlResource.java | 135 ++++++++++++++ 39 files changed, 1530 insertions(+), 1494 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/PathBasedPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/PathBasedPropertySourceProvider.java b/core/src/main/java/org/apache/tamaya/core/PathBasedPropertySourceProvider.java deleted file mode 100644 index 31dbf4b..0000000 --- a/core/src/main/java/org/apache/tamaya/core/PathBasedPropertySourceProvider.java +++ /dev/null @@ -1,93 +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; - -import org.apache.tamaya.core.formats.ConfigurationFormat; -import org.apache.tamaya.core.resources.Resource; -import org.apache.tamaya.core.resources.ResourceLoader; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spi.ServiceContext; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link PropertySourceProvider} that reads configuration from some given resource paths - * and using the given formats. The resource path are resolved using the current - * {@link org.apache.tamaya.core.resources.ResourceLoader} active. - */ -public class PathBasedPropertySourceProvider implements PropertySourceProvider { - /** The logger used. */ - private static final Logger LOG = Logger.getLogger(PathBasedPropertySourceProvider.class.getName()); - /** The property source base name, will be used for creating a useful name of the - * {@link org.apache.tamaya.spi.PropertySource} created. */ - private String sourceName; - /** The config formats supported for the given location/resource paths. */ - private List<ConfigurationFormat> configFormats = new ArrayList<>(); - /** The paths to be evaluated. */ - private List<String> paths = new ArrayList<>(); - - /** - * Creates a new instance. - * @param sourceName the base name of the configuration, used for creating PropertySource child names. - * @param formats the formats to be used, not null, not empty. - * @param paths the paths to be resolved, not null, not empty. - */ - public PathBasedPropertySourceProvider(String sourceName, List<ConfigurationFormat> formats, String... paths) { - this.sourceName = Objects.requireNonNull(sourceName); - this.configFormats.addAll(Objects.requireNonNull(formats)); - this.paths.addAll(Arrays.asList(Objects.requireNonNull(paths))); - } - - /** - * Creates a new instance. - * @param sourceName the base name of the configuration, used for creating PropertySource child names. - * @param format the format to be used. - * @param paths the paths to be resolved, not null, not empty. - */ - public PathBasedPropertySourceProvider(String sourceName, ConfigurationFormat format, String... paths) { - this.sourceName = Objects.requireNonNull(sourceName); - this.configFormats.add(Objects.requireNonNull(format)); - this.paths.addAll(Arrays.asList(Objects.requireNonNull(paths))); - } - - @Override - public Collection<PropertySource> getPropertySources() { - List<PropertySource> propertySources = new ArrayList<>(); - paths.forEach((path) -> { - for (Resource res : ServiceContext.getInstance().getService(ResourceLoader.class).get().getResources(path)) { - try { - for (ConfigurationFormat format : configFormats) { - propertySources.addAll(format.readConfiguration(sourceName, res)); - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to add resource based config: " + res.getName(), e); - } - } - }); - return propertySources; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/ResourcePropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/ResourcePropertySourceProvider.java b/core/src/main/java/org/apache/tamaya/core/ResourcePropertySourceProvider.java deleted file mode 100644 index de6b578..0000000 --- a/core/src/main/java/org/apache/tamaya/core/ResourcePropertySourceProvider.java +++ /dev/null @@ -1,102 +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; - -import org.apache.tamaya.core.formats.ConfigurationFormat; -import org.apache.tamaya.core.resources.Resource; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Objects; -import java.util.logging.Logger; - -/** - * Implementation of a {@link org.apache.tamaya.spi.PropertySourceProvider} that is based on a single resource - * and a number of formats. - */ -public class ResourcePropertySourceProvider implements PropertySourceProvider { - /** The logger used. */ - private static final Logger LOG = Logger.getLogger(ResourcePropertySourceProvider.class.getName()); - /** The supported formats. */ - private List<ConfigurationFormat> formats = new ArrayList<>(); - /** The resource. */ - private Resource resource; - /** The source name used for creating the PropertySource names. */ - private String sourceName; - - /** - * Creates a new instance. - * @param resource the {@link org.apache.tamaya.core.resources.Resource}, not null. - * @param formats the supported formats, not empty. - */ - public ResourcePropertySourceProvider(String sourceName, Resource resource, ConfigurationFormat... formats) { - this(sourceName, resource, Arrays.asList(formats)); - } - - /** - * Creates a new instance. - * @param resource the {@link org.apache.tamaya.core.resources.Resource}, not null. - * @param formats the supported formats, not empty. - */ - public ResourcePropertySourceProvider(String sourceName, Resource resource, List<ConfigurationFormat> formats) { - this.resource = Objects.requireNonNull(resource); - this.sourceName = Objects.requireNonNull(sourceName); - if(formats.size()==0){ - throw new IllegalArgumentException("Format required."); - } - this.formats.addAll(formats); - } - - - /** - * Get the underlying resource. - * - * @return the underlying resource, never null. - */ - public Resource getResource() { - return this.resource; - } - - - @Override - public String toString() { - return "ResourcePropertySourceProvider{" + - "resource=" + resource + - ", formats=+" + formats + - '}'; - } - - @Override - public Collection<PropertySource> getPropertySources() { - List<PropertySource> propertySources = new ArrayList<>(); - for (ConfigurationFormat format : formats) { - try { - propertySources.addAll(format.readConfiguration(sourceName, resource)); - } catch (Exception e) { - LOG.info(() -> "Format was not matching: " + format + " for resource: " + resource.getName()); - } - } - return propertySources; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/formats/ConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/formats/ConfigurationFormat.java b/core/src/main/java/org/apache/tamaya/core/formats/ConfigurationFormat.java deleted file mode 100644 index 5d289bb..0000000 --- a/core/src/main/java/org/apache/tamaya/core/formats/ConfigurationFormat.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.formats; - -import org.apache.tamaya.core.resources.Resource; -import org.apache.tamaya.spi.PropertySource; - -import java.io.IOException; -import java.util.Collection; - -/** - * Implementations current this class encapsulate the mechanism how to read a - * resource including interpreting the format correctly (e.g. xml vs. - * properties). In most cases file only contains entries of the same priority, which would then - * result in only one {@link PropertySource}. Complex file formats, hoiwever, may contain entries - * of different priorities. In this cases, each ordinal type found must be returned as a separate - * {@link PropertySource} instance. - */ -@FunctionalInterface -public interface ConfigurationFormat { - - /** - * Reads a list {@link org.apache.tamaya.spi.PropertySource} instances from a resource, using this format. - * If the configuration format only contains entries of one ordinal type, normally only one single - * instance of PropertySource is returned. Nevertheless custom formats may contain different sections or parts, - * where each part maps to a different target rdinal (eg defaults, domain config and app config). In the - * ladder case multiple PropertySources can be returned, each one with its own ordinal and the corresponding - * entries. - * - * @param sourceName name to be used for constructing a useful name for the created - * {@link org.apache.tamaya.spi.PropertySource} instances. - * @param resource the configuration resource, not null - * @return the corresponding {@link org.apache.tamaya.spi.PropertySource} instances, never {@code null}. - */ - Collection<PropertySource> readConfiguration(String sourceName, Resource resource) throws IOException; - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/formats/PropertiesFormat.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/formats/PropertiesFormat.java b/core/src/main/java/org/apache/tamaya/core/formats/PropertiesFormat.java deleted file mode 100644 index 3584f8e..0000000 --- a/core/src/main/java/org/apache/tamaya/core/formats/PropertiesFormat.java +++ /dev/null @@ -1,114 +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.formats; - -import org.apache.tamaya.core.resources.Resource; -import org.apache.tamaya.spi.PropertySource; - -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link org.apache.tamaya.core.formats.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()); - - /** - * The target ordinal. - */ - private int ordinal; - - /** - * Creates a new format instance, hereby producing entries with the given ordinal, if not overridden by the - * configuration itself. - * TODO document and implement override feature - * @param ordinal the target ordinal. - */ - public PropertiesFormat(int ordinal) { - this.ordinal = ordinal; - } - - /** - * Get the target ordinal, produced by this format. - * - * @return the target ordinal - */ - public int getOrdinal() { - return ordinal; - } - - @SuppressWarnings("unchecked") - @Override - public Collection<PropertySource> readConfiguration(String baseName, Resource resource) { - final String sourceName = (baseName==null?"Properties:":baseName) + resource.getName(); - if (resource.exists()) { - List<PropertySource> propertySources = new ArrayList<>(); - try (InputStream is = resource.getInputStream()) { - final Properties p = new Properties(); - p.load(is); - propertySources.add(new PropertySource() { - @Override - public int getOrdinal() { - return ordinal; - } - - @Override - public String getName() { - return sourceName; - } - - @Override - public Optional<String> get(String key) { - return Optional.ofNullable(p.getProperty(key)); - } - - @Override - public Map<String, String> getProperties() { - return Map.class.cast(p); - } - }); - return propertySources; - } catch (Exception e) { - LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + resource); - } - } - return Collections.emptyList(); - } - - @Override - public String toString() { - return "PropertiesFormat{" + - "ordinal=" + ordinal + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/formats/PropertiesXmlFormat.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/formats/PropertiesXmlFormat.java b/core/src/main/java/org/apache/tamaya/core/formats/PropertiesXmlFormat.java deleted file mode 100644 index 3f77810..0000000 --- a/core/src/main/java/org/apache/tamaya/core/formats/PropertiesXmlFormat.java +++ /dev/null @@ -1,115 +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.formats; - -import org.apache.tamaya.core.resources.Resource; -import org.apache.tamaya.spi.PropertySource; - -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link org.apache.tamaya.core.formats.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()); - - /** - * The target ordinal. - */ - private int ordinal; - - /** - * Creates a new format instance, producing entries for the given ordinal, if not overridden by a - * config entry itself. - * TODO document and implement override feature - * @param ordinal the target ordinal. - */ - public PropertiesXmlFormat(int ordinal) { - this.ordinal = ordinal; - } - - /** - * Get the target ordinal, produced by this format. - * - * @return the target ordinal - */ - public int getOrdinal() { - return ordinal; - } - - @SuppressWarnings("unchecked") - @Override - public Collection<PropertySource> readConfiguration(String baseName, Resource resource) { - if (resource.exists()) { - final String sourceName = (baseName==null?"Properties:":baseName) + resource.getName(); - List<PropertySource> propertySources = new ArrayList<>(); - try (InputStream is = resource.getInputStream()) { - final Properties p = new Properties(); - p.loadFromXML(is); - propertySources.add(new PropertySource() { - @Override - public int getOrdinal() { - return ordinal; - } - - @Override - public String getName() { - return sourceName; - } - - @Override - public Optional<String> get(String key) { - return Optional.ofNullable(p.getProperty(key)); - } - - @Override - public Map<String, String> getProperties() { - return Map.class.cast(p); - } - }); - return propertySources; - } catch (Exception e) { - LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + resource); - } - } - return Collections.emptyList(); - } - - @Override - public String toString() { - return "PropertiesXmlFormat{" + - "ordinal=" + ordinal + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java deleted file mode 100644 index 21806cd..0000000 --- a/core/src/main/java/org/apache/tamaya/core/internal/resource/ClassPathResource.java +++ /dev/null @@ -1,178 +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.resource; - -import org.apache.tamaya.core.resources.Resource; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.Objects; - -/** - * Implementation of {@link Resource} to be loaded from the classpath. - */ -public class ClassPathResource implements Resource { - - private final String path; - - private ClassLoader classLoader; - - - /** - * Create a new resource using the current context class loader. - * - * @param path the resource path, not null - * @see java.lang.ClassLoader#getResourceAsStream(String) - */ - public ClassPathResource(String path) { - this(path, (ClassLoader) null); - } - - /** - * Create a new resource using the given class loader. - * - * @param path the resource path, not null - * @param classLoader the class loader to load the resource with, - * or {@code null} for the current context class loader - * @see ClassLoader#getResourceAsStream(String) - */ - public ClassPathResource(String path, ClassLoader classLoader) { - Objects.requireNonNull(path, "Path null"); - if (path.startsWith("/")) { - path = path.substring(1); - } - this.path = path.trim(); - if(classLoader==null){ - classLoader = Thread.currentThread().getContextClassLoader(); - } - if(classLoader==null){ - classLoader = getClass().getClassLoader(); - } - this.classLoader = classLoader; - } - - /** - * @return the path for this resource. - */ - public final String getPath() { - return this.path; - } - - /** - * @return the ClassLoader that this resource will be accessed from. - */ - public final ClassLoader getClassLoader() { - return this.classLoader; - } - - - /** - * Checks if the given resource is resolvable from the configured classloader. - * - * @see java.lang.ClassLoader#getResource(String) - */ - @Override - public boolean exists() { - return (resolveURL() != null); - } - - /** - * Resolves a URL for the underlying class path resource. - * - * @return the resolved URL, or {@code null} - */ - protected URL resolveURL() { - return this.classLoader.getResource(this.path); - } - - /** - * This implementation opens an InputStream for the given class path resource. - * - * @see java.lang.ClassLoader#getResourceAsStream(String) - * @see java.lang.Class#getResourceAsStream(String) - */ - @Override - public InputStream getInputStream() throws IOException { - InputStream is = this.classLoader.getResourceAsStream(this.path); - if (is == null) { - throw new IOException(getName() + " does not exist"); - } - return is; - } - - @Override - public URI toURI() throws IOException { - try { - return resolveURL().toURI(); - } catch (URISyntaxException e) { - throw new IOException(e); - } - } - - @Override - public long lastModified() throws IOException { - return 0; - } - - /** - * This implementation returns the name current the file that this class path - * resource refers to. - */ - @Override - public String getName() { - return "classpath:"+path; - } - - /** - * This implementation returns a description that includes the class path location. - */ - @Override - public String toString() { - return "ClassPathResource[" + path + ']'; - } - - /** - * This implementation compares the underlying class path locations. - */ - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (obj instanceof ClassPathResource) { - ClassPathResource otherRes = (ClassPathResource) obj; - return (this.path.equals(otherRes.path) && - Objects.equals(this.classLoader, otherRes.classLoader)); - } - return false; - } - - /** - * This implementation returns the hash code current the underlying - * class path location. - */ - @Override - public int hashCode() { - return getName().hashCode(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java deleted file mode 100644 index 5198717..0000000 --- a/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java +++ /dev/null @@ -1,93 +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.resource; - -import org.apache.tamaya.core.resources.Resource; -import org.apache.tamaya.core.resources.ResourceLoader; - -import javax.annotation.Priority; -import java.io.File; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Enumeration; -import java.util.List; -import java.util.logging.Logger; - -/** - * Simple default implementation of the resource loader, which does only support direct references to files. - */ -@Priority(0) -public class DefaultResourceLoader implements ResourceLoader { - - private static final Logger LOG = Logger.getLogger(DefaultResourceLoader.class.getName()); - - @Override - public List<Resource> getResources(ClassLoader classLoader, Collection<String> expressions) { - List<Resource> resources = new ArrayList<>(); - for (String expression : expressions) { - if (tryClassPath(classLoader, expression, resources) || tryFile(expression, resources) || - tryURL(expression, resources)) { - continue; - } - LOG.warning("Failed to resolve resource: " + expression); - } - return resources; - } - - private boolean tryClassPath(ClassLoader classLoader, String expression, List<Resource> resources) { - try { - Enumeration<URL> urls = classLoader.getResources(expression); - while (urls.hasMoreElements()) { - URL url = urls.nextElement(); - resources.add(new UrlResource(url)); - } - return !resources.isEmpty(); - } catch (Exception e) { - LOG.finest(() -> "Failed to load resource from CP: " + expression); - } - return false; - } - - private boolean tryFile(String expression, List<Resource> resources) { - try { - File file = new File(expression); - if (file.exists()) { - resources.add(new FileResource(file)); - return true; - } - } catch (Exception e) { - LOG.finest(() -> "Failed to load resource from file: " + expression); - } - return false; - } - - private boolean tryURL(String expression, List<Resource> resources) { - try { - URL url = new URL(expression); - resources.add(new UrlResource(url)); - return true; - } catch (Exception e) { - LOG.finest(() -> "Failed to load resource from file: " + expression); - } - return false; - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/internal/resource/FileResource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/FileResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/FileResource.java deleted file mode 100644 index e0096e5..0000000 --- a/core/src/main/java/org/apache/tamaya/core/internal/resource/FileResource.java +++ /dev/null @@ -1,162 +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.resource; - -import org.apache.tamaya.core.resources.Resource; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.util.Objects; - -/** - * Implementation of {@link org.apache.tamaya.core.resources.Resource} to be loaded from a file. - * @see java.io.File - */ -public class FileResource implements Resource { - - private final File file; - - /** - * Creates a new instance. - * - * @param file a File, not null. - */ - public FileResource(File file) { - this.file = Objects.requireNonNull(file, "File must not be null"); - } - - /** - * Crreates a new instance. - * - * @param filePath a file path - */ - public FileResource(String filePath) { - Objects.requireNonNull(filePath, "Path must not be null"); - this.file = new File(filePath); - } - - - /** - * Get the file path for this resource. - */ - public final String getPath() { - return this.file.getPath(); - } - - - /** - * This implementation returns whether the underlying file exists. - * - * @see java.io.File#exists() - */ - @Override - public boolean exists() { - return this.file.exists(); - } - - /** - * This implementation checks whether the underlying file is marked as readable - * (and corresponds to an actual file with content, not to a directory). - * - * @see java.io.File#canRead() - * @see java.io.File#isDirectory() - */ - @Override - public boolean isAccessible() { - return (this.file.canRead() && !this.file.isDirectory()); - } - - /** - * This implementation opens a FileInputStream for the underlying file. - * - * @see java.io.FileInputStream - */ - @Override - public InputStream getInputStream() throws IOException { - return new FileInputStream(this.file); - } - - /** - * This implementation returns a URI for the underlying file. - * - * @see java.io.File#toURI() - */ - @Override - public URI toURI() throws IOException { - return this.file.toURI(); - } - - /** - * Returns the underlying File's length. - */ - @Override - public long length() throws IOException { - return this.file.length(); - } - - @Override - public long lastModified() throws IOException { - return file.lastModified(); - } - - /** - * Returns the name of the current file. - * - * @see java.io.File#getName() - */ - @Override - public String getName() { - return this.file.getName(); - } - - /** - * Returns a description that includes the absolute - * path of the current file. - * - * @see java.io.File#getAbsolutePath() - */ - @Override - public String toString() { - return "File [" + this.file.getAbsolutePath() + "]"; - } - - - // implementation current WritableResource - - /** - * Compares the underlying Files. - */ - @Override - public boolean equals(Object obj) { - return (obj == this || - (obj instanceof FileResource && this.file.equals(((FileResource) obj).file))); - } - - /** - * Returns hash code current the underlying File reference. - */ - @Override - public int hashCode() { - return this.file.hashCode(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java deleted file mode 100644 index 09510b4..0000000 --- a/core/src/main/java/org/apache/tamaya/core/internal/resource/InputStreamResource.java +++ /dev/null @@ -1,117 +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.resource; - -import org.apache.tamaya.core.resources.Resource; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.util.Objects; - -/** - * Simple Resource encapsulating an InputStream. - */ -public class InputStreamResource implements Resource { - - /** The InputStream. */ - private final InputStream inputStream; - /** The read flag. */ - private boolean read = false; - /** The name of the resource. */ - private String name; - - - /** - * Create a new InputStreamResource. - * - * @param inputStream the InputStream to use - */ - public InputStreamResource(InputStream inputStream) { - this(inputStream, "InputStream:"); - } - - /** - * Create a new InputStreamResource. - * - * @param inputStream the InputStream to use - * @param name where the InputStream comes from - */ - public InputStreamResource(InputStream inputStream, String name) { - this.inputStream = Objects.requireNonNull(inputStream); - this.name = (name != null ? name : "InputStream"); - } - - - /** - * This implementation always returns {@code true}. - */ - @Override - public boolean exists() { - return true; - } - - @Override - public URI toURI() throws IOException { - throw new IOException("URI not available."); - } - - @Override - public long lastModified() throws IOException { - throw new IOException("lastModified not available."); - } - - /** - * Accesses the input stream. Hereby the input stream can only accessed once. - */ - @Override - public InputStream getInputStream() throws IOException { - if (this.read) { - throw new IllegalStateException("InputStream can only be read once!"); - } - this.read = true; - return this.inputStream; - } - - /** - * This implementation returns the passed-in description, if any. - */ - public String toString() { - return this.name != null ? this.name : super.toString(); - } - - - /** - * Compares the underlying InputStream. - */ - @Override - public boolean equals(Object obj) { - return (obj == this || - (obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream))); - } - - /** - * This implementation returns the hash code current the underlying InputStream. - */ - @Override - public int hashCode() { - return this.inputStream.hashCode(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java deleted file mode 100644 index 3a74608..0000000 --- a/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java +++ /dev/null @@ -1,135 +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.resource; - -import org.apache.tamaya.core.resources.Resource; - -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.util.Objects; - -/** - * Implementation of a resource based on a {@code java.net.URL}. - */ -public class UrlResource implements Resource { - - /** - * Original URL, used for actual access. - */ - private final URL url; - - /** - * Create a new instance based on the given URL. - * - * @param url a URL - */ - public UrlResource(URL url) { - this.url = Objects.requireNonNull(url, "URL null"); - } - - /** - * Create a new URLResource based on a URL path. - * - * @param path a URL path - * @throws MalformedURLException if the given URL path is not valid - * @see java.net.URL#URL(String) - */ - public UrlResource(String path) throws MalformedURLException { - Objects.requireNonNull(path, "Path must not be null"); - this.url = new URL(path); - } - - - /** - * This implementation opens an InputStream for the given URL. - * - * @see java.net.URL#openConnection() - * @see java.net.URLConnection#setUseCaches(boolean) - * @see java.net.URLConnection#getInputStream() - */ - @Override - public InputStream getInputStream() throws IOException { - URLConnection con = null; - try { - con = this.url.openConnection(); - useCachesIfNecessary(con); - return con.getInputStream(); - } catch (IOException e) { - if (con instanceof HttpURLConnection) { - ((HttpURLConnection) con).disconnect(); - } - throw e; - } - } - - @Override - public URI toURI() throws IOException { - try { - return this.url.toURI(); - } catch (URISyntaxException e) { - throw new IOException("Failed to create URI from " + url); - } - } - - @Override - public String getName() { - return this.url.toString(); - } - - @Override - public String toString() { - return "URL [" + this.url + "]"; - } - - /** - * Compares the underlying URL references. - */ - @Override - public boolean equals(Object obj) { - return (obj == this || - (obj instanceof UrlResource && this.url.equals(((UrlResource) obj).url))); - } - - /** - * This implementation returns the hash code current the underlying URL reference. - */ - @Override - public int hashCode() { - return this.url.hashCode(); - } - - /** - * Set the {@link URLConnection#setUseCaches "useCaches"} flag on the - * given connection, preferring {@code false} but leaving the - * flag at {@code true} for JNLP based resources. - * - * @param con the URLConnection to set the flag on - */ - private void useCachesIfNecessary(URLConnection con) { - con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP")); - } - -} - http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java b/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java index 2a0107c..e58be54 100644 --- a/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java +++ b/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java @@ -26,7 +26,9 @@ import java.util.logging.Level; import java.util.logging.Logger; /** - * Base class for {@link PropertySource}s + * A base class for {@link PropertySource}s. It provides a {@link #initializeOrdinal(int)} method that + * reads the ordinal from the config source itself, allowing the ordinal to be "self-configured" by + * the configuration read. */ public abstract class BasePropertySource implements PropertySource { @@ -57,7 +59,7 @@ public abstract class BasePropertySource implements PropertySource { * * @param defaultOrdinal of the {@link PropertySource} */ - protected void initialzeOrdinal(final int defaultOrdinal) { + protected void initializeOrdinal(final int defaultOrdinal) { this.ordinal = defaultOrdinal; Optional<String> ordinal = get(PropertySource.TAMAYA_ORDINAL); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java b/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java index 38ec905..9e5d5dc 100644 --- a/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java +++ b/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java @@ -36,6 +36,8 @@ public final class DefaultOrdinal { */ public static final int PROPERTY_SOURCE = 1000; + //X TODO disuss default ordinal of SystemProperties + /** * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertySource} */ http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java b/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java index d1a00f4..596ea73 100644 --- a/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java +++ b/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java @@ -29,7 +29,7 @@ import java.util.Map; public class EnvironmentPropertySource extends BasePropertySource { public EnvironmentPropertySource() { - initialzeOrdinal(DefaultOrdinal.ENVIRONMENT_PROPERTIES); + initializeOrdinal(DefaultOrdinal.ENVIRONMENT_PROPERTIES); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java b/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java index afd9f93..e06e185 100644 --- a/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java +++ b/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java @@ -28,10 +28,6 @@ import java.util.Properties; */ public class SystemPropertySource extends BasePropertySource { - - //X TODO disuss default ordinal of SystemProperties - - /** * cashed System.getProperties() filled in our Map */ @@ -45,7 +41,7 @@ public class SystemPropertySource extends BasePropertySource { public SystemPropertySource() { - initialzeOrdinal(DefaultOrdinal.SYSTEM_PROPERTIES); + initializeOrdinal(DefaultOrdinal.SYSTEM_PROPERTIES); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/resources/InputStreamSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/resources/InputStreamSupplier.java b/core/src/main/java/org/apache/tamaya/core/resources/InputStreamSupplier.java deleted file mode 100644 index 4da6c53..0000000 --- a/core/src/main/java/org/apache/tamaya/core/resources/InputStreamSupplier.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.core.resources; - -import java.io.IOException; -import java.io.InputStream; - -/** - * Simple interface for a component that provides data based on an InputStream. - */ -@FunctionalInterface -public interface InputStreamSupplier { - - /** - * Access the input stream. - * @return the input stream for use. - * @throws IOException i the input stream could not be obtained. - */ - InputStream getInputStream() throws IOException; - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/resources/Resource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/resources/Resource.java b/core/src/main/java/org/apache/tamaya/core/resources/Resource.java deleted file mode 100644 index 7e17d1b..0000000 --- a/core/src/main/java/org/apache/tamaya/core/resources/Resource.java +++ /dev/null @@ -1,114 +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.resources; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.util.Objects; - -/** - * Interface for an abstract resource. The effective resource implementation can be completely arbitrary. - * By default files, classpath resources and URLs are supported, but alternate implementations are possible. - * - * @see #getInputStream() - * @see #toURI() - */ -public interface Resource extends InputStreamSupplier { - - /** - * Return whether this resource actually exists. Depending on the resource this can delegate to - * {@link java.io.File#exists()} or whatever may be appropriate to check accessibility of the resource. - */ - default boolean exists() { - // Try to open a file first, if that fails try to open the stream... - try { - return new File(toURI()).exists(); - } catch (IOException ex) { - // Fallback - try { - InputStream is = getInputStream(); - is.close(); - return true; - } catch (Exception e) { - // ignore, just return false for non existing - return false; - } - } - } - - /** - * Checks whether the resource is accessible, meaning {@link #getInputStream()} should return a InputStream for reading the - * resource's content. - * - * @see #getInputStream() - */ - default boolean isAccessible() { - return true; - } - - /** - * Returns the resource as an URI. - * - * @throws IOException if the resource cannot be resolved as URI. - */ - URI toURI() throws IOException; - - /** - * Determines the length for this resource. - * - * @throws IOException if the resource is not readable. - */ - default long length() throws IOException { - try(InputStream is = this.getInputStream();) { - Objects.requireNonNull(is, "resource not available"); - long length = 0; - byte[] buf = new byte[256]; - int bytesRead; - while ((bytesRead = is.read(buf)) > 0) { - length += bytesRead; - } - return length; - } - } - - /** - * Determine the last-modified timestamp for a resource, as UTC ms timestamp - * - * @throws IOException if the resource is not accessible. - */ - default long lastModified() throws IOException{ - return new File(toURI()).lastModified(); - } - - /** - * Get a name for the resource. The name should be identifying the resource and also - * never change, so it must be eligible for hashcode/equals implementations. - */ - default String getName() { - try { - return toURI().toString(); - } catch (Exception e) { - return toString(); - } - } - - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/java/org/apache/tamaya/core/resources/ResourceLoader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/tamaya/core/resources/ResourceLoader.java b/core/src/main/java/org/apache/tamaya/core/resources/ResourceLoader.java deleted file mode 100644 index 0f33201..0000000 --- a/core/src/main/java/org/apache/tamaya/core/resources/ResourceLoader.java +++ /dev/null @@ -1,90 +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.resources; - -import java.util.Arrays; -import java.util.Collection; - -/** - * Interface to be implemented by modules. By default only direct file/resource resolution is supported, whereas - * extension modules may add functionality to perform ant styled pattern resolution of resources. - */ -public interface ResourceLoader { - - /** - * Resolves resource expressions to a list of {@link org.apache.tamaya.core.resources.Resource}s. Hereby - * the ordering of resources matches the input of the resolved expressions. Nevertheless be aware that - * there is no determined ordering of resources located within a classloader. - * - * @param expressions the expressions to be resolved, not empty. - * @return the corresponding collection of current {@link org.apache.tamaya.core.resources.Resource}s found, never - * null. - * . - */ - default Collection<Resource> getResources(Collection<String> expressions) { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if (cl == null) { - cl = getClass().getClassLoader(); - } - return getResources(cl, expressions); - } - - /** - * Resolves resource expressions to a list of {@link org.apache.tamaya.core.resources.Resource}s. Hereby - * the ordering of resources matches the input of the resolved expressions. Nevertheless be aware that - * there is no determined ordering of resources located within a classloader. - * - * @param expressions the expressions to be resolved, not empty. - * @return the corresponding collection of current {@link org.apache.tamaya.core.resources.Resource}s found, never - * null. - * . - */ - default Collection<Resource> getResources(String... expressions) { - return getResources(Arrays.asList(expressions)); - } - - /** - * Resolves resource expressions to a list of {@link org.apache.tamaya.core.resources.Resource}s, considerubg - * the given classloader for classloader dependent resources. Hereby - * the ordering of resources matches the input of the resolved expressions. Nevertheless be aware that - * there is no determined ordering of resources located within a classloader. - * - * @param expressions the expressions to be resolved, not empty. - * @return the corresponding collection of current {@link org.apache.tamaya.core.resources.Resource}s found, never - * null. - * . - */ - default Collection<Resource> getResources(ClassLoader classLoader, String... expressions){ - return getResources(classLoader, Arrays.asList(expressions)); - } - - /** - * Resolves resource expressions to a list of {@link org.apache.tamaya.core.resources.Resource}s, considerubg - * the given classloader for classloader dependent resources. Hereby - * the ordering of resources matches the input of the resolved expressions. Nevertheless be aware that - * there is no determined ordering of resources located within a classloader. - * - * @param expressions the expressions to be resolved, not empty. - * @return the corresponding collection of current {@link org.apache.tamaya.core.resources.Resource}s found, never - * null. - * . - */ - Collection<Resource> getResources(ClassLoader classLoader, Collection<String> expressions); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/main/resources/META-INF/services/org.apache.tamaya.core.resources.ResourceLoader ---------------------------------------------------------------------- diff --git a/core/src/main/resources/META-INF/services/org.apache.tamaya.core.resources.ResourceLoader b/core/src/main/resources/META-INF/services/org.apache.tamaya.core.resources.ResourceLoader index d3242e4..a964d3c 100644 --- a/core/src/main/resources/META-INF/services/org.apache.tamaya.core.resources.ResourceLoader +++ b/core/src/main/resources/META-INF/services/org.apache.tamaya.core.resources.ResourceLoader @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.core.internal.resource.DefaultResourceLoader +org.apache.tamaya.resource.internal.DefaultResourceLoader http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java b/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java index 357a262..d6dc867 100644 --- a/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java +++ b/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java @@ -67,7 +67,7 @@ public class BasePropertySourceTest { private static class OverriddenOrdinalPropertySource extends BasePropertySource { private OverriddenOrdinalPropertySource() { - initialzeOrdinal(250); + initializeOrdinal(250); } @Override @@ -86,7 +86,7 @@ public class BasePropertySourceTest { private static class OverriddenInvalidOrdinalPropertySource extends BasePropertySource { private OverriddenInvalidOrdinalPropertySource() { - initialzeOrdinal(1); + initializeOrdinal(1); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java b/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java new file mode 100644 index 0000000..b67d17e --- /dev/null +++ b/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java @@ -0,0 +1,51 @@ +/* + * 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.testdata; + +import org.apache.tamaya.core.propertysource.BasePropertySource; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * Test provider reading properties from classpath:cfg/defaults/**.properties. + */ +public class TestPropertyDefaultSource extends BasePropertySource{ + + private Map<String,String> properties = new HashMap<>(); + + public TestPropertyDefaultSource() { + initializeOrdinal(100); + properties.put("name","Anatole"); + properties.put("name2","Sabine"); + properties = Collections.unmodifiableMap(properties); + } + + @Override + public String getName() { + return "default-testdata-properties"; + } + + @Override + public Map<String, String> getProperties() { + return properties; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSourceProvider.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSourceProvider.java b/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSourceProvider.java deleted file mode 100644 index dffe67d..0000000 --- a/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSourceProvider.java +++ /dev/null @@ -1,32 +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.testdata; - -import org.apache.tamaya.core.PathBasedPropertySourceProvider; -import org.apache.tamaya.core.formats.PropertiesFormat; - -/** - * Test provider reading properties from classpath:cfg/defaults/**.properties. - */ -public class TestPropertyDefaultSourceProvider extends PathBasedPropertySourceProvider{ - - public TestPropertyDefaultSourceProvider() { - super("default-testdata-properties", new PropertiesFormat(100), "cfg/defaults/test1.properties"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java b/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java index 718aaae..902027b 100644 --- a/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java +++ b/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java @@ -18,15 +18,56 @@ */ package org.apache.tamaya.core.testdata; -import org.apache.tamaya.core.PathBasedPropertySourceProvider; -import org.apache.tamaya.core.formats.PropertiesFormat; +import org.apache.tamaya.core.propertysource.BasePropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Test provider reading properties from classpath:cfg/final/**.properties. */ -public class TestPropertySourceProvider extends PathBasedPropertySourceProvider{ +public class TestPropertySourceProvider implements PropertySourceProvider { + + private List<PropertySource> list = new ArrayList<>(); + + public TestPropertySourceProvider(){ + list.add(new MyPropertySource()); + list = Collections.unmodifiableList(list); + } - public TestPropertySourceProvider() { - super("final-testdata-properties", new PropertiesFormat(200), "cfg/final/anotherTestFile.properties"); + @Override + public Collection<PropertySource> getPropertySources() { + return list; } + + private static class MyPropertySource extends BasePropertySource { + + private Map<String, String> properties = new HashMap<>(); + + public MyPropertySource() { + initializeOrdinal(200); + properties.put("name", "Robin"); + properties.put("name3", "Lukas"); + properties.put("name4", "Sereina"); + properties.put("name5", "Benjamin"); + properties = Collections.unmodifiableMap(properties); + } + + @Override + public String getName() { + return "final-testdata-properties"; + } + + @Override + public Map<String, String> getProperties() { + return properties; + } + } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource new file mode 100644 index 0000000..2b774a9 --- /dev/null +++ b/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -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.core.testdata.TestPropertyDefaultSource \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider ---------------------------------------------------------------------- diff --git a/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider index 9db0ef4..c45e07c 100644 --- a/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider +++ b/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider @@ -16,5 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.core.testdata.TestPropertyDefaultSourceProvider org.apache.tamaya.core.testdata.TestPropertySourceProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/resources/cfg/defaults/test1.properties ---------------------------------------------------------------------- diff --git a/core/src/test/resources/cfg/defaults/test1.properties b/core/src/test/resources/cfg/defaults/test1.properties deleted file mode 100644 index 17a854d..0000000 --- a/core/src/test/resources/cfg/defaults/test1.properties +++ /dev/null @@ -1,20 +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 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. -# -name=Anatole -name2=Sabine \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/core/src/test/resources/cfg/final/anotherTestFile.properties ---------------------------------------------------------------------- diff --git a/core/src/test/resources/cfg/final/anotherTestFile.properties b/core/src/test/resources/cfg/final/anotherTestFile.properties deleted file mode 100644 index 9e405f6..0000000 --- a/core/src/test/resources/cfg/final/anotherTestFile.properties +++ /dev/null @@ -1,22 +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 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. -# -name=Robin -name3=Lukas -name4=Sereina -name5=Benjamin \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/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 new file mode 100644 index 0000000..46865b0 --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.format; + +import org.apache.tamaya.core.resources.Resource; +import org.apache.tamaya.spi.PropertySource; + +import java.io.IOException; +import java.util.Collection; + +/** + * Implementations current this class encapsulate the mechanism how to read a + * resource including interpreting the format correctly (e.g. xml vs. + * properties). In most cases file only contains entries of the same priority, which would then + * result in only one {@link PropertySource}. Complex file formats, hoiwever, may contain entries + * of different priorities. In this cases, each ordinal type found must be returned as a separate + * {@link PropertySource} instance. + */ +@FunctionalInterface +public interface ConfigurationFormat { + + /** + * Reads a list {@link org.apache.tamaya.spi.PropertySource} instances from a resource, using this format. + * If the configuration format only contains entries of one ordinal type, normally only one single + * instance of PropertySource is returned. Nevertheless custom formats may contain different sections or parts, + * where each part maps to a different target rdinal (eg defaults, domain config and app config). In the + * ladder case multiple PropertySources can be returned, each one with its own ordinal and the corresponding + * entries. + * + * @param sourceName name to be used for constructing a useful name for the created + * {@link org.apache.tamaya.spi.PropertySource} instances. + * @param resource the configuration resource, not null + * @return the corresponding {@link org.apache.tamaya.spi.PropertySource} instances, never {@code null}. + */ + Collection<PropertySource> readConfiguration(String sourceName, Resource resource) throws IOException; + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/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 new file mode 100644 index 0000000..22f2325 --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesFormat.java @@ -0,0 +1,114 @@ +/* + * 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.core.resources.Resource; +import org.apache.tamaya.spi.PropertySource; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +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()); + + /** + * The target ordinal. + */ + private int ordinal; + + /** + * Creates a new format instance, hereby producing entries with the given ordinal, if not overridden by the + * configuration itself. + * TODO document and implement override feature + * @param ordinal the target ordinal. + */ + public PropertiesFormat(int ordinal) { + this.ordinal = ordinal; + } + + /** + * Get the target ordinal, produced by this format. + * + * @return the target ordinal + */ + public int getOrdinal() { + return ordinal; + } + + @SuppressWarnings("unchecked") + @Override + public Collection<PropertySource> readConfiguration(String baseName, Resource resource) { + final String sourceName = (baseName==null?"Properties:":baseName) + resource.getName(); + if (resource.exists()) { + List<PropertySource> propertySources = new ArrayList<>(); + try (InputStream is = resource.getInputStream()) { + final Properties p = new Properties(); + p.load(is); + propertySources.add(new PropertySource() { + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return sourceName; + } + + @Override + public Optional<String> get(String key) { + return Optional.ofNullable(p.getProperty(key)); + } + + @Override + public Map<String, String> getProperties() { + return Map.class.cast(p); + } + }); + return propertySources; + } catch (Exception e) { + LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + resource); + } + } + return Collections.emptyList(); + } + + @Override + public String toString() { + return "PropertiesFormat{" + + "ordinal=" + ordinal + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5731e09/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 new file mode 100644 index 0000000..e042e32 --- /dev/null +++ b/modules/formats/src/main/java/org/apache/tamaya/format/PropertiesXmlFormat.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.format; + +import org.apache.tamaya.core.resources.Resource; +import org.apache.tamaya.spi.PropertySource; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +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()); + + /** + * The target ordinal. + */ + private int ordinal; + + /** + * Creates a new format instance, producing entries for the given ordinal, if not overridden by a + * config entry itself. + * TODO document and implement override feature + * @param ordinal the target ordinal. + */ + public PropertiesXmlFormat(int ordinal) { + this.ordinal = ordinal; + } + + /** + * Get the target ordinal, produced by this format. + * + * @return the target ordinal + */ + public int getOrdinal() { + return ordinal; + } + + @SuppressWarnings("unchecked") + @Override + public Collection<PropertySource> readConfiguration(String baseName, Resource resource) { + if (resource.exists()) { + final String sourceName = (baseName==null?"Properties:":baseName) + resource.getName(); + List<PropertySource> propertySources = new ArrayList<>(); + try (InputStream is = resource.getInputStream()) { + final Properties p = new Properties(); + p.loadFromXML(is); + propertySources.add(new PropertySource() { + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return sourceName; + } + + @Override + public Optional<String> get(String key) { + return Optional.ofNullable(p.getProperty(key)); + } + + @Override + public Map<String, String> getProperties() { + return Map.class.cast(p); + } + }); + return propertySources; + } catch (Exception e) { + LOG.log(Level.FINEST, e, () -> "Failed to read config from resource: " + resource); + } + } + return Collections.emptyList(); + } + + @Override + public String toString() { + return "PropertiesXmlFormat{" + + "ordinal=" + ordinal + + '}'; + } +}