/
WeldProvider.java
114 lines (97 loc) · 4.35 KB
/
WeldProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.weld;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.enterprise.inject.spi.CDIProvider;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.as.weld.deployment.WeldDeployment;
import org.jboss.as.weld.logging.WeldLogger;
import org.jboss.as.weld.services.ModuleGroupSingletonProvider;
import org.jboss.as.weld.util.Reflections;
import org.jboss.weld.AbstractCDI;
import org.jboss.weld.Container;
import org.jboss.weld.ContainerState;
import org.jboss.weld.bean.builtin.BeanManagerProxy;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.logging.BeanManagerLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.wildfly.security.manager.WildFlySecurityManager;
/**
* Service provider for {@link CDI}.
*
* @author Jozef Hartinger
*
*/
public class WeldProvider implements CDIProvider {
private static final ConcurrentMap<Container, CdiImpl> containers = new ConcurrentHashMap<>();
static void containerInitialized(Container container, BeanManagerImpl rootBeanManager, WeldDeployment deployment) {
containers.put(container, new WeldProvider.CdiImpl(container, new BeanManagerProxy(rootBeanManager), deployment));
}
static void containerShutDown(Container container) {
containers.remove(container);
}
@Override
public CDI<Object> getCDI() {
if (ModuleGroupSingletonProvider.deploymentClassLoaders.isEmpty()) {
throw WeldLogger.ROOT_LOGGER.weldNotInitialized();
}
final Container container = Container.instance();
checkContainerState(container);
return containers.get(container);
}
private static void checkContainerState(Container container) {
ContainerState state = container.getState();
if (state.equals(ContainerState.STOPPED) || state.equals(ContainerState.SHUTDOWN)) {
throw BeanManagerLogger.LOG.beanManagerNotAvailable();
}
}
private static class CdiImpl extends AbstractCDI<Object> {
private final Container container;
private final BeanManagerProxy rootBeanManager;
private final WeldDeployment deployment;
public CdiImpl(Container container, BeanManagerProxy rootBeanManager, WeldDeployment deployment) {
this.container = container;
this.rootBeanManager = rootBeanManager;
this.deployment = deployment;
}
@Override
public BeanManager getBeanManager() {
checkContainerState(container);
final String callerName = getCallingClassName();
if(callerName.startsWith("org.glassfish.soteria")) {
//the Jakarta EE Security RI uses CDI.current() to perform bean lookup, however
//as it is part of the container its bean archive does not have visibility to deployment beans
//we use this code path to enable it to get the bean manager of the current module
//so it can look up the deployment beans it needs to work
try {
BeanManager bm = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
if(bm != null) {
return bm;
}
} catch (NamingException e) {
//ignore
}
}
final ClassLoader tccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
final Class<?> callerClass = Reflections.loadClass(callerName, tccl);
if (callerClass != null) {
final BeanDeploymentArchive bda = deployment.getBeanDeploymentArchive(callerClass);
if (bda != null) {
return new BeanManagerProxy(container.beanDeploymentArchives().get(bda));
}
}
// fallback for cases when we are unable to load the class or no BeanManager exists yet for the given BDA
return rootBeanManager;
}
@Override
public String toString() {
return "Weld instance for deployment " + BeanManagerProxy.unwrap(rootBeanManager).getContextId();
}
}
}