Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisher.java20
-rw-r--r--org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSource.java26
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java53
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfigurationAdmin.java27
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisherTests.java33
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSourceTests.java23
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryOne.properties3
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryTwo.properties3
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/withServicePid.properties3
9 files changed, 173 insertions, 18 deletions
diff --git a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisher.java b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisher.java
index c51ebeaf..859826e9 100644
--- a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisher.java
+++ b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisher.java
@@ -23,6 +23,7 @@ import org.osgi.service.cm.ConfigurationAdmin;
import org.eclipse.virgo.kernel.serviceability.NonNull;
import org.eclipse.virgo.util.common.IterableEnumeration;
+import org.eclipse.virgo.util.common.StringUtils;
/**
* <code>ConfigurationPublisher</code>, publishes kernel configuration to {@link ConfigurationAdmin}.
@@ -64,7 +65,7 @@ final class ConfigurationPublisher {
@SuppressWarnings("unchecked")
private void populateConfigurationWithProperties(@NonNull String pid, @NonNull Properties properties) throws IOException {
- Configuration config = this.configAdmin.getConfiguration(pid, null);
+ Configuration config = getConfiguration(pid, properties);
Dictionary configProperties = config.getProperties();
if (configProperties == null) {
@@ -79,4 +80,21 @@ final class ConfigurationPublisher {
config.update(configProperties);
}
+ /**
+ * @param pid
+ * @param properties
+ * @return
+ * @throws IOException
+ */
+ private Configuration getConfiguration(String pid, Properties properties) throws IOException {
+ Configuration result = null;
+ String factoryPid = properties.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
+ if (StringUtils.hasText(factoryPid)) {
+ result = this.configAdmin.createFactoryConfiguration(factoryPid, null);
+ } else {
+ result = this.configAdmin.getConfiguration(pid, null);
+ }
+ return result;
+ }
+
}
diff --git a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSource.java b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSource.java
index 402a0124..9745545f 100644
--- a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSource.java
+++ b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSource.java
@@ -19,10 +19,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
+import java.util.Set;
import java.util.TreeMap;
+import org.eclipse.virgo.util.common.StringUtils;
import org.eclipse.virgo.util.io.FileSystemUtils;
import org.eclipse.virgo.util.io.IOUtils;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ConfigurationAdmin;
/**
* Implementation of {@link PropertiesSource} that loads all the configuration supplied by the kernel user.
@@ -68,8 +72,10 @@ final class UserConfigurationPropertiesSource implements PropertiesSource {
for (File dir : this.kernelConfigDirectories) {
File[] configFiles = getPropertiesFiles(dir);
for (File file : configFiles) {
- String pid = createPid(file);
Properties properties = readPropertiesFromFile(file);
+
+ String pid = computePid(file, properties, result.keySet());
+
// Last pid encountered wins; no merging is performed
result.put(pid, properties);
}
@@ -101,7 +107,7 @@ final class UserConfigurationPropertiesSource implements PropertiesSource {
}
return props;
}
-
+
private static String createPid(final File file) {
return trimExtension(file.getName());
}
@@ -110,4 +116,20 @@ final class UserConfigurationPropertiesSource implements PropertiesSource {
int lpDot = name.lastIndexOf('.');
return lpDot == -1 ? name : name.substring(0, lpDot);
}
+
+ private String computePid(File file, Properties properties, Set<String> existingPids) {
+
+ // check for factory pid in properties
+ String pid = properties.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
+ if (StringUtils.hasText(pid)) {
+ // need something unique - so multiple factory pids can be deployed
+ return pid + "-" + createPid(file);
+ }
+ // account for service.pid as a property in the file
+ pid = properties.getProperty(Constants.SERVICE_PID);
+ if (StringUtils.hasText(pid)) {
+ return pid;
+ }
+ return createPid(file);
+ }
}
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java
index ad67c5ca..e24678ce 100644
--- a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java
@@ -21,6 +21,19 @@ public class StubConfiguration implements Configuration {
private Hashtable properties = null;
+ private final String pid;
+
+ private final String factoryPid;
+
+ public StubConfiguration() {
+ this(null, null);
+ }
+
+ public StubConfiguration(String pid, String factoryPid) {
+ this.pid = pid;
+ this.factoryPid = factoryPid;
+ }
+
public void delete() throws IOException {
throw new UnsupportedOperationException();
}
@@ -30,16 +43,17 @@ public class StubConfiguration implements Configuration {
}
public String getFactoryPid() {
- throw new UnsupportedOperationException();
+ return factoryPid;
}
public String getPid() {
- throw new UnsupportedOperationException();
+ return pid;
}
@SuppressWarnings("unchecked")
public Dictionary getProperties() {
- if (this.properties == null) return null;
+ if (this.properties == null)
+ return null;
Hashtable propertiesCopy = new Hashtable();
propertiesCopy.putAll(this.properties);
return propertiesCopy;
@@ -54,6 +68,37 @@ public class StubConfiguration implements Configuration {
}
public void update(Dictionary dictionary) throws IOException {
- this.properties = (Hashtable)dictionary;
+ this.properties = (Hashtable) dictionary;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((pid == null) ? 0 : pid.hashCode());
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ StubConfiguration other = (StubConfiguration) obj;
+ if (pid == null) {
+ if (other.pid != null)
+ return false;
+ } else if (!pid.equals(other.pid))
+ return false;
+ return true;
}
}
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfigurationAdmin.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfigurationAdmin.java
index ece41256..93171756 100644
--- a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfigurationAdmin.java
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfigurationAdmin.java
@@ -14,14 +14,15 @@ package org.eclipse.virgo.kernel;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
-
public class StubConfigurationAdmin implements ConfigurationAdmin {
-
+
+ private final AtomicInteger factoryConfigurationCounter = new AtomicInteger(0);
private final Map<String, Configuration> configurations = new HashMap<String, Configuration>();
public Configuration createFactoryConfiguration(String arg0) throws IOException {
@@ -29,7 +30,8 @@ public class StubConfigurationAdmin implements ConfigurationAdmin {
}
public Configuration createFactoryConfiguration(String arg0, String arg1) throws IOException {
- throw new UnsupportedOperationException();
+ final String pid = arg0 + "-" + System.currentTimeMillis() + "-" + factoryConfigurationCounter.incrementAndGet();
+ return getConfiguration(pid, arg1, arg0);
}
public Configuration getConfiguration(String arg0) throws IOException {
@@ -37,15 +39,22 @@ public class StubConfigurationAdmin implements ConfigurationAdmin {
}
public Configuration getConfiguration(String pid, String location) throws IOException {
+ return getConfiguration(pid, location, null);
+ }
+
+ public Configuration[] listConfigurations(String arg0) throws IOException, InvalidSyntaxException {
+ if (arg0 == null) {
+ return configurations.values().toArray(new Configuration[configurations.values().size()]);
+ }
+ throw new UnsupportedOperationException("only support 'null' filter in stub");
+ }
+
+ private Configuration getConfiguration(String pid, String location, String factoryPid) {
Configuration configuration = this.configurations.get(pid);
if (configuration == null) {
- configuration = new StubConfiguration();
+ configuration = new StubConfiguration(pid, factoryPid);
configurations.put(pid, configuration);
- }
+ }
return configuration;
}
-
- public Configuration[] listConfigurations(String arg0) throws IOException, InvalidSyntaxException {
- throw new UnsupportedOperationException();
- }
}
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisherTests.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisherTests.java
index 413c3b6d..5b77b952 100644
--- a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisherTests.java
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationPublisherTests.java
@@ -13,7 +13,10 @@ package org.eclipse.virgo.kernel.config.internal;
import static org.junit.Assert.assertEquals;
+import java.io.File;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
@@ -24,7 +27,6 @@ import org.eclipse.virgo.kernel.config.internal.PropertiesSource;
import org.junit.Test;
import org.osgi.service.cm.Configuration;
-
/**
*/
public class ConfigurationPublisherTests {
@@ -78,7 +80,7 @@ public class ConfigurationPublisherTests {
Configuration configuration = configAdmin.getConfiguration(pidOne, null);
assertConfigurationEquals(configuration, propertiesOne);
-
+
configuration = configAdmin.getConfiguration(pidTwo, null);
assertConfigurationEquals(configuration, propertiesTwo);
@@ -119,6 +121,33 @@ public class ConfigurationPublisherTests {
assertEquals("bof", configuration.getProperties().get("boo"));
}
+ @Test
+ public void testPublicationIncludingFactoryConfigurations() throws Exception {
+ File[] dirs = new File[] { new File("src/test/resources/UserConfigurationPropertiesSourceTests")};
+
+ UserConfigurationPropertiesSource source = new UserConfigurationPropertiesSource(dirs);
+
+ StubConfigurationAdmin configAdmin = new StubConfigurationAdmin();
+
+ ConfigurationPublisher publisher = new ConfigurationPublisher(configAdmin, source);
+ publisher.publishConfigurations();
+
+ // make sure nothing broke on the way here and file name based pids still work
+ Configuration three = configAdmin.getConfiguration("three", null);
+ assertConfigurationEquals(three, source.getConfigurationProperties().get("three"));
+
+ // check on factories
+ List<Configuration> factories = new ArrayList<Configuration>();
+ Configuration[] all = configAdmin.listConfigurations(null);
+ for (Configuration c : all) {
+ if ("factory.pid".equals(c.getFactoryPid())) {
+ factories.add(c);
+ }
+ }
+
+ assertEquals(2, factories.size());
+ }
+
private void assertConfigurationEquals(Configuration configuration, Properties properties) {
for (String s : properties.stringPropertyNames()) {
assertEquals(properties.getProperty(s), configuration.getProperties().get(s));
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSourceTests.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSourceTests.java
index cde779fd..916b9c4f 100644
--- a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSourceTests.java
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/UserConfigurationPropertiesSourceTests.java
@@ -37,9 +37,32 @@ public class UserConfigurationPropertiesSourceTests {
Properties one = properties.get("one");
Properties two = properties.get("two");
+ Properties three = properties.get("three");
assertNotNull(one);
assertNotNull(two);
+ assertNotNull(three);
assertEquals("bar", one.getProperty("foo"));
assertEquals("baz", two.getProperty("bar"));
+ assertEquals("three", three.getProperty("foo"));
+ }
+
+ @Test
+ public void testReadFactoryUserConfiguration() {
+ File[] dirs = new File[]{
+ new File("src/test/resources/" + getClass().getSimpleName())
+ };
+
+ UserConfigurationPropertiesSource source = new UserConfigurationPropertiesSource(dirs);
+ Map<String, Properties> properties = source.getConfigurationProperties();
+
+ assertEquals(5, properties.size());
+
+ Properties one = properties.get("factory.pid-factoryOne");
+ Properties two = properties.get("factory.pid-factoryTwo");
+
+ assertNotNull(one);
+ assertNotNull(two);
+ assertEquals("one", one.getProperty("foo"));
+ assertEquals("two", two.getProperty("foo"));
}
}
diff --git a/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryOne.properties b/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryOne.properties
new file mode 100644
index 00000000..114554ce
--- /dev/null
+++ b/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryOne.properties
@@ -0,0 +1,3 @@
+service.factoryPid = factory.pid
+
+foo=one \ No newline at end of file
diff --git a/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryTwo.properties b/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryTwo.properties
new file mode 100644
index 00000000..96288204
--- /dev/null
+++ b/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/factoryTwo.properties
@@ -0,0 +1,3 @@
+service.factoryPid = factory.pid
+
+foo = two \ No newline at end of file
diff --git a/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/withServicePid.properties b/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/withServicePid.properties
new file mode 100644
index 00000000..636ac8c4
--- /dev/null
+++ b/org.eclipse.virgo.kernel.core/src/test/resources/UserConfigurationPropertiesSourceTests/withServicePid.properties
@@ -0,0 +1,3 @@
+service.pid = three
+
+foo=three \ No newline at end of file

Back to the top