Skip to content

Package: LocatorConfig

LocatorConfig

nameinstructionbranchcomplexitylinemethod
LocatorConfig(URL, List, List)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
addAllStrings(List, Collection)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
fromURL(URL)
M: 77 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
getPackageNames()
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%
getProvidedInterfaceNames()
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%
getURL()
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.locator.guice;
15:
16: import java.net.URL;
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.Collections;
20: import java.util.List;
21:
22: import org.apache.commons.configuration.ConfigurationException;
23: import org.apache.commons.configuration.XMLConfiguration;
24: import org.eclipse.kapua.locator.KapuaLocatorErrorCodes;
25: import org.eclipse.kapua.locator.KapuaLocatorException;
26:
27: public class LocatorConfig {
28:
29: private static final String SERVICE_RESOURCE_INTERFACES = "provided.api";
30: private static final String SERVICE_RESOURCE_PACKAGES = "packages.package";
31:
32: private final URL url;
33: private final List<String> packageNames;
34: private final List<String> providedInterfaceNames;
35:
36: private LocatorConfig(final URL url, final List<String> packageNames, final List<String> providedInterfaceNames) {
37: this.url = url;
38: this.packageNames = packageNames;
39: this.providedInterfaceNames = providedInterfaceNames;
40: }
41:
42: public static LocatorConfig fromURL(final URL url) throws KapuaLocatorException {
43:
44:• if (url == null) {
45: throw new IllegalArgumentException("'url' must not be null");
46: }
47:
48: final List<String> packageNames = new ArrayList<>();
49: final List<String> providedInterfaceNames = new ArrayList<>();
50:
51: final XMLConfiguration xmlConfig;
52: try {
53: xmlConfig = new XMLConfiguration(url);
54: } catch (ConfigurationException e) {
55: throw new KapuaLocatorException(KapuaLocatorErrorCodes.INVALID_CONFIGURATION, e);
56: }
57:
58: Object props = xmlConfig.getProperty(SERVICE_RESOURCE_PACKAGES);
59:• if (props instanceof Collection<?>) {
60: addAllStrings(packageNames, (Collection<?>) props);
61: }
62:• if (props instanceof String) {
63: packageNames.add((String) props);
64: }
65:
66: props = xmlConfig.getProperty(SERVICE_RESOURCE_INTERFACES);
67:• if (props instanceof Collection<?>) {
68: addAllStrings(providedInterfaceNames, (Collection<?>) props);
69: }
70:• if (props instanceof String) {
71: providedInterfaceNames.add((String) props);
72: }
73:
74: return new LocatorConfig(url, Collections.unmodifiableList(packageNames), Collections.unmodifiableList(providedInterfaceNames));
75: }
76:
77: private static void addAllStrings(final List<String> list, Collection<?> other) {
78:• for (Object entry : other) {
79:• if (entry instanceof String) {
80: list.add((String) entry);
81: }
82: }
83: }
84:
85: public URL getURL() {
86: return url;
87: }
88:
89: public Collection<String> getPackageNames() {
90: return packageNames;
91: }
92:
93: public Collection<String> getProvidedInterfaceNames() {
94: return providedInterfaceNames;
95: }
96: }