Skip to content

Package: BeanManagerInstanceCreator

BeanManagerInstanceCreator

nameinstructionbranchcomplexitylinemethod
BeanManagerInstanceCreator(Object)
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
cleanupBean(BeanManagerInstanceCreator.CDIManagedBean)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
close()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getOrCreateComponent(Class)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$close$1(Class, BeanManagerInstanceCreator.CDIManagedBean)
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%
lambda$getOrCreateComponent$0(Class, Class)
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 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.io.IOException;
16: import java.util.concurrent.ConcurrentHashMap;
17: import java.util.concurrent.ConcurrentMap;
18:
19: import jakarta.enterprise.context.spi.CreationalContext;
20: import jakarta.enterprise.inject.spi.AnnotatedType;
21: import jakarta.enterprise.inject.spi.BeanManager;
22: import jakarta.enterprise.inject.spi.InjectionTarget;
23: import jakarta.json.bind.JsonbException;
24:
25: import org.eclipse.yasson.internal.JsonBinding;
26: import org.eclipse.yasson.internal.properties.MessageKeys;
27: import org.eclipse.yasson.internal.properties.Messages;
28: import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;
29:
30: /**
31: * CDI instance manager.
32: * Instances are created and stored per instance of {@link JsonBinding}.
33: * Calling close on JsonBinding, cleans up Jsonb CDI instances and in case of "dependant" scope its dependencies.
34: *
35: * CDI API dependency is optional, this class is never referenced / loaded if CDI API is not resolvable.
36: */
37: public class BeanManagerInstanceCreator implements JsonbComponentInstanceCreator {
38:
39: private final BeanManager beanManager;
40:
41: private final ConcurrentMap<Class<?>, CDIManagedBean<?>> injectionTargets = new ConcurrentHashMap<>();
42:
43: /**
44: * Creates a new instance.
45: *
46: * @param beanManager Bean manager.
47: */
48: public BeanManagerInstanceCreator(Object beanManager) {
49:• if (!(beanManager instanceof BeanManager)) {
50: throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR,
51: "beanManager instance should be of type '" + BeanManager.class + "'"));
52: }
53: this.beanManager = (BeanManager) beanManager;
54: }
55:
56: /**
57: * Creates an instance of the CDI managed bean.
58: * Calls CDI API to inject into the bean.
59: *
60: * @param componentClass bean class to be instantiated.
61: * @return New instance of bean class with injected content.
62: */
63: @Override
64: @SuppressWarnings("unchecked")
65: public <T> T getOrCreateComponent(Class<T> componentClass) {
66: return (T) injectionTargets.computeIfAbsent(componentClass, clazz -> {
67: final AnnotatedType<T> aType = beanManager.createAnnotatedType(componentClass);
68: final InjectionTarget<T> injectionTarget = beanManager.getInjectionTargetFactory(aType)
69: .createInjectionTarget(null);
70: CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
71: final T beanInstance = injectionTarget.produce(creationalContext);
72: injectionTarget.inject(beanInstance, creationalContext);
73: injectionTarget.postConstruct(beanInstance);
74: return new CDIManagedBean(beanInstance, injectionTarget, creationalContext);
75: }).getInstance();
76: }
77:
78: @Override
79: public void close() throws IOException {
80: injectionTargets.forEach((clazz, target) -> cleanupBean(target));
81: injectionTargets.clear();
82: }
83:
84: private <T> void cleanupBean(CDIManagedBean<T> bean) {
85: bean.getInjectionTarget().preDestroy(bean.getInstance());
86: bean.getInjectionTarget().dispose(bean.getInstance());
87: bean.getCreationalContext().release();
88: }
89:
90: /**
91: * Holder for bean instance and its injection target.
92: */
93: private static final class CDIManagedBean<T> {
94: private final T instance;
95: private final InjectionTarget<T> injectionTarget;
96: private final CreationalContext<T> creationalContext;
97:
98: CDIManagedBean(T instance, InjectionTarget<T> injectionTarget, CreationalContext<T> creationalContext) {
99: this.instance = instance;
100: this.injectionTarget = injectionTarget;
101: this.creationalContext = creationalContext;
102: }
103:
104: /**
105: * @return CDI InjectionTarget
106: */
107: private InjectionTarget<T> getInjectionTarget() {
108: return injectionTarget;
109: }
110:
111: /**
112: * @return managed instance of a bean
113: */
114: private T getInstance() {
115: return instance;
116: }
117:
118: /**
119: * @return creational context
120: */
121: private CreationalContext<T> getCreationalContext() {
122: return creationalContext;
123: }
124: }
125: }