Skip to content

Package: GuiceLocatorImpl

GuiceLocatorImpl

nameinstructionbranchcomplexitylinemethod
GuiceLocatorImpl()
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
GuiceLocatorImpl(String)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getComponent(Class)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getFactory(Class)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getService(Class)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getServices()
M: 40 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
lambda$new$0()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$new$1()
M: 6 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.util.ArrayList;
17: import java.util.List;
18: import java.util.Map;
19:
20: import org.eclipse.kapua.KapuaRuntimeException;
21: import org.eclipse.kapua.commons.core.ServiceModuleConfiguration;
22: import org.eclipse.kapua.commons.core.ServiceModuleProvider;
23: import org.eclipse.kapua.locator.KapuaLocator;
24: import org.eclipse.kapua.locator.KapuaLocatorErrorCodes;
25: import org.eclipse.kapua.model.KapuaObjectFactory;
26: import org.eclipse.kapua.service.KapuaService;
27:
28: import com.google.inject.Binding;
29: import com.google.inject.ConfigurationException;
30: import com.google.inject.Guice;
31: import com.google.inject.Injector;
32: import com.google.inject.Key;
33:
34: /**
35: * Kapua locator implementation bases on Guice framework
36: */
37: public class GuiceLocatorImpl extends KapuaLocator {
38:
39: private final Injector injector;
40:
41: public GuiceLocatorImpl() {
42: injector = Guice.createInjector(new KapuaModule());
43: ServiceModuleConfiguration.setConfigurationProvider(() -> {
44: return injector.getInstance(ServiceModuleProvider.class);
45: });
46: }
47:
48: public GuiceLocatorImpl(String resourceName) {
49: injector = Guice.createInjector(new KapuaModule(resourceName));
50: ServiceModuleConfiguration.setConfigurationProvider(() -> {
51: return injector.getInstance(ServiceModuleProvider.class);
52: });
53: }
54:
55: @Override
56: public <S extends KapuaService> S getService(Class<S> serviceClass) {
57: try {
58: return injector.getInstance(serviceClass);
59: } catch (ConfigurationException e) {
60: throw new KapuaRuntimeException(KapuaLocatorErrorCodes.SERVICE_UNAVAILABLE, e, serviceClass);
61: }
62: }
63:
64: @Override
65: public <F extends KapuaObjectFactory> F getFactory(Class<F> factoryClass) {
66: try {
67: return injector.getInstance(factoryClass);
68: } catch (ConfigurationException e) {
69: throw new KapuaRuntimeException(KapuaLocatorErrorCodes.FACTORY_UNAVAILABLE, factoryClass);
70: }
71: }
72:
73: public <T> T getComponent(Class<T> componentClass) {
74: try {
75: return injector.getInstance(componentClass);
76: } catch (ConfigurationException e) {
77: throw new KapuaRuntimeException(KapuaLocatorErrorCodes.COMPONENT_UNAVAILABLE, componentClass);
78: }
79: }
80:
81: @Override
82: public List<KapuaService> getServices() {
83: final List<KapuaService> servicesList = new ArrayList<>();
84: final Map<Key<?>, Binding<?>> bindings = injector.getBindings();
85:• for (Binding<?> binding : bindings.values()) {
86: final Class<?> clazz = binding.getKey().getTypeLiteral().getRawType();
87:• if (KapuaService.class.isAssignableFrom(clazz)) {
88: servicesList.add(injector.getInstance(clazz.asSubclass(KapuaService.class)));
89: }
90: }
91: return servicesList;
92: }
93:
94: }