[jboss-cvs] JBossAS SVN: r68079 - in projects/microcontainer/trunk/kernel/src: main/org/jboss/kernel/plugins/annotations and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Dec 9 09:06:09 EST 2007


Author: alesj
Date: 2007-12-09 09:06:08 -0500 (Sun, 09 Dec 2007)
New Revision: 68079

Added:
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Cleanup.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/CleanupOnly.java
Modified:
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Aliases.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AbstractAnnotationPlugin.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AliasesAnnotationPlugin.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AnnotationPlugin.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BasicBeanAnnotationAdapter.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BeanAnnotationAdapter.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/DescribeAction.java
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasAnnotationTestCase.java
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasTestCase.java
Log:
Unwind the annotation apply if neccessary.
Introducing cleanup on the BeanAnnotationAdapter/Plugin.
Using meta annotations to describe the actual IoC annotations to do the cleanup.

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Aliases.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Aliases.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Aliases.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -34,6 +34,7 @@
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE})
+ at Cleanup
 public @interface Aliases
 {
    /**

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Cleanup.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Cleanup.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/Cleanup.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -0,0 +1,38 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.beans.metadata.api.annotations;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * Marks that an annotation is cleaned up while uninstalling.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at Target(ElementType.ANNOTATION_TYPE)
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface Cleanup
+{
+}

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/CleanupOnly.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/CleanupOnly.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/api/annotations/CleanupOnly.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -0,0 +1,38 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.beans.metadata.api.annotations;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * Marks that an annotation is only used while uninstalling.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at Target(ElementType.ANNOTATION_TYPE)
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface CleanupOnly
+{
+}

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AbstractAnnotationPlugin.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AbstractAnnotationPlugin.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AbstractAnnotationPlugin.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -33,6 +33,8 @@
 import org.jboss.beans.metadata.spi.BeanMetaData;
 import org.jboss.beans.metadata.spi.MetaDataVisitor;
 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
+import org.jboss.beans.metadata.api.annotations.Cleanup;
+import org.jboss.beans.metadata.api.annotations.CleanupOnly;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
 import org.jboss.metadata.spi.MetaData;
 import org.jboss.reflect.spi.AnnotatedInfo;
@@ -50,6 +52,8 @@
 {
    private Class<C> annotation;
    private Set<ElementType> types;
+   private boolean isCleanup;
+   private boolean isCleanupOnly;
 
    protected AbstractAnnotationPlugin(Class<C> annotation)
    {
@@ -71,6 +75,8 @@
                log.warn("Unsupported type " + type + " on annotation " + annotation);
          }
       }
+      isCleanupOnly = annotation.isAnnotationPresent(CleanupOnly.class);
+      isCleanup = isCleanupOnly || annotation.isAnnotationPresent(Cleanup.class);
    }
 
    /**
@@ -177,37 +183,82 @@
    {
       boolean trace = log.isTraceEnabled();
       
-      Class<C> annotationClass = getAnnotation();
-      C annotation = retrieval.getAnnotation(annotationClass);
-      if (annotation == null)
+      if (isCleanupOnly == false)
       {
+         Class<C> annotationClass = getAnnotation();
+         C annotation = retrieval.getAnnotation(annotationClass);
+         if (annotation == null)
+         {
+            if (trace)
+               log.trace("No annotation: " + annotationClass.getName());
+            return;
+         }
+         if (isMetaDataAlreadyPresent(info, annotation, visitor.getControllerContext()))
+         {
+            if (trace)
+               log.trace("MetaDataAlreadyPresent, ignoring " + annotation);
+            return;
+         }
          if (trace)
-            log.trace("No annotation: " + annotationClass.getName());
-         return;
+            log.trace("Applying annotation: " + annotation);
+         List<? extends MetaDataVisitorNode> nodes = internalApplyAnnotation(info, retrieval, annotation, visitor.getControllerContext());
+         if (nodes != null && nodes.isEmpty() == false)
+         {
+            for(MetaDataVisitorNode node : nodes)
+            {
+               node.initialVisit(visitor);
+               node.describeVisit(visitor);
+            }
+         }
       }
-      if (isMetaDataAlreadyPresent(info, annotation, visitor.getControllerContext()))
+      else if (trace)
+         log.trace("Annotation " + annotation + " is @CleanupOnly, nothing to apply on install.");
+   }
+
+   public void cleanAnnotation(T info, MetaData retrieval, MetaDataVisitor visitor) throws Throwable
+   {
+      boolean trace = log.isTraceEnabled();
+
+      if (isCleanup)
       {
-         if (trace)
-            log.trace("MetaDataAlreadyPresent, ignoring " + annotation);
-         return;
-      }
-      if (trace)
-         log.trace("Applying annotation: " + annotation);
-      List<? extends MetaDataVisitorNode> nodes = internalApplyAnnotation(info, retrieval, annotation, visitor.getControllerContext());
-      if (nodes != null && nodes.isEmpty() == false)
-      {
-         for(MetaDataVisitorNode node : nodes)
+         Class<C> annotationClass = getAnnotation();
+         C annotation = retrieval.getAnnotation(annotationClass);
+         if (annotation == null)
          {
-            node.initialVisit(visitor);
-            node.describeVisit(visitor);
+            if (trace)
+               log.trace("No annotation: " + annotationClass.getName());
          }
+         else
+         {
+            if (trace)
+               log.trace("Cleaning annotation: " + annotation);
+            internalCleanAnnotation(info, retrieval, annotation, visitor.getControllerContext());
+         }
       }
+      else if (trace)
+         log.trace("Annotation " + annotation + " is not a @Cleanup annotation.");
    }
 
+   /**
+    * Clean annotation's actions.
+    *
+    * @param info the info
+    * @param retrieval the metadata
+    * @param annotation the annotation
+    * @param context the context
+    * @throws Throwable for any error
+    */
+   protected void internalCleanAnnotation(T info, MetaData retrieval, C annotation, KernelControllerContext context) throws Throwable
+   {
+      // empty      
+   }
+
    protected void toString(JBossStringBuilder buffer)
    {
       buffer.append("@annotation=").append(annotation);
       buffer.append(" ,types=").append(types);
+      buffer.append(" ,cleanup=").append(isCleanup);
+      buffer.append(" ,cleanupOnly=").append(isCleanupOnly);
    }
 
    public void toShortString(JBossStringBuilder buffer)

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AliasesAnnotationPlugin.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AliasesAnnotationPlugin.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AliasesAnnotationPlugin.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -32,6 +32,7 @@
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
 import org.jboss.reflect.spi.ClassInfo;
 import org.jboss.util.StringPropertyReplacer;
+import org.jboss.metadata.spi.MetaData;
 
 /**
  * Aliases annotation plugin.
@@ -61,7 +62,6 @@
             // impl detail (_Alias)
             if (controller.getContext(alias + "_Alias", null) == null)
             {
-               // TODO!! impl remove
                controller.addAlias(alias, beanMetaData.getName());
             }
             else
@@ -75,4 +75,26 @@
       // no metadata added
       return null;
    }
+
+   protected void internalCleanAnnotation(ClassInfo info, MetaData retrieval, Aliases annotation, KernelControllerContext context) throws Throwable
+   {
+      BeanMetaData beanMetaData = context.getBeanMetaData();
+      Set<Object> aliases = beanMetaData.getAliases();
+      Controller controller = context.getController();
+      for(String alias : annotation.value())
+      {
+         // check for ${property}
+         if (annotation.replace())
+            alias = StringPropertyReplacer.replaceProperties(alias);
+
+         if (aliases == null || aliases.contains(alias) == false)
+         {
+            // impl detail (_Alias)
+            if (controller.getContext(alias + "_Alias", null) != null)
+            {
+               controller.removeAlias(alias);
+            }
+         }         
+      }
+   }
 }

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AnnotationPlugin.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AnnotationPlugin.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/AnnotationPlugin.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -61,4 +61,14 @@
     * @throws Throwable for any error
     */
    void applyAnnotation(T info, MetaData retrieval, MetaDataVisitor visitor) throws Throwable;
+
+   /**
+    * Check if the annotation requires cleaning after itself.
+    *
+    * @param info the info
+    * @param retrieval metadata instance
+    * @param visitor current context visitor
+    * @throws Throwable for any error
+    */
+   void cleanAnnotation(T info, MetaData retrieval, MetaDataVisitor visitor) throws Throwable;
 }

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BasicBeanAnnotationAdapter.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BasicBeanAnnotationAdapter.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BasicBeanAnnotationAdapter.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -158,6 +158,23 @@
 
    public void applyAnnotations(MetaDataVisitor visitor) throws Throwable
    {
+      handleAnnotations(visitor, true);
+   }
+
+   public void cleanAnnotations(MetaDataVisitor visitor) throws Throwable
+   {
+      handleAnnotations(visitor, false);
+   }
+
+   /**
+    * Handle apply or cleanup of annotations.
+    *
+    * @param visitor the metadata visitor
+    * @param isApplyPhase is this apply phase
+    * @throws Throwable for any error
+    */
+   protected void handleAnnotations(MetaDataVisitor visitor, boolean isApplyPhase) throws Throwable
+   {
       KernelControllerContext context = visitor.getControllerContext();
       Kernel kernel = context.getKernel();
       KernelMetaDataRepository repository = kernel.getMetaDataRepository();
@@ -172,7 +189,12 @@
       // class
       ClassInfo classInfo = info.getClassInfo();
       for(AnnotationPlugin plugin : classAnnotationPlugins)
-         plugin.applyAnnotation(classInfo, retrieval, visitor);
+      {
+         if (isApplyPhase)
+            plugin.applyAnnotation(classInfo, retrieval, visitor);
+         else
+            plugin.cleanAnnotation(classInfo, retrieval, visitor);
+      }
 
       // constructors
       Set<ConstructorInfo> constructors = info.getConstructors();
@@ -185,7 +207,12 @@
             if (cmdr != null)
             {
                for(AnnotationPlugin plugin : constructorAnnotationPlugins)
-                  plugin.applyAnnotation(ci, cmdr, visitor);
+               {
+                  if (isApplyPhase)
+                     plugin.applyAnnotation(ci, cmdr, visitor);
+                  else
+                     plugin.cleanAnnotation(ci, cmdr, visitor);
+               }
             }
             else if (trace)
                log.trace("No annotations for " + ci);
@@ -210,7 +237,12 @@
                if (cmdr != null)
                {
                   for(AnnotationPlugin plugin : propertyAnnotationPlugins)
-                     plugin.applyAnnotation(pi, cmdr, visitor);
+                  {
+                     if (isApplyPhase)
+                        plugin.applyAnnotation(pi, cmdr, visitor);
+                     else
+                        plugin.cleanAnnotation(pi, cmdr, visitor);
+                  }
                }
                else if (trace)
                   log.trace("No annotations for property " + pi.getName());
@@ -233,7 +265,12 @@
                if (cmdr != null)
                {
                   for(AnnotationPlugin plugin : methodAnnotationPlugins)
-                     plugin.applyAnnotation(mi, cmdr, visitor);
+                  {
+                     if (isApplyPhase)
+                        plugin.applyAnnotation(mi, cmdr, visitor);
+                     else
+                        plugin.cleanAnnotation(mi, cmdr, visitor);
+                  }
                }
                else if (trace)
                   log.trace("No annotations for " + mi);
@@ -256,7 +293,12 @@
                if (cmdr != null)
                {
                   for(AnnotationPlugin plugin : methodAnnotationPlugins)
-                     plugin.applyAnnotation(smi, cmdr, visitor);
+                  {
+                     if (isApplyPhase)
+                        plugin.applyAnnotation(smi, cmdr, visitor);
+                     else
+                        plugin.cleanAnnotation(smi, cmdr, visitor);
+                  }
                }
                else if (trace)
                   log.trace("No annotations for " + smi);
@@ -277,7 +319,12 @@
             if (cmdr != null)
             {
                for(AnnotationPlugin plugin : fieldAnnotationPlugins)
-                  plugin.applyAnnotation(fi, cmdr, visitor);
+               {
+                  if (isApplyPhase)
+                     plugin.applyAnnotation(fi, cmdr, visitor);
+                  else
+                     plugin.cleanAnnotation(fi, cmdr, visitor);
+               }
             }
             else if (trace)
                log.trace("No annotations for field " + fi.getName());

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BeanAnnotationAdapter.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BeanAnnotationAdapter.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/BeanAnnotationAdapter.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -37,4 +37,12 @@
     * @throws Throwable for any error
     */
    void applyAnnotations(MetaDataVisitor visitor) throws Throwable;
+
+   /**
+    * Clean the annotations.
+    *
+    * @param visitor the visitor
+    * @throws Throwable for any error
+    */
+   void cleanAnnotations(MetaDataVisitor visitor) throws Throwable;
 }

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/DescribeAction.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/DescribeAction.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/DescribeAction.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -72,4 +72,22 @@
       }
    }
 
+   protected void uninstallActionInternal(KernelControllerContext context)
+   {
+      // handle custom annotations
+      AnnotationMetaDataVisitor annotationsVisitor = new AnnotationMetaDataVisitor(context);
+      annotationsVisitor.before();
+      try
+      {
+         BeanAnnotationAdapterFactory.getBeanAnnotationAdapter().cleanAnnotations(annotationsVisitor);
+      }
+      catch(Throwable t)
+      {
+         log.debug("Error while cleaning the annotations: " + t);
+      }
+      finally
+      {
+         annotationsVisitor.after();
+      }
+   }
 }
\ No newline at end of file

Modified: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasAnnotationTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasAnnotationTestCase.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasAnnotationTestCase.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -26,6 +26,7 @@
 import org.jboss.beans.metadata.plugins.AbstractBeanMetaData;
 import org.jboss.test.kernel.dependency.support.AliasSimpleBeanImpl;
 import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
 
 /**
  * Plain alias tests.
@@ -64,4 +65,9 @@
    {
       // do nothing should be part of @annotations
    }
+
+   protected ControllerState getDirectAliasUnistallState()
+   {
+      return ControllerState.ERROR;
+   }
 }

Modified: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasTestCase.java	2007-12-09 11:01:57 UTC (rev 68078)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/dependency/test/PlainAliasTestCase.java	2007-12-09 14:06:08 UTC (rev 68079)
@@ -134,11 +134,12 @@
       assertUninstall("OriginalBean");
       assertEquals(ControllerState.ERROR, context1.getState());
       assertEquals(ControllerState.ERROR, alias.getState());
-      assertEquals(ControllerState.START, directAlias.getState());
+      assertEquals(getDirectAliasUnistallState(), directAlias.getState());
       assertNull(getUtil().getContext("OriginalBean"));
       assertNull(getUtil().getContext("MyAlias"));
 
       context1 = assertInstall(0, "OriginalBean");
+      directAlias = assertContext("MyAlias_Alias");
       assertEquals(ControllerState.INSTALLED, directAlias.getState());
       assertEquals(context1, assertContext("MyAlias"));
 
@@ -150,6 +151,11 @@
       checkLastSimpleAliasInstall(context1);
    }
 
+   protected ControllerState getDirectAliasUnistallState()
+   {
+      return ControllerState.START;
+   }
+
    protected void checkLastSimpleAliasInstall(ControllerContext context) throws Throwable
    {
       installAlias();




More information about the jboss-cvs-commits mailing list