Package: ConfigurationAdminDumpContributor

ConfigurationAdminDumpContributor

nameinstructionbranchcomplexitylinemethod
ConfigurationAdminDumpContributor(ConfigurationAdmin)
M: 12 C: 14
54%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
appendFooter(StringBuilder)
M: 6 C: 5
45%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
appendHeader(StringBuilder, String)
M: 6 C: 41
87%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
appendProperties(StringBuilder, Dictionary)
M: 6 C: 18
75%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
appendProperty(StringBuilder, Object, Object)
M: 6 C: 16
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
contribute(Dump)
M: 16 C: 51
76%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 12
86%
M: 0 C: 1
100%
getName()
M: 6 C: 2
25%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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 static org.eclipse.virgo.util.io.IOUtils.closeQuietly;
15:
16: import java.io.IOException;
17: import java.io.Writer;
18: import java.util.Dictionary;
19: import java.util.Enumeration;
20:
21: import org.osgi.service.cm.Configuration;
22: import org.osgi.service.cm.ConfigurationAdmin;
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import org.eclipse.virgo.medic.dump.Dump;
27: import org.eclipse.virgo.medic.dump.DumpContributionFailedException;
28: import org.eclipse.virgo.medic.dump.DumpContributor;
29:
30: public class ConfigurationAdminDumpContributor implements DumpContributor {
31:
32: private static final String PROPERTY_PATTERN = "%s:\t%s\n";
33:
34: private final Logger logger = LoggerFactory.getLogger(this.getClass());
35:
36: private final ConfigurationAdmin configurationAdmin;
37:
38: public ConfigurationAdminDumpContributor(ConfigurationAdmin configurationAdmin) {
39: this.configurationAdmin = configurationAdmin;
40: }
41:
42: public void contribute(Dump dump) throws DumpContributionFailedException {
43: StringBuilder sb = new StringBuilder();
44:
45: try {
46:• for (Configuration configuration : configurationAdmin.listConfigurations(null)) {
47: appendHeader(sb, configuration.getPid());
48: appendProperties(sb, configuration.getProperties());
49: appendFooter(sb);
50: }
51: } catch (Exception e) {
52: logger.warn("Could not enumerate existing configurations");
53: }
54:
55: Writer out = null;
56: try {
57: out = dump.createFileWriter("configurationAdmin.properties");
58: out.write(sb.toString());
59: } catch (IOException e) {
60: logger.warn("Could not write configurationAdmin dump");
61: } finally {
62: closeQuietly(out);
63: }
64: }
65:
66: public String getName() {
67: return "configurationAdmin";
68: }
69:
70: private void appendHeader(StringBuilder sb, String pid) {
71:• for (int i = 0; i < pid.length() + 4; i++) {
72: sb.append("#");
73: }
74: sb.append("\n# ").append(pid).append(" #\n");
75:• for (int i = 0; i < pid.length() + 4; i++) {
76: sb.append("#");
77: }
78: sb.append("\n\n");
79: }
80:
81: private void appendProperties(StringBuilder sb, Dictionary<String, Object> properties) {
82: Enumeration<String> keys = properties.keys();
83:• while (keys.hasMoreElements()) {
84: Object key = keys.nextElement();
85: appendProperty(sb, key, properties.get(key));
86: }
87: }
88:
89: private void appendProperty(StringBuilder sb, Object key, Object value) {
90: sb.append(String.format(PROPERTY_PATTERN, key, value));
91: }
92:
93: private void appendFooter(StringBuilder sb) {
94: sb.append("\n\n");
95: }
96:
97: }