TAMAYA-274: Implemented OSGI integration, tested in Karaf (using Karaf shell custom commands).
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/92053860 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/92053860 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/92053860 Branch: refs/heads/java8 Commit: 920538603ef43802b16f60eb8ef41b55b96e6c15 Parents: c345b5c Author: anatole <anat...@apache.org> Authored: Mon Sep 18 17:15:43 2017 +0200 Committer: anatole <anat...@apache.org> Committed: Mon Sep 18 17:15:43 2017 +0200 ---------------------------------------------------------------------- osgi/common/bnd.bnd | 7 +- osgi/common/pom.xml | 6 +- .../java/org/apache/tamaya/osgi/Activator.java | 49 +-- .../tamaya/osgi/DefaultOSGIConfigMapper.java | 45 +++ .../apache/tamaya/osgi/OSGIConfigMapper.java | 38 +++ .../tamaya/osgi/OSGIConfigRootMapper.java | 36 --- .../tamaya/osgi/TamayaConfigAdminImpl.java | 170 ----------- .../apache/tamaya/osgi/TamayaConfigPlugin.java | 295 +++++++++++++++++++ .../tamaya/osgi/TamayaOSGIConfiguration.java | 139 --------- .../apache/tamaya/osgi/attic/ConfigPrinter.java | 115 ++++++++ .../tamaya/osgi/attic/ConfigPrinterService.java | 28 ++ .../osgi/attic/TamayaConfigAdminImpl.java | 167 +++++++++++ .../osgi/attic/TamayaOSGIConfiguration.java | 139 +++++++++ .../tamaya/osgi/TamayaConfigAdminImplTest.java | 75 ----- osgi/karaf-features/pom.xml | 60 +--- osgi/karaf-shell/bnd.bnd | 37 +++ osgi/karaf-shell/pom.xml | 71 +++++ .../tamaya/karaf/shell/ConfigCommand.java | 50 ++++ .../karaf/shell/DefaultDisableCommand.java | 68 +++++ .../karaf/shell/EvaluateCMConfigCommand.java | 63 ++++ .../tamaya/karaf/shell/GetPolicyCommand.java | 42 +++ .../apache/tamaya/karaf/shell/InfoCommand.java | 46 +++ .../tamaya/karaf/shell/PropertyCommand.java | 75 +++++ .../karaf/shell/PropertySourceCommand.java | 59 ++++ .../karaf/shell/PropertySourcesCommand.java | 48 +++ .../tamaya/karaf/shell/SetPolicyCommand.java | 68 +++++ .../org/apache/tamaya/karaf/shell/commands | 27 ++ osgi/pom.xml | 115 +------- 28 files changed, 1538 insertions(+), 600 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/bnd.bnd ---------------------------------------------------------------------- diff --git a/osgi/common/bnd.bnd b/osgi/common/bnd.bnd index 56e697f..22c802c 100644 --- a/osgi/common/bnd.bnd +++ b/osgi/common/bnd.bnd @@ -10,9 +10,9 @@ javac.source: 1.8 javac.target: 1.8 Bundle-Version: ${version}.${tstamp} -Bundle-Name: Apache Tamaya - OSGI ConfigAdmin +Bundle-Name: Apache Tamaya - OSGI ConfigurationPlugin Bundle-SymbolicName: org.apache.tamaya.osgi -Bundle-Description: Apacha Tamaya Configuration - OSGI ConfigAdmin +Bundle-Description: Apacha Tamaya Configuration - OSGI ConfigurationPlugin Bundle-Category: Implementation Bundle-Copyright: (C) Apache Foundation Bundle-License: Apache Licence version 2 @@ -29,4 +29,5 @@ Import-Package: \ org.apache.tamaya.spi,\ org.apache.tamaya.functions,\ org.apache.tamaya.spisupport -Export-Service: org.osgi.service.cm.ConfigurationAdmin +Export-Service: org.osgi.service.cm.ConfigurationPlugin,\ + org.apache.tamaya.osgi.TamayaConfigPlugin http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/pom.xml ---------------------------------------------------------------------- diff --git a/osgi/common/pom.xml b/osgi/common/pom.xml index e0a5411..9455104 100644 --- a/osgi/common/pom.xml +++ b/osgi/common/pom.xml @@ -29,14 +29,14 @@ <artifactId>tamaya-osgi_alpha</artifactId> <packaging>jar</packaging> - <name>Apache Tamaya :: OSGi Integration :: ConfigAdmin</name> - <description>Tamaya Based OSGI Implementation of ConfigAdmin and Config Injection</description> + <name>Apache Tamaya :: OSGi Integration :: ConfigurationPlugin</name> + <description>Tamaya Based OSGI ConfigurationPlugin Implementation</description> <dependencies> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> - <scope>test</scope> + <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/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 83e1acf..d6c5726 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 @@ -18,11 +18,11 @@ */ package org.apache.tamaya.osgi; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.*; +import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; +import org.osgi.service.cm.ConfigurationPlugin; +import org.osgi.service.component.annotations.Reference; import java.util.Dictionary; import java.util.Hashtable; @@ -40,31 +40,44 @@ import java.util.logging.Logger; */ public class Activator implements BundleActivator { - private static final String SERVICE_RANKING_PROP = "org.apache.tamaya.osgi.cm.ranking"; - - private static final Integer DEFAULT_RANKING = 10; + private static final Integer DEFAULT_RANKING = 100000; private static final Logger LOG = Logger.getLogger(Activator.class.getName()); - private ServiceRegistration<ConfigurationAdmin> registration; + private ServiceRegistration<?> registration; + @Override public void start(BundleContext context) throws Exception { - Dictionary<String, Object> props = new Hashtable<>(); - String ranking = context.getProperty(SERVICE_RANKING_PROP); + ServiceReference<ConfigurationAdmin> cmRef = context.getServiceReference(ConfigurationAdmin.class); + ConfigurationAdmin cm = context.getService(cmRef); + Configuration configuration = cm.getConfiguration("tamaya-osgi", null); + Dictionary<String, Object> props = null; + if (configuration != null + && configuration.getProperties() != null) { + props = configuration.getProperties(); + } else { + props = new Hashtable<>(); + } + String ranking = context.getProperty(Constants.SERVICE_RANKING); if (ranking == null) { - ranking = System.getProperty(SERVICE_RANKING_PROP); + ranking = System.getProperty(Constants.SERVICE_RANKING); } if (ranking == null) { - props.put(Constants.SERVICE_RANKING, DEFAULT_RANKING); - LOG.fine("Using default ranking for Tamaya OSGI ConfigAdmin service: " + DEFAULT_RANKING); + ranking = DEFAULT_RANKING.toString(); + LOG.fine("Using default ranking for Tamaya OSGI Config plugin: " + DEFAULT_RANKING); } else { - props.put(Constants.SERVICE_RANKING, Integer.valueOf(ranking)); - LOG.fine("Using custom ranking for Tamaya OSGI ConfigAdmin service: " + ranking); + ranking = Integer.valueOf(ranking).toString(); + LOG.fine("Using custom ranking for Tamaya OSGI Config plugin: " + ranking); } - TamayaConfigAdminImpl cm = new TamayaConfigAdminImpl(context); - registration = context.registerService(ConfigurationAdmin.class, cm, props); - LOG.info("Registered Tamaya OSGI ConfigAdmin service."); + props.put(Constants.SERVICE_RANKING, DEFAULT_RANKING); + TamayaConfigPlugin plugin = new TamayaConfigPlugin(context); + LOG.info("Registering Tamaya OSGI Config plugin with ranking: " + ranking); + registration = context.registerService( + TamayaConfigPlugin.class, + plugin, props); + LOG.info("Registered Tamaya OSGI Config plugin."); + configuration.update(props); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/DefaultOSGIConfigMapper.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/DefaultOSGIConfigMapper.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/DefaultOSGIConfigMapper.java new file mode 100644 index 0000000..861efb2 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/DefaultOSGIConfigMapper.java @@ -0,0 +1,45 @@ +/* + * 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.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; +import org.osgi.framework.ServiceReference; + +import java.util.Arrays; + +/** + * Created by atsticks on 18.09.17. + */ +public class DefaultOSGIConfigMapper implements OSGIConfigMapper { + + @Override + public org.apache.tamaya.Configuration getConfiguration(String pid) { + if (pid != null) { + return ConfigurationProvider.getConfiguration() + .with(ConfigurationFunctions.section("[" + pid + ']', true)); + } + return null; + } + + @Override + public String toString() { + return "Default OSGIConfigRootMapper([symbolicName:version/properties] -> [bundle:symbolicName]"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java new file mode 100644 index 0000000..3841d4b --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java @@ -0,0 +1,38 @@ +/* + * 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.apache.tamaya.Configuration; +import org.osgi.framework.ServiceReference; + +/** + * Mapping function for mapping Tamaya configuration sections to OSGI pids. + */ +public interface OSGIConfigMapper { + + /** + * 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 + * @return return the corresponding config root section. For ommitting any root section simply return an empty + * String. + */ + Configuration getConfiguration(String pid); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigRootMapper.java deleted file mode 100644 index 79cc387..0000000 --- a/osgi/common/src/main/java/org/apache/tamaya/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.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/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigAdminImpl.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigAdminImpl.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigAdminImpl.java deleted file mode 100644 index d9ba1bf..0000000 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigAdminImpl.java +++ /dev/null @@ -1,170 +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.osgi; - -import java.io.IOException; -import java.lang.ref.WeakReference; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.service.cm.Configuration; -import org.osgi.service.cm.ConfigurationAdmin; - -/** - * 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(); - } - - @Override - public Configuration createFactoryConfiguration(String factoryPid) throws IOException { - return createFactoryConfiguration(factoryPid, null); - } - - @Override - public Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException { - String key = "factory:"+factoryPid; - if(location!=null){ - key += "::"+location; - } - Configuration config = this.configs.get(key); - if (config == null) { - Dictionary<String, Object> parentConfig = getParentConfig(null, factoryPid, location); - config = new TamayaOSGIConfiguration(null, factoryPid, configRootMapper, parentConfig); - this.configs.put(key, config); - } - return config; - } - - @Override - public Configuration getConfiguration(String pid, String location) throws IOException { - String key = "config:"+pid; - if(location!=null){ - key += "::"+location; - } - Configuration config = this.configs.get(key); - if (config == null) { - Dictionary<String, Object> parentConfig = getParentConfig(pid, null, location); - config = new TamayaOSGIConfiguration(pid, null, configRootMapper, parentConfig); - this.configs.put(key, config); - } - return config; - } - - @Override - public Configuration getConfiguration(String pid) throws IOException { - return getConfiguration(pid, null); - } - - private Dictionary<String, Object> getParentConfig(String pid, String factoryPid, String location) { - Dictionary<String, Object> parentConfig = null; - if (context != null) { - try { - ServiceReference[] refs = context.getAllServiceReferences(ConfigurationAdmin.class.getName(), null); - for (ServiceReference<ConfigurationAdmin> ref : refs) { - ConfigurationAdmin parentCand = context.getService(ref); - if (parentCand != null && !(parentCand instanceof TamayaConfigAdminImpl)) { - try { - parentConfig = parentCand.getConfiguration(pid, factoryPid).getProperties(); - } catch (IOException e) { - LOG.log(Level.WARNING, "Error reading parent OSGI config.", e); - } - } - } - } catch (Exception e) { - LOG.log(Level.SEVERE, "Cannot not evaluate parent/base OSGI config.", e); - } - } - return parentConfig; - } - - @Override - public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException { - List<Configuration> result = new ArrayList<>(); - if (filter == null || context == null) { - return this.configs.values().toArray(new Configuration[this.configs.size()]); - } else { - Filter flt = context.createFilter(filter); - for(Configuration config:this.configs.values()) { - if (flt.match(config.getProperties())) { - result.add(config); - } - } - return result.toArray(new Configuration[result.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; - if(context!=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 "[" + pid +']'; - } else{ - return "[" + 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/92053860/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 new file mode 100644 index 0000000..509a3d0 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java @@ -0,0 +1,295 @@ +/* + * 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.*; +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.*; +import java.util.logging.Level; +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 { + 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; + + /** + * 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 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); + initDefaultOpMode(); + initConfigs(); + } + + private void initConfigs() { + // Check for matching bundles already installed... + for(Bundle bundle:context.getBundles()){ + switch(bundle.getState()){ + case Bundle.ACTIVE: + configureBundle(bundle); + break; + } + } + } + + private void configureBundle(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; + } + if(enabledTamaya != null && !Boolean.parseBoolean(enabledTamaya)){ + LOG.finest("Bundle is disabled for Tamaya: " + bundle.getSymbolicName()); + return; + } + if(disabled){ + LOG.finest("tamaya.disabled=false: not configuring bundle: " + bundle.getSymbolicName()); + return; + } + + String tamayaPid = bundle.getHeaders().get("Tamaya-PID"); + String pid = tamayaPid!=null?tamayaPid:bundle.getSymbolicName(); + if(pid==null){ + pid = bundle.getLocation(); + } + if(pid==null){ + 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); + } + } + + 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); + } + } + } + + private void initDefaultEnabled() { + String disabledVal = (String)getConfigValue("tamaya.disabled"); + if(disabledVal==null){ + disabledVal = System.getProperty("tamaya.disabled"); + } + if(disabledVal!=null){ + this.disabled = Boolean.parseBoolean(disabledVal); + if(this.disabled) { + LOG.info("Tamaya Config is disabled by default. Add Tamaya-Enabled to your bundle manifests to enable it."); + }else{ + LOG.info("Tamaya Config is enabled by default. Add Tamaya-Disabled to your bundle manifests to disable it."); + } + } + } + + + void setConfigValue(String key, Object value){ + Configuration config = null; + try { + config = cm.getConfiguration(COMPONENTID); + Dictionary<String, Object> props = null; + if (config != null + && config.getProperties() != null) { + props = config.getProperties(); + } else { + props = new Hashtable<String, Object>(); + } + Object val = props.get(key); + if(val==null) { + props.put(key, value); + config.update(props); + } + LOG.finest("Updated Tamaya Plugin config: "+key + "=" + value); + } catch (IOException e) { + LOG.log(Level.WARNING, "Error writing Tamaya config.", e); + } + } + + Object getConfigValue(String key){ + Configuration config = null; + try { + config = cm.getConfiguration(COMPONENTID); + Dictionary<String, Object> props = null; + if (config != null + && config.getProperties() != null) { + props = config.getProperties(); + } + if(props!=null){ + return props.get(key); + } + } catch (IOException e) { + LOG.log(Level.WARNING, "Error reading Tamaya config.", e); + } + 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/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaOSGIConfiguration.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaOSGIConfiguration.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaOSGIConfiguration.java deleted file mode 100644 index 962cc38..0000000 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaOSGIConfiguration.java +++ /dev/null @@ -1,139 +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.osgi; - -import java.io.IOException; -import java.util.*; -import java.util.logging.Logger; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.functions.ConfigurationFunctions; -import org.osgi.service.cm.Configuration; - -/** - * Tamaya based implementation of an OSGI {@link Configuration}. - */ -public class TamayaOSGIConfiguration implements Configuration { - private static final Logger LOG = Logger.getLogger(TamayaOSGIConfiguration.class.getName()); - private final Dictionary<String, Object> parentConfig; - private final String pid; - private final String factoryPid; - private OSGIConfigRootMapper rootMapper; - private boolean overriding = true; - private String bundleLocation; - - /** - * 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 parentConfig the OSGI config for the given context, may be null.. - */ - TamayaOSGIConfiguration(String confPid, String factoryPid, OSGIConfigRootMapper configRootMapper, - Dictionary<String, Object> parentConfig) { - this.pid = confPid; - this.factoryPid = factoryPid; - this.parentConfig = parentConfig; - this.rootMapper = Objects.requireNonNull(configRootMapper); - } - - public boolean isOverriding() { - return overriding; - } - - public void setOverriding(boolean overriding){ - this.overriding = overriding; - } - - @Override - public String getPid() { - return pid; - } - - @Override - public Dictionary<String, Object> getProperties() { - Dictionary<String, Object> properties = new Hashtable<>(); - - final String rootKey = this.rootMapper.getTamayaConfigRoot(pid, factoryPid); - LOG.info("Configuration: Evaluating Tamaya configuration for '" + rootKey + "'."); - org.apache.tamaya.Configuration tamayConfig = ConfigurationProvider.getConfiguration(); - if(overriding){ - if(parentConfig!=null) { - putAll(properties, parentConfig); - } - putAll(properties, tamayConfig.with(ConfigurationFunctions.section(rootKey, true)).getProperties()); - }else{ - putAll(properties, tamayConfig.with(ConfigurationFunctions.section(rootKey, true)).getProperties()); - if(parentConfig!=null) { - putAll(properties, parentConfig); - } - } - return properties; - } - - private void putAll(Dictionary<String, Object> target, Dictionary<String, Object> data) { - Enumeration<String> keys = data.keys(); - while(keys.hasMoreElements()){ - String key = keys.nextElement(); - target.put(key, data.get(key)); - } - } - - private void putAll(Dictionary<String, Object> target, Map<String, String> data) { - for(Map.Entry<String,String> en:data.entrySet()){ - target.put(en.getKey(), en.getValue()); - } - } - - @Override - public void update(Dictionary<String, ?> properties) throws IOException { - throw new UnsupportedOperationException("Mutability is not supported."); - } - - @Override - public void delete() throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public String getFactoryPid() { - return factoryPid; - } - - @Override - public void update() throws IOException { - // Nothing to do since, we load everything dynamically. - } - - @Override - public void setBundleLocation(String location) { - this.bundleLocation = location; - } - - @Override - public String getBundleLocation() { - return this.bundleLocation; - } - - @Override - public long getChangeCount() { - return 0; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinter.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinter.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinter.java new file mode 100644 index 0000000..519ea04 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinter.java @@ -0,0 +1,115 @@ +///* +// * Licensed to the Apache Software Foundation (ASF) under one +// * or more contributor license agreements. See the NOTICE file +// * distributed with this work for additional information +// * regarding copyright ownership. The ASF licenses this file +// * to you under the Apache License, Version 2.0 (the +// * "License"); you may not use this file except in compliance +// * with the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, +// * software distributed under the License is distributed on an +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// * KIND, either express or implied. See the License for the +// * specific language governing permissions and limitations +// * under the License. +// */ +//package org.apache.tamaya.osgi; +// +//import org.osgi.service.cm.ConfigurationAdmin; +//import org.osgi.service.component.annotations.*; +// +//import java.util.Map; +//import java.util.concurrent.ExecutorService; +//import java.util.concurrent.Executors; +//import java.util.logging.Logger; +// +///** +// * Created by atsticks on 06.09.17. +// */ +//@Component(name = ConfigPrinter.COMPONENT_NAME, +// configurationPolicy = ConfigurationPolicy.OPTIONAL, +// configurationPid = ConfigPrinter.COMPONENT_NAME, +// service = ConfigPrinterService.class) +//public class ConfigPrinter implements ConfigPrinterService { +// +// public static final String COMPONENT_NAME = "ConfigPrinter"; +// public static final String COMPONENT_LABEL = "Managed ConfigPrinter Service"; +// +// private static final Logger LOG = Logger.getLogger(ConfigPrinter.class.getName()); +// +// private static ExecutorService executor = Executors.newCachedThreadPool(); +// private Worker worker = new Worker(); +// +// private ConfigurationAdmin cm; +// +// @Reference +// void setConfigurationAdmin(ConfigurationAdmin cm) { +// this.cm = cm; +// } +// +// /** +// * Called when all of the SCR Components required dependencies have been +// * satisfied. +// */ +// @Activate +// @Modified +// public void updateConfig(final Map<String,String> properties) { +// LOG.info("Activating the " + COMPONENT_LABEL); +// if(properties!=null) { +// worker.setConfig(properties.toString()); +// }else{ +// worker.setConfig("no config."); +// } +// } +// +// /** +// * Called when any of the SCR Components required dependencies become +// * unsatisfied. +// */ +// @Deactivate +// public void deactivate() { +// LOG.info("Deactivating the " + COMPONENT_LABEL); +// } +// +// @Override +// public void startPrinter() { +// executor.execute(worker); +// } +// +// @Override +// public void stopPrinter() { +// if (!executor.isTerminated()) { +// executor.shutdownNow(); +// } +// } +// +// /** +// * Thread worker that continuously prints a message. +// */ +// private static class Worker implements Runnable { +// +// private String config; +// +// public void run() { +// boolean running = true; +// int messageCount = 0; +// while (running) { +// try { +// LOG.info("Config " + (++messageCount) + ": " + config); +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// running = false; +// LOG.info("Thread shutting down"); +// } +// } +// } +// +// public void setConfig(String config) { +// this.config = config; +// } +// +// } +//} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinterService.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinterService.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinterService.java new file mode 100644 index 0000000..9eb7033 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/ConfigPrinterService.java @@ -0,0 +1,28 @@ +///* +// * 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; +// +///** +// * Created by atsticks on 06.09.17. +// */ +//public interface ConfigPrinterService { +// void startPrinter(); +// +// void stopPrinter(); +//} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaConfigAdminImpl.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaConfigAdminImpl.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaConfigAdminImpl.java new file mode 100644 index 0000000..757c3be --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaConfigAdminImpl.java @@ -0,0 +1,167 @@ +///* +// * 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.io.IOException; +//import java.util.*; +//import java.util.concurrent.ConcurrentHashMap; +//import java.util.logging.Level; +//import java.util.logging.Logger; +// +//import org.osgi.framework.BundleContext; +//import org.osgi.framework.Filter; +//import org.osgi.framework.InvalidSyntaxException; +//import org.osgi.framework.ServiceReference; +//import org.osgi.service.cm.Configuration; +//import org.osgi.service.cm.ConfigurationAdmin; +// +///** +// * 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 cached configurations. */ +// private Map<String,Configuration> configs = new ConcurrentHashMap<>(); +// /** The configuration section mapper. */ +// private OSGIConfigMapper configRootMapper; +// +// /** +// * Create a new config. +// * @param context the OSGI context +// */ +// TamayaConfigAdminImpl(BundleContext context) { +// this.context = context; +// this.configRootMapper = loadConfigRootMapper(); +// } +// +// @Override +// public Configuration createFactoryConfiguration(String factoryPid) throws IOException { +// return createFactoryConfiguration(factoryPid, null); +// } +// +// @Override +// public Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException { +// String key = "factory:"+factoryPid; +// if(location!=null){ +// key += "::"+location; +// } +// Configuration config = this.configs.get(key); +// if (config == null) { +// Dictionary<String, Object> parentConfig = getParentConfig(null, factoryPid, location); +// config = new TamayaOSGIConfiguration(null, factoryPid, configRootMapper, parentConfig); +// this.configs.put(key, config); +// } +// return config; +// } +// +// @Override +// public Configuration getConfiguration(String pid, String location) throws IOException { +// String key = "config:"+pid; +// if(location!=null){ +// key += "::"+location; +// } +// Configuration config = this.configs.get(key); +// if (config == null) { +// Dictionary<String, Object> parentConfig = getParentConfig(pid, null, location); +// config = new TamayaOSGIConfiguration(pid, null, configRootMapper, parentConfig); +// this.configs.put(key, config); +// } +// return config; +// } +// +// @Override +// public Configuration getConfiguration(String pid) throws IOException { +// return getConfiguration(pid, null); +// } +// +// private Dictionary<String, Object> getParentConfig(String pid, String factoryPid, String location) { +// Dictionary<String, Object> parentConfig = null; +// if (context != null) { +// try { +// ServiceReference[] refs = context.getAllServiceReferences(ConfigurationAdmin.class.getName(), null); +// for (ServiceReference<ConfigurationAdmin> ref : refs) { +// ConfigurationAdmin parentCand = context.getService(ref); +// if (parentCand != null && !(parentCand instanceof TamayaConfigAdminImpl)) { +// try { +// parentConfig = parentCand.getConfiguration(pid, factoryPid).getProperties(); +// } catch (IOException e) { +// LOG.log(Level.WARNING, "Error reading parent OSGI config.", e); +// } +// } +// } +// } catch (Exception e) { +// LOG.log(Level.SEVERE, "Cannot not evaluate parent/base OSGI config.", e); +// } +// } +// return parentConfig; +// } +// +// @Override +// public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException { +// List<Configuration> result = new ArrayList<>(); +// if (filter == null || context == null) { +// return this.configs.values().toArray(new Configuration[this.configs.size()]); +// } else { +// Filter flt = context.createFilter(filter); +// for(Configuration config:this.configs.values()) { +// if (flt.match(config.getProperties())) { +// result.add(config); +// } +// } +// return result.toArray(new Configuration[result.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 OSGIConfigMapper loadConfigRootMapper() { +// OSGIConfigMapper mapper = null; +// if(context!=null) { +// ServiceReference<OSGIConfigMapper> ref = context.getServiceReference(OSGIConfigMapper.class); +// if (ref != null) { +// mapper = context.getService(ref); +// } +// } +// if(mapper==null){ +// mapper = new OSGIConfigMapper() { +// @Override +// public String getTamayaConfigRoot(String pid, String factoryPid) { +// if(pid!=null) { +// return "[" + pid +']'; +// } else{ +// return "[" + 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/92053860/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaOSGIConfiguration.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaOSGIConfiguration.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaOSGIConfiguration.java new file mode 100644 index 0000000..dfadfb8 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/attic/TamayaOSGIConfiguration.java @@ -0,0 +1,139 @@ +///* +// * 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.io.IOException; +//import java.util.*; +//import java.util.logging.Logger; +// +//import org.apache.tamaya.ConfigurationProvider; +//import org.apache.tamaya.functions.ConfigurationFunctions; +//import org.osgi.service.cm.Configuration; +// +///** +// * Tamaya based implementation of an OSGI {@link Configuration}. +// */ +//public class TamayaOSGIConfiguration implements Configuration { +// private static final Logger LOG = Logger.getLogger(TamayaOSGIConfiguration.class.getName()); +// private final Dictionary<String, Object> parentConfig; +// private final String pid; +// private final String factoryPid; +// private OSGIConfigMapper rootMapper; +// private boolean overriding = true; +// private String bundleLocation; +// +// /** +// * 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 parentConfig the OSGI config for the given context, may be null.. +// */ +// TamayaOSGIConfiguration(String confPid, String factoryPid, OSGIConfigMapper configRootMapper, +// Dictionary<String, Object> parentConfig) { +// this.pid = confPid; +// this.factoryPid = factoryPid; +// this.parentConfig = parentConfig; +// this.rootMapper = Objects.requireNonNull(configRootMapper); +// } +// +// public boolean isOverriding() { +// return overriding; +// } +// +// public void setOverriding(boolean overriding){ +// this.overriding = overriding; +// } +// +// @Override +// public String getPid() { +// return pid; +// } +// +// @Override +// public Dictionary<String, Object> getProperties() { +// Dictionary<String, Object> properties = new Hashtable<>(); +// +// final String rootKey = this.rootMapper.getTamayaConfigRoot(pid, factoryPid); +// LOG.info("Configuration: Evaluating Tamaya configuration for '" + rootKey + "'."); +// org.apache.tamaya.Configuration tamayConfig = ConfigurationProvider.getConfiguration(); +// if(overriding){ +// if(parentConfig!=null) { +// putAll(properties, parentConfig); +// } +// putAll(properties, tamayConfig.with(ConfigurationFunctions.section(rootKey, true)).getProperties()); +// }else{ +// putAll(properties, tamayConfig.with(ConfigurationFunctions.section(rootKey, true)).getProperties()); +// if(parentConfig!=null) { +// putAll(properties, parentConfig); +// } +// } +// return properties; +// } +// +// private void putAll(Dictionary<String, Object> target, Dictionary<String, Object> data) { +// Enumeration<String> keys = data.keys(); +// while(keys.hasMoreElements()){ +// String key = keys.nextElement(); +// target.put(key, data.get(key)); +// } +// } +// +// private void putAll(Dictionary<String, Object> target, Map<String, String> data) { +// for(Map.Entry<String,String> en:data.entrySet()){ +// target.put(en.getKey(), en.getValue()); +// } +// } +// +// @Override +// public void update(Dictionary<String, ?> properties) throws IOException { +// throw new UnsupportedOperationException("Mutability is not supported."); +// } +// +// @Override +// public void delete() throws IOException { +// throw new UnsupportedOperationException(); +// } +// +// @Override +// public String getFactoryPid() { +// return factoryPid; +// } +// +// @Override +// public void update() throws IOException { +// // Nothing to do since, we load everything dynamically. +// } +// +// @Override +// public void setBundleLocation(String location) { +// this.bundleLocation = location; +// } +// +// @Override +// public String getBundleLocation() { +// return this.bundleLocation; +// } +// +// @Override +// public long getChangeCount() { +// return 0; +// } +// +//} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigAdminImplTest.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigAdminImplTest.java b/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigAdminImplTest.java deleted file mode 100644 index 93f98d0..0000000 --- a/osgi/common/src/test/java/org/apache/tamaya/osgi/TamayaConfigAdminImplTest.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.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 TamayaConfigAdminImplTest { - - private TamayaConfigAdminImpl configAdmin = new TamayaConfigAdminImpl(null); - - @Test - public void createFactoryConfiguration() 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 http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-features/pom.xml ---------------------------------------------------------------------- diff --git a/osgi/karaf-features/pom.xml b/osgi/karaf-features/pom.xml index b7cc919..eb4c7e1 100644 --- a/osgi/karaf-features/pom.xml +++ b/osgi/karaf-features/pom.xml @@ -27,63 +27,19 @@ limitations under the License. </parent> <artifactId>tamaya-karaf-features</artifactId> - <packaging>jar</packaging> - <description>Tamaya Karaf Integration Tests Suite.</description> + <packaging>feature</packaging> <name>Apache Tamaya :: Karaf :: Features</name> - - <properties> - <karaf.version>4.0.7</karaf.version> - <pax.exam.version>4.5.0</pax.exam.version> - </properties> + <description>Tamaya Karaf Feature Descriptor.</description> <build> <plugins> - - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-resources-plugin</artifactId> - <executions> - <execution> - <id>features</id> - <phase>generate-resources</phase> - <goals> - <goal>copy-resources</goal> - </goals> - <configuration> - <resources> - <resource> - <directory>src/main/features</directory> - <filtering>true</filtering> - </resource> - </resources> - <outputDirectory>target/features</outputDirectory> - </configuration> - </execution> - </executions> - </plugin> - <plugin> - <groupId>org.codehaus.mojo</groupId> - <artifactId>build-helper-maven-plugin</artifactId> - <version>1.12</version> - <executions> - <execution> - <id>attach-artifacts</id> - <phase>package</phase> - <goals> - <goal>attach-artifact</goal> - </goals> - <configuration> - <artifacts> - <artifact> - <file>target/features/features.xml</file> - <type>xml</type> - <classifier>features</classifier> - </artifact> - </artifacts> - </configuration> - </execution> - </executions> + <groupId>org.apache.karaf.tooling</groupId> + <artifactId>karaf-maven-plugin</artifactId> + <configuration> + <startLevel>80</startLevel> + <aggregateFeatures>true</aggregateFeatures> + </configuration> </plugin> </plugins> </build> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/bnd.bnd ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/bnd.bnd b/osgi/karaf-shell/bnd.bnd new file mode 100644 index 0000000..a7c8013 --- /dev/null +++ b/osgi/karaf-shell/bnd.bnd @@ -0,0 +1,37 @@ +-buildpath: \ + osgi.annotation; version=6.0.0,\ + osgi.core; version=6.0,\ + osgi.cmpn; version=6.0 + +-testpath: \ + ${junit} + +javac.source: 1.8 +javac.target: 1.8 + +Bundle-Version: ${version}.${tstamp} +Bundle-Name: Apache Tamaya - Karaf Shell Commands +Bundle-SymbolicName: org.apache.tamaya.osgi.karaf.shell +Bundle-Description: Apacha Tamaya Configuration - Karaf Shell Extensions +Bundle-Category: Implementation +Bundle-Copyright: (C) Apache Foundation +Bundle-License: Apache Licence version 2 +Bundle-Vendor: Apache Software Foundation +Bundle-ContactAddress: dev-tam...@incubator.apache.org +Bundle-DocURL: http://tamaya.apache.org +Export-Package: \ + org.apache.tamaya.karaf.shell +Import-Package: \ + org.osgi.service.cm,\ + org.osgi.framework,\ + org.apache.tamaya,\ + org.apache.tamaya.spi,\ + org.apache.tamaya.functions,\ + org.apache.tamaya.spisupport,\ + org.apache.tamaya.osgi,\ + org.apache.felix.service.command,\ + org.apache.karaf.shell.api.console,\ + org.apache.karaf.shell.api.action,\ + org.apache.karaf.shell.api.action.lifecycle,\ + org.apache.karaf.shell.support.completers +Karaf-Commands: org.apache.tamaya.karaf.shell \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/pom.xml ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/pom.xml b/osgi/karaf-shell/pom.xml new file mode 100644 index 0000000..cb857dc --- /dev/null +++ b/osgi/karaf-shell/pom.xml @@ -0,0 +1,71 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <!-- + + 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. + --> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-osgi-all</artifactId> + <version>0.4-incubating-SNAPSHOT</version> + </parent> + + <artifactId>tamaya-karaf-shell_alpha</artifactId> + <packaging>jar</packaging> + <name>Apache Tamaya :: Karaf :: Shell</name> + <description>Tamaya Karaf Shell Commands</description> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-osgi_alpha</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.karaf.shell</groupId> + <artifactId>org.apache.karaf.shell.core</artifactId> + <version>${dependency.karaf.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.karaf.shell</groupId> + <artifactId>org.apache.karaf.shell.console</artifactId> + <version>${dependency.karaf.version}</version> + <scope>provided</scope> + </dependency> + + + <!-- Testing --> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/ConfigCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/ConfigCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/ConfigCommand.java new file mode 100644 index 0000000..84a862c --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/ConfigCommand.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy 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.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; + +import java.io.IOException; + +@Command(scope = "tamaya", name = "config", description="Show the current Tamaya configuration.") +@Service +public class ConfigCommand implements Action{ + + @Argument(index = 0, name = "section", description = "A regular expression selecting the section to be filtered.", + required = false, multiValued = false) + String section = null; + + public Object execute() throws IOException { + Configuration config = ConfigurationProvider.getConfiguration(); + if(section!=null){ + return config + .with(ConfigurationFunctions.section(section)) + .query(ConfigurationFunctions.textInfo()); + } + System.out.println(config.query(ConfigurationFunctions.textInfo())); + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java new file mode 100644 index 0000000..0c14da5 --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java @@ -0,0 +1,68 @@ +/* + * 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.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Completion; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.completers.StringsCompleter; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +import java.io.IOException; +import java.util.List; + +@Command(scope = "tamaya", name = "disable-by-default", description="Disables Tamaya by default for all bundles/services (default=false)." + + " Disabling it allows to explicitly enable bundles using 'Tamaya-Enable^manifest entries.") +@Service +public class DefaultDisableCommand implements Action{ + + @Reference + private TamayaConfigPlugin configPlugin; + + @Argument(index = 0, name = "disabled", description = "The boolean value to disable Tamaya by default.", + required = true, multiValued = false) + @Completion(OperationModeCompleter.class) + boolean disabled; + + @Override + public Object execute() throws IOException { + this.configPlugin.setDefaultDisabled(disabled); + return null; + } + + @Service + public static final class OperationModeCompleter implements Completer { + + @Override + public int complete(Session session, CommandLine commandLine, List<String> candidates) { + StringsCompleter delegate = new StringsCompleter(); + for(TamayaConfigPlugin.OperationMode mode:TamayaConfigPlugin.OperationMode.values()) { + delegate.getStrings().add(mode.toString()); + } + return delegate.complete(session, commandLine, candidates); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java new file mode 100644 index 0000000..d22022a --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; + +import java.io.IOException; + +@Command(scope = "tamaya", name = "cm-config", description="Show the current Tamaya configuration.") +@Service +public class EvaluateCMConfigCommand implements Action{ + + @Argument(index = 0, name = "pid", description = "The component's PID.", + required = true, multiValued = false) + String pid = null; + + @Argument(index = 1, name = "location", description = "The component's configuration location.", + required = false, multiValued = false) + String location = null; + + @Reference + private ConfigurationAdmin cm; + + public Object execute() throws IOException { + Configuration config = cm.getConfiguration(pid, location); + System.out.println("OSGI Configuration for PID: " + pid); + System.out.println("----------------------------------------------------------"); + if(config!=null){ + System.out.println("PID: " + config.getPid()); + System.out.println("Factory-PID: " + config.getFactoryPid()); + System.out.println("Location: " + config.getBundleLocation()); + System.out.println("Change Count: " + config.getChangeCount()); + System.out.println("Properties:"); + System.out.println(config.getProperties()); + }else{ + System.out.println("No OSGI Config found for PID: " + config.getPid()); + } + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java new file mode 100644 index 0000000..3f1f1f0 --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java @@ -0,0 +1,42 @@ +/* + * 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.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +import java.io.IOException; + +@Command(scope = "tamaya", name = "get-policy", description="Get the current Tamaya overriding policy.") +@Service +public class GetPolicyCommand implements Action{ + + @Reference + private TamayaConfigPlugin configPlugin; + + @Override + public Object execute() throws IOException { + System.out.println(this.configPlugin.getOperationMode()); + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java new file mode 100644 index 0000000..f9dafed --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import java.io.IOException; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +@Command(scope = "tamaya", name = "info", description="Show he current Tamaya status.") +@Service +public class InfoCommand implements Action { + + @Reference + private TamayaConfigPlugin configPlugin; + + public Object execute() throws IOException { + Configuration config = ConfigurationProvider.getConfiguration(); + System.out.println(config.toString() + "\n\n" + + "OSGI OperationPolicy: " + configPlugin.getOperationMode() + '\n' + + "Default Disabled : " + configPlugin.isDefaultDisabled()); + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/92053860/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java new file mode 100644 index 0000000..04c792e --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.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.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.io.IOException; + +@Command(scope = "tamaya", name = "property", description="Show the current Tamaya configuration.") +@Service +public class PropertyCommand implements Action{ + + @Argument(index = 0, name = "key", description = "The target property source id.", + required = false, multiValued = false) + String key = null; + + @Option(name = "propertysource", aliases = "ps", description = "The target property source id.", + required = false, multiValued = false) + String propertysource = null; + + public Object execute() throws IOException { + Configuration config = ConfigurationProvider.getConfiguration(); + if(propertysource!=null){ + PropertySource ps = config.getContext().getPropertySource(propertysource); + if(ps==null){ + System.out.println("No such propertysource: " + propertysource); + }else { + PropertyValue val = ps.get(key); + if(val==null){ + System.out.println("PropertySource: " + propertysource + "\nUndefined key: " + key); + }else { + System.out.println("PropertySource: " + propertysource + "\n" + + ps.get(key)); + } + } + }else{ + for(PropertySource ps:config.getContext().getPropertySources()){ + PropertyValue val = ps.get(key); + if(val==null){ + System.out.println("PropertySource: " + propertysource + "\nUndefined key: " + key); + }else { + System.out.println("PropertySource: " + propertysource + "\n" + + ps.get(key)); + } + } + } + return null; + } + +} \ No newline at end of file