http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ComponentFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ComponentFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ComponentFactory.java new file mode 100644 index 0000000..906b704 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ComponentFactory.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal; + +import org.apache.tamaya.spi.ServiceContextManager; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * Small helper class for loading of configured instances. + */ +public final class ComponentFactory<T> { + + private Class<T> type; + private Map<String, T> serviceContext = new HashMap<>(); + private Set<String> loaded = new HashSet<>(); + + /** + * Constructor. + * @param type the service type, not null. + */ + public ComponentFactory(Class<T> type){ + this.type = Objects.requireNonNull(type); + for(T service: ServiceContextManager.getServiceContext().getServices(type)){ + serviceContext.put(service.getClass().getName(), service); + } + } + + /** + * Creates an instance of the given type based on a type configuration. + * Type hereby is + * <ul> + * <li>A fully qualified class name</li> + * <li>A simple class name of a filter class registered with the current + * ServiceContext.</li> + * </ul> + * @param identifier the configured type + * @return the component found, or null. + */ + public T getComponent(String identifier) + throws IllegalAccessException, InstantiationException { + T comp = this.serviceContext.get(identifier); + if(comp==null){ + for(Map.Entry<String, T> en:serviceContext.entrySet()){ + if(en.getKey().endsWith("."+identifier)){ + comp = en.getValue(); + } + } + } + // Multiple instances: create a new instance using the parameterless constructor for all subsequent + // resolutions. + if(loaded.contains(comp.getClass().getName())){ + return (T)comp.getClass().newInstance(); + } + // Ensure that the next access will return a new instance. + loaded.add(comp.getClass().getName()); + return comp; + } + + public Collection<T> loadInstances(NodeList nodeList) { + List<T> items = new ArrayList<>(); + for(int i=0;i<nodeList.getLength();i++){ + Node node = nodeList.item(i); + if(node.getNodeName().equals("filter")){ + String type = node.getNodeValue(); + try { + T item = getComponent(type); + ComponentConfigurator.configure(item, node); + items.add(item); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return items; + } + + @Override + public String toString() { + return "ComponentFactory{" + + "type=" + type + + ", serviceContext=" + serviceContext + + '}'; + } + +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ContextReader.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ContextReader.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ContextReader.java new file mode 100644 index 0000000..5754e4f --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/ContextReader.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.metamodel.internal; + +import org.apache.tamaya.metamodel.MetaContext; +import org.apache.tamaya.metamodel.spi.MetaConfigurationReader; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.annotation.Priority; +import java.util.logging.Logger; + +/** + * Meta-configuration reader that reads the shared context data. + */ +@Priority(-1) +public class ContextReader implements MetaConfigurationReader { + + private static final Logger LOG = Logger.getLogger(ContextReader.class.getName()); + + @Override + public void read(Document document, ConfigurationContextBuilder contextBuilder) { + NodeList nodeList = document.getDocumentElement().getElementsByTagName("context"); + String contextName = "DEFAULT"; + LOG.finer("Reading " + nodeList.getLength() + " meta context entries..."); + for(int i=0;i<nodeList.getLength();i++){ + Node node = nodeList.item(i); + if(node.getNodeName().equals("context")){ + Node nameNode = node.getAttributes().getNamedItem("name"); + if(nameNode!=null){ + contextName = nameNode.getTextContent(); + } + MetaContext context = MetaContext.getInstance(contextName); + NodeList entryNodes = node.getChildNodes(); + for(int c=0;c<entryNodes.getLength();c++){ + Node entryNode = entryNodes.item(c); + if("context-entry".equals(entryNode.getNodeName())){ + String key = entryNode.getAttributes().getNamedItem("name").getNodeValue(); + String value = entryNode.getTextContent(); + // TODO add support for placeholders here... + LOG.finest("Applying context entry: " + key + '=' + value + " on " + contextName); + context.setProperty(key, value); + } + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DSLLoadingConfigurationProviderSpi.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DSLLoadingConfigurationProviderSpi.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DSLLoadingConfigurationProviderSpi.java new file mode 100644 index 0000000..266ed5a --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DSLLoadingConfigurationProviderSpi.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.metamodel.internal; + +import org.apache.tamaya.metamodel.MetaConfiguration; +import org.apache.tamaya.spi.*; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spisupport.DefaultConfiguration; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +import javax.annotation.Priority; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Comparator; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * ConfigurationContext that uses {@link MetaConfiguration} to configure the + * Tamaya configuration context. + */ +@Priority(10) +public class DSLLoadingConfigurationProviderSpi implements ConfigurationProviderSpi{ + + private volatile Configuration config; + private final Object LOCK = new Object(); + + @Override + public ConfigurationContextBuilder getConfigurationContextBuilder() { + return ServiceContextManager.getServiceContext().getService(ConfigurationContextBuilder.class); + } + + @Override + public void setConfiguration(Configuration config) { + this.config = Objects.requireNonNull(config); + } + + @Override + public boolean isConfigurationSettable() { + return true; + } + + @Override + public void setConfigurationContext(ConfigurationContext context){ + this.config = Objects.requireNonNull(createConfiguration(context)); + } + + @Override + public boolean isConfigurationContextSettable() { + return true; + } + + @Override + public Configuration getConfiguration() { + checkInitialized(); + return config; + } + + @Override + public Configuration createConfiguration(ConfigurationContext context) { + return new DefaultConfiguration(context); + } + + @Override + public ConfigurationContext getConfigurationContext() { + checkInitialized(); + return config.getContext(); + } + + private void checkInitialized() { + if(config==null){ + synchronized (LOCK) { + if(config==null){ + MetaConfiguration.configure(); + } + if(config==null){ + // load defaults + this.config = new DefaultConfiguration( + new DefaultConfigurationContextBuilder() + .addDefaultPropertyConverters() + .addDefaultPropertyFilters() + .addDefaultPropertySources() + .sortPropertyFilter( + (Comparator<PropertyFilter>) + DefaultConfigurationContextBuilder.DEFAULT_PROPERTYFILTER_COMPARATOR) + .sortPropertySources(DefaultConfigurationContextBuilder.DEFAULT_PROPERTYSOURCE_COMPARATOR) + .build()); + } + } + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DefaultRefreshablePropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DefaultRefreshablePropertySourceProvider.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DefaultRefreshablePropertySourceProvider.java deleted file mode 100644 index f968a15..0000000 --- a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/DefaultRefreshablePropertySourceProvider.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.metamodel.internal; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; - -import java.util.Collection; -import java.util.Objects; - -/** - * Wrapped property source that allows dynamically reassigning the property source's - * ordinal value. This is needed for reordering the property sources to - * match the DSL configured ordering. - */ -public final class DefaultRefreshablePropertySourceProvider implements PropertySourceProvider, Refreshable { - - private SourceConfig sourceConfig; - private PropertySourceProvider wrapped; - private Collection<PropertySource> propertSources; - - public DefaultRefreshablePropertySourceProvider(SourceConfig sourceConfig) - throws IllegalAccessException, InstantiationException, - ClassNotFoundException { - this.sourceConfig = Objects.requireNonNull(sourceConfig); - this.wrapped = Objects.requireNonNull(sourceConfig.create(PropertySourceProvider.class)); - this.propertSources = Objects.requireNonNull(wrapped.getPropertySources()); - } - - @Override - public Collection<PropertySource> getPropertySources() { - return this.propertSources; - } - - @Override - public ConfigurationContext refresh(ConfigurationContext context) { - Collection<PropertySource> newPropertySources = - Objects.requireNonNull(wrapped.getPropertySources()); - ConfigurationContextBuilder builder = context.toBuilder(); - // remove previous sources - builder.removePropertySources(this.propertSources); - // add new sources - builder.addPropertySources(newPropertySources); - return builder.build(); - } - - @Override - public String toString() { - return "WrappedPropertySource{" + - "sourceConfig=" + sourceConfig + - ", wrapped=" + wrapped + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/FactoryManager.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/FactoryManager.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/FactoryManager.java deleted file mode 100644 index a8129f6..0000000 --- a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/FactoryManager.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.metamodel.internal; - -import org.apache.tamaya.metamodel.spi.PropertySourceFactory; -import org.apache.tamaya.metamodel.spi.PropertySourceProviderFactory; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Small manager component to maintain the factories to be referenced by type names. - */ -final class FactoryManager { - - private static final Logger LOG = Logger.getLogger(FactoryManager.class.getName()); - private static final FactoryManager INSTANCE = new FactoryManager(); - - private Map<String, PropertySourceFactory> sourceFactories = new HashMap<>(); - private Map<String, PropertySourceProviderFactory> providerFactories = new HashMap<>(); - - private FactoryManager(){ - for(PropertySourceFactory f: ServiceContextManager.getServiceContext().getServices(PropertySourceFactory.class)){ - this.sourceFactories.put(f.getType(), f); - } - for(PropertySourceProviderFactory f: ServiceContextManager.getServiceContext().getServices(PropertySourceProviderFactory.class)){ - this.providerFactories.put(f.getType(), f); - } - } - - public static FactoryManager getInstance(){ - return INSTANCE; - } - - public PropertySourceFactory getSourceFactory(String type){ - PropertySourceFactory fact = this.sourceFactories.get(type); - if(fact==null){ - try{ - Class<? extends PropertySource> psType = (Class<? extends PropertySource>)Class.forName(type); - fact = new ImplicitPropertySourceFactory(psType); - this.sourceFactories.put(type, fact); - return fact; - }catch(Exception e){ - LOG.log(Level.SEVERE, "Failed to load PropertySourceFactory: " + type); - } - } - throw new IllegalArgumentException("No such PropertySourceFactory: " + type); - } - - public PropertySourceProviderFactory getProviderFactory(String type){ - PropertySourceProviderFactory fact = this.providerFactories.get(type); - if(fact==null){ - try{ - Class<? extends PropertySourceProvider> psType = (Class<? extends PropertySourceProvider>)Class.forName(type); - fact = new ImplicitPropertySourceProviderFactory(psType); - this.providerFactories.put(type, fact); - return fact; - }catch(Exception e){ - LOG.log(Level.SEVERE, "Failed to load PropertySourceProviderFactory: " + type); - } - } - throw new IllegalArgumentException("No such PropertySourceProviderFactory: " + type); - } - - @Override - public String toString() { - return "FactoryManager{" + - "providerFactories=" + providerFactories.keySet() + - ", sourceFactories=" + sourceFactories.keySet() + - '}'; - } - - private static class ImplicitPropertySourceFactory implements PropertySourceFactory{ - - private Class<? extends PropertySource> type; - - ImplicitPropertySourceFactory(Class<? extends PropertySource> type){ - this.type = Objects.requireNonNull(type); - } - - @Override - public PropertySource create(Map<String, String> config) { - try { - return this.type.newInstance(); - } catch (Exception e) { - throw new IllegalStateException("Cannot instantiate PropertySource: " + this.type.getName(), e); - } - } - - @Override - public String getType() { - return null; - } - } - - private static class ImplicitPropertySourceProviderFactory implements PropertySourceProviderFactory{ - - private Class<? extends PropertySourceProvider> type; - - ImplicitPropertySourceProviderFactory(Class<? extends PropertySourceProvider> type){ - this.type = Objects.requireNonNull(type); - } - - @Override - public PropertySourceProvider create(Map<String, String> config) { - try { - return this.type.newInstance(); - } catch (Exception e) { - throw new IllegalStateException("Cannot instantiate PropertySourceProvider: " + this.type.getName(), e); - } - } - - @Override - public String getType() { - return null; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyConverterReader.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyConverterReader.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyConverterReader.java new file mode 100644 index 0000000..f82e92d --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyConverterReader.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.metamodel.internal; + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.metamodel.spi.ItemFactoryManager; +import org.apache.tamaya.metamodel.spi.MetaConfigurationReader; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Metaconfiguration reader to read property sources and property source providers. + */ +public class PropertyConverterReader implements MetaConfigurationReader{ + + private static final Logger LOG = Logger.getLogger(PropertyConverterReader.class.getName()); + + @Override + public void read(Document document, ConfigurationContextBuilder contextBuilder) { + NodeList nodeList = document.getDocumentElement().getElementsByTagName("property-converters"); + if(nodeList.getLength()==0){ + LOG.finer("No property converters configured"); + return; + } + if(nodeList.getLength()>1){ + LOG.warning("Multiple property-converters sections configured, onyl reading first..."); + return; + } + nodeList = nodeList.item(0).getChildNodes(); + for(int i=0;i<nodeList.getLength();i++){ + Node node = nodeList.item(i); + try{ + if(node.getNodeName().equals("converter")){ + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + try { + ItemFactory<PropertyConverter> converterFactory = ItemFactoryManager.getInstance().getFactory(PropertyConverter.class, type); + if(converterFactory==null){ + LOG.severe("No such property converter: " + type); + continue; + } + Map<String,String> params = ComponentConfigurator.extractParameters(node); + PropertyConverter converter = converterFactory.create(params); + if(converter!=null) { + ComponentConfigurator.configure(converter, node); + Class targetType = Class.forName(params.get("targetType")); + LOG.finer("Adding converter for type " + targetType.getName() + ": " + converter.getClass()); + contextBuilder.addPropertyConverters(TypeLiteral.of(targetType), converter); + } + } catch (Exception e) { + LOG.log(Level.SEVERE, "Failed to configure PropertyConverter: " + type, e); + } + }else if(node.getNodeName().equals("default-converters")){ + LOG.finer("Adding default property converters..."); + contextBuilder.addDefaultPropertyConverters(); + } + }catch(Exception e){ + LOG.log(Level.SEVERE, "Failed to read property converter configuration: " + node, e); + } + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterOrderingReader.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterOrderingReader.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterOrderingReader.java new file mode 100644 index 0000000..818b785 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterOrderingReader.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.metamodel.internal; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.metamodel.spi.ItemFactoryManager; +import org.apache.tamaya.metamodel.spi.MetaConfigurationReader; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.annotation.Priority; +import java.util.Comparator; +import java.util.logging.Logger; + + +/** + * Metaconfiguration reader that reads the configuration combination policy to be used. + */ +@Priority(Integer.MAX_VALUE) +public class PropertyFilterOrderingReader implements MetaConfigurationReader{ + + private static final Logger LOG = Logger.getLogger(PropertyFilterOrderingReader.class.getName()); + + @Override + public void read(Document document, ConfigurationContextBuilder contextBuilder) { + NodeList nodeList = document.getDocumentElement().getElementsByTagName("property-filter-order"); + if(nodeList.getLength()==0){ + LOG.finer("No property filter ordering configured."); + return; + } + if(nodeList.getLength()>1){ + throw new ConfigException("Only one property filter order can be applied."); + } + Node node = nodeList.item(0); + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + ItemFactory<Comparator> comparatorFactory = ItemFactoryManager.getInstance().getFactory(Comparator.class, type); + Comparator comparator = comparatorFactory.create(ComponentConfigurator.extractParameters(node)); + ComponentConfigurator.configure(comparator, node); + LOG.finer("Sorting property filters using comparator: " + comparator.getClass().getName()); + contextBuilder.sortPropertyFilter(comparator); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterReader.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterReader.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterReader.java new file mode 100644 index 0000000..94bb6ec --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertyFilterReader.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.metamodel.internal; + +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.metamodel.spi.ItemFactoryManager; +import org.apache.tamaya.metamodel.spi.MetaConfigurationReader; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + + +/** + * Metaconfiguration reader that reads the configuration filters to be used. + */ +public class PropertyFilterReader implements MetaConfigurationReader{ + + private static final Logger LOG = Logger.getLogger(PropertyFilterReader.class.getName()); + + @Override + public void read(Document document, ConfigurationContextBuilder contextBuilder) { + NodeList nodeList = document.getDocumentElement().getElementsByTagName("property-filters"); + if(nodeList.getLength()==0){ + LOG.finer("No property filters configured."); + return; + } + if(nodeList.getLength()>1){ + LOG.warning("Multiple property-filters sections configured, onyl reading first..."); + return; + } + nodeList = nodeList.item(0).getChildNodes(); + for(int i=0;i<nodeList.getLength();i++){ + Node node = nodeList.item(i); + try { + if ("filter".equals(node.getNodeName())) { + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + ItemFactory<PropertyFilter> filterFactory = ItemFactoryManager.getInstance().getFactory(PropertyFilter.class, type); + if(filterFactory==null){ + LOG.severe("No such property filter: " + type); + continue; + } + Map<String,String> params = ComponentConfigurator.extractParameters(node); + PropertyFilter filter = filterFactory.create(params); + if(filter!=null) { + ComponentConfigurator.configure(filter, params); + LOG.finer("Adding configured property filter: " + filter.getClass().getName()); + contextBuilder.addPropertyFilters(filter); + } + } else if ("default-filters".equals(node.getNodeName())) { + LOG.finer("Adding default property filters..."); + contextBuilder.addDefaultPropertyFilters(); + } + }catch(Exception e){ + LOG.log(Level.SEVERE, "Failed to read property filter configuration: " + node, e); + } + } + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceOrderingReader.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceOrderingReader.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceOrderingReader.java new file mode 100644 index 0000000..b811afa --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceOrderingReader.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.metamodel.internal; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.metamodel.spi.ItemFactoryManager; +import org.apache.tamaya.metamodel.spi.MetaConfigurationReader; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyValueCombinationPolicy; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.annotation.Priority; +import java.util.Comparator; +import java.util.logging.Logger; + + +/** + * Metaconfiguration reader that reads the configuration combination policy to be used. + */ +@Priority(Integer.MAX_VALUE) +public class PropertySourceOrderingReader implements MetaConfigurationReader{ + + private static final Logger LOG = Logger.getLogger(PropertySourceOrderingReader.class.getName()); + + @Override + public void read(Document document, ConfigurationContextBuilder contextBuilder) { + NodeList nodeList = document.getDocumentElement().getElementsByTagName("property-source-order"); + if(nodeList.getLength()==0){ + LOG.finer("No property source ordering defined."); + return; + } + if(nodeList.getLength()>1){ + throw new ConfigException("Only one property source order can be applied."); + } + Node node = nodeList.item(0); + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + ItemFactory<Comparator> comparatorFactory = ItemFactoryManager.getInstance().getFactory(Comparator.class, type); + Comparator comparator = comparatorFactory.create(ComponentConfigurator.extractParameters(node)); + ComponentConfigurator.configure(comparator, node); + LOG.finer("Sorting property sources using comparator: " + comparator.getClass().getName()); + contextBuilder.sortPropertySources(comparator); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceReader.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceReader.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceReader.java new file mode 100644 index 0000000..8f42a3b --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/PropertySourceReader.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.metamodel.internal; + +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.metamodel.spi.ItemFactoryManager; +import org.apache.tamaya.metamodel.spi.MetaConfigurationReader; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Metaconfiguration reader to read property sources and property source providers. + */ +public class PropertySourceReader implements MetaConfigurationReader{ + + private static final Logger LOG = Logger.getLogger(PropertySourceReader.class.getName()); + + @Override + public void read(Document document, ConfigurationContextBuilder contextBuilder) { + NodeList nodeList = document.getDocumentElement().getElementsByTagName("property-sources"); + if(nodeList.getLength()==0){ + LOG.finer("No property sources configured."); + return; + } + if(nodeList.getLength()>1){ + LOG.warning("Multiple property-sources sections configured, onyl reading first..."); + return; + } + nodeList = nodeList.item(0).getChildNodes(); + for(int i=0;i<nodeList.getLength();i++){ + Node node = nodeList.item(i); + try{ + if(node.getNodeName().equals("source")){ + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + try { + ItemFactory<PropertySource> sourceFactory = ItemFactoryManager.getInstance().getFactory(PropertySource.class, type); + if(sourceFactory==null){ + LOG.severe("No such property source: " + type); + continue; + } + Map<String,String> params = ComponentConfigurator.extractParameters(node); + PropertySource ps = sourceFactory.create(params); + if(ps!=null) { + ComponentConfigurator.configure(ps, params); + LOG.finer("Adding configured property source: " + ps.getName()); + contextBuilder.addPropertySources(ps); + } + } catch (Exception e) { + LOG.log(Level.SEVERE, "Failed to configure PropertySource: " + type, e); + } + }else if(node.getNodeName().equals("source-provider")){ + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + try { + ItemFactory<PropertySourceProvider> providerFactory = ItemFactoryManager.getInstance().getFactory(PropertySourceProvider.class, type); + if(providerFactory==null){ + LOG.severe("No such property source provider: " + type); + continue; + } + Map<String,String> params = ComponentConfigurator.extractParameters(node); + PropertySourceProvider prov = providerFactory.create(params); + if(prov!=null) { + ComponentConfigurator.configure(prov, node); + LOG.finer("Adding configured property source provider: " + prov.getClass().getName()); + contextBuilder.addPropertySources(prov.getPropertySources()); + } + } catch (Exception e) { + LOG.log(Level.SEVERE, "Failed to configure PropertySourceProvider: " + type, e); + } + }else if(node.getNodeName().equals("default-sources")){ + LOG.finer("Adding default property sources."); + contextBuilder.addDefaultPropertySources(); + } + }catch(Exception e){ + LOG.log(Level.SEVERE, "Failed to read property source configuration: " + node, e); + } + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/Refreshable.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/Refreshable.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/Refreshable.java deleted file mode 100644 index bd0103e..0000000 --- a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/Refreshable.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.metamodel.internal; - -import org.apache.tamaya.spi.ConfigurationContext; - -/** - * Common interface for refreshable items. - */ -public interface Refreshable { - - /** - * Refreshes the given configuration context, by applying any changes - * needed to reflect the change. - * @param context the configuration context, not null. - * @return the new configuration context. In case no changes are - * needed or the changes could be applied implicitly, the - * instance passed as input should be returned. - */ - ConfigurationContext refresh(ConfigurationContext context); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/SourceConfig.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/SourceConfig.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/SourceConfig.java index 4987111..00d823e 100644 --- a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/SourceConfig.java +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/SourceConfig.java @@ -1,233 +1,233 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.metamodel.internal; - - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -/** - * Resolver to resolve/map DSL related source expressions into PropertySources - * loadable by a ConfigurationContext. Hereby the ordering of loaded property sources must be - * honored if possible by implicitly adapting/Overriding the default ordinal for the sources - * added. - */ -public class SourceConfig { - - private boolean enabled; - private String type; - private String name; - private Integer ordinal; - private long refreshInterval; - private Map<String,String> sourceConfiguration = new HashMap<>(); - - private SourceConfig(Builder builder) { - enabled = builder.enabled; - type = builder.type; - name = builder.name; - refreshInterval = builder.refreshInterval; - ordinal = builder.ordinal; - sourceConfiguration = builder.sourceConfiguration; - } - - /** - * New builder builder. - * - * @param type the type - * @return the builder - */ - public static Builder newBuilder(String type) { - return new Builder(type); - } - - /** - * New builder builder using this instance's settings. - * - * @return the builder - */ - public Builder toBuilder() { - Builder builder = new Builder(this.type); - builder.enabled = this.enabled; - builder.type = this.type; - builder.ordinal = this.ordinal; - builder.name = this.name; - builder.sourceConfiguration = this.sourceConfiguration; - return builder; - } - - /** - * Is enabled boolean. - * - * @return the boolean - */ - public boolean isEnabled() { - return enabled; - } - - /** - * Gets source configuration. - * - * @return the source configuration - */ - public Map<String, String> getSourceConfiguration() { - return sourceConfiguration; - } - - /** - * Gets type. - * - * @return the type - */ - public String getType() { - return type; - } - - /** - * Gets the current refresh interval, default is 0 meaning the property - * source is never refreshed. - * - * @return the refresh interval - */ - public long getRefreshInterval() { - return refreshInterval; - } - - /** - * Gets name. - * - * @return the name - */ - public String getName() { - return name; - } - - @Override - public String toString() { - return "PropertySourceConfig{" + - "enabled=" + enabled + - ", type='" + type + '\'' + - ", name='" + name + '\'' + - ", ordinal=" + ordinal + - ", sourceConfiguration=" + sourceConfiguration + - '}'; - } - - - public <T> T create(Class<T> type) - throws ClassNotFoundException, IllegalAccessException, - InstantiationException { - return (T)Class.forName(this.getType()).newInstance(); - } - - - /** - * {@code PropertySourceConfig} builder static inner class. - */ - public static final class Builder { - private boolean enabled; - private String type; - private String name; - private Integer ordinal; - private long refreshInterval; - private Map<String, String> sourceConfiguration; - - private Builder(String type) { - this.type = Objects.requireNonNull(type); - if(type.trim().isEmpty()){ - throw new IllegalArgumentException("Type is empty."); - } - } - - /** - * Sets the {@code refreshInterval} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code refreshInterval} to set - * @return a reference to this Builder - */ - public Builder withRefreshInterval(long val) { - refreshInterval = val; - return this; - } - - /** - * Sets the {@code ordinal} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code ordinal} to set - * @return a reference to this Builder - */ - public Builder withOrdinal(Integer val) { - ordinal = val; - return this; - } - - /** - * Sets the {@code enabled} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code enabled} to set - * @return a reference to this Builder - */ - public Builder withEnabled(boolean val) { - enabled = val; - return this; - } - - /** - * Sets the {@code type} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code type} to set - * @return a reference to this Builder - */ - public Builder withType(String val) { - type = val; - return this; - } - - /** - * Sets the {@code name} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code name} to set - * @return a reference to this Builder - */ - public Builder withName(String val) { - name = val; - return this; - } - - /** - * Sets the {@code sourceConfiguration} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code sourceConfiguration} to set - * @return a reference to this Builder - */ - public Builder withSourceConfiguration(Map<String, String> val) { - sourceConfiguration = val; - return this; - } - - /** - * Returns a {@code PropertySourceConfig} built from the parameters previously set. - * - * @return a {@code PropertySourceConfig} built with parameters of this {@code PropertySourceConfig.Builder} - */ - public SourceConfig build() { - return new SourceConfig(this); - } - } -} +///* +// * Licensed to the Apache Software Foundation (ASF) under one +// * or more contributor license agreements. See the NOTICE file +// * distributed with this work for additional information +// * regarding copyright ownership. The ASF licenses this file +// * to you under the Apache License, Version 2.0 (the +// * "License"); you may not use this file except in compliance +// * with the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, +// * software distributed under the License is distributed on an +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// * KIND, either express or implied. See the License for the +// * specific language governing permissions and limitations +// * under the License. +// */ +//package org.apache.tamaya.metamodel.internal; +// +// +//import java.util.HashMap; +//import java.util.Map; +//import java.util.Objects; +// +///** +// * Resolver to resolve/map DSL related source expressions into PropertySources +// * loadable by a ConfigurationContext. Hereby the ordering of loaded property sources must be +// * honored if possible by implicitly adapting/Overriding the default ordinal for the sources +// * added. +// */ +//public class SourceConfig { +// +// private boolean enabled; +// private String type; +// private String name; +// private Integer ordinal; +// private long refreshInterval; +// private Map<String,String> sourceConfiguration = new HashMap<>(); +// +// private SourceConfig(Builder builder) { +// enabled = builder.enabled; +// type = builder.type; +// name = builder.name; +// refreshInterval = builder.refreshInterval; +// ordinal = builder.ordinal; +// sourceConfiguration = builder.sourceConfiguration; +// } +// +// /** +// * New builder builder. +// * +// * @param type the type +// * @return the builder +// */ +// public static Builder newBuilder(String type) { +// return new Builder(type); +// } +// +// /** +// * New builder builder using this instance's settings. +// * +// * @return the builder +// */ +// public Builder toBuilder() { +// Builder builder = new Builder(this.type); +// builder.enabled = this.enabled; +// builder.type = this.type; +// builder.ordinal = this.ordinal; +// builder.name = this.name; +// builder.sourceConfiguration = this.sourceConfiguration; +// return builder; +// } +// +// /** +// * Is enabled boolean. +// * +// * @return the boolean +// */ +// public boolean isEnabled() { +// return enabled; +// } +// +// /** +// * Gets source configuration. +// * +// * @return the source configuration +// */ +// public Map<String, String> getSourceConfiguration() { +// return sourceConfiguration; +// } +// +// /** +// * Gets type. +// * +// * @return the type +// */ +// public String getType() { +// return type; +// } +// +// /** +// * Gets the current refresh interval, default is 0 meaning the property +// * source is never refreshed. +// * +// * @return the refresh interval +// */ +// public long getRefreshInterval() { +// return refreshInterval; +// } +// +// /** +// * Gets name. +// * +// * @return the name +// */ +// public String getName() { +// return name; +// } +// +// @Override +// public String toString() { +// return "PropertySourceConfig{" + +// "enabled=" + enabled + +// ", type='" + type + '\'' + +// ", name='" + name + '\'' + +// ", ordinal=" + ordinal + +// ", sourceConfiguration=" + sourceConfiguration + +// '}'; +// } +// +// +// public <T> T create(Class<T> type) +// throws ClassNotFoundException, IllegalAccessException, +// InstantiationException { +// return (T)Class.forName(this.getType()).newInstance(); +// } +// +// +// /** +// * {@code PropertySourceConfig} builder static inner class. +// */ +// public static final class Builder { +// private boolean enabled; +// private String type; +// private String name; +// private Integer ordinal; +// private long refreshInterval; +// private Map<String, String> sourceConfiguration; +// +// private Builder(String type) { +// this.type = Objects.requireNonNull(type); +// if(type.trim().isEmpty()){ +// throw new IllegalArgumentException("Type is empty."); +// } +// } +// +// /** +// * Sets the {@code refreshInterval} and returns a reference to this Builder so that the methods can be chained together. +// * +// * @param val the {@code refreshInterval} to set +// * @return a reference to this Builder +// */ +// public Builder withRefreshInterval(long val) { +// refreshInterval = val; +// return this; +// } +// +// /** +// * Sets the {@code ordinal} and returns a reference to this Builder so that the methods can be chained together. +// * +// * @param val the {@code ordinal} to set +// * @return a reference to this Builder +// */ +// public Builder withOrdinal(Integer val) { +// ordinal = val; +// return this; +// } +// +// /** +// * Sets the {@code enabled} and returns a reference to this Builder so that the methods can be chained together. +// * +// * @param val the {@code enabled} to set +// * @return a reference to this Builder +// */ +// public Builder withEnabled(boolean val) { +// enabled = val; +// return this; +// } +// +// /** +// * Sets the {@code type} and returns a reference to this Builder so that the methods can be chained together. +// * +// * @param val the {@code type} to set +// * @return a reference to this Builder +// */ +// public Builder withType(String val) { +// type = val; +// return this; +// } +// +// /** +// * Sets the {@code name} and returns a reference to this Builder so that the methods can be chained together. +// * +// * @param val the {@code name} to set +// * @return a reference to this Builder +// */ +// public Builder withName(String val) { +// name = val; +// return this; +// } +// +// /** +// * Sets the {@code sourceConfiguration} and returns a reference to this Builder so that the methods can be chained together. +// * +// * @param val the {@code sourceConfiguration} to set +// * @return a reference to this Builder +// */ +// public Builder withSourceConfiguration(Map<String, String> val) { +// sourceConfiguration = val; +// return this; +// } +// +// /** +// * Returns a {@code PropertySourceConfig} built from the parameters previously set. +// * +// * @return a {@code PropertySourceConfig} built with parameters of this {@code PropertySourceConfig.Builder} +// */ +// public SourceConfig build() { +// return new SourceConfig(this); +// } +// } +//} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/CLIArgumentsFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/CLIArgumentsFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/CLIArgumentsFactory.java new file mode 100644 index 0000000..ae4bf24 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/CLIArgumentsFactory.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal.factories; + +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.CLIPropertySource; +import org.apache.tamaya.spisupport.EnvironmentPropertySource; + +import java.util.Map; + +/** + * Factory for configuring CLI argument based property sources. + */ +public final class CLIArgumentsFactory implements ItemFactory<PropertySource>{ + @Override + public String getName() { + return "CLI"; + } + + @Override + public PropertySource create(Map<String,String> parameters) { + return new CLIPropertySource(); + } + + @Override + public Class<? extends PropertySource> getType() { + return PropertySource.class; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/EnvPropertiesFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/EnvPropertiesFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/EnvPropertiesFactory.java new file mode 100644 index 0000000..f6c9909 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/EnvPropertiesFactory.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal.factories; + +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.EnvironmentPropertySource; + +import java.util.Map; + +/** + * Factory for configuring environment properties based property sources. + */ +public final class EnvPropertiesFactory implements ItemFactory<PropertySource>{ + @Override + public String getName() { + return "env-properties"; + } + + @Override + public PropertySource create(Map<String,String> parameters) { + return new EnvironmentPropertySource(); + } + + @Override + public Class<? extends PropertySource> getType() { + return PropertySource.class; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/FilePropertySourceFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/FilePropertySourceFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/FilePropertySourceFactory.java new file mode 100644 index 0000000..d3961d4 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/FilePropertySourceFactory.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal.factories; + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationFormats; +import org.apache.tamaya.format.MappedConfigurationDataPropertySource; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.resource.ConfigResources; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.PropertiesResourcePropertySource; +import org.apache.tamaya.spisupport.SimplePropertySource; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Factory for configuring file based property sources. + */ +public final class FilePropertySourceFactory extends ResourcePropertySourceFactory{ + + private static final Logger LOG = Logger.getLogger(FilePropertySourceFactory.class.getName()); + + @Override + public String getName() { + return "file"; + } + + @Override + protected String example() { + return "<source type=\""+getName()+"\">\n" + + " <param name=\"location\">c:/temp/config.xml</param>\n" + + " <param name=\"formats\">xml-properties</param>\n" + + "</source>\n"; + } + + @Override + protected URL createResource(String location) { + try { + Path path = Paths.get(location); + if(!path.toFile().exists()){ + LOG.info("Cannot read resource '" + location + "': no such file."); + }else if(!path.toFile().canRead()){ + LOG.info("Cannot read resource '" + location + "': not readable."); + } + return path.toUri().toURL(); + } catch (MalformedURLException e) { + LOG.warning("Invalid file '" + location + "'."); + return null; + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceFactory.java new file mode 100644 index 0000000..a36fd7f --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceFactory.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal.factories; + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationFormats; +import org.apache.tamaya.format.MappedConfigurationDataPropertySource; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.PropertiesResourcePropertySource; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Factory for configuring resource based property sources. + */ +public class ResourcePropertySourceFactory extends URLPropertySourceFactory{ + + private static final Logger LOG = Logger.getLogger(ResourcePropertySourceFactory.class.getName()); + + @Override + public String getName() { + return "classpath"; + } + + + protected String example() { + return "<source type=\""+getName()+"\">\n" + + " <param name=\"location\">/META-INF/config.xml</param>\n" + + " <param name=\"formats\">xml-properties</param>\n" + + "</source>\n"; + } + + protected URL createResource(String location) { + try { + return getClass().getClassLoader().getResource(location); + } catch (Exception e) { + LOG.warning("Invalid resource '" + location + "'."); + return null; + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceProviderFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceProviderFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceProviderFactory.java new file mode 100644 index 0000000..77f22d4 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/ResourcePropertySourceProviderFactory.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.metamodel.internal.factories; + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationFormats; +import org.apache.tamaya.format.MappedConfigurationDataPropertySource; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.resource.ConfigResources; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; + +import java.net.URL; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Factory for configuring resource based property sources. + */ +public class ResourcePropertySourceProviderFactory implements ItemFactory<PropertySourceProvider>{ + + private static final Logger LOG = Logger.getLogger(ResourcePropertySourceProviderFactory.class.getName()); + + @Override + public String getName() { + return "resource"; + } + + @Override + public PropertySourceProvider create(Map<String,String> parameters) { + String location = parameters.get("location"); + if(location==null){ + LOG.warning("Cannot read 'location' from " + parameters + ", example: " + example()); + return null; + } + Collection<URL> resources = createResources(location); + List<PropertySource> propertySources = new ArrayList<>(); + if(resources!=null) { + String[] formats = getFormats(parameters); + for(URL resource:resources) { + ConfigurationData data; + try { + if (formats.length == 0) { + data = ConfigurationFormats.readConfigurationData(resource); + } else { + data = ConfigurationFormats.readConfigurationData(resource, + ConfigurationFormats.getFormats(formats)); + } + propertySources.add(new MappedConfigurationDataPropertySource(data)); + } catch (Exception e) { + LOG.log(Level.SEVERE, "Failed to read property source from resource: " + location, e); + } + } + } + final List<PropertySource> finalPropertySources = Collections.unmodifiableList(propertySources); + return new PropertySourceProvider() { + @Override + public Collection<PropertySource> getPropertySources() { + return finalPropertySources; + } + }; + } + + @Override + public Class<? extends PropertySourceProvider> getType() { + return PropertySourceProvider.class; + } + + protected String[] getFormats(Map<String, String> parameters) { + String val = parameters.get("formats"); + if(val==null){ + return new String[0]; + } + String[] formats = val.split(","); + for(int i=0;i<formats.length;i++){ + formats[i] = formats[i].trim(); + } + return formats; + } + + protected String example() { + return "<source-provider type=\""+getName()+"\">\n" + + " <param name=\"location\">/META-INF/**/config.xml</param>\n" + + " <param name=\"formats\">xml-properties</param>\n" + + "</source-provider>\n"; + } + + protected Collection<URL> createResources(String location) { + try { + return ConfigResources.getResourceResolver().getResources(location.split(":")); + } catch (Exception e) { + LOG.warning("Invalid resource expression '" + location + "'."); + return null; + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/SysPropertiesFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/SysPropertiesFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/SysPropertiesFactory.java new file mode 100644 index 0000000..aec4837 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/SysPropertiesFactory.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal.factories; + +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.SystemPropertySource; + +import java.util.Map; + +/** + * Factory for configuring system properties based property sources. + */ +public final class SysPropertiesFactory implements ItemFactory<PropertySource>{ + @Override + public String getName() { + return "sys-properties"; + } + + @Override + public PropertySource create(Map<String,String> parameters) { + return new SystemPropertySource(); + } + + @Override + public Class<? extends PropertySource> getType() { + return PropertySource.class; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/URLPropertySourceFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/URLPropertySourceFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/URLPropertySourceFactory.java new file mode 100644 index 0000000..342eebb --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/internal/factories/URLPropertySourceFactory.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tamaya.metamodel.internal.factories; + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationFormats; +import org.apache.tamaya.format.MappedConfigurationDataPropertySource; +import org.apache.tamaya.functions.Supplier; +import org.apache.tamaya.metamodel.internal.ComponentConfigurator; +import org.apache.tamaya.metamodel.spi.ItemFactory; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.PropertiesResourcePropertySource; + +import javax.security.auth.RefreshFailedException; +import javax.security.auth.Refreshable; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Factory for configuring resource based property sources. + */ +public class URLPropertySourceFactory implements ItemFactory<PropertySource>{ + + private static final Logger LOG = Logger.getLogger(FilePropertySourceFactory.class.getName()); + + @Override + public String getName() { + return "url"; + } + + @Override + public PropertySource create(Map<String,String> parameters) { + String location = parameters.get("location"); + if(location==null){ + LOG.warning("Cannot read 'location' from " + parameters + ", example: " + example()); + return null; + } + URL resource = createResource(location); + if(resource!=null) { + String[] formats = getFormats(parameters); + String name = resource.toString(); + RefreshablePropertySource ps = new RefreshablePropertySource(name, new LazyDataSupplier(resource, formats)); + ComponentConfigurator.configure(ps, parameters); + return ps; + } + return null; + } + + protected String example() { + return "<source type=\""+getName()+"\">\n" + + " <param name=\"location\">http://127.0.0.1:1110/config.xml</param>\n" + + " <param name=\"formats\">xml-properties</param>\n" + + "</source>\n"; + } + + protected URL createResource(String location) { + try { + return new URL(location); + } catch (MalformedURLException e) { + LOG.warning("Invalid url '" + location + "'."); + return null; + } + } + + protected String[] getFormats(Map<String, String> parameters) { + String val = parameters.get("formats"); + if(val==null){ + return new String[0]; + } + String[] formats = val.split(","); + for(int i=0;i<formats.length;i++){ + formats[i] = formats[i].trim(); + } + return formats; + } + + @Override + public Class<? extends PropertySource> getType() { + return PropertySource.class; + } + + private static final class LazyDataSupplier implements Supplier<ConfigurationData> { + + private String[] formats; + private URL resource; + + public LazyDataSupplier(URL resource, String[] formats) { + this.formats = Objects.requireNonNull(formats); + this.resource = Objects.requireNonNull(resource); + } + + @Override + public ConfigurationData get() { + ConfigurationData data; + try { + if (formats.length == 0) { + data = ConfigurationFormats.readConfigurationData(resource); + } else { + data = ConfigurationFormats.readConfigurationData(resource, + ConfigurationFormats.getFormats(formats)); + } + return data; + } catch (Exception e) { + LOG.log(Level.INFO, "Failed to read property source from resource: " + resource, e); + return null; + } + } + } + + private static final class RefreshablePropertySource extends MappedConfigurationDataPropertySource + implements Refreshable{ + + public RefreshablePropertySource(String name, Supplier<ConfigurationData> dataSupplier) { + super(name, dataSupplier); + } + + public RefreshablePropertySource(String name, int defaultOrdinal, Supplier<ConfigurationData> dataSupplier) { + super(name, dataSupplier); + } + + @Override + public boolean isCurrent() { + return false; + } + + @Override + public void refresh() throws RefreshFailedException { + super.load(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/afc19d0e/metamodel/src/main/java/org/apache/tamaya/metamodel/spi/ItemFactory.java ---------------------------------------------------------------------- diff --git a/metamodel/src/main/java/org/apache/tamaya/metamodel/spi/ItemFactory.java b/metamodel/src/main/java/org/apache/tamaya/metamodel/spi/ItemFactory.java new file mode 100644 index 0000000..1cfb8b9 --- /dev/null +++ b/metamodel/src/main/java/org/apache/tamaya/metamodel/spi/ItemFactory.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.metamodel.spi; + +import java.util.Map; + +/** + * Factory for items that are configurable using meta-configuration. + * This allows easy registration using the name, instead of the fully qualified + * class name. + */ +public interface ItemFactory<T> { + + /** + * Get the factory name. + * @return the factory name, not null. + */ + String getName(); + + /** + * Create a new instance. + * @param parameters the parameters for configuring the instance. + * @return the new instance, not null. + */ + T create(Map<String,String> parameters); + + /** + * Get the target type created by this factory. This can be used to + * assign the factory to an acording item base type, e.g. a PropertySource, + * PropertySourceProvider, PropertyFilter etc. + * @return the target type, not null. + */ + Class<? extends T> getType(); + +}