Package: UserConfigurationPropertiesSource

UserConfigurationPropertiesSource

nameinstructionbranchcomplexitylinemethod
UserConfigurationPropertiesSource(File[])
M: 12 C: 9
43%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
computePid(File, Properties, Set)
M: 6 C: 31
84%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
createPid(File)
M: 6 C: 4
40%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getConfigurationProperties()
M: 6 C: 58
91%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getPropertiesFiles(File)
M: 6 C: 10
63%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
readPropertiesFromFile(File)
M: 11 C: 25
69%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 8
80%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
trimExtension(String)
M: 8 C: 12
60%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
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.BufferedInputStream;
15: import java.io.File;
16: import java.io.FileInputStream;
17: import java.io.FilenameFilter;
18: import java.io.IOException;
19: import java.io.InputStream;
20: import java.util.Map;
21: import java.util.Properties;
22: import java.util.Set;
23: import java.util.TreeMap;
24:
25: import org.eclipse.virgo.util.common.StringUtils;
26: import org.eclipse.virgo.util.io.FileSystemUtils;
27: import org.eclipse.virgo.util.io.IOUtils;
28: import org.osgi.framework.Constants;
29: import org.osgi.service.cm.ConfigurationAdmin;
30:
31: /**
32: * Implementation of {@link PropertiesSource} that loads all the configuration supplied by the kernel user.
33: *
34: * <p/>
35: *
36: * User configuration is loaded from all properties files found in the directories listed in the
37: * {@link KernelConfiguration#getConfigDirectories() configuration directories}.
38: *
39: * <strong>Concurrent Semantics</strong><br />
40: *
41: * Threadsafe.
42: *
43: */
44: final class UserConfigurationPropertiesSource implements PropertiesSource {
45:
46: private static final String PROPERTIES_FILE_SUFFIX = ".properties";
47:
48: private static final FilenameFilter PROPERTIES_FILENAME_FILTER = new FilenameFilter() {
49:
50: public boolean accept(File dir, String name) {
51: return name.endsWith(PROPERTIES_FILE_SUFFIX);
52: }
53: };
54:
55: private final File[] kernelConfigDirectories;
56:
57: /**
58: * Creates a new <code>UserConfigurationPropertiesSource</code> that loads config files from the supplied user
59: * config directories.
60: *
61: * @param kernelConfigDirectories the directories containing the users kernel config files.
62: */
63: public UserConfigurationPropertiesSource(File[] kernelConfigDirectories) {
64: this.kernelConfigDirectories = kernelConfigDirectories;
65: }
66:
67: /**
68: * {@inheritDoc}
69: */
70: public Map<String, Properties> getConfigurationProperties() {
71: Map<String, Properties> result = new TreeMap<String, Properties>();
72:• for (File dir : this.kernelConfigDirectories) {
73: File[] configFiles = getPropertiesFiles(dir);
74:• for (File file : configFiles) {
75: Properties properties = readPropertiesFromFile(file);
76:
77: String pid = computePid(file, properties, result.keySet());
78:
79: // Last pid encountered wins; no merging is performed
80: result.put(pid, properties);
81: }
82: }
83: return result;
84: }
85:
86: private static File[] getPropertiesFiles(File directory) {
87:• if (directory.isDirectory()) {
88: return FileSystemUtils.listFiles(directory, PROPERTIES_FILENAME_FILTER);
89: }
90: return new File[0];
91: }
92:
93: private Properties readPropertiesFromFile(File file) {
94:• if (!file.exists()) {
95: return null;
96: }
97: Properties props = new Properties();
98: InputStream is = null;
99: try {
100: is = new BufferedInputStream(new FileInputStream(file));
101: props.load(is);
102: } catch (IOException ioe) {
103: // silently ignored
104: return null;
105: } finally {
106: IOUtils.closeQuietly(is);
107: }
108: return props;
109: }
110:
111: private static String createPid(final File file) {
112: return trimExtension(file.getName());
113: }
114:
115: private static String trimExtension(final String name) {
116: int lpDot = name.lastIndexOf('.');
117:• return lpDot == -1 ? name : name.substring(0, lpDot);
118: }
119:
120: private String computePid(File file, Properties properties, Set<String> existingPids) {
121:
122: // check for factory pid in properties
123: String pid = properties.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
124:• if (StringUtils.hasText(pid)) {
125: // need something unique - so multiple factory pids can be deployed
126: return pid + "-" + createPid(file);
127: }
128: // account for service.pid as a property in the file
129: pid = properties.getProperty(Constants.SERVICE_PID);
130:• if (StringUtils.hasText(pid)) {
131: return pid;
132: }
133: return createPid(file);
134: }
135: }