Skip to content

Package: JsonbComponentInstanceCreatorFactory

JsonbComponentInstanceCreatorFactory

nameinstructionbranchcomplexitylinemethod
JsonbComponentInstanceCreatorFactory()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getBeanManager(JsonbComponentInstanceCreatorFactory.BeanManagerProvider)
M: 49 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getCdiBeanManager()
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%
getComponentInstanceCreator()
M: 23 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getJndiBeanManager()
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%
lambda$getCdiBeanManager$0()
M: 31 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
lambda$getCdiBeanManager$1()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
lambda$getJndiBeanManager$2()
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
lambda$getJndiBeanManager$3()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 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 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: package org.eclipse.yasson.internal.components;
14:
15: import java.lang.reflect.Constructor;
16: import java.lang.reflect.Method;
17: import java.security.AccessController;
18: import java.security.PrivilegedAction;
19: import java.util.logging.Level;
20: import java.util.logging.Logger;
21:
22: import jakarta.json.bind.JsonbException;
23:
24: import org.eclipse.yasson.internal.properties.MessageKeys;
25: import org.eclipse.yasson.internal.properties.Messages;
26: import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;
27:
28: /**
29: * Factory method for default Jsonb component instance creators.
30: */
31: public class JsonbComponentInstanceCreatorFactory {
32:
33: private static final Logger LOGGER = Logger.getLogger(JsonbComponentInstanceCreator.class.getName());
34:
35: private JsonbComponentInstanceCreatorFactory() {
36: throw new IllegalStateException("This class should never be instantiated");
37: }
38:
39: /**
40: * JNDI bean manager name.
41: */
42: public static final String BEAN_MANAGER_NAME = "java:comp/BeanManager";
43:
44: /**
45: * Initial context class.
46: */
47: public static final String INITIAL_CONTEXT_CLASS = "javax.naming.InitialContext";
48: private static final String CDI_SPI_CLASS = "jakarta.enterprise.inject.spi.CDI";
49:
50: /**
51: * First check a CDI provider, if available use those.
52: * Try to lookup in a JNDI if no provider is registered.
53: * If one of the above is found {@link BeanManagerInstanceCreator} is returned,
54: * or {@link DefaultConstructorCreator} otherwise.
55: *
56: * @return Component instance creator, either CDI or default constructor.
57: */
58: public static JsonbComponentInstanceCreator getComponentInstanceCreator() {
59: Object beanManager = getCdiBeanManager();
60:• if (beanManager == null) {
61: beanManager = getJndiBeanManager();
62: }
63:• if (beanManager == null) {
64: LOGGER.finest(Messages.getMessage(MessageKeys.BEAN_MANAGER_NOT_FOUND_USING_DEFAULT));
65: return new DefaultConstructorCreator();
66: }
67: return new BeanManagerInstanceCreator(beanManager);
68: }
69:
70: /**
71: * Get bean manager with CDI api.
72: *
73: * @return bean manager instance or null if CDI API dependency is not available.
74: */
75: private static Object getCdiBeanManager() {
76: return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
77: try {
78: return getBeanManager(() -> {
79: Class<?> cdiClass = Class.forName(CDI_SPI_CLASS);
80: Method current = cdiClass.getMethod("current");
81: Method getBeanManager = cdiClass.getMethod("getBeanManager");
82: Object cdiObject = current.invoke(cdiClass);
83:• if (cdiObject == null) {
84: return null;
85: }
86: return getBeanManager.invoke(cdiObject);
87: });
88: } catch (ClassNotFoundException e) {
89: LOGGER.finest(Messages.getMessage(MessageKeys.NO_CDI_API_PROVIDER, CDI_SPI_CLASS));
90: return null;
91: }
92: });
93: }
94:
95: /**
96: * Get bean manager from JNDI context.
97: *
98: * @return bean manager instance or null if javax.naming is not available.
99: */
100: private static Object getJndiBeanManager() {
101: return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
102: try {
103: return getBeanManager(() -> {
104: Class<?> initialContextClass = Class.forName(INITIAL_CONTEXT_CLASS);
105: Method lookupMethod = initialContextClass.getMethod("lookup", String.class);
106: Constructor<?> initialContextConstructor = initialContextClass.getConstructor();
107: Object initialContextObject = initialContextConstructor.newInstance();
108: return lookupMethod.invoke(initialContextObject, BEAN_MANAGER_NAME);
109: });
110: } catch (ClassNotFoundException e) {
111: LOGGER.finest(Messages.getMessage(MessageKeys.NO_JNDI_ENVIRONMENT, INITIAL_CONTEXT_CLASS));
112: return null;
113: }
114: });
115: }
116:
117: /**
118: * Handles common invocation exceptions for getting bean manager reflectively.
119: *
120: * @return bean manager instance or null if javax.naming is not available or insufficient permissions.
121: */
122: private static Object getBeanManager(BeanManagerProvider command) throws ClassNotFoundException {
123: try {
124: return command.provide();
125: } catch (NoSuchMethodException | InstantiationException e) {
126: throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, e.getMessage()), e);
127: } catch (IllegalAccessException e) {
128: //insufficient permissions for reflective invocation, don't fail in this case
129: LOGGER.warning(e.getMessage());
130: LOGGER.warning(Messages.getMessage(MessageKeys.ILLEGAL_ACCESS, "lookup CDI bean manager"));
131: return null;
132: } catch (ClassNotFoundException e) {
133: throw e;
134: } catch (ReflectiveOperationException e) {
135: //likely no CDI container is running or bean manager JNDI lookup fails.
136: LOGGER.log(Level.FINEST, Messages.getMessage(MessageKeys.NO_CDI_ENVIRONMENT), e);
137: return null;
138: }
139: }
140:
141: /**
142: * Provides CDI bean manager instance, declares all exceptions thrown with reflective calls.
143: */
144: private interface BeanManagerProvider {
145: Object provide() throws ReflectiveOperationException;
146: }
147: }