Package: CommandLinePropertiesSource

CommandLinePropertiesSource

nameinstructionbranchcomplexitylinemethod
CommandLinePropertiesSource(BundleContext, EventLogger)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
appendProperty(String, String, Properties)
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
formatArgumentList(List)
M: 43 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getConfigurationProperties()
M: 72 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
getProperties(String, Map)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
processCommand(String, List, Map)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
processPlanCommand(List, Map)
M: 77 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

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.commandline;
13:
14: import java.util.ArrayList;
15: import java.util.HashMap;
16: import java.util.List;
17: import java.util.Map;
18: import java.util.Properties;
19:
20: import org.osgi.framework.BundleContext;
21:
22:
23: import org.eclipse.virgo.medic.eventlog.EventLogger;
24: import org.eclipse.virgo.nano.config.internal.PropertiesSource;
25: import org.eclipse.virgo.nano.diagnostics.KernelLogEvents;
26:
27: public final class CommandLinePropertiesSource implements PropertiesSource {
28:         
29:         private static final String PROPERTY_USERREGION_COMMANDLINE_ARTIFACTS = "commandLineArtifacts";
30:
31:         private static final String PROPERTY_UNRECOGNIZED_LAUNCHER_ARGUMENTS = "eclipse.commands";
32:         
33:         private static final String COMMAND_PREFIX = "-";
34:         
35:         private static final String COMMAND_PLAN = "plan";
36:         
37:         private static final String PID_KERNEL_REGION = "org.eclipse.virgo.kernel.userregion";
38:         
39:         private static final String TEMPLATE_VERSIONED_PLAN_REPOSITORY_URI = "repository:plan/%s/%s";
40:         
41:         private static final String TEMPLATE_UNVERSIONED_PLAN_REPOSITORY_URI = "repository:plan/%s";
42:         
43:         private final String unrecognizedArguments;
44:         
45:         private final EventLogger eventLogger;
46:         
47:         public CommandLinePropertiesSource(BundleContext bundleContext, EventLogger eventLogger) {
48:                 this.unrecognizedArguments = bundleContext.getProperty(PROPERTY_UNRECOGNIZED_LAUNCHER_ARGUMENTS);
49:                 this.eventLogger = eventLogger;
50:         }
51:
52:         public Map<String, Properties> getConfigurationProperties() {
53:                 
54:                 Map<String, Properties> configuration = new HashMap<String, Properties>();
55:                 
56:•                if (this.unrecognizedArguments != null) {
57:                         String[] components = this.unrecognizedArguments.split("\n");
58:                         
59:                         List<String> arguments = null;
60:                         String command = null;
61:                         
62:•                        for (int i = 0; i < components.length; i++) {
63:•                                if (components[i].startsWith(COMMAND_PREFIX)) {
64:•                                        if (command != null) {
65:                                                 processCommand(command, arguments, configuration);
66:                                         }                                        
67:                                         command = components[i].substring(COMMAND_PREFIX.length());
68:                                         arguments = new ArrayList<String>();                                        
69:•                                } else if (arguments != null) {
70:                                         arguments.add(components[i]);
71:                                 }
72:                         }
73:                         
74:•                        if (command != null) {
75:                                 processCommand(command, arguments, configuration);
76:                         }
77:                 }
78:                 
79:                 return configuration;
80:         }
81:
82:         private void processCommand(String command, List<String> arguments, Map<String, Properties> configuration) {
83:•                if (COMMAND_PLAN.equals(command)) {
84:                         processPlanCommand(arguments, configuration);
85:                 }
86:         }
87:         
88:         private void processPlanCommand(List<String> arguments, Map<String, Properties> configuration) {
89:                 String repositoryUri = null;
90:                 
91:•                if (arguments.size() == 1) {
92:                         repositoryUri = String.format(TEMPLATE_UNVERSIONED_PLAN_REPOSITORY_URI, arguments.get(0));
93:•                } else if (arguments.size() == 2) {
94:                         repositoryUri = String.format(TEMPLATE_VERSIONED_PLAN_REPOSITORY_URI, arguments.get(0), arguments.get(1));
95:                 } else {
96:                         this.eventLogger.log(KernelLogEvents.KERNEL_PLAN_ARGUMENTS_INCORRECT, arguments.size(), formatArgumentList(arguments));                        
97:                 }
98:                 
99:•                if (repositoryUri != null) {
100:                         Properties properties = getProperties(PID_KERNEL_REGION, configuration);
101:                         appendProperty(PROPERTY_USERREGION_COMMANDLINE_ARTIFACTS, repositoryUri, properties);
102:                 }                
103:         }
104:         
105:         private String formatArgumentList(List<String> arguments) {
106:•                if (arguments.size() == 0) {
107:                         return "";
108:                 }
109:                 
110:                 StringBuilder argumentsBuilder = new StringBuilder();
111:                 
112:•                for (int i = 0; i < arguments.size(); i++) {
113:                         argumentsBuilder.append(arguments.get(i));
114:•                        if ((i + 1) < arguments.size()) {
115:                                 argumentsBuilder.append(", ");
116:                         }
117:                 }
118:                 
119:                 return argumentsBuilder.toString();
120:         }
121:         
122:         private Properties getProperties(String pid, Map<String, Properties> configuration) {
123:                 Properties properties = configuration.get(pid);
124:•                if (properties == null) {
125:                         properties = new Properties();
126:                         configuration.put(pid, properties);
127:                 }
128:                 return properties;
129:         }
130:         
131:         private void appendProperty(String key, String value, Properties properties) {
132:                 String property = properties.getProperty(key);
133:•                if (property != null) {
134:                         property = property + "," + value;
135:                 } else {
136:                         property = value;
137:                 }
138:                 properties.put(key, property);
139:         }
140:
141: }