TAMAYA-300: Added config history. DId a small refactoring.
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/cbd23125 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/cbd23125 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/cbd23125 Branch: refs/heads/master Commit: cbd23125f7f47186b651f0ff6bd0e1a1e50a4b78 Parents: cdd839c Author: anatole <[email protected]> Authored: Tue Sep 19 12:59:30 2017 +0200 Committer: anatole <[email protected]> Committed: Tue Sep 19 12:59:30 2017 +0200 ---------------------------------------------------------------------- .../java/org/apache/tamaya/osgi/Activator.java | 9 +- .../org/apache/tamaya/osgi/ConfigChanger.java | 170 +++++++++++++ .../org/apache/tamaya/osgi/ConfigHistory.java | 168 +++++++++++++ .../org/apache/tamaya/osgi/OperationMode.java | 31 +++ .../apache/tamaya/osgi/TamayaConfigPlugin.java | 250 +++++++------------ .../tamaya/osgi/TamayaConfigPluginTest.java | 75 ++++++ 6 files changed, 542 insertions(+), 161 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cbd23125/osgi/common/src/main/java/org/apache/tamaya/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/Activator.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/Activator.java index d6c5726..a1522a1 100644 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/Activator.java +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/Activator.java @@ -44,7 +44,9 @@ public class Activator implements BundleActivator { private static final Logger LOG = Logger.getLogger(Activator.class.getName()); - private ServiceRegistration<?> registration; + private ServiceRegistration<TamayaConfigPlugin> registration; + + private TamayaConfigPlugin plugin; @Override @@ -71,11 +73,11 @@ public class Activator implements BundleActivator { LOG.fine("Using custom ranking for Tamaya OSGI Config plugin: " + ranking); } props.put(Constants.SERVICE_RANKING, DEFAULT_RANKING); - TamayaConfigPlugin plugin = new TamayaConfigPlugin(context); + this.plugin = new TamayaConfigPlugin(context); LOG.info("Registering Tamaya OSGI Config plugin with ranking: " + ranking); registration = context.registerService( TamayaConfigPlugin.class, - plugin, props); + this.plugin, props); LOG.info("Registered Tamaya OSGI Config plugin."); configuration.update(props); } @@ -83,6 +85,7 @@ public class Activator implements BundleActivator { @Override public void stop(BundleContext context) throws Exception { if (registration != null) { + context.removeBundleListener(this.plugin); registration.unregister(); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cbd23125/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java new file mode 100644 index 0000000..29c4720 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java @@ -0,0 +1,170 @@ +/* + * 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.osgi; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; + +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by atsticks on 19.09.17. + */ +final class ConfigChanger { + + private static final Logger LOG = Logger.getLogger(TamayaConfigPlugin.class.getName()); + private static final OSGIConfigMapper DEFAULT_CONFIG_MAPPER = new DefaultOSGIConfigMapper(); + + private BundleContext context; + private ConfigurationAdmin cm; + + public ConfigChanger(BundleContext context){ + this.context = context; + ServiceReference<ConfigurationAdmin> cmRef = context.getServiceReference(ConfigurationAdmin.class); + this.cm = context.getService(cmRef); + } + + public BundleContext getContext(){ + return context; + } + + public ConfigurationAdmin getConfigurationAdmin(){ + return cm; + } + + public void configure(String pid, Bundle bundle, OperationMode defaultOpMode) { + OperationMode opMode = Objects.requireNonNull(defaultOpMode); + String opVal = bundle.getHeaders().get("Tamaya-OperationMode"); + if(opVal!=null){ + opMode = OperationMode.valueOf(opVal.toUpperCase()); + } + LOG.finest("Evaluating Tamaya Config for PID: " + pid); + ConfigHistory.configuring(pid, "operationMode="+opMode); + org.apache.tamaya.Configuration tamayaConfig = configMapper().getConfiguration(pid); + if (tamayaConfig == null) { + LOG.finest("No Tamaya configuration for PID: " + pid); + return; + } + try { + // TODO Check for Bundle.getLocation() usage here... + Configuration osgiConfig = cm.getConfiguration(pid, bundle.getLocation()); + if(osgiConfig!=null){ + Dictionary<String, Object> dictionary = osgiConfig.getProperties(); + if(dictionary==null){ + dictionary = new Hashtable<>(); + } + modifyConfiguration(pid, tamayaConfig, dictionary, opMode); + if(!dictionary.isEmpty()) { + osgiConfig.update(dictionary); + LOG.info("Updated configuration for PID: " + pid + ": " + dictionary); + } + } + ConfigHistory.configured(pid, "SUCCESS"); + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to initialize configuration for PID: " + pid, e); + ConfigHistory.configured(pid, "FAILED: " + e); + } + + } + + public void modifyConfiguration(String pid, org.apache.tamaya.Configuration config, Dictionary<String, Object> dictionary, OperationMode opMode) { + LOG.info(() -> "Updating configuration for PID: " + pid + "..."); + dictionary.put("tamaya.opMode", opMode.toString()); + ConfigHistory.propertySet(pid, "tamaya.opMode", opMode.toString(), null); + dictionary.put("tamaya.modified.at", new Date().toString()); + ConfigHistory.propertySet(pid, "tamaya.modified.at", dictionary.get("tamaya.modified.at"), null); + + Map<String, Object> dictionaryMap = new HashMap<>(); + Enumeration<String> keys = dictionary.keys(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + Object value = dictionary.get(key); + dictionaryMap.put(key, value); + } + for (Map.Entry<String, Object> dictEntry : dictionaryMap.entrySet()) { + Object configuredValue = config.getOrDefault(dictEntry.getKey(), dictEntry.getValue().getClass(), null); + switch (opMode) { + case EXTEND: + break; + case OVERRIDE: + if (configuredValue != null) { + LOG.info(() -> "Setting key " + dictEntry.getKey() + " to " + configuredValue); + ConfigHistory.propertySet(pid,dictEntry.getKey(), configuredValue, dictEntry.getValue()); + dictionary.put(dictEntry.getKey(), configuredValue); + } + break; + case UPDATE_ONLY: + if (configuredValue != null) { + LOG.info(() -> "Setting key " + dictEntry.getKey() + " to " + configuredValue); + ConfigHistory.propertySet(pid,dictEntry.getKey(), configuredValue, dictEntry.getValue()); + dictionary.put(dictEntry.getKey(), configuredValue); + + } + } + } + for (Map.Entry<String, String> configEntry : config.getProperties().entrySet()) { + Object dictValue = dictionary.get(configEntry.getKey()); + switch (opMode) { + case EXTEND: + if(dictValue==null){ + LOG.info(() -> "Setting key " + configEntry.getKey() + " to " + configEntry.getValue()); + ConfigHistory.propertySet(pid,configEntry.getKey(), configEntry.getValue(), null); + dictionary.put(configEntry.getKey(), configEntry.getValue()); + } + break; + case OVERRIDE: + LOG.info(() -> "Setting key " + configEntry.getKey() + " to " + configEntry.getValue()); + ConfigHistory.propertySet(pid,configEntry.getKey(), configEntry.getValue(), null); + dictionary.put(configEntry.getKey(), configEntry.getValue()); + break; + case UPDATE_ONLY: + if(dictValue!=null){ + LOG.info(() -> "Setting key " + configEntry.getKey() + " to " + configEntry.getValue()); + ConfigHistory.propertySet(pid,configEntry.getKey(), configEntry.getValue(), dictValue); + dictionary.put(configEntry.getKey(), configEntry.getValue()); + } + break; + } + } + } + + /** + * 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 OSGIConfigMapper configMapper() { + OSGIConfigMapper mapper = null; + if(context!=null) { + ServiceReference<OSGIConfigMapper> ref = context.getServiceReference(OSGIConfigMapper.class); + if (ref != null) { + mapper = context.getService(ref); + } + } + if(mapper==null){ + return DEFAULT_CONFIG_MAPPER; + } + return mapper; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cbd23125/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java new file mode 100644 index 0000000..8fdd70a --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigHistory.java @@ -0,0 +1,168 @@ +/* + * 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.osgi; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * Class storing the history of changers done to the OSGI configuration by Tamaya. + * This class can be used in the future to restore the previous state, if needed. + */ +public final class ConfigHistory { + + public enum TaskType{ + PROPERTY, + BEGIN, + END, + } + + private static int maxHistory = 10000; + private static List<ConfigHistory> history = new LinkedList<ConfigHistory>(){ + + @Override + public boolean add(ConfigHistory o) { + boolean val = super.add(o); + if(val && size() > maxHistory){ + remove(); + } + return val; + } + }; + + private long timestamp = System.currentTimeMillis(); + + private TaskType type; + private Object previousValue; + private Object value; + private String key; + private String pid; + + private ConfigHistory(TaskType taskType, String pid){ + this.type = Objects.requireNonNull(taskType); + this.pid = Objects.requireNonNull(pid); + } + + public static ConfigHistory configuring(String pid, String info){ + ConfigHistory h = new ConfigHistory(TaskType.BEGIN, pid) + .setValue(info); + synchronized (history){ + history.add(h); + } + return h; + } + public static ConfigHistory configured(String pid, String info){ + ConfigHistory h = new ConfigHistory(TaskType.END, pid) + .setValue(info); + synchronized (history){ + history.add(h); + } + return h; + } + public static ConfigHistory propertySet(String pid, String key, Object value, Object previousValue){ + ConfigHistory h = new ConfigHistory(TaskType.PROPERTY, pid) + .setKey(key) + .setPreviousValue(previousValue) + .setValue(value); + synchronized (history){ + history.add(h); + } + return h; + } + + public static List<ConfigHistory> history(){ + return history(null); + } + + public static void clearHistory(){ + clearHistory(null); + } + + public static void clearHistory(String pid){ + synchronized (history){ + if(pid==null || pid.isEmpty()) { + history.clear(); + }else{ + history.removeAll(history(pid)); + } + } + } + + public static List<ConfigHistory> history(String pid) { + if(pid==null || pid.isEmpty()){ + return new ArrayList<>(history); + } + synchronized (history) { + List<ConfigHistory> result = new ArrayList<>(); + for (ConfigHistory h : history) { + if (h.getPid().startsWith(pid)) { + result.add(h); + } + } + return result; + } + } + + public TaskType getType(){ + return type; + } + + public String getPid() { + return pid; + } + + public Object getPreviousValue() { + return previousValue; + } + + public ConfigHistory setPreviousValue(Object previousValue) { + this.previousValue = previousValue; + return this; + } + + public Object getValue() { + return value; + } + + public ConfigHistory setValue(Object value) { + this.value = value; + return this; + } + + public String getKey() { + return key; + } + + public ConfigHistory setKey(String key) { + this.key = key; + return this; + } + + @Override + public String toString() { + return "ConfigHistory{" + + "timestamp=" + timestamp + + ", previousValue=" + previousValue + + ", value=" + value + + ", key='" + key + '\'' + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cbd23125/osgi/common/src/main/java/org/apache/tamaya/osgi/OperationMode.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/OperationMode.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/OperationMode.java new file mode 100644 index 0000000..4489892 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/OperationMode.java @@ -0,0 +1,31 @@ +/* + * 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.osgi; + +/** + * Operation mode applied to the config read. + */ +public enum OperationMode{ + /** Only add properties not existing in the config. */ + EXTEND, + /** Override existing properties and add new properties. */ + OVERRIDE, + /** Override existing properties only. */ + UPDATE_ONLY +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cbd23125/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java index 509a3d0..1b844f0 100644 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java @@ -21,7 +21,6 @@ package org.apache.tamaya.osgi; import org.osgi.framework.*; import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; -import org.osgi.service.cm.ConfigurationPlugin; import java.io.IOException; import java.util.*; @@ -32,48 +31,74 @@ import java.util.logging.Logger; * Tamaya plugin that updates/extends the component configurations managed * by {@link ConfigurationAdmin}, based on the configured {@link OperationMode}. */ -public class TamayaConfigPlugin { +public class TamayaConfigPlugin implements BundleListener, ServiceListener{ static final String COMPONENTID = "TamayaConfigPlugin"; /** the logger. */ private static final Logger LOG = Logger.getLogger(TamayaConfigPlugin.class.getName()); - private static final OSGIConfigMapper DEFAULT_CONFIG_MAPPER = new DefaultOSGIConfigMapper(); private static final String TAMAYA_DISABLED = "tamaya.disabled"; private boolean disabled = false; + private OperationMode defaultOpMode = OperationMode.OVERRIDE; - /** - * Operation mode applied to the config read. - */ - public enum OperationMode{ - /** Only add properties not existing in the config. */ - EXTEND, - /** Only override existing properties, but do not add any new ones. */ - OVERRIDE, - /** Override existing properties and add new properties, but do not remove - * properties not existing in Tamaya. */ - EXTEND_AND_OVERRIDE, - /** Use Tamaya config only. */ - SYNCH + private ConfigChanger configChanger; + + @Override + public void serviceChanged(ServiceEvent event) { + switch(event.getType()){ + case ServiceEvent.REGISTERED: + case ServiceEvent.MODIFIED: + configureService(event); + break; + case ServiceEvent.UNREGISTERING: + // unconfigure...? Corrently nothing here. + break; + } } - private BundleContext context; - private OperationMode opMode = OperationMode.EXTEND_AND_OVERRIDE; - private ConfigurationAdmin cm; /** * Create a new config. * @param context the OSGI context */ TamayaConfigPlugin(BundleContext context) { - this.context = context; - ServiceReference<ConfigurationAdmin> cmRef = context.getServiceReference(ConfigurationAdmin.class); - this.cm = context.getService(cmRef); + configChanger = new ConfigChanger(context); + initDefaultEnabled(); initDefaultOpMode(); initConfigs(); } + public void setDefaultDisabled(boolean disabled){ + this.disabled = disabled; + setConfigValue(TAMAYA_DISABLED, disabled); + } + + public boolean isDefaultDisabled(){ + return disabled; + } + + public OperationMode getDefaultOperationMode(){ + return defaultOpMode; + } + + public void setDefaultOperationMode(OperationMode mode){ + this.defaultOpMode = Objects.requireNonNull(mode); + setConfigValue(OperationMode.class.getSimpleName(), defaultOpMode.toString()); + } + + @Override + public void bundleChanged(BundleEvent event) { + switch(event.getType()){ + case BundleEvent.STARTING: + case BundleEvent.LAZY_ACTIVATION: +// case BundleEvent.UPDATED: +// TODO add checks for preventing endlee loop for updates here... + configureBundle(event.getBundle()); + break; + } + } + private void initConfigs() { // Check for matching bundles already installed... - for(Bundle bundle:context.getBundles()){ + for(Bundle bundle:configChanger.getContext().getBundles()){ switch(bundle.getState()){ case Bundle.ACTIVE: configureBundle(bundle); @@ -82,24 +107,24 @@ public class TamayaConfigPlugin { } } - private void configureBundle(Bundle bundle) { + private void configureService(ServiceEvent event) { // Optional MANIFEST entries - String enabledTamaya = bundle.getHeaders().get("Tamaya-Enabled"); - String disabledTamaya = bundle.getHeaders().get("Tamaya-Disabled"); - - if(Boolean.parseBoolean(disabledTamaya)){ - LOG.finest("Bundle is disabled for Tamaya: " + bundle.getSymbolicName()); + Bundle bundle = event.getServiceReference().getBundle(); + if(!isBundleEnabled(bundle)){ return; } - if(enabledTamaya != null && !Boolean.parseBoolean(enabledTamaya)){ - LOG.finest("Bundle is disabled for Tamaya: " + bundle.getSymbolicName()); + String pid = (String)event.getServiceReference().getProperty("service.pid"); + if(pid==null){ + LOG.finest("No service pid for: " + event.getServiceReference()); return; } - if(disabled){ - LOG.finest("tamaya.disabled=false: not configuring bundle: " + bundle.getSymbolicName()); + configChanger.configure(pid, event.getServiceReference().getBundle(), defaultOpMode); + } + + private void configureBundle(Bundle bundle) { + if(!isBundleEnabled(bundle)){ return; } - String tamayaPid = bundle.getHeaders().get("Tamaya-PID"); String pid = tamayaPid!=null?tamayaPid:bundle.getSymbolicName(); if(pid==null){ @@ -109,42 +134,31 @@ public class TamayaConfigPlugin { LOG.finest(() -> "No PID/location for bundle " + bundle.getSymbolicName() + '('+bundle.getBundleId()+')'); return; } - LOG.finest("Evaluating Tamaya Config for PID: " + pid); - org.apache.tamaya.Configuration tamayaConfig = configMapper().getConfiguration(pid); - if (tamayaConfig == null) { - LOG.finest("No Tamaya configuration for PID: " + pid); - return; - } - try { - // TODO Check for Bundle.getLocation() usage here... - Configuration osgiConfig = cm.getConfiguration(pid, bundle.getLocation()); - if(osgiConfig!=null){ - Dictionary<String, Object> dictionary = osgiConfig.getProperties(); - if(dictionary==null){ - dictionary = new Hashtable<>(); - } - modifyConfiguration(pid, tamayaConfig, dictionary); - if(!dictionary.isEmpty()) { - osgiConfig.update(dictionary); - LOG.info("Updated configuration for PID: " + pid + ": " + dictionary); - } - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to initialize configuration for PID: " + pid, e); - } + configChanger.configure(pid, bundle, defaultOpMode); } - private void initDefaultOpMode() { - String opVal = (String)getConfigValue(OperationMode.class.getName()); - if(opVal!=null){ - try{ - opMode = OperationMode.valueOf(opVal); - }catch(Exception e){ - LOG.warning("Invalid OperationMode: " + opMode +", using default: " + opMode); - } + + public boolean isBundleEnabled(Bundle bundle){ + // Optional MANIFEST entries + String enabledTamaya = bundle.getHeaders().get("Tamaya-Enabled"); + String disabledTamaya = bundle.getHeaders().get("Tamaya-Disabled"); + + if(Boolean.parseBoolean(disabledTamaya)){ + LOG.finest("Bundle is disabled for Tamaya: " + bundle.getSymbolicName()); + return false; } + if(enabledTamaya != null && !Boolean.parseBoolean(enabledTamaya)){ + LOG.finest("Bundle is disabled for Tamaya: " + bundle.getSymbolicName()); + return false; + } + if(disabled){ + LOG.finest("tamaya.disabled=false: not configuring bundle: " + bundle.getSymbolicName()); + return false; + } + return true; } + private void initDefaultEnabled() { String disabledVal = (String)getConfigValue("tamaya.disabled"); if(disabledVal==null){ @@ -160,11 +174,22 @@ public class TamayaConfigPlugin { } } + private void initDefaultOpMode() { + String opVal = (String)getConfigValue(OperationMode.class.getName()); + if(opVal!=null){ + try{ + defaultOpMode = OperationMode.valueOf(opVal); + }catch(Exception e){ + LOG.warning("Invalid OperationMode: " + opVal +", using default: " + defaultOpMode); + } + } + } + void setConfigValue(String key, Object value){ Configuration config = null; try { - config = cm.getConfiguration(COMPONENTID); + config = configChanger.getConfigurationAdmin().getConfiguration(COMPONENTID); Dictionary<String, Object> props = null; if (config != null && config.getProperties() != null) { @@ -186,7 +211,7 @@ public class TamayaConfigPlugin { Object getConfigValue(String key){ Configuration config = null; try { - config = cm.getConfiguration(COMPONENTID); + config = configChanger.getConfigurationAdmin().getConfiguration(COMPONENTID); Dictionary<String, Object> props = null; if (config != null && config.getProperties() != null) { @@ -201,95 +226,4 @@ public class TamayaConfigPlugin { return null; } - public OperationMode getOperationMode(){ - return opMode; - } - - public void setDefaultDisabled(boolean disabled){ - this.disabled = disabled; - setConfigValue(TAMAYA_DISABLED, disabled); - } - - public boolean isDefaultDisabled(){ - return disabled; - } - - public void setOperationMode(OperationMode mode){ - this.opMode = Objects.requireNonNull(mode); - setConfigValue(OperationMode.class.getSimpleName(), opMode.toString()); - } - - public void modifyConfiguration(String target, org.apache.tamaya.Configuration config, Dictionary<String, Object> dictionary) { - LOG.info(() -> "Updating configuration for " + target + "..."); - dictionary.put("tamaya.opMode", getOperationMode().toString()); - dictionary.put("tamaya.modified.at", new Date().toString()); - - Map<String, Object> dictionaryMap = new HashMap<>(); - Enumeration<String> keys = dictionary.keys(); - while (keys.hasMoreElements()) { - String key = keys.nextElement(); - Object value = dictionary.get(key); - dictionaryMap.put(key, value); - } - for (Map.Entry<String, Object> dictEntry : dictionaryMap.entrySet()) { - Object configuredValue = config.getOrDefault(dictEntry.getKey(), dictEntry.getValue().getClass(), null); - switch (opMode) { - case OVERRIDE: - case EXTEND_AND_OVERRIDE: - if (configuredValue != null) { - LOG.info(() -> "Setting key " + dictEntry.getKey() + " to " + configuredValue); - dictionary.put(dictEntry.getKey(), configuredValue); - } - break; - case SYNCH: - if (configuredValue != null) { - LOG.info(() -> "Setting key " + dictEntry.getKey() + " to " + configuredValue); - dictionary.put(dictEntry.getKey(), configuredValue); - } else { - LOG.info(() -> "Removing key " + dictEntry.getKey()); - dictionary.remove(dictEntry.getKey()); - } - } - } - for (Map.Entry<String, String> configEntry : config.getProperties().entrySet()) { - Object dictValue = dictionary.get(configEntry.getKey()); - switch (opMode) { - case EXTEND: - case EXTEND_AND_OVERRIDE: - LOG.info(() -> "Setting key " + configEntry.getKey() + " to " + configEntry.getValue()); - dictionary.put(configEntry.getKey(), configEntry.getValue()); - break; - case SYNCH: - if (dictValue != null) { - LOG.info(() -> "Setting key " + configEntry.getKey() + " to " + configEntry.getValue()); - dictionary.put(configEntry.getKey(), configEntry.getValue()); - } else { - LOG.info(() -> "Removing key " + configEntry.getKey()); - dictionary.remove(configEntry.getKey()); - } - break; - } - } - } - - /** - * 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 OSGIConfigMapper configMapper() { - OSGIConfigMapper mapper = null; - if(context!=null) { - ServiceReference<OSGIConfigMapper> ref = context.getServiceReference(OSGIConfigMapper.class); - if (ref != null) { - mapper = context.getService(ref); - } - } - if(mapper==null){ - return DEFAULT_CONFIG_MAPPER; - } - return mapper; - } - - } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cbd23125/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigPluginTest.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigPluginTest.java b/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigPluginTest.java new file mode 100644 index 0000000..723c3d5 --- /dev/null +++ b/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigPluginTest.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.osgi; + +import org.junit.Test; +import org.osgi.service.cm.Configuration; + +import static org.junit.Assert.*; + +/** + * Created by atsticks on 10.12.16. + */ +public class TamayaConfigPluginTest { + + private TamayaConfigPlugin configAdmin = new TamayaConfigPlugin(null); + + @Test + public void createConfigurationOverride() throws Exception { +// Configuration config = configAdmin.createFactoryConfiguration("tamaya"); +// assertNotNull(config); +// assertFalse(config.getProperties().isEmpty()); +// assertEquals(config.getProperties().size(), 4); +// assertEquals(config.getProperties().get("my.testProperty1"), "success1"); + } +// +// @Test +// public void createFactoryConfigurationWithLocation() throws Exception { +// Configuration config = configAdmin.createFactoryConfiguration("tamaya", "location"); +// assertNotNull(config); +// assertFalse(config.getProperties().isEmpty()); +// assertEquals(config.getProperties().size(), 4); +// assertEquals(config.getProperties().get("my.testProperty2"), "success2"); +// } +// +// @Test +// public void getConfiguration() throws Exception { +// Configuration config = configAdmin.getConfiguration("tamaya"); +// assertNotNull(config); +// assertFalse(config.getProperties().isEmpty()); +// assertEquals(config.getProperties().size(), 4); +// assertEquals(config.getProperties().get("my.testProperty3"), "success3"); +// } +// +// @Test +// public void getConfigurationWithLocation() throws Exception { +// Configuration config = configAdmin.getConfiguration("tamaya", "location"); +// assertNotNull(config); +// assertFalse(config.getProperties().isEmpty()); +// assertEquals(config.getProperties().size(), 4); +// assertEquals(config.getProperties().get("my.testProperty4"), "success4"); +// } +// +// @Test +// public void listConfigurations() throws Exception { +// Configuration[] configs = configAdmin.listConfigurations(".*"); +// assertNotNull(configs); +// } + +} \ No newline at end of file
