TAMAYA-202: Use bnd-maven-plugin. Minimize required config. Renamed invalid package in yaml format module.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/2949a137 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/2949a137 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/2949a137 Branch: refs/heads/master Commit: 2949a137a09206aca5e6656bd38736ff975f0f0b Parents: 487e924 Author: anatole <[email protected]> Authored: Sat Nov 26 01:13:57 2016 +0100 Committer: anatole <[email protected]> Committed: Sat Nov 26 01:13:57 2016 +0100 ---------------------------------------------------------------------- apache-commons/bnd.bnd | 2 + .../commons/CommonsConfigPropertySource.java | 88 +++++ .../tamaya/commons/IniConfigurationFormat.java | 75 ++++ .../commons/CommonsConfigPropertySource.java | 88 ----- .../commons/IniConfigurationFormat.java | 75 ---- camel/bnd.bnd | 2 + configured-sysprops/bnd.bnd | 2 + .../se/ConfiguredSystemProperties.java | 340 ------------------- .../sysprops/ConfiguredSystemProperties.java | 340 +++++++++++++++++++ consul/bnd.bnd | 2 + etcd/bnd.bnd | 2 + hazelcast/bnd.bnd | 2 + jodatime/bnd.bnd | 2 + metamodel/bnd.bnd | 4 + osgi/bnd.bnd | 2 + .../tamaya/integration/osgi/Activator.java | 134 -------- .../integration/osgi/OSGIConfigRootMapper.java | 36 -- .../osgi/OSGIEnhancedConfiguration.java | 117 ------- .../integration/osgi/TamayaConfigAdminImpl.java | 196 ----------- .../osgi/TamayaConfigurationImpl.java | 127 ------- .../java/org/apache/tamaya/osgi/Activator.java | 134 ++++++++ .../tamaya/osgi/OSGIConfigRootMapper.java | 36 ++ .../tamaya/osgi/OSGIEnhancedConfiguration.java | 117 +++++++ .../tamaya/osgi/TamayaConfigAdminImpl.java | 196 +++++++++++ .../tamaya/osgi/TamayaConfigurationImpl.java | 127 +++++++ osgi/src/test/resources/felix.properties | 2 +- propertysources/bnd.bnd | 2 + remote/bnd.bnd | 2 + server/bnd.bnd | 3 + tamaya-sandbox.iml | 13 + usagetracker/bnd.bnd | 3 + validation/bnd.bnd | 3 + 32 files changed, 1160 insertions(+), 1114 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/apache-commons/bnd.bnd ---------------------------------------------------------------------- diff --git a/apache-commons/bnd.bnd b/apache-commons/bnd.bnd new file mode 100644 index 0000000..2e5837e --- /dev/null +++ b/apache-commons/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.commons \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/apache-commons/src/main/java/org/apache/tamaya/commons/CommonsConfigPropertySource.java ---------------------------------------------------------------------- diff --git a/apache-commons/src/main/java/org/apache/tamaya/commons/CommonsConfigPropertySource.java b/apache-commons/src/main/java/org/apache/tamaya/commons/CommonsConfigPropertySource.java new file mode 100644 index 0000000..4aa47ad --- /dev/null +++ b/apache-commons/src/main/java/org/apache/tamaya/commons/CommonsConfigPropertySource.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.commons; + +//X TODO Move out into separate commons-config integration module... + +import org.apache.commons.configuration.Configuration; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; + +/** + * PropertySource that wraps {@link org.apache.commons.configuration.Configuration}. + */ +public class CommonsConfigPropertySource implements PropertySource { + + private Configuration commonsConfig; + private int ordinal; + private String name; + + public CommonsConfigPropertySource(int ordinal, String name, Configuration commonsConfig) { + this.commonsConfig = Objects.requireNonNull(commonsConfig); + this.ordinal = ordinal; + this.name = Objects.requireNonNull(name); + } + + public CommonsConfigPropertySource(String name, Configuration commonsConfig) { + commonsConfig = Objects.requireNonNull(commonsConfig); + this.name = Objects.requireNonNull(name); + try { + this.ordinal = commonsConfig.getInt(PropertySource.TAMAYA_ORDINAL); + } catch (Exception e) { + this.ordinal = 0; + } + } + + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return name; + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, commonsConfig.getString(key), + getName()); + } + + @Override + public Map<String, String> getProperties() { + Map<String, String> config = new HashMap<>(); + Iterator<String> keyIter = commonsConfig.getKeys(); + while (keyIter.hasNext()) { + String key = keyIter.next(); + config.put(key, commonsConfig.getString(key)); + } + return config; + } + + @Override + public boolean isScannable() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java b/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java new file mode 100644 index 0000000..916d4ed --- /dev/null +++ b/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java @@ -0,0 +1,75 @@ +/* + * 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.commons; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalINIConfiguration; +import org.apache.commons.configuration.SubnodeConfiguration; +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationDataBuilder; +import org.apache.tamaya.format.ConfigurationFormat; + +import java.io.InputStream; +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Implements a ini file format based on the APache Commons + * {@link org.apache.commons.configuration.HierarchicalINIConfiguration}. + */ +public class IniConfigurationFormat implements ConfigurationFormat { + + public ConfigurationData readConfiguration(URL url) { + ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(url.toString(), this); + try { + HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url); + for(String section:commonIniConfiguration.getSections()){ + SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section); + Map<String, String> properties = new HashMap<>(); + Iterator<String> keyIter = sectionConfig.getKeys(); + while(keyIter.hasNext()){ + String key = keyIter.next(); + properties.put(key, sectionConfig.getString(key)); + } + builder.addSectionProperties(section, properties); + } + } catch (ConfigurationException e) { + throw new ConfigException("Failed to parse ini-file format from " + url, e); + } + return builder.build(); + } + + @Override + public String getName() { + throw new RuntimeException("Not implemented yet!"); + } + + @Override + public boolean accepts(URL url) { + throw new RuntimeException("Not implemented yet!"); + } + + @Override + public ConfigurationData readConfiguration(String resource, InputStream inputStream) { + throw new RuntimeException("Not implemented yet!"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/apache-commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java ---------------------------------------------------------------------- diff --git a/apache-commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java b/apache-commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java deleted file mode 100644 index 2cbd4c3..0000000 --- a/apache-commons/src/main/java/org/apache/tamaya/integration/commons/CommonsConfigPropertySource.java +++ /dev/null @@ -1,88 +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.integration.commons; - -//X TODO Move out into separate commons-config integration module... - -import org.apache.commons.configuration.Configuration; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Objects; - -/** - * PropertySource that wraps {@link org.apache.commons.configuration.Configuration}. - */ -public class CommonsConfigPropertySource implements PropertySource { - - private Configuration commonsConfig; - private int ordinal; - private String name; - - public CommonsConfigPropertySource(int ordinal, String name, Configuration commonsConfig) { - this.commonsConfig = Objects.requireNonNull(commonsConfig); - this.ordinal = ordinal; - this.name = Objects.requireNonNull(name); - } - - public CommonsConfigPropertySource(String name, Configuration commonsConfig) { - commonsConfig = Objects.requireNonNull(commonsConfig); - this.name = Objects.requireNonNull(name); - try { - this.ordinal = commonsConfig.getInt(PropertySource.TAMAYA_ORDINAL); - } catch (Exception e) { - this.ordinal = 0; - } - } - - @Override - public int getOrdinal() { - return ordinal; - } - - @Override - public String getName() { - return name; - } - - @Override - public PropertyValue get(String key) { - return PropertyValue.of(key, commonsConfig.getString(key), - getName()); - } - - @Override - public Map<String, String> getProperties() { - Map<String, String> config = new HashMap<>(); - Iterator<String> keyIter = commonsConfig.getKeys(); - while (keyIter.hasNext()) { - String key = keyIter.next(); - config.put(key, commonsConfig.getString(key)); - } - return config; - } - - @Override - public boolean isScannable() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/apache-commons/src/main/java/org/apache/tamaya/integration/commons/IniConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/apache-commons/src/main/java/org/apache/tamaya/integration/commons/IniConfigurationFormat.java b/apache-commons/src/main/java/org/apache/tamaya/integration/commons/IniConfigurationFormat.java deleted file mode 100644 index 67cf7fc..0000000 --- a/apache-commons/src/main/java/org/apache/tamaya/integration/commons/IniConfigurationFormat.java +++ /dev/null @@ -1,75 +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.integration.commons; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.HierarchicalINIConfiguration; -import org.apache.commons.configuration.SubnodeConfiguration; -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.format.ConfigurationData; -import org.apache.tamaya.format.ConfigurationDataBuilder; -import org.apache.tamaya.format.ConfigurationFormat; - -import java.io.InputStream; -import java.net.URL; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * Implements a ini file format based on the APache Commons - * {@link org.apache.commons.configuration.HierarchicalINIConfiguration}. - */ -public class IniConfigurationFormat implements ConfigurationFormat { - - public ConfigurationData readConfiguration(URL url) { - ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(url.toString(), this); - try { - HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url); - for(String section:commonIniConfiguration.getSections()){ - SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section); - Map<String, String> properties = new HashMap<>(); - Iterator<String> keyIter = sectionConfig.getKeys(); - while(keyIter.hasNext()){ - String key = keyIter.next(); - properties.put(key, sectionConfig.getString(key)); - } - builder.addSectionProperties(section, properties); - } - } catch (ConfigurationException e) { - throw new ConfigException("Failed to parse ini-file format from " + url, e); - } - return builder.build(); - } - - @Override - public String getName() { - throw new RuntimeException("Not implemented yet!"); - } - - @Override - public boolean accepts(URL url) { - throw new RuntimeException("Not implemented yet!"); - } - - @Override - public ConfigurationData readConfiguration(String resource, InputStream inputStream) { - throw new RuntimeException("Not implemented yet!"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/camel/bnd.bnd ---------------------------------------------------------------------- diff --git a/camel/bnd.bnd b/camel/bnd.bnd new file mode 100644 index 0000000..94e84d1 --- /dev/null +++ b/camel/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.integration.camel \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/configured-sysprops/bnd.bnd ---------------------------------------------------------------------- diff --git a/configured-sysprops/bnd.bnd b/configured-sysprops/bnd.bnd new file mode 100644 index 0000000..97daf88 --- /dev/null +++ b/configured-sysprops/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.sysprops \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/configured-sysprops/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java ---------------------------------------------------------------------- diff --git a/configured-sysprops/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java b/configured-sysprops/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java deleted file mode 100644 index bb4abbe..0000000 --- a/configured-sysprops/src/main/java/org/apache/tamaya/integration/se/ConfiguredSystemProperties.java +++ /dev/null @@ -1,340 +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.integration.se; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.Reader; -import java.io.Writer; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.logging.Logger; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; - -/** - * Properties implementation class that can be applied as current System properties by calling - * {@link ConfiguredSystemProperties#install()}. The system properties will - * then behave contextually depending on the current runtime configuration active. - */ -public class ConfiguredSystemProperties extends Properties { - - private static final long serialVersionUID = 2152870929299226804L; - - private static final Logger LOG = Logger.getLogger(ConfiguredSystemProperties.class.getName()); - private Properties initialProperties; - private static volatile Properties contextualProperties; - - private final Object LOCK = new Object(); - - - private ConfiguredSystemProperties(Properties initialProperties) { - super(initialProperties); - this.initialProperties = initialProperties; - } - - public static void install() { - Properties props = System.getProperties(); - if (props instanceof ConfiguredSystemProperties) { - return; - } - ConfiguredSystemProperties systemProps = new ConfiguredSystemProperties(props); - LOG.finest("Installing enhanced system properties..."); - System.setProperties(systemProps); - LOG.info("Installed enhanced system properties successfully."); - } - - public static void uninstall() { - Properties props = System.getProperties(); - if (props instanceof ConfiguredSystemProperties) { - Properties initialProperties = ((ConfiguredSystemProperties) props).initialProperties; - LOG.finest("Uninstalling enhanced system properties..."); - System.setProperties(initialProperties); - LOG.info("Uninstalled enhanced system properties successfully."); - } - } - - @Override - public String getProperty(String key) { - return getContextualProperties().getProperty(key); - } - - @Override - public String getProperty(String key, String defaultValue) { - return getContextualProperties().getProperty(key, defaultValue); - } - - @Override - public Enumeration<?> propertyNames() { - return getContextualProperties().propertyNames(); - } - - @Override - public Set<String> stringPropertyNames() { - return getContextualProperties().stringPropertyNames(); - } - - @Override - public synchronized int size() { - return getContextualProperties().size(); - } - - @Override - public synchronized Enumeration<Object> keys() { - return getContextualProperties().keys(); - } - - @Override - public synchronized Enumeration<Object> elements() { - return getContextualProperties().elements(); - } - - @Override - public synchronized boolean contains(Object value) { - return getContextualProperties().contains(value); - } - - @Override - public boolean containsValue(Object value) { - return getContextualProperties().containsValue(value); - } - - @Override - public synchronized boolean containsKey(Object key) { - return getContextualProperties().containsKey(key); - } - - @Override - public synchronized Object get(Object key) { - return getContextualProperties().get(key); - } - - @Override - public synchronized Object clone() { - return getContextualProperties().clone(); - } - - @Override - public Set<Object> keySet() { - return getContextualProperties().keySet(); - } - - @Override - public Set<Map.Entry<Object, Object>> entrySet() { - return getContextualProperties().entrySet(); - } - - @Override - public Collection<Object> values() { - return getContextualProperties().values(); - } - - - @Override - public String toString() { - return getContextualProperties().toString(); - } - - @Override - public synchronized Object setProperty(String key, String value) { - return getContextualProperties().setProperty(key, value); - } - - @Override - public synchronized void load(Reader reader) throws IOException { - getContextualProperties().load(reader); - } - - @Override - public synchronized void load(InputStream inStream) throws IOException { - getContextualProperties().load(inStream); - } - - @SuppressWarnings("deprecation") - @Override - public void save(OutputStream out, String comments) { - super.save(out, comments); - } - - @Override - public void store(Writer writer, String comments) throws IOException { - getContextualProperties().store(writer, comments); - } - - @Override - public void store(OutputStream out, String comments) throws IOException { - getContextualProperties().store(out, comments); - } - - @Override - public void loadFromXML(InputStream in) throws IOException { - getContextualProperties().loadFromXML(in); - } - - @Override - public void storeToXML(OutputStream os, String comment) throws IOException { - getContextualProperties().storeToXML(os, comment); - } - - @Override - public void storeToXML(OutputStream os, String comment, String encoding) throws IOException { - getContextualProperties().storeToXML(os, comment, encoding); - } - - @Override - public void list(PrintStream out) { - getContextualProperties().list(out); - } - - @Override - public void list(PrintWriter out) { - getContextualProperties().list(out); - } - - @Override - public boolean isEmpty() { - return getContextualProperties().isEmpty(); - } - - @Override - public Object put(Object key, Object value) { - return getContextualProperties().put(key, value); - } - - @Override - public Object remove(Object key) { - return getContextualProperties().remove(key); - } - - @Override - public void putAll(Map<?, ?> t) { - getContextualProperties().putAll(t); - } - - @Override - public void clear() { - getContextualProperties().clear(); - } - - @Override - public boolean equals(Object o) { - return getContextualProperties().equals(o); - } - - @Override - public int hashCode() { - return getContextualProperties().hashCode(); - } - -// JDK 8 and later... -// @Override -// public Object getOrDefault(Object key, Object defaultValue) { -// return getContextualProperties().getOrDefault(key, defaultValue); -// } -// -// @Override -// public void forEach(BiConsumer<? super Object, ? super Object> action) { -// getContextualProperties().forEach(action); -// } -// -// -// @Override -// public Object computeIfAbsent(Object key, Function<? super Object, ?> mappingFunction) { -// return getContextualProperties().computeIfAbsent(key, mappingFunction); -// } -// -// @Override -// public synchronized Object computeIfPresent(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction) { -// return getContextualProperties().computeIfPresent(key, remappingFunction); -// } -// -// @Override -// public synchronized Object compute(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction) { -// return getContextualProperties().compute(key, remappingFunction); -// } -// @Override -// public void replaceAll(BiFunction<? super Object, ? super Object, ?> function) { -// getContextualProperties().replaceAll(function); -// } -// -// @Override -// public Object putIfAbsent(Object key, Object value) { -// return getContextualProperties().putIfAbsent(key, value); -// } -// -// @Override -// public boolean remove(Object key, Object value) { -// return getContextualProperties().remove(key, value); -// } -// -// @Override -// public boolean replace(Object key, Object oldValue, Object newValue) { -// return getContextualProperties().replace(key, oldValue, newValue); -// } -// -// @Override -// public Object replace(Object key, Object value) { -// return getContextualProperties().replace(key, value); -// } -// -// @Override -// public Object merge(Object key, Object value, BiFunction<? super Object, ? super Object, ?> remappingFunction) { -// return getContextualProperties().merge(key, value, remappingFunction); -// } - - public Properties getInitialProperties() { - return initialProperties; - } - - /** - * Uninstalls the contextual system properties for the current context, as determined by the current - * context provider active. - */ - public static void resetProperties() { - contextualProperties.clear(); - } - - protected Properties getContextualProperties() { - if (contextualProperties == null) { - synchronized (LOCK) { - if (contextualProperties == null) { - contextualProperties = createNewProperties(); - } - } - } - return contextualProperties; - } - - protected Properties createNewProperties() { - Properties props = new Properties(initialProperties); - Configuration config = ConfigurationProvider.getConfiguration(); - Map<String, String> configMap = config.getProperties(); - for (Map.Entry<String, String> en : configMap.entrySet()) { - props.put(en.getKey(), en.getValue()); - } - return props; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/configured-sysprops/src/main/java/org/apache/tamaya/sysprops/ConfiguredSystemProperties.java ---------------------------------------------------------------------- diff --git a/configured-sysprops/src/main/java/org/apache/tamaya/sysprops/ConfiguredSystemProperties.java b/configured-sysprops/src/main/java/org/apache/tamaya/sysprops/ConfiguredSystemProperties.java new file mode 100644 index 0000000..18f0349 --- /dev/null +++ b/configured-sysprops/src/main/java/org/apache/tamaya/sysprops/ConfiguredSystemProperties.java @@ -0,0 +1,340 @@ +/* + * 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.sysprops; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.Writer; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.logging.Logger; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; + +/** + * Properties implementation class that can be applied as current System properties by calling + * {@link ConfiguredSystemProperties#install()}. The system properties will + * then behave contextually depending on the current runtime configuration active. + */ +public class ConfiguredSystemProperties extends Properties { + + private static final long serialVersionUID = 2152870929299226804L; + + private static final Logger LOG = Logger.getLogger(ConfiguredSystemProperties.class.getName()); + private Properties initialProperties; + private static volatile Properties contextualProperties; + + private final Object LOCK = new Object(); + + + private ConfiguredSystemProperties(Properties initialProperties) { + super(initialProperties); + this.initialProperties = initialProperties; + } + + public static void install() { + Properties props = System.getProperties(); + if (props instanceof ConfiguredSystemProperties) { + return; + } + ConfiguredSystemProperties systemProps = new ConfiguredSystemProperties(props); + LOG.finest("Installing enhanced system properties..."); + System.setProperties(systemProps); + LOG.info("Installed enhanced system properties successfully."); + } + + public static void uninstall() { + Properties props = System.getProperties(); + if (props instanceof ConfiguredSystemProperties) { + Properties initialProperties = ((ConfiguredSystemProperties) props).initialProperties; + LOG.finest("Uninstalling enhanced system properties..."); + System.setProperties(initialProperties); + LOG.info("Uninstalled enhanced system properties successfully."); + } + } + + @Override + public String getProperty(String key) { + return getContextualProperties().getProperty(key); + } + + @Override + public String getProperty(String key, String defaultValue) { + return getContextualProperties().getProperty(key, defaultValue); + } + + @Override + public Enumeration<?> propertyNames() { + return getContextualProperties().propertyNames(); + } + + @Override + public Set<String> stringPropertyNames() { + return getContextualProperties().stringPropertyNames(); + } + + @Override + public synchronized int size() { + return getContextualProperties().size(); + } + + @Override + public synchronized Enumeration<Object> keys() { + return getContextualProperties().keys(); + } + + @Override + public synchronized Enumeration<Object> elements() { + return getContextualProperties().elements(); + } + + @Override + public synchronized boolean contains(Object value) { + return getContextualProperties().contains(value); + } + + @Override + public boolean containsValue(Object value) { + return getContextualProperties().containsValue(value); + } + + @Override + public synchronized boolean containsKey(Object key) { + return getContextualProperties().containsKey(key); + } + + @Override + public synchronized Object get(Object key) { + return getContextualProperties().get(key); + } + + @Override + public synchronized Object clone() { + return getContextualProperties().clone(); + } + + @Override + public Set<Object> keySet() { + return getContextualProperties().keySet(); + } + + @Override + public Set<Map.Entry<Object, Object>> entrySet() { + return getContextualProperties().entrySet(); + } + + @Override + public Collection<Object> values() { + return getContextualProperties().values(); + } + + + @Override + public String toString() { + return getContextualProperties().toString(); + } + + @Override + public synchronized Object setProperty(String key, String value) { + return getContextualProperties().setProperty(key, value); + } + + @Override + public synchronized void load(Reader reader) throws IOException { + getContextualProperties().load(reader); + } + + @Override + public synchronized void load(InputStream inStream) throws IOException { + getContextualProperties().load(inStream); + } + + @SuppressWarnings("deprecation") + @Override + public void save(OutputStream out, String comments) { + super.save(out, comments); + } + + @Override + public void store(Writer writer, String comments) throws IOException { + getContextualProperties().store(writer, comments); + } + + @Override + public void store(OutputStream out, String comments) throws IOException { + getContextualProperties().store(out, comments); + } + + @Override + public void loadFromXML(InputStream in) throws IOException { + getContextualProperties().loadFromXML(in); + } + + @Override + public void storeToXML(OutputStream os, String comment) throws IOException { + getContextualProperties().storeToXML(os, comment); + } + + @Override + public void storeToXML(OutputStream os, String comment, String encoding) throws IOException { + getContextualProperties().storeToXML(os, comment, encoding); + } + + @Override + public void list(PrintStream out) { + getContextualProperties().list(out); + } + + @Override + public void list(PrintWriter out) { + getContextualProperties().list(out); + } + + @Override + public boolean isEmpty() { + return getContextualProperties().isEmpty(); + } + + @Override + public Object put(Object key, Object value) { + return getContextualProperties().put(key, value); + } + + @Override + public Object remove(Object key) { + return getContextualProperties().remove(key); + } + + @Override + public void putAll(Map<?, ?> t) { + getContextualProperties().putAll(t); + } + + @Override + public void clear() { + getContextualProperties().clear(); + } + + @Override + public boolean equals(Object o) { + return getContextualProperties().equals(o); + } + + @Override + public int hashCode() { + return getContextualProperties().hashCode(); + } + +// JDK 8 and later... +// @Override +// public Object getOrDefault(Object key, Object defaultValue) { +// return getContextualProperties().getOrDefault(key, defaultValue); +// } +// +// @Override +// public void forEach(BiConsumer<? super Object, ? super Object> action) { +// getContextualProperties().forEach(action); +// } +// +// +// @Override +// public Object computeIfAbsent(Object key, Function<? super Object, ?> mappingFunction) { +// return getContextualProperties().computeIfAbsent(key, mappingFunction); +// } +// +// @Override +// public synchronized Object computeIfPresent(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction) { +// return getContextualProperties().computeIfPresent(key, remappingFunction); +// } +// +// @Override +// public synchronized Object compute(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction) { +// return getContextualProperties().compute(key, remappingFunction); +// } +// @Override +// public void replaceAll(BiFunction<? super Object, ? super Object, ?> function) { +// getContextualProperties().replaceAll(function); +// } +// +// @Override +// public Object putIfAbsent(Object key, Object value) { +// return getContextualProperties().putIfAbsent(key, value); +// } +// +// @Override +// public boolean remove(Object key, Object value) { +// return getContextualProperties().remove(key, value); +// } +// +// @Override +// public boolean replace(Object key, Object oldValue, Object newValue) { +// return getContextualProperties().replace(key, oldValue, newValue); +// } +// +// @Override +// public Object replace(Object key, Object value) { +// return getContextualProperties().replace(key, value); +// } +// +// @Override +// public Object merge(Object key, Object value, BiFunction<? super Object, ? super Object, ?> remappingFunction) { +// return getContextualProperties().merge(key, value, remappingFunction); +// } + + public Properties getInitialProperties() { + return initialProperties; + } + + /** + * Uninstalls the contextual system properties for the current context, as determined by the current + * context provider active. + */ + public static void resetProperties() { + contextualProperties.clear(); + } + + protected Properties getContextualProperties() { + if (contextualProperties == null) { + synchronized (LOCK) { + if (contextualProperties == null) { + contextualProperties = createNewProperties(); + } + } + } + return contextualProperties; + } + + protected Properties createNewProperties() { + Properties props = new Properties(initialProperties); + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String, String> configMap = config.getProperties(); + for (Map.Entry<String, String> en : configMap.entrySet()) { + props.put(en.getKey(), en.getValue()); + } + return props; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/consul/bnd.bnd ---------------------------------------------------------------------- diff --git a/consul/bnd.bnd b/consul/bnd.bnd new file mode 100644 index 0000000..47b26c8 --- /dev/null +++ b/consul/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.consul \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/etcd/bnd.bnd ---------------------------------------------------------------------- diff --git a/etcd/bnd.bnd b/etcd/bnd.bnd new file mode 100644 index 0000000..fc51493 --- /dev/null +++ b/etcd/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.etcd \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/hazelcast/bnd.bnd ---------------------------------------------------------------------- diff --git a/hazelcast/bnd.bnd b/hazelcast/bnd.bnd new file mode 100644 index 0000000..1a7a3a7 --- /dev/null +++ b/hazelcast/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.hazelcast \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/jodatime/bnd.bnd ---------------------------------------------------------------------- diff --git a/jodatime/bnd.bnd b/jodatime/bnd.bnd new file mode 100644 index 0000000..ecd9d5d --- /dev/null +++ b/jodatime/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.jodatime \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/metamodel/bnd.bnd ---------------------------------------------------------------------- diff --git a/metamodel/bnd.bnd b/metamodel/bnd.bnd new file mode 100644 index 0000000..c61ef5e --- /dev/null +++ b/metamodel/bnd.bnd @@ -0,0 +1,4 @@ +Export-Package: \ + org.apache.tamaya.metamodel,\ + org.apache.tamaya.metamodel.dsl,\ + org.apache.tamaya.metamodel.spi \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/bnd.bnd ---------------------------------------------------------------------- diff --git a/osgi/bnd.bnd b/osgi/bnd.bnd new file mode 100644 index 0000000..222f607 --- /dev/null +++ b/osgi/bnd.bnd @@ -0,0 +1,2 @@ +Export-Package: \ + org.apache.tamaya.osgi \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/integration/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/integration/osgi/Activator.java b/osgi/src/main/java/org/apache/tamaya/integration/osgi/Activator.java deleted file mode 100644 index 7425dfb..0000000 --- a/osgi/src/main/java/org/apache/tamaya/integration/osgi/Activator.java +++ /dev/null @@ -1,134 +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.integration.osgi; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.inject.ConfigurationInjection; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ConfigurationAdmin; -import org.osgi.util.tracker.ServiceTracker; - -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Activator that registers the Tamaya based Service Class for {@link ConfigurationAdmin}, - * using a default service priority of {@code 0}. This behaviour is configurable based on OSGI properties: - * <ul> - * <li><p><b>org.tamaya.integration.osgi.cm.ranking, type: int</b> allows to configure the OSGI service ranking for - * Tamaya based ConfigurationAdmin instance. The default ranking used is 10.</p></li> - * <li><p><b>org.tamaya.integration.osgi.cm.override, type: boolean</b> allows to configure if Tamaya should - * register its ConfigAdmin service. Default is true.</p></li> - * </ul> - */ -public class Activator implements BundleActivator { - - private static final String SERVICE_RANKING_PROP = "org.tamaya.integration.osgi.cm.ranking"; - - private static final String SERVICE_OVERRIDE_PROP = "org.tamaya.integration.osgi.cm.override"; - - private static final String SERVICE_INJECT_PROP = "org.tamaya.integration.osgi.cm.inject"; - - private static final Integer DEFAULT_RANKING = 10; - - private static final Logger LOG = Logger.getLogger(Activator.class.getName()); - - private ServiceRegistration<ConfigurationAdmin> registration; - - private ServiceTracker<Object, Object> injectionTracker; - - @Override - public void start(BundleContext context) throws Exception { - String val = context.getProperty(SERVICE_OVERRIDE_PROP); - if(val == null || Boolean.parseBoolean(val)){ - Dictionary<String, Object> props = new Hashtable<>(); - String ranking = context.getProperty(SERVICE_RANKING_PROP); - if (ranking == null) { - props.put(Constants.SERVICE_RANKING, DEFAULT_RANKING); - } else { - props.put(Constants.SERVICE_RANKING, Integer.valueOf(ranking)); - } - TamayaConfigAdminImpl cm = new TamayaConfigAdminImpl(context); - registration = context.registerService(ConfigurationAdmin.class, cm, props); - } - - // register injection mechanisms, if not configured otherwise - val = context.getProperty(SERVICE_INJECT_PROP); - if(val == null || Boolean.parseBoolean(val)){ - injectionTracker = new ServiceTracker<Object, Object>(context, Object.class, null) { - @Override - public Object addingService(ServiceReference<Object> reference) { - Object service = context.getService(reference); - Object pidObj = reference.getProperty(Constants.SERVICE_PID); - if (pidObj instanceof String) { - String pid = (String) pidObj; - ConfigurationAdmin configAdmin = null; - ServiceReference<ConfigurationAdmin> adminRef = - context.getServiceReference(ConfigurationAdmin.class); - if(adminRef!=null){ - configAdmin = context.getService(adminRef); - } - try { - Configuration targetConfig = null; - if(configAdmin != null){ - org.osgi.service.cm.Configuration osgiConfig = configAdmin.getConfiguration(pid); - if(osgiConfig!=null){ - targetConfig = new OSGIEnhancedConfiguration(osgiConfig); - } - } - if(targetConfig==null){ - targetConfig = ConfigurationProvider.getConfiguration(); - } - ConfigurationInjection.getConfigurationInjector().configure(service, targetConfig); - } catch (Exception e) { - LOG.log(Level.WARNING, "Error configuring Service: " + service, e); - } - } else { - LOG.log(Level.SEVERE, "Unsupported pid: " + pidObj); - } - return service; - } - - @Override - public void removedService(ServiceReference<Object> reference, Object service) { - context.ungetService(reference); - } - }; - injectionTracker.open(); - } - } - - @Override - public void stop(BundleContext context) throws Exception { - if (registration != null) { - registration.unregister(); - } - if(injectionTracker!=null){ - injectionTracker.close(); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIConfigRootMapper.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIConfigRootMapper.java b/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIConfigRootMapper.java deleted file mode 100644 index 836df8b..0000000 --- a/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIConfigRootMapper.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.integration.osgi; - -/** - * Mapping function for mapping Tamaya configuration sections to OSGI pids. - */ -public interface OSGIConfigRootMapper { - - /** - * Map the given OSGI pid to a corresponding configuration section in Tamaya. Es an example (and this is also the - * default implemented) a configuration mapping for {@code pid/factoryPid==myBundle} could be {@code [bundle:myBundle]}. - * This mapping is used as a prefix when collecting the corresponding entries for the OSGI configuration. - * @param pid the OSGI pid, or null - * @param factoryPid the OSGI factoryPid, or null - * @return return the corresponding config root section. For ommitting any root section simply return an empty - * String. - */ - String getTamayaConfigRoot(String pid, String factoryPid); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIEnhancedConfiguration.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIEnhancedConfiguration.java b/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIEnhancedConfiguration.java deleted file mode 100644 index 5e813af..0000000 --- a/osgi/src/main/java/org/apache/tamaya/integration/osgi/OSGIEnhancedConfiguration.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.integration.osgi; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spisupport.BasePropertySource; -import org.apache.tamaya.spisupport.DefaultConfiguration; -import org.apache.tamaya.spisupport.DefaultConfigurationContext; - -import java.util.Dictionary; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -/** - * Configuration object that also reflects the values provided by the OSGI ConfigAdmin Configuration. - * Similar to other tamaya areas adding a tamaya.ordinal into the corresponding OSGI configuration for - * a pif/factoryPid allows to control the ordinal/priority of the OSGI configuration related to other - * configured Tamaya Property Sources. Overall the configuration evaluation for Tamaya follows the - * same rules, with the difference that each bunldle owns its own ConfigAdmin based part. From - * Tamaya, the granularity depends on the implementation of the ConfigurationProviderSpi. By default - * Tamaya configuration is managed as a global resource/config tree, wheres bundle specific sections are - * selected only. - */ -public class OSGIEnhancedConfiguration extends DefaultConfiguration{ - /** The default ordinal used for the OSGI config, */ - private static final int OSGI_DEFAULT_ORDINAL = 0; - - /** - * Constructor. - * - * @param osgiConfiguration The OSGI configuration found. - */ - public OSGIEnhancedConfiguration(org.osgi.service.cm.Configuration osgiConfiguration) { - super(new OSGIConfigurationContext(osgiConfiguration)); - } - - /** - * Class that models a Tamaya ConfigurationContext, which implicitly contains the bundle specific - * Configuration wrapped into a Tamaya PropertySource. - */ - private static final class OSGIConfigurationContext extends DefaultConfigurationContext{ - private OSGIPropertySource osgiPropertySource; - - public OSGIConfigurationContext(org.osgi.service.cm.Configuration osgiConfiguration){ - if(osgiConfiguration!=null) { - this.osgiPropertySource = new OSGIPropertySource(osgiConfiguration); - } - } - - @Override - public List<PropertySource> getPropertySources() { - List<PropertySource> sources = super.getPropertySources(); - if(osgiPropertySource!=null){ - sources.add(osgiPropertySource); - } - return sources; - } - } - - /** - * Tamaya PropertySource providing the values from an OSGI Configuration. - */ - private static final class OSGIPropertySource extends BasePropertySource{ - - private final org.osgi.service.cm.Configuration osgiConfiguration; - - public OSGIPropertySource(org.osgi.service.cm.Configuration osgiConfiguration){ - this.osgiConfiguration = Objects.requireNonNull(osgiConfiguration); - } - - @Override - public int getDefaultOrdinal() { - String val = System.getProperty("osgi.defaultOrdinal"); - if(val!=null){ - return Integer.parseInt(val.trim()); - } - return OSGI_DEFAULT_ORDINAL; - } - - @Override - public String getName() { - return "OSGIConfig:pid="+ - (osgiConfiguration.getPid()!=null?osgiConfiguration.getPid():osgiConfiguration.getFactoryPid()); - } - - @Override - public Map<String, String> getProperties() { - Map<String, String> map = new HashMap<>(); - Dictionary<String,Object> dict = osgiConfiguration.getProperties(); - Enumeration<String> keys = dict.keys(); - while(keys.hasMoreElements()){ - String key = keys.nextElement(); - map.put(key,String.valueOf(dict.get(key))); - } - return map; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigAdminImpl.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigAdminImpl.java b/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigAdminImpl.java deleted file mode 100644 index 7bf4da7..0000000 --- a/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigAdminImpl.java +++ /dev/null @@ -1,196 +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.integration.osgi; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceFactory; -import org.osgi.framework.ServiceReference; -import org.osgi.service.cm.Configuration; -import org.osgi.service.cm.ConfigurationAdmin; -import org.osgi.service.cm.ManagedService; -import org.osgi.service.cm.ManagedServiceFactory; -import org.osgi.util.tracker.ServiceTracker; - -/** - * Tamaya based implementation of an OSGI {@link ConfigurationAdmin}. - */ -public class TamayaConfigAdminImpl implements ConfigurationAdmin { - /** the logger. */ - private static final Logger LOG = Logger.getLogger(TamayaConfigAdminImpl.class.getName()); - - /** The OSGI context. */ - private final BundleContext context; - /** THe optional OSGI parent service. */ - private ConfigurationAdmin parent; - /** The cached configurations. */ - private Map<String,Configuration> configs = new ConcurrentHashMap<>(); - /** The configuration section mapper. */ - private OSGIConfigRootMapper configRootMapper; - - /** - * Create a new config. - * @param context the OSGI context - */ - TamayaConfigAdminImpl(BundleContext context) { - this.context = context; - this.configRootMapper = loadConfigRootMapper(); - ServiceReference<ConfigurationAdmin> ref = context.getServiceReference(ConfigurationAdmin.class); - this.parent = ref!=null?context.getService(ref):null; - ServiceTracker<ManagedService, ManagedService> serviceTracker = new ServiceTracker<ManagedService, - ManagedService>(context, ManagedService.class, null) { - @Override - public ManagedService addingService(ServiceReference<ManagedService> reference) { - ManagedService service = context.getService(reference); - Object pidObj = reference.getProperty(Constants.SERVICE_PID); - if (pidObj instanceof String) { - String pid = (String) pidObj; - try { - Configuration config = getConfiguration(pid); - if(config==null){ - service.updated(null); - } else{ - service.updated(config.getProperties()); - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Error configuring ManagedService: " + service, e); - } - } else { - LOG.log(Level.SEVERE, "Unsupported pid: " + pidObj); - } - return service; - } - - @Override - public void removedService(ServiceReference<ManagedService> reference, ManagedService service) { - context.ungetService(reference); - } - }; - serviceTracker.open(); - - ServiceTracker<ServiceFactory, ServiceFactory> factoryTracker - = new ServiceTracker<ServiceFactory, ServiceFactory>(context, ServiceFactory.class, null) { - @Override - public ServiceFactory addingService(ServiceReference<ServiceFactory> reference) { - ServiceFactory factory = context.getService(reference); - if(factory instanceof ManagedServiceFactory) { - Object pidObj = reference.getProperty(Constants.SERVICE_PID); - if (pidObj instanceof String) { - String pid = (String) pidObj; - try { - Configuration config = getConfiguration(pid); - if (config != null) { - ((ManagedServiceFactory) factory).updated(config.getFactoryPid(), config.getProperties()); - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Error configuring ManagedServiceFactory: " + factory, e); - } - } else { - LOG.log(Level.SEVERE, "Unsupported pid: " + pidObj); - } - } - return factory; - } - - @Override - public void removedService(ServiceReference<ServiceFactory> reference, ServiceFactory service) { - super.removedService(reference, service); - } - }; - factoryTracker.open(); - } - - @Override - public Configuration createFactoryConfiguration(String factoryPid) throws IOException { - return createFactoryConfiguration(factoryPid, null); - } - - @Override - public Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException { - return new TamayaConfigurationImpl(factoryPid, null, configRootMapper, this.parent); - } - - @Override - public Configuration getConfiguration(String pid, String location) throws IOException { - return getConfiguration(pid); - } - - @Override - public Configuration getConfiguration(String pid) throws IOException { - return new TamayaConfigurationImpl(pid, null, configRootMapper, this.parent); - } - - @Override - public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException { - Collection<Configuration> result; - if (filter == null) { - result = this.configs.values(); - } else { - result = new ArrayList<>(); - Filter flt = context.createFilter(filter); - for (Configuration config : this.configs.values()) { - if (flt.match(config.getProperties())) { - result.add(config); - } - } - } - return result.isEmpty() ? null : result.toArray(new Configuration[configs.size()]); - } - - /** - * Loads the configuration toor mapper using the OSGIConfigRootMapper OSGI service resolving mechanism. If no - * such service is available it loads the default mapper. - * @return the mapper to be used, bever null. - */ - private OSGIConfigRootMapper loadConfigRootMapper() { - OSGIConfigRootMapper mapper = null; - ServiceReference<OSGIConfigRootMapper> ref = context.getServiceReference(OSGIConfigRootMapper.class); - if(ref!=null){ - mapper = context.getService(ref); - } - if(mapper==null){ - mapper = new OSGIConfigRootMapper() { - @Override - public String getTamayaConfigRoot(String pid, String factoryPid) { - if(pid!=null) { - return "[bundle:" + pid +']'; - } else{ - return "[bundle:" + factoryPid +']'; - } - } - @Override - public String toString(){ - return "Default OSGIConfigRootMapper(pid -> [bundle:pid], factoryPid -> [bundle:factoryPid]"; - } - }; - } - return mapper; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigurationImpl.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigurationImpl.java b/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigurationImpl.java deleted file mode 100644 index c7b0864..0000000 --- a/osgi/src/main/java/org/apache/tamaya/integration/osgi/TamayaConfigurationImpl.java +++ /dev/null @@ -1,127 +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.integration.osgi; - -import java.io.IOException; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.functions.PropertyMatcher; -import org.apache.tamaya.functions.ConfigurationFunctions; -import org.osgi.service.cm.Configuration; -import org.osgi.service.cm.ConfigurationAdmin; - -/** - * Tamaya based implementation of an OSGI {@link Configuration}. - */ -public class TamayaConfigurationImpl implements Configuration { - private static final Logger LOG = Logger.getLogger(TamayaConfigurationImpl.class.getName()); - private final String pid; - private final String factoryPid; - private Map<String, String> properties = new HashMap<>(); - private org.apache.tamaya.Configuration config; - - /** - * Constructor. - * @param confPid the OSGI pid - * @param factoryPid the factory pid - * @param configRootMapper the mapper that maps the pids to a tamaya root section. - * @param parent the (optional delegating parent, used as default). - */ - TamayaConfigurationImpl(String confPid, String factoryPid, OSGIConfigRootMapper configRootMapper, - ConfigurationAdmin parent) { - this.pid = confPid; - this.factoryPid = factoryPid; - if(parent!=null){ - try { - Dictionary<String, Object> conf = parent.getConfiguration(confPid, factoryPid).getProperties(); - if(conf!=null) { - LOG.info("Configuration: Adding default parameters from parent: " + parent.getClass().getName()); - Enumeration<String> keys = conf.keys(); - while (keys.hasMoreElements()) { - String key = keys.nextElement(); - this.properties.put(key, conf.get(key).toString()); - } - } - } catch (IOException e) { - LOG.log(Level.WARNING, "Error reading parent OSGI config.", e); - } - } - this.config = ConfigurationProvider.getConfiguration(); - final String rootKey = configRootMapper.getTamayaConfigRoot(pid, factoryPid); - LOG.info("Configuration: Evaluating Tamaya configuration for '" + rootKey + "'."); - this.properties.putAll( - config.with(ConfigurationFunctions.section(rootKey, true)).getProperties()); - } - - @Override - public String getPid() { - return pid; - } - - @Override - public Dictionary<String, Object> getProperties() { - return new Hashtable<String, Object>(properties); - } - - @Override - public void update(Dictionary<String, ?> properties) throws IOException { - throw new UnsupportedOperationException("Nuatability not yet supported."); - // ConfigChangeProvider.createChangeRequest(this.config) - } - - @Override - public void delete() throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public String getFactoryPid() { - return factoryPid; - } - - @Override - public void update() throws IOException { - this.config = ConfigurationProvider.getConfiguration(); - this.properties = config.with(ConfigurationFunctions.filter(new PropertyMatcher() { - @Override - public boolean test(String key, String value) { -// TODO define name space / SPI - return false; - } - })).getProperties(); - } - - @Override - public void setBundleLocation(String location) { - } - - @Override - public String getBundleLocation() { - return null; - } - - @Override - public long getChangeCount() { - return 0; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/osgi/Activator.java b/osgi/src/main/java/org/apache/tamaya/osgi/Activator.java new file mode 100644 index 0000000..91dfda4 --- /dev/null +++ b/osgi/src/main/java/org/apache/tamaya/osgi/Activator.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.integration.osgi; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.inject.ConfigurationInjection; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.osgi.util.tracker.ServiceTracker; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Activator that registers the Tamaya based Service Class for {@link ConfigurationAdmin}, + * using a default service priority of {@code 0}. This behaviour is configurable based on OSGI properties: + * <ul> + * <li><p><b>org.tamaya.integration.osgi.cm.ranking, type: int</b> allows to configure the OSGI service ranking for + * Tamaya based ConfigurationAdmin instance. The default ranking used is 10.</p></li> + * <li><p><b>org.tamaya.integration.osgi.cm.override, type: boolean</b> allows to configure if Tamaya should + * register its ConfigAdmin service. Default is true.</p></li> + * </ul> + */ +public class Activator implements BundleActivator { + + private static final String SERVICE_RANKING_PROP = "org.tamaya.integration.osgi.cm.ranking"; + + private static final String SERVICE_OVERRIDE_PROP = "org.tamaya.integration.osgi.cm.override"; + + private static final String SERVICE_INJECT_PROP = "org.tamaya.integration.osgi.cm.inject"; + + private static final Integer DEFAULT_RANKING = 10; + + private static final Logger LOG = Logger.getLogger(Activator.class.getName()); + + private ServiceRegistration<ConfigurationAdmin> registration; + + private ServiceTracker<Object, Object> injectionTracker; + + @Override + public void start(BundleContext context) throws Exception { + String val = context.getProperty(SERVICE_OVERRIDE_PROP); + if(val == null || Boolean.parseBoolean(val)){ + Dictionary<String, Object> props = new Hashtable<>(); + String ranking = context.getProperty(SERVICE_RANKING_PROP); + if (ranking == null) { + props.put(Constants.SERVICE_RANKING, DEFAULT_RANKING); + } else { + props.put(Constants.SERVICE_RANKING, Integer.valueOf(ranking)); + } + TamayaConfigAdminImpl cm = new TamayaConfigAdminImpl(context); + registration = context.registerService(ConfigurationAdmin.class, cm, props); + } + + // register injection mechanisms, if not configured otherwise +// val = context.getProperty(SERVICE_INJECT_PROP); +// if(val == null || Boolean.parseBoolean(val)){ +// injectionTracker = new ServiceTracker<Object, Object>(context, Object.class, null) { +// @Override +// public Object addingService(ServiceReference<Object> reference) { +// Object service = context.getService(reference); +// Object pidObj = reference.getProperty(Constants.SERVICE_PID); +// if (pidObj instanceof String) { +// String pid = (String) pidObj; +// ConfigurationAdmin configAdmin = null; +// ServiceReference<ConfigurationAdmin> adminRef = +// context.getServiceReference(ConfigurationAdmin.class); +// if(adminRef!=null){ +// configAdmin = context.getService(adminRef); +// } +// try { +// Configuration targetConfig = null; +// if(configAdmin != null){ +// org.osgi.service.cm.Configuration osgiConfig = configAdmin.getConfiguration(pid); +// if(osgiConfig!=null){ +// targetConfig = new OSGIEnhancedConfiguration(osgiConfig); +// } +// } +// if(targetConfig==null){ +// targetConfig = ConfigurationProvider.getConfiguration(); +// } +// ConfigurationInjection.getConfigurationInjector().configure(service, targetConfig); +// } catch (Exception e) { +// LOG.log(Level.WARNING, "Error configuring Service: " + service, e); +// } +// } else { +// LOG.log(Level.SEVERE, "Unsupported pid: " + pidObj); +// } +// return service; +// } +// +// @Override +// public void removedService(ServiceReference<Object> reference, Object service) { +// context.ungetService(reference); +// } +// }; +// injectionTracker.open(); +// } + } + + @Override + public void stop(BundleContext context) throws Exception { + if (registration != null) { + registration.unregister(); + } + if(injectionTracker!=null){ + injectionTracker.close(); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java b/osgi/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java new file mode 100644 index 0000000..836df8b --- /dev/null +++ b/osgi/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.integration.osgi; + +/** + * Mapping function for mapping Tamaya configuration sections to OSGI pids. + */ +public interface OSGIConfigRootMapper { + + /** + * Map the given OSGI pid to a corresponding configuration section in Tamaya. Es an example (and this is also the + * default implemented) a configuration mapping for {@code pid/factoryPid==myBundle} could be {@code [bundle:myBundle]}. + * This mapping is used as a prefix when collecting the corresponding entries for the OSGI configuration. + * @param pid the OSGI pid, or null + * @param factoryPid the OSGI factoryPid, or null + * @return return the corresponding config root section. For ommitting any root section simply return an empty + * String. + */ + String getTamayaConfigRoot(String pid, String factoryPid); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2949a137/osgi/src/main/java/org/apache/tamaya/osgi/OSGIEnhancedConfiguration.java ---------------------------------------------------------------------- diff --git a/osgi/src/main/java/org/apache/tamaya/osgi/OSGIEnhancedConfiguration.java b/osgi/src/main/java/org/apache/tamaya/osgi/OSGIEnhancedConfiguration.java new file mode 100644 index 0000000..5e813af --- /dev/null +++ b/osgi/src/main/java/org/apache/tamaya/osgi/OSGIEnhancedConfiguration.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.integration.osgi; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spisupport.BasePropertySource; +import org.apache.tamaya.spisupport.DefaultConfiguration; +import org.apache.tamaya.spisupport.DefaultConfigurationContext; + +import java.util.Dictionary; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * Configuration object that also reflects the values provided by the OSGI ConfigAdmin Configuration. + * Similar to other tamaya areas adding a tamaya.ordinal into the corresponding OSGI configuration for + * a pif/factoryPid allows to control the ordinal/priority of the OSGI configuration related to other + * configured Tamaya Property Sources. Overall the configuration evaluation for Tamaya follows the + * same rules, with the difference that each bunldle owns its own ConfigAdmin based part. From + * Tamaya, the granularity depends on the implementation of the ConfigurationProviderSpi. By default + * Tamaya configuration is managed as a global resource/config tree, wheres bundle specific sections are + * selected only. + */ +public class OSGIEnhancedConfiguration extends DefaultConfiguration{ + /** The default ordinal used for the OSGI config, */ + private static final int OSGI_DEFAULT_ORDINAL = 0; + + /** + * Constructor. + * + * @param osgiConfiguration The OSGI configuration found. + */ + public OSGIEnhancedConfiguration(org.osgi.service.cm.Configuration osgiConfiguration) { + super(new OSGIConfigurationContext(osgiConfiguration)); + } + + /** + * Class that models a Tamaya ConfigurationContext, which implicitly contains the bundle specific + * Configuration wrapped into a Tamaya PropertySource. + */ + private static final class OSGIConfigurationContext extends DefaultConfigurationContext{ + private OSGIPropertySource osgiPropertySource; + + public OSGIConfigurationContext(org.osgi.service.cm.Configuration osgiConfiguration){ + if(osgiConfiguration!=null) { + this.osgiPropertySource = new OSGIPropertySource(osgiConfiguration); + } + } + + @Override + public List<PropertySource> getPropertySources() { + List<PropertySource> sources = super.getPropertySources(); + if(osgiPropertySource!=null){ + sources.add(osgiPropertySource); + } + return sources; + } + } + + /** + * Tamaya PropertySource providing the values from an OSGI Configuration. + */ + private static final class OSGIPropertySource extends BasePropertySource{ + + private final org.osgi.service.cm.Configuration osgiConfiguration; + + public OSGIPropertySource(org.osgi.service.cm.Configuration osgiConfiguration){ + this.osgiConfiguration = Objects.requireNonNull(osgiConfiguration); + } + + @Override + public int getDefaultOrdinal() { + String val = System.getProperty("osgi.defaultOrdinal"); + if(val!=null){ + return Integer.parseInt(val.trim()); + } + return OSGI_DEFAULT_ORDINAL; + } + + @Override + public String getName() { + return "OSGIConfig:pid="+ + (osgiConfiguration.getPid()!=null?osgiConfiguration.getPid():osgiConfiguration.getFactoryPid()); + } + + @Override + public Map<String, String> getProperties() { + Map<String, String> map = new HashMap<>(); + Dictionary<String,Object> dict = osgiConfiguration.getProperties(); + Enumeration<String> keys = dict.keys(); + while(keys.hasMoreElements()){ + String key = keys.nextElement(); + map.put(key,String.valueOf(dict.get(key))); + } + return map; + } + } +}
