[jboss-cvs] JBossAS SVN: r72727 - trunk/server/src/main/org/jboss/deployment.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Apr 25 10:13:32 EDT 2008


Author: scott.stark at jboss.org
Date: 2008-04-25 10:13:31 -0400 (Fri, 25 Apr 2008)
New Revision: 72727

Modified:
   trunk/server/src/main/org/jboss/deployment/AnnotatedClassFilter.java
   trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java
Log:
JBAS-5460, include client main in classes to scan

Modified: trunk/server/src/main/org/jboss/deployment/AnnotatedClassFilter.java
===================================================================
--- trunk/server/src/main/org/jboss/deployment/AnnotatedClassFilter.java	2008-04-25 14:10:44 UTC (rev 72726)
+++ trunk/server/src/main/org/jboss/deployment/AnnotatedClassFilter.java	2008-04-25 14:13:31 UTC (rev 72727)
@@ -23,6 +23,7 @@
 
 import java.io.IOException;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -50,10 +51,17 @@
    private int rootLength;
    private HashSet<String> childPaths = new HashSet<String>();
    private HashMap<VirtualFile, Class<?>> pathToClasses = new HashMap<VirtualFile, Class<?>>();
+   private String clientClassName;
 
    public AnnotatedClassFilter(VFSDeploymentUnit unit, ClassLoader loader, VirtualFile classpathRoot)
    {
+      this(unit, loader, classpathRoot, "");
+   }
+   public AnnotatedClassFilter(VFSDeploymentUnit unit, ClassLoader loader,
+         VirtualFile classpathRoot, String clientClassName)
+   {
       this.loader = loader;
+      this.clientClassName = clientClassName;
 
       // Work out the root length. If there is a root, we need to add one to jump across the next /
       String rootName = classpathRoot.getPathName();
@@ -111,7 +119,9 @@
             className = getClassName(file);
             Class<?> c = loader.loadClass(className);
             Annotation[] annotations = c.getAnnotations();
-            if(annotations != null && annotations.length > 0)
+            boolean includeClass = (annotations != null && annotations.length > 0)
+               || (className.equals(clientClassName) && hasAnnotations(c));
+            if(includeClass)
             {
                pathToClasses.put(file, c);
                accepts = true;
@@ -158,6 +168,29 @@
       return name;
    }
 
+   /**
+    * Completely scan a class for annotations
+    * @param cls
+    * @return true if the class has annotations, false otherwise
+    */
+   protected boolean hasAnnotations(Class<?> cls)
+   {
+      if(cls == null)
+         return false;
+      
+      // Note: this also returns true if super class has annotations
+      if(cls.getAnnotations().length > 0)
+         return true;
+      
+      for(Field f : cls.getDeclaredFields())
+      {
+         if(f.getAnnotations().length > 0)
+            return true;
+      }
+      
+      return hasAnnotations(cls.getSuperclass());
+   }
+
    class NoChildFilter implements VirtualFileFilter
    {
       public boolean accepts(VirtualFile file)

Modified: trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java
===================================================================
--- trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java	2008-04-25 14:10:44 UTC (rev 72726)
+++ trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java	2008-04-25 14:13:31 UTC (rev 72727)
@@ -46,6 +46,7 @@
 import org.jboss.metadata.ejb.spec.EjbJarMetaData;
 import org.jboss.metadata.web.spec.Web25MetaData;
 import org.jboss.metadata.web.spec.WebMetaData;
+import org.jboss.virtual.VFSUtils;
 import org.jboss.virtual.VirtualFile;
 
 /**
@@ -161,10 +162,11 @@
 
       try
       {
+         String mainClassName = getMainClassName(unit);
          Map<VirtualFile, Class<?>> classpathClasses = new HashMap<VirtualFile, Class<?>>();
          for(VirtualFile path : classpath)
          {
-            AnnotatedClassFilter classVisitor = new AnnotatedClassFilter(unit, loader, path);
+            AnnotatedClassFilter classVisitor = new AnnotatedClassFilter(unit, loader, path, mainClassName);
             path.visit(classVisitor);
             Map<VirtualFile, Class<?>> classes = classVisitor.getAnnotatedClasses();
             if(classes != null && classes.size() > 0)
@@ -174,42 +176,13 @@
                classpathClasses.putAll(classes);
             }
          }
-         // Always need to include a client app main class
-         Class clientMainClass = null;
-         if(ejbJarMetaData == null && webMetaData == null && clientMetaData == null)
-         {
-            VirtualFile mfvf = unit.getMetaDataFile("MANIFEST.MF");
-            if(mfvf != null)
-            {
-               Manifest mf = new Manifest(mfvf.openStream());
-               mfvf.close();
-               String mainClass = mf.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
-               if(mainClass != null)
-               {
-                  VirtualFile mainClassVF = unit.getFile(mainClass.replace('.', '/')+".class");
-                  if(mainClassVF != null)
-                  {
-                     try
-                     {
-                        clientMainClass = loader.loadClass(mainClass);
-                        log.info("Saw Main-Class: "+mainClass);
-                        classpathClasses.put(mainClassVF, clientMainClass);
-                     }
-                     catch(ClassNotFoundException e)
-                     {
-                        log.debug("Ignoring invalid Main-Class: "+mainClass, e);
-                     }
-                  }
-               }
-            }
-         }
 
          if(classpathClasses.size() > 0)
          {
             AnnotationFinder<AnnotatedElement> finder = new DefaultAnnotationFinder<AnnotatedElement>();
             if(webMetaData != null)
                processJBossWebMetaData(unit, finder, classpathClasses);
-            else if(clientMetaData != null || clientMainClass != null)
+            else if(clientMetaData != null || mainClassName != null)
                processJBossClientMetaData(unit, finder, classpathClasses);
             else
                processJBossMetaData(unit, finder, classpathClasses);
@@ -248,6 +221,30 @@
          unit.addAttachment(EJB_ANNOTATED_ATTACHMENT_NAME, annotationMetaData, EjbJarMetaData.class);
    }
 
+   protected String getMainClassName(VFSDeploymentUnit unit)
+      throws IOException
+   {
+      VirtualFile file = unit.getMetaDataFile("MANIFEST.MF");
+      log.trace("parsing " + file);
+
+      if(file == null)
+      {
+         return null;
+      }
+
+      try
+      {
+         Manifest mf = VFSUtils.readManifest(file);
+         Attributes attrs = mf.getMainAttributes();
+         String className = attrs.getValue(Attributes.Name.MAIN_CLASS);
+         return className;
+      }
+      finally
+      {
+         file.close();
+      }
+   }
+
    /**
     * Process annotations.
     *




More information about the jboss-cvs-commits mailing list