[jboss-dev-forums] [Design the new POJO MicroContainer] - Re: Annotation Tests in deployers-vfs

alesj do-not-reply at jboss.com
Fri May 30 10:24:19 EDT 2008


This is what I came up with:

  | public abstract class AbstractAnnotationDeployer<D, C> extends AbstractComponentDeployer<D, C>
  | {
  |    /** The annotation processors */
  |    private AnnotationProcessor<?, C>[] processors;
  | 
  |    public AbstractAnnotationDeployer(AnnotationProcessor<?, C>... processors)
  |    {
  |       super();
  |       if (processors != null && processors.length > 0)
  |       {
  |          addInput(AnnotationEnvironment.class);
  |          this.processors = processors;
  |          for (AnnotationProcessor processor : processors)
  |          {
  |             addInput(processor.getAnnotation());
  |          }
  |       }
  |    }
  | 
  |    public void internalDeploy(DeploymentUnit unit) throws DeploymentException
  |    {
  |       super.internalDeploy(unit);
  | 
  |       if (processors != null && compVisitor != null)
  |       {
  |          try
  |          {
  |             AnnotationEnvironment env = unit.getAttachment(AnnotationEnvironment.class);
  |             for (AnnotationProcessor<?, C> processor : processors)
  |             {
  |                List<C> components = new ArrayList<C>();
  | 
  |                if (env != null)
  |                {
  |                   // from classes
  |                   Set<Class<?>> classes = env.classIsAnnotatedWith(processor.getAnnotation());
  |                   for (Class clazz : classes)
  |                   {
  |                      C component = processor.createComponent(clazz);
  |                      if (component != null)
  |                         components.add(component);
  |                   }
  |                }
  | 
  |                // from attachments
  |                Annotation annotation = unit.getAttachment(processor.getAnnotation());
  |                if (annotation != null)
  |                {
  |                   C component = processor.createComponent(annotation);
  |                   if (component != null)
  |                      components.add(component);
  |                }
  | 
  |                int i = 0;
  |                try
  |                {
  |                   for (i = 0; i < components.size(); i++)
  |                   {
  |                      deployComponent(unit, processor, components.get(i));
  |                   }
  |                }
  |                catch (Throwable t)
  |                {
  |                   for (; i >= 0; i--)
  |                   {
  |                      try
  |                      {
  |                         compVisitor.undeploy(unit, components.get(i));
  |                      }
  |                      catch (Throwable ignored)
  |                      {
  |                      }
  |                   }
  |                   throw DeploymentException.rethrowAsDeploymentException("Error deploying: " + unit.getName(), t);
  |                }
  |             }
  |          }
  |          catch (Throwable t)
  |          {
  |             super.internalUndeploy(unit);
  |             throw DeploymentException.rethrowAsDeploymentException("Error deploying: " + unit.getName(), t);
  |          }
  |       }
  |    }
  | 
  |    protected void deployComponent(DeploymentUnit unit, AnnotationProcessor<?, C> processor, C component) throws DeploymentException
  |    {
  |       String name = processor.getComponentName(component);
  |       DeploymentUnit componentUnit = unit.getComponent(name);
  |       C previous = null;
  |       if (componentUnit != null)
  |          previous = componentUnit.getAttachment(getOutput());
  | 
  |       if (previous != null)
  |       {
  |          processor.merge(previous, component);
  |       }
  |       else if (processor.isValidComponent(component))
  |       {
  |          compVisitor.deploy(unit, component);
  |       }
  |    }
  | 
  |    public void internalUndeploy(DeploymentUnit unit)
  |    {
  |       if (processors != null && compVisitor != null)
  |       {
  |          try
  |          {
  |             AnnotationEnvironment env = unit.getAttachment(AnnotationEnvironment.class);
  |             for (AnnotationProcessor<?, C> processor : processors)
  |             {
  |                List<C> components = new ArrayList<C>();
  | 
  |                if (env != null)
  |                {
  |                   // from classes
  |                   Set<Class<?>> classes = env.classIsAnnotatedWith(processor.getAnnotation());
  |                   for (Class clazz : classes)
  |                   {
  |                      C component = processor.createUndeployableComponent(clazz);
  |                      if (component != null)
  |                         components.add(component);
  |                   }
  |                }
  | 
  |                // from attachments
  |                Annotation annotation = unit.getAttachment(processor.getAnnotation());
  |                if (annotation != null)
  |                {
  |                   C component = processor.createUndeployableComponent(annotation);
  |                   if (component != null)
  |                      components.add(component);
  |                }
  | 
  |                for (int i = components.size() - 1; i >= 0; i--)
  |                {
  |                   try
  |                   {
  |                      compVisitor.undeploy(unit, components.get(i));
  |                   }
  |                   catch (Throwable ignored)
  |                   {
  |                      log.warn("Error during undeploy: " + unit.getName(), ignored);
  |                   }
  |                }
  |             }
  |          }
  |          catch (Throwable ignored)
  |          {
  |             log.warn("Error during undeploy: " + unit.getName(), ignored);
  |          }
  |       }
  | 
  |       super.internalUndeploy(unit);
  |    }
  | }
  | 

The two issues that I see here are
 - I'm not able to differ between previous (merged) component while undeploying
 - I have to re-create component for undeploy (that's why I use that createUndeployableComponent method)

Does this somehow fit into the picture you have/had?


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4154666#4154666

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4154666



More information about the jboss-dev-forums mailing list