Package: ServiceComponent

ServiceComponent

nameinstructionbranchcomplexitylinemethod
ServiceComponent()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
activate(Map)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
deactivate()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
modified(Map)
M: 32 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
parse(Map)
M: 91 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%
start(ServerConfiguration)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
stop()
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017 Red Hat Inc
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *******************************************************************************/
9: package org.eclipse.kura.broker.artemis.xml;
10:
11: import java.util.Arrays;
12: import java.util.HashSet;
13: import java.util.Map;
14: import java.util.Set;
15:
16: import org.eclipse.kura.broker.artemis.core.ServerConfiguration;
17: import org.eclipse.kura.broker.artemis.core.ServerManager;
18: import org.eclipse.kura.broker.artemis.core.UserAuthentication;
19: import org.eclipse.kura.configuration.ConfigurableComponent;
20: import org.slf4j.Logger;
21: import org.slf4j.LoggerFactory;
22:
23: public class ServiceComponent implements ConfigurableComponent {
24:
25: private static final Logger logger = LoggerFactory.getLogger(ServiceComponent.class);
26:
27: private ServerConfiguration configuration;
28: private ServerManager server;
29:
30: public void activate(final Map<String, Object> properties) throws Exception {
31: final ServerConfiguration cfg = parse(properties);
32:• if (cfg != null) {
33: start(cfg);
34: }
35: }
36:
37: public void modified(final Map<String, Object> properties) throws Exception {
38: final ServerConfiguration cfg = parse(properties);
39:
40:• if (this.configuration == cfg) {
41: logger.debug("Configuration identical .... skipping update");
42: return;
43: }
44:
45:• if (this.configuration != null && this.configuration.equals(cfg)) {
46: logger.debug("Configuration equal .... skipping update");
47: return;
48: }
49:
50: stop();
51:• if (cfg != null) {
52: start(cfg);
53: }
54: }
55:
56: public void deactivate() throws Exception {
57: stop();
58: }
59:
60: private void start(final ServerConfiguration configuration) throws Exception {
61: logger.info("Starting Artemis");
62:
63: this.server = new ServerManager(configuration);
64: this.server.start();
65:
66: this.configuration = configuration;
67: }
68:
69: private void stop() throws Exception {
70: logger.info("Stopping Artemis");
71:
72:• if (this.server != null) {
73: this.server.stop();
74: this.server = null;
75: }
76:
77: this.configuration = null;
78: }
79:
80: private ServerConfiguration parse(final Map<String, Object> properties) {
81:
82: // is enabled?
83:
84:• if (!Boolean.TRUE.equals(properties.get("enabled"))) {
85: return null;
86: }
87:
88: // parse broker XML
89:
90: final String brokerXml = (String) properties.get("brokerXml");
91:• if (brokerXml == null || brokerXml.isEmpty()) {
92: return null;
93: }
94:
95: // parse required protocols
96:
97: final Set<String> requiredProtocols = new HashSet<>();
98: {
99: final Object v = properties.get("requiredProtocols");
100:• if (v instanceof String[]) {
101: requiredProtocols.addAll(Arrays.asList((String[]) v));
102:• } else if (v instanceof String) {
103: final String vs = (String) v;
104: final String[] reqs = vs.split("\\s*,\\s*");
105: requiredProtocols.addAll(Arrays.asList(reqs));
106: }
107: }
108:
109: // create security configuration
110:
111: final UserAuthentication.Builder auth = new UserAuthentication.Builder();
112:
113: final String defaultUser = (String) properties.get("defaultUser");
114:• if (defaultUser != null) {
115: auth.defaultUser(defaultUser);
116: }
117:
118: auth.parseUsers((String) properties.get("users"));
119:
120: // create result
121:
122: final ServerConfiguration cfg = new ServerConfiguration();
123: cfg.setBrokerXml(brokerXml);
124: cfg.setRequiredProtocols(requiredProtocols);
125: cfg.setUserAuthentication(auth.build());
126: return cfg;
127: }
128:
129: }