private Collection<Object> createInterceptors() { Collection<InterceptorMetadata<?>> allInterceptors = this.mbMetaData.getAllInterceptors(); Collection<Object> interceptorInstances = new ArrayList<Object>(); for (InterceptorMetadata<?> interceptor : allInterceptors) { String interceptorClassName = interceptor.getInterceptorClass().getClassName(); try { Class<?> interceptorClass = Class.forName(interceptorClassName, false, this.managedBeanClass .getClassLoader()); Object interceptorInstance = this.createInstance(interceptorClass); interceptorInstances.add(interceptorInstance); } catch (ClassNotFoundException cnfe) { throw new RuntimeException("Could not load interceptor class: " + interceptorClassName, cnfe); } } return interceptorInstances; }
@Override public void addManagedBeans(ManagedBeanMetaData... managedBeans) { for (ManagedBeanMetaData managedBean : managedBeans) { this.managedBeans.put(managedBean.getName(), managedBean); } }
private Class<?> getManagedBeanClass(ClassLoader cl, ManagedBeanMetaData managedBean) throws DeploymentException { String klass = managedBean.getManagedBeanClass(); try { return cl.loadClass(klass); } catch (ClassNotFoundException cnfe) { throw new DeploymentException(cnfe); } }
@SuppressWarnings("unchecked") private void handlePostConstruct(ManagedBeanInstance<T> managedBeanInstance) throws Exception { List<InterceptorMetadata<?>> postConstructs = this.mbMetaData.getPostConstructs(); Collection<InterceptorInvocation<?>> interceptorInvocations = new ArrayList<InterceptorInvocation<?>>(postConstructs.size()); for (InterceptorMetadata<?> interceptor : postConstructs) { Object instance = null; if (interceptor.isTargetClass()) { instance = managedBeanInstance.getInstance(); } else { String interceptorClassName = interceptor.getInterceptorClass().getClassName(); instance = managedBeanInstance.getInterceptor(interceptorClassName); } // TODO: Interceptor invocation creation will be simplified in jboss-interceptors project. InterceptorInvocation<?> interceptorInvocation = new InterceptorInvocation(instance, interceptor, InterceptionType.POST_CONSTRUCT); interceptorInvocations.add(interceptorInvocation); } InterceptionChain interceptorChain = new SimpleInterceptionChain(interceptorInvocations, InterceptionType.POST_CONSTRUCT, managedBeanInstance.getInstance(), null); DefaultInvocationContextFactory invocationCtxFactory = new DefaultInvocationContextFactory(); InvocationContext invocationCtx = invocationCtxFactory.newInvocationContext(interceptorChain, managedBeanInstance.getInstance(), null, null); // invoke post-construct invocationCtx.proceed(); }
@SuppressWarnings("unchecked") @Override public Object invoke(ManagedBeanInstance<T> managedBeanInstance, Method method, Object[] args) throws Throwable { Collection<InterceptorMetadata<?>> aroundInvokeInterceptors = this.mbMetaData.getAroundInvokes(method); Collection<InterceptorInvocation<?>> interceptorInvocations = new ArrayList<InterceptorInvocation<?>>(aroundInvokeInterceptors.size()); for (InterceptorMetadata<?> aroundInvokeInterceptor : aroundInvokeInterceptors) { Object instance = null; if (aroundInvokeInterceptor.isTargetClass()) { instance = managedBeanInstance.getInstance(); } else { String interceptorClassName = aroundInvokeInterceptor.getInterceptorClass().getClassName(); instance = managedBeanInstance.getInterceptor(interceptorClassName); } // TODO: Interceptor invocation creation should be simplified in jboss-interceptors project. InterceptorInvocation<?> interceptorInvocation = new InterceptorInvocation(instance, aroundInvokeInterceptor, InterceptionType.AROUND_INVOKE); interceptorInvocations.add(interceptorInvocation); } InterceptionChain interceptorChain = new SimpleInterceptionChain(interceptorInvocations, InterceptionType.AROUND_INVOKE, managedBeanInstance.getInstance(), method); DefaultInvocationContextFactory invocationCtxFactory = new DefaultInvocationContextFactory(); InvocationContext invocationCtx = invocationCtxFactory.newInvocationContext(interceptorChain, managedBeanInstance.getInstance(), method, args); // invoke return invocationCtx.proceed(); }
public static String getManagedBeanManagerMCBeanName(JavaEEComponentInformer informer, DeploymentUnit deploymentUnit, ManagedBeanMetaData managedBean) { StringBuilder sb = new StringBuilder("org.jboss.managedbean:"); String applicationName = informer.getApplicationName(deploymentUnit); String moduleName = informer.getModuleName(deploymentUnit); if (applicationName != null) { sb.append("application="); sb.append(applicationName); } sb.append(",module="); sb.append(moduleName); sb.append(",name="); sb.append(managedBean.getName()); return sb.toString(); } }
private boolean isInterceptorToJavaEE6ManagedBean(final Class<?> klass, final ManagedBeanDeploymentMetaData managedBeanDeploymentMetaData) { if (managedBeanDeploymentMetaData == null) { return false; } Collection<ManagedBeanMetaData> managedBeans = managedBeanDeploymentMetaData.getManagedBeans(); if (managedBeans == null || managedBeans.isEmpty()) { return false; } for (ManagedBeanMetaData managedBean : managedBeans) { Collection<InterceptorMetadata<?>> interceptors = managedBean.getAllInterceptors(); if (interceptors == null || interceptors.isEmpty()) { continue; } for (InterceptorMetadata<?> interceptor : interceptors) { final String interceptorClassName = interceptor.getInterceptorClass().getClassName(); if (klass.getName().equals(interceptorClassName)) { return true; } } } return false; } }
private Class<?> getBeanClass(DeploymentUnit unit, ManagedBeanMetaData managedBean) throws ClassNotFoundException { ClassLoader cl = unit.getClassLoader(); return Class.forName(managedBean.getManagedBeanClass(), false, cl); }
/** * Returns the managed bean (if any) whose managed bean class name is equal to the passed <code>mbClassName</code> * @param unit The deployment unit within which the Managed Bean is scanned for. Note that, the child of parent * deployment units of the passed <code>unit</code> will <i>not</i> be looked into * @param mbClassName The fully qualified class name of the managed bean being searched * @return */ private ManagedBeanMetaData getManagedBean(DeploymentUnit unit, String mbClassName) { ManagedBeanDeploymentMetaData managedBeanDeploymentMetaData = unit.getAttachment(ManagedBeanDeploymentMetaData.class); if (managedBeanDeploymentMetaData == null) { return null; } Collection<ManagedBeanMetaData> managedBeans = managedBeanDeploymentMetaData.getManagedBeans(); if (managedBeans == null || managedBeans.isEmpty()) { return null; } for (ManagedBeanMetaData managedBean : managedBeans) { if (mbClassName.equals(managedBean.getManagedBeanClass())) { return managedBean; } } return null; }
throw new DeploymentException("Could not load managed bean class " + managedBean.getManagedBeanClass() + " in unit " + unit, cnfe);