use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder in project Payara by payara.
the class ManagedBeanManagerImpl method createManagedBean.
/**
* @param desc can be null if JCDI enabled bundle.
* @param managedBeanClass
* @return
* @throws Exception
*/
@Override
public <T> T createManagedBean(ManagedBeanDescriptor desc, Class<T> managedBeanClass) throws Exception {
JCDIService jcdiService = habitat.getService(JCDIService.class);
BundleDescriptor bundleDescriptor = null;
if (desc == null) {
bundleDescriptor = getBundle();
} else {
bundleDescriptor = desc.getBundleDescriptor();
}
if (bundleDescriptor == null) {
throw new IllegalStateException("Class " + managedBeanClass + " is not a valid EE ManagedBean class");
}
T callerObject = null;
if ((jcdiService != null) && jcdiService.isJCDIEnabled(bundleDescriptor)) {
// Have 299 create, inject, and call PostConstruct on managed bean
JCDIService.JCDIInjectionContext<?> jcdiContext = jcdiService.createManagedObject(managedBeanClass, bundleDescriptor);
callerObject = (T) jcdiContext.getInstance();
// Need to keep track of context in order to destroy properly
Map<Object, JCDIService.JCDIInjectionContext<?>> bundleNonManagedObjs = jcdiManagedBeanInstanceMap.get(bundleDescriptor);
bundleNonManagedObjs.put(callerObject, jcdiContext);
} else {
if (desc == null) {
throw new IllegalArgumentException("CDI not enabled and no managed bean found for class: " + managedBeanClass.getName());
}
JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(null);
// This is the object passed back to the caller.
callerObject = (T) interceptorInvoker.getProxy();
Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
// Inject interceptor instances
for (int i = 0; i < interceptorInstances.length; i++) {
inject(interceptorInstances[i], desc);
}
interceptorInvoker.invokeAroundConstruct();
// This is the managed bean class instance
Object managedBean = interceptorInvoker.getTargetInstance();
inject(managedBean, desc);
interceptorInvoker.invokePostConstruct();
desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
}
return callerObject;
}
use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder in project Payara by payara.
the class ManagedBeanManagerImpl method loadManagedBeans.
@Override
public void loadManagedBeans(Application app) {
JCDIService jcdiService = habitat.getService(JCDIService.class);
for (BundleDescriptor bundle : app.getBundleDescriptors()) {
if (!bundleEligible(bundle)) {
continue;
}
boolean isCDIBundle = (jcdiService != null && jcdiService.isJCDIEnabled(bundle));
for (ManagedBeanDescriptor next : bundle.getManagedBeans()) {
try {
Set<String> interceptorClasses = next.getAllInterceptorClasses();
Class<?> targetClass = bundle.getClassLoader().loadClass(next.getBeanClassName());
InterceptorInfo interceptorInfo = new InterceptorInfo();
interceptorInfo.setTargetClass(targetClass);
interceptorInfo.setInterceptorClassNames(interceptorClasses);
interceptorInfo.setAroundConstructInterceptors(next.getAroundConstructCallbackInterceptors(targetClass, getConstructor(targetClass, isCDIBundle)));
interceptorInfo.setPostConstructInterceptors(next.getCallbackInterceptors(LifecycleCallbackDescriptor.CallbackType.POST_CONSTRUCT));
interceptorInfo.setPreDestroyInterceptors(next.getCallbackInterceptors(LifecycleCallbackDescriptor.CallbackType.PRE_DESTROY));
if (next.hasAroundInvokeMethod()) {
interceptorInfo.setHasTargetClassAroundInvoke(true);
}
Map<Method, List<? extends InterceptorDescriptor>> interceptorChains = new HashMap<>();
for (Method m : targetClass.getMethods()) {
interceptorChains.put(m, next.getAroundInvokeInterceptors(m));
}
interceptorInfo.setAroundInvokeInterceptorChains(interceptorChains);
// TODO can optimize this out for the non-JAXRS, non-application specified interceptor case
interceptorInfo.setSupportRuntimeDelegate(true);
JavaEEInterceptorBuilderFactory interceptorBuilderFactory = habitat.getService(JavaEEInterceptorBuilderFactory.class);
JavaEEInterceptorBuilder builder = interceptorBuilderFactory.createBuilder(interceptorInfo);
next.setInterceptorBuilder(builder);
compEnvManager.bindToComponentNamespace(next);
String jndiName = next.getGlobalJndiName();
ManagedBeanNamingProxy namingProxy = new ManagedBeanNamingProxy(next, habitat);
if (processType.isServer()) {
namingManager.publishObject(jndiName, namingProxy, true);
} else {
// Can't store them in server's global naming service so keep
// them in local map.
appClientManagedBeans.put(jndiName, namingProxy);
}
} catch (Exception e) {
throw new RuntimeException("Error binding ManagedBean " + next.getBeanClassName() + " with name = " + next.getName(), e);
}
}
jcdiManagedBeanInstanceMap.put(bundle, Collections.synchronizedMap(new HashMap<>()));
}
}
use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder in project Payara by payara.
the class ManagedBeanManagerImpl method createManagedBean.
/**
* @param desc can be null if JCDI enabled bundle.
* @param managedBeanClass
* @param invokePostConstruct
* @return
* @throws Exception
*/
@Override
public <T> T createManagedBean(ManagedBeanDescriptor desc, Class<T> managedBeanClass, boolean invokePostConstruct) throws Exception {
JCDIService jcdiService = habitat.getService(JCDIService.class);
BundleDescriptor bundleDescriptor = null;
if (desc == null) {
bundleDescriptor = getBundle();
} else {
bundleDescriptor = desc.getBundleDescriptor();
}
if (bundleDescriptor == null) {
throw new IllegalStateException("Class " + managedBeanClass + " is not a valid EE ManagedBean class");
}
T callerObject = null;
if ((jcdiService != null) && jcdiService.isJCDIEnabled(bundleDescriptor)) {
// Have 299 create, inject, and call PostConstruct (if desired) on managed bean
JCDIService.JCDIInjectionContext<?> jcdiContext = jcdiService.createManagedObject(managedBeanClass, bundleDescriptor, invokePostConstruct);
callerObject = (T) jcdiContext.getInstance();
// Need to keep track of context in order to destroy properly
Map<Object, JCDIService.JCDIInjectionContext<?>> bundleNonManagedObjs = jcdiManagedBeanInstanceMap.get(bundleDescriptor);
bundleNonManagedObjs.put(callerObject, jcdiContext);
} else {
if (desc == null) {
throw new IllegalArgumentException("CDI not enabled and no managed bean found for class: " + managedBeanClass.getName());
}
JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
// This is the managed bean class instance
T managedBean = managedBeanClass.newInstance();
InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(managedBean);
// This is the object passed back to the caller.
callerObject = (T) interceptorInvoker.getProxy();
Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
inject(managedBean, desc);
// Inject interceptor instances
for (int i = 0; i < interceptorInstances.length; i++) {
inject(interceptorInstances[i], desc);
}
interceptorInvoker.invokePostConstruct();
desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
}
return callerObject;
}
use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder in project Payara by payara.
the class ManagedBeanManagerImpl method registerRuntimeInterceptor.
/**
* Apply a runtime interceptor instance to all managed beans in the given module
* @param interceptorInstance
* @param bundle bundle descripto
*/
@Override
public void registerRuntimeInterceptor(Object interceptorInstance, BundleDescriptor bundle) {
for (ManagedBeanDescriptor next : bundle.getManagedBeans()) {
JavaEEInterceptorBuilder builder = (JavaEEInterceptorBuilder) next.getInterceptorBuilder();
builder.addRuntimeInterceptor(interceptorInstance);
}
}
use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder in project Payara by payara.
the class ACCJCDIServiceImpl method createEEManagedObject.
private <T> T createEEManagedObject(ManagedBeanDescriptor desc) throws Exception {
JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(null);
Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
// Inject interceptor instances
for (int i = 0; i < interceptorInstances.length; i++) {
injectionMgr.injectInstance(interceptorInstances[i], desc.getGlobalJndiName(), false);
}
interceptorInvoker.invokeAroundConstruct();
// This is the managed bean class instance
@SuppressWarnings("unchecked") T managedBean = (T) interceptorInvoker.getTargetInstance();
injectionMgr.injectInstance(managedBean, desc);
interceptorInvoker.invokePostConstruct();
desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
return managedBean;
}
Aggregations