JBoss Community

Adding interceptor at runtime / dynamically

created by Ales Justin in EJB Development - View the full discussion

I'm trying to add EJB' AOP intreceptor for Weld, in some deployer.

Should this work? Or when does EJB container assemble its AOP interceptors?

 

As I tried debugging, but the interceptor is never hit.

This is the test: https://github.com/alesj/core/compare/weld-848

 

public class WeldEjbLifecycleInterceptorDeployer extends WeldAwareMetadataDeployer<JBossMetaData>
{
   private Boolean done;
 
   public WeldEjbLifecycleInterceptorDeployer()
   {
      super(JBossMetaData.class, false);
      setStage(DeploymentStages.PRE_REAL);
   }
 
   @Override
   protected void internalDeploy(VFSDeploymentUnit unit, JBossMetaData jbmd, Collection<VirtualFile> wbXml) throws DeploymentException
   {
      if (done != null)
         return;
 
      try
      {
         AspectDefinition def = new AspectDefinition("weld-aspect", Scope.PER_INSTANCE, new GenericAspectFactory(WeldLifecycleInterceptor.class.getName(), null));
         AdviceFactory advice = new AdviceFactory(def, "invoke");
         PointcutExpression pointcut = new PointcutExpression("weld-pointcut", "execution(* @" + PostActivate.class.getName() + "->*(..)) OR execution(* @" + PrePassivate.class.getName() + "->*(..))");
         InterceptorFactory[] interceptors = {advice};
         AdviceBinding binding = new AdviceBinding("weld-binding", pointcut, null, null, interceptors);
 
         AspectManager manager = AspectManager.getTopLevelAspectManager();
         DomainDefinition dd = manager.getContainer("Intercepted Bean");
         AspectManager domain = dd.getManager();
         domain.addAspectDefinition(def);
         domain.addInterceptorFactory(advice.getName(), advice);
         domain.addPointcut(pointcut);
         domain.addBinding(binding);
 
         done = true;
      }
      catch (Exception e)
      {
         done = false;
         throw DeploymentException.rethrowAsDeploymentException("Error setting Weld lifecycle interceptor", e);
      }
   }
 
   public static class WeldLifecycleInterceptor extends SessionBeanInterceptor implements Interceptor
   {
      public String getName()
      {
         return WeldLifecycleInterceptor.class.getName();
      }
 
      public Object invoke(final Invocation invocation) throws Throwable
      {
         return aroundInvoke(new InvocationContext()
         {
            public Object getTarget()
            {
               return invocation.getTargetObject();
            }
 
            public Method getMethod()
            {
               return null;
            }
 
            public Object[] getParameters()
            {
               return new Object[0];
            }
 
            public void setParameters(Object[] params)
            {
            }
 
            public Map<String, Object> getContextData()
            {
               return Collections.emptyMap();
            }
 
            public Object getTimer()
            {
               return null;
            }
 
            public Object proceed() throws Exception
            {
               try
               {
                  return invocation.invokeNext();
               }
               catch (Throwable t)
               {
                  throw new Exception(t);
               }
            }
         });
      }
   }
}
 

Reply to this message by going to Community

Start a new discussion in EJB Development at Community