Package: ConsoleConfigurationConvertor

ConsoleConfigurationConvertor

nameinstructionbranchcomplexitylinemethod
ConsoleConfigurationConvertor(BundleContext, ConfigurationAdmin)
M: 6 C: 14
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
checkPortAvailability(String, String, String)
M: 53 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
deleteConfiguration(String)
M: 23 C: 10
30%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
start()
M: 6 C: 28
82%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
stop()
M: 6 C: 10
63%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
updateConfiguration(String, String, String, String)
M: 71 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%

Coverage

1:
2: package org.eclipse.virgo.nano.config.internal;
3:
4: import java.io.IOException;
5: import java.net.BindException;
6: import java.net.ServerSocket;
7: import java.util.Dictionary;
8: import java.util.Hashtable;
9:
10: import org.osgi.framework.BundleContext;
11: import org.osgi.framework.Constants;
12: import org.osgi.framework.ServiceRegistration;
13: import org.osgi.service.cm.Configuration;
14: import org.osgi.service.cm.ConfigurationAdmin;
15: import org.osgi.service.cm.ConfigurationException;
16: import org.osgi.service.cm.ManagedService;
17: import org.slf4j.Logger;
18: import org.slf4j.LoggerFactory;
19:
20: /**
21: *
22: * This class reads the merged shell configuration and registers it separated in the @link(ConfigurationAdmin).
23: * <p />
24: *
25: * <strong>Concurrent Semantics</strong><br />
26: * Thread-safe.
27: */
28: public class ConsoleConfigurationConvertor {
29:
30: private final Logger logger = LoggerFactory.getLogger(this.getClass());
31:
32: private final BundleContext context;
33:
34: private final ConfigurationAdmin configAdmin;
35:
36: private ServiceRegistration<ManagedService> configuratorRegistration;
37:
38: private static final String CONSOLE_PID = "osgi.console";
39:
40: private static final String TELNET_PID = "osgi.console.telnet";
41:
42: private static final String SSH_PID = "osgi.console.ssh";
43:
44: private static final String TELNET_PORT = "telnet.port";
45:
46: private static final String TELNET_HOST = "telnet.host";
47:
48: private static final String TELNET_ENABLED = "telnet.enabled";
49:
50: private static final String SSH_PORT = "ssh.port";
51:
52: private static final String SSH_HOST = "ssh.host";
53:
54: private static final String SSH_ENABLED = "ssh.enabled";
55:
56: private static final String HOST = "host";
57:
58: private static final String PORT = "port";
59:
60: private static final String ENABLED = "enabled";
61:
62: private static final String TELNET_SERVICE = "telnet";
63:
64: private static final String SSH_SERVICE = "ssh";
65:
66: private static final Object monitor = new Object();
67:
68: ConsoleConfigurationConvertor(BundleContext context, ConfigurationAdmin configAdmin) {
69: this.context = context;
70: this.configAdmin = configAdmin;
71: }
72:
73: public void start() {
74: Dictionary<String, String> consoleProperties = new Hashtable<String, String>();
75: consoleProperties.put(Constants.SERVICE_PID, CONSOLE_PID);
76: synchronized (ConsoleConfigurationConvertor.monitor) {
77: this.configuratorRegistration = this.context.registerService(ManagedService.class, new ConsoleConfigurator(), consoleProperties);
78: }
79: }
80:
81: private void updateConfiguration(String pid, String host, String port, String enabled) {
82:         boolean isPortAvailable;
83:•         if (pid.contains(TELNET_SERVICE)) {
84:                 isPortAvailable = checkPortAvailability(port, enabled, TELNET_SERVICE);
85:         } else {
86:                 isPortAvailable = checkPortAvailability(port, enabled, SSH_SERVICE);
87:         }
88:         
89:•         if(!isPortAvailable) {
90:                 return;
91:         }
92:         
93: try {
94: Configuration configuration = this.configAdmin.getConfiguration(pid, null);
95: Dictionary<String, String> properties = new Hashtable<String, String>();
96: properties.put(HOST, host);
97: properties.put(PORT, port);
98: properties.put(ENABLED, enabled);
99: configuration.update(properties);
100: } catch (IOException e) {
101: String message = String.format("Unable to update configuration with pid '%s'", pid);
102: this.logger.error(message, e);
103: }
104: }
105:
106: public void stop() {
107:         deleteConfiguration(TELNET_PID);
108:         deleteConfiguration(SSH_PID);
109:         deleteConfiguration(CONSOLE_PID);
110: }
111:
112: private void deleteConfiguration(String pid) {
113: try {
114:                         Configuration configuration = configAdmin.getConfiguration(pid, null);
115:                         configuration.delete();
116:         } catch (IOException e) {
117:                         String message = String.format("Unable to delete configuration with pid: " + pid);
118: this.logger.error(message, e);
119:                 }
120: }
121:
122: private boolean checkPortAvailability(String portStr, String enabled, String service) {
123:•         if ("false".equalsIgnoreCase(enabled)) {
124:                 return true;
125:         }
126:         int port = Integer.parseInt(portStr);
127:         ServerSocket socket = null;
128:         try {
129:                 socket = new ServerSocket(port);
130:                 return true;
131:         } catch (BindException e) {
132:                 String message = "Port " + port + " already in use; " + service + " access to console will not be available";
133:                 this.logger.error(message, e);
134:         } catch (IOException e) {
135:                         // do nothing
136:                 } finally {
137:•                 if (socket != null) {
138:                         try {
139:                                         socket.close();
140:                                 } catch (IOException e) {
141:                                         // do nothing
142:                                 }
143:                 }
144:         }
145:         return false;
146: }
147:
148: class ConsoleConfigurator implements ManagedService {
149:
150: private Dictionary<String,String> properties;
151:
152: @SuppressWarnings("unchecked")
153:                 @Override
154: public void updated(Dictionary props) throws ConfigurationException {
155: if (props != null) {
156: this.properties = props;
157: this.properties.put(Constants.SERVICE_PID, CONSOLE_PID);
158: } else {
159: return;
160: }
161: synchronized (ConsoleConfigurationConvertor.monitor) {
162: ConsoleConfigurationConvertor.this.configuratorRegistration.setProperties(this.properties);
163: }
164:
165: String telnetHost = this.properties.get(TELNET_HOST);
166: String telnetPort = this.properties.get(TELNET_PORT);
167: String telnetEnabled = this.properties.get(TELNET_ENABLED);
168: updateConfiguration(TELNET_PID, telnetHost, telnetPort, telnetEnabled);
169:
170: String sshHost = this.properties.get(SSH_HOST);
171: String sshPort = this.properties.get(SSH_PORT);
172: String sshEnabled = this.properties.get(SSH_ENABLED);
173: updateConfiguration(SSH_PID, sshHost, sshPort, sshEnabled);
174: }
175: }
176: }