Skip to content

Package: KapuaLocator

KapuaLocator

nameinstructionbranchcomplexitylinemethod
KapuaLocator()
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%
createInstance()
M: 55 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
getInstance()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
locatorClassName()
M: 25 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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;
15:
16: import java.util.ServiceLoader;
17:
18: import org.eclipse.kapua.KapuaRuntimeErrorCodes;
19: import org.eclipse.kapua.KapuaRuntimeException;
20: import org.slf4j.Logger;
21: import org.slf4j.LoggerFactory;
22:
23: /**
24: * Interface to load KapuaService instances in a given environment.<br>
25: * Implementations of the KapuaServiceLocator can decide whether to return local instances or to acts as a proxy to remote instances.<br>
26: * The locator is self initialized, it looks for the proper locator implementation class looking at {@link KapuaLocator#LOCATOR_CLASS_NAME_SYSTEM_PROPERTY} system property or falling back to the
27: * {@link KapuaLocator#LOCATOR_CLASS_NAME_ENVIRONMENT_PROPERTY} (if the previous property is not defined).
28: *
29: * @since 1.0
30: */
31: public abstract class KapuaLocator implements KapuaServiceLoader {
32:
33: private static final Logger logger = LoggerFactory.getLogger(KapuaLocator.class);
34:
35: private static final KapuaLocator INSTANCE = createInstance();
36:
37: /**
38: * {@link KapuaLocator} implementation classname specified via "System property" constants
39: */
40: public static final String LOCATOR_CLASS_NAME_SYSTEM_PROPERTY = "locator.class.impl";
41:
42: /**
43: * {@link KapuaLocator} implementation classname specified via "Environment property" constants
44: */
45: public static final String LOCATOR_CLASS_NAME_ENVIRONMENT_PROPERTY = "LOCATOR_CLASS_IMPL";
46:
47: // TODO do we need synchronization?
48:
49: /**
50: * Creates the {@link KapuaLocator} instance,
51: *
52: * @return
53: */
54: private static KapuaLocator createInstance() {
55: try {
56: logger.info("initializing Servicelocator instance... ");
57: String locatorImplementation = locatorClassName();
58:• if (locatorImplementation != null && !locatorImplementation.trim().isEmpty()) {
59: try {
60: return (KapuaLocator) Class.forName(locatorImplementation).newInstance();
61: } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
62: logger.info("An error occurred during Servicelocator initialization", e);
63: }
64: }
65:
66: // proceed with the default service locator instantiation if env variable is null or some error occurred during the specific service locator instantiation
67:
68: logger.info("initialize Servicelocator with the default instance... ");
69: ServiceLoader<KapuaLocator> serviceLocatorLoaders = ServiceLoader.load(KapuaLocator.class);
70:• for (KapuaLocator locator : serviceLocatorLoaders) {
71: // simply return the first
72: logger.info("initialize Servicelocator with the default instance... DONE");
73: return locator;
74: }
75: } catch (Exception e) {
76: logger.error("Error initializing locator...", e);
77: throw e;
78: }
79: // none returned
80:
81: throw new KapuaRuntimeException(KapuaRuntimeErrorCodes.SERVICE_LOCATOR_UNAVAILABLE);
82: }
83:
84: /**
85: * Return the {@link KapuaLocator} instance (singleton).
86: *
87: * @return
88: */
89: public static KapuaLocator getInstance() {
90: return INSTANCE;
91: }
92:
93: /**
94: * Get the locator classname implementation looking at the {@link KapuaLocator#LOCATOR_CLASS_NAME_SYSTEM_PROPERTY} system property or falling back to the
95: * {@link KapuaLocator#LOCATOR_CLASS_NAME_ENVIRONMENT_PROPERTY} environment variable.
96: *
97: * @return
98: */
99: static String locatorClassName() {
100: String locatorClass = System.getProperty(LOCATOR_CLASS_NAME_SYSTEM_PROPERTY);
101:• if (locatorClass != null && !locatorClass.isEmpty()) {
102: return locatorClass;
103: }
104:
105: locatorClass = System.getenv(LOCATOR_CLASS_NAME_ENVIRONMENT_PROPERTY);
106:• if (locatorClass != null && !locatorClass.isEmpty()) {
107: return locatorClass;
108: }
109:
110: logger.debug("No service locator class resolved. Falling back to default.");
111: return null;
112: }
113: }