Package: ConfigurationPublisher

ConfigurationPublisher

nameinstructionbranchcomplexitylinemethod
ConfigurationPublisher(ConfigurationAdmin, PropertiesSource[])
M: 9 C: 16
64%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
createConfigurationExporterService()
M: 6 C: 18
75%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getConfiguration(String, Properties)
M: 6 C: 24
80%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
populateConfigurationWithProperties(String, Properties)
M: 6 C: 65
92%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
publishConfigurations()
M: 6 C: 43
88%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
registerConfigurationExporterService(BundleContext)
M: 6 C: 15
71%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.nano.config.internal;
13:
14: import java.io.IOException;
15: import java.util.Dictionary;
16: import java.util.Hashtable;
17: import java.util.Map;
18: import java.util.Properties;
19: import java.util.Map.Entry;
20:
21: import org.osgi.framework.BundleContext;
22: import org.osgi.service.cm.Configuration;
23: import org.osgi.service.cm.ConfigurationAdmin;
24: import org.slf4j.Logger;
25: import org.slf4j.LoggerFactory;
26:
27: import org.eclipse.virgo.nano.core.ConfigurationExporter;
28: import org.eclipse.virgo.nano.serviceability.NonNull;
29: import org.eclipse.virgo.util.common.IterableEnumeration;
30: import org.eclipse.virgo.util.common.StringUtils;
31:
32: /**
33: * <code>ConfigurationPublisher</code>, publishes kernel configuration to {@link ConfigurationAdmin}.
34: * <p/>
35: * Properties files in {@link KernelConfiguration#getConfigDirectories() config directories} are read in and applied to
36: * {@link Configuration Configurations} owned by <code>ConfigurationAdmin</code>. A file called
37: * <code><i>name</i>.properties</code> results in a <Code>Configuration</code> with the service pid
38: * <code><i>name</i></code>.
39: * <p/>
40: *
41: * <strong>Concurrent Semantics</strong><br />
42: *
43: * Thread-safe.
44: *
45: *
46: */
47: final class ConfigurationPublisher {
48:
49: private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
50:
51: private final ConfigurationAdmin configAdmin;
52:
53: private final PropertiesSource[] sources;
54:
55: private final static String KERNEL_REGION_CONFIGURATION_PID = KernelConfigurationPropertiesSource.KERNEL_CONFIGURATION_PID;
56:
57: private final static String USER_REGION_CONFIGURATION_PID = "org.eclipse.virgo.kernel.userregion";
58:
59: ConfigurationPublisher(ConfigurationAdmin configAdmin, PropertiesSource... sources) {
60: this.configAdmin = configAdmin;
61:• this.sources = (sources == null ? new PropertiesSource[0] : sources);
62: }
63:
64: void publishConfigurations() throws IOException {
65:• for (PropertiesSource source : this.sources) {
66: Map<String, Properties> configurationProperties = source.getConfigurationProperties();
67:• if (configurationProperties != null) {
68:• for (Entry<String, Properties> entry : configurationProperties.entrySet()) {
69: populateConfigurationWithProperties(entry.getKey(), entry.getValue());
70: }
71: }
72: }
73: }
74:
75: void registerConfigurationExporterService(@NonNull BundleContext context) throws IOException {
76:         ConfigurationExporter configurationExporter = createConfigurationExporterService();
77: context.registerService(ConfigurationExporter.class, configurationExporter, null);
78: }
79:
80:         private ConfigurationExporter createConfigurationExporterService() throws IOException {
81:                 Configuration kernelregionConfiguration = this.configAdmin.getConfiguration(KERNEL_REGION_CONFIGURATION_PID);
82:         Configuration userregionConfiguration = this.configAdmin.getConfiguration(USER_REGION_CONFIGURATION_PID);
83:         
84: ConfigurationExporter configurationExporter = new StandardConfigurationExporter(userregionConfiguration, kernelregionConfiguration);
85:                 return configurationExporter;
86:         }
87:
88: @SuppressWarnings("unchecked")
89: private void populateConfigurationWithProperties(@NonNull String pid, @NonNull Properties properties) throws IOException {
90: Configuration config = getConfiguration(pid, properties);
91:
92: Dictionary configProperties = config.getProperties();
93:• if (configProperties == null) {
94: configProperties = new Hashtable();
95: }
96:
97:• for (Object key : new IterableEnumeration(properties.keys())) {
98: Object value = properties.get(key);
99: configProperties.put(key, value);
100: }
101: try {
102:         config.update(configProperties);
103: } catch (RuntimeException e) {
104:                         LOGGER.error(String.format("Failed to update configuration for pid '%s'", pid), e);
105:                 }
106: }
107:
108: /**
109: * @param pid
110: * @param properties
111: * @return
112: * @throws IOException
113: */
114: private Configuration getConfiguration(String pid, Properties properties) throws IOException {
115: Configuration result = null;
116: String factoryPid = properties.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
117:• if (StringUtils.hasText(factoryPid)) {
118: result = this.configAdmin.createFactoryConfiguration(factoryPid, null);
119: } else {
120: result = this.configAdmin.getConfiguration(pid, null);
121: }
122: return result;
123: }
124:
125: }