Package: PropertiesBridge

PropertiesBridge

nameinstructionbranchcomplexitylinemethod
PropertiesBridge(HashGenerator, ConfigurationAdmin)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
buildAtrifactDescriptor(File, String)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
buildForManagedServiceFactoryConfiguration(File, String, Properties)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createArtifactDescriptor(File, Properties)
M: 0 C: 38
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
generateArtifactDescriptor(File)
M: 0 C: 42
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
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.kernel.artifact.properties;
13:
14: import static java.nio.charset.StandardCharsets.UTF_8;
15:
16: import java.io.File;
17: import java.io.FileInputStream;
18: import java.io.IOException;
19: import java.io.InputStreamReader;
20: import java.io.Reader;
21: import java.util.Properties;
22:
23: import org.eclipse.virgo.repository.ArtifactBridge;
24: import org.eclipse.virgo.repository.ArtifactDescriptor;
25: import org.eclipse.virgo.repository.ArtifactGenerationException;
26: import org.eclipse.virgo.repository.HashGenerator;
27: import org.eclipse.virgo.repository.builder.ArtifactDescriptorBuilder;
28: import org.eclipse.virgo.repository.builder.AttributeBuilder;
29: import org.eclipse.virgo.util.common.StringUtils;
30: import org.eclipse.virgo.util.io.IOUtils;
31: import org.osgi.framework.Constants;
32: import org.osgi.framework.Version;
33: import org.osgi.service.cm.ConfigurationAdmin;
34:
35: /**
36: * An {@link ArtifactBridge} that creates {@link ArtifactDescriptor ArtifactDescriptors} for .properties files.
37: * <p />
38: *
39: * <strong>Concurrent Semantics</strong><br />
40: *
41: * Thread-safe
42: *
43: */
44: public final class PropertiesBridge implements ArtifactBridge {
45:
46: static final String PROPERTIES_SUFFIX = ".properties";
47:
48: static final String ARTIFACT_TYPE = "configuration";
49:
50: private final HashGenerator hashGenerator;
51:
52: private final ConfigurationAdmin configAdmin;
53:
54: public PropertiesBridge(HashGenerator hashGenerator, ConfigurationAdmin configAdmin) {
55: this.hashGenerator = hashGenerator;
56: this.configAdmin = configAdmin;
57: }
58:
59: public ArtifactDescriptor generateArtifactDescriptor(File artifactFile) throws ArtifactGenerationException {
60:• if (artifactFile.getName().endsWith(PROPERTIES_SUFFIX)) {
61: Reader reader = null;
62: try {
63: reader = new InputStreamReader(new FileInputStream(artifactFile), UTF_8);
64: Properties properties = new Properties();
65: properties.load(reader);
66: return createArtifactDescriptor(artifactFile, properties);
67: } catch (IOException e) {
68: throw new ArtifactGenerationException("Failed processing properties file", ARTIFACT_TYPE, e);
69: } finally {
70: IOUtils.closeQuietly(reader);
71: }
72: }
73: return null;
74: }
75:
76: private ArtifactDescriptor createArtifactDescriptor(File propertiesFile, Properties properties) throws IOException {
77:
78: String name = properties.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
79:• if (StringUtils.hasText(name)) {
80: // this is a factory configuration - need to generate actual PID for a new configuration
81: return buildForManagedServiceFactoryConfiguration(propertiesFile, name, properties);
82: }
83:
84: name = properties.getProperty(Constants.SERVICE_PID);
85:• if (!StringUtils.hasText(name)) {
86: String fileName = propertiesFile.getName();
87: name = fileName.substring(0, fileName.length() - PROPERTIES_SUFFIX.length());
88: }
89:
90: return buildAtrifactDescriptor(propertiesFile, name).build();
91: }
92:
93: /**
94: * @param propertiesFile
95: * @param name
96: * @param properties
97: * @return
98: * @throws IOException
99: */
100: private ArtifactDescriptor buildForManagedServiceFactoryConfiguration(File propertiesFile, String factoryPid, Properties properties)
101: throws IOException {
102:
103: // generated service.pid - will use as a name for artifactId
104: String pid = configAdmin.createFactoryConfiguration(factoryPid, null).getPid();
105:
106: ArtifactDescriptorBuilder builder = buildAtrifactDescriptor(propertiesFile, pid);
107: builder.addAttribute(new AttributeBuilder().setName(ConfigurationAdmin.SERVICE_FACTORYPID).setValue(factoryPid).build());
108:
109: return builder.build();
110: }
111:
112: private ArtifactDescriptorBuilder buildAtrifactDescriptor(File propertiesFile, String name) {
113: ArtifactDescriptorBuilder artifactDescriptorBuilder = new ArtifactDescriptorBuilder();
114:
115: artifactDescriptorBuilder //
116: .setUri(propertiesFile.toURI()) //
117: .setType(ARTIFACT_TYPE) //
118: .setName(name) //
119: .setVersion(Version.emptyVersion);
120:
121: this.hashGenerator.generateHash(artifactDescriptorBuilder, propertiesFile);
122:
123: return artifactDescriptorBuilder;
124: }
125: }