[jboss-cvs] JBossAS SVN: r74681 - in trunk/server/src: main/org/jboss/deployment and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jun 17 08:04:02 EDT 2008


Author: alesj
Date: 2008-06-17 08:04:02 -0400 (Tue, 17 Jun 2008)
New Revision: 74681

Added:
   trunk/server/src/main/org/jboss/deployment/AltAnnotationMetaDataDeployer.java
Modified:
   trunk/server/src/etc/deployers/metadata-deployer-beans.xml
   trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java
Log:
Alternative annotation metadata scanning deployer.
Useful on deployments that don't contain a lot of JEE annotated classes.
It will only load those classes with specified annotations.

Modified: trunk/server/src/etc/deployers/metadata-deployer-beans.xml
===================================================================
--- trunk/server/src/etc/deployers/metadata-deployer-beans.xml	2008-06-17 12:03:08 UTC (rev 74680)
+++ trunk/server/src/etc/deployers/metadata-deployer-beans.xml	2008-06-17 12:04:02 UTC (rev 74681)
@@ -9,13 +9,20 @@
    <!-- A reference metadata resolving deployer that creates and endpoint map
       for the deployments
    -->
-   <bean name="ReferenceMetaDataResolverDeployer"
-      class="org.jboss.deployment.MappedReferenceMetaDataResolverDeployer">
+   <bean name="ReferenceMetaDataResolverDeployer" class="org.jboss.deployment.MappedReferenceMetaDataResolverDeployer"/>
+
+   <!-- Generic scanning deployer - using javassist to do lookup
+   <bean name="GenScanDeployer" class="org.jboss.deployers.plugins.annotations.GenericAnnotationDeployer">
+     <property name="output">org.jboss.deployers.spi.annotations.AnnotationEnvironment</property>
    </bean>
 
+   <bean name="AnnotationMetaDataDeployer" class="org.jboss.deployment.AltAnnotationMetaDataDeployer">
+      <property name="metaDataCompleteIsDefault">false</property>
+   </bean>
+    -->
+
    <!-- Extract annotations into a standard metadata view -->
-   <bean name="AnnotationMetaDataDeployer"
-      class="org.jboss.deployment.AnnotationMetaDataDeployer">
+   <bean name="AnnotationMetaDataDeployer" class="org.jboss.deployment.AnnotationMetaDataDeployer">
       <property name="metaDataCompleteIsDefault">false</property>
    </bean>
 

Copied: trunk/server/src/main/org/jboss/deployment/AltAnnotationMetaDataDeployer.java (from rev 74670, trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java)
===================================================================
--- trunk/server/src/main/org/jboss/deployment/AltAnnotationMetaDataDeployer.java	                        (rev 0)
+++ trunk/server/src/main/org/jboss/deployment/AltAnnotationMetaDataDeployer.java	2008-06-17 12:04:02 UTC (rev 74681)
@@ -0,0 +1,211 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * 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.deployment;
+
+import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.annotation.Resource;
+import javax.annotation.Resources;
+import javax.annotation.security.DeclareRoles;
+import javax.annotation.security.DenyAll;
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.annotation.security.RunAs;
+import javax.ejb.ApplicationException;
+import javax.ejb.EJB;
+import javax.ejb.EJBs;
+import javax.ejb.Init;
+import javax.ejb.Local;
+import javax.ejb.LocalHome;
+import javax.ejb.MessageDriven;
+import javax.ejb.PostActivate;
+import javax.ejb.PrePassivate;
+import javax.ejb.Remote;
+import javax.ejb.RemoteHome;
+import javax.ejb.Remove;
+import javax.ejb.Stateful;
+import javax.ejb.Stateless;
+import javax.ejb.Timeout;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionManagement;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.ExcludeClassInterceptors;
+import javax.interceptor.ExcludeDefaultInterceptors;
+import javax.interceptor.Interceptors;
+import javax.persistence.Entity;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContexts;
+import javax.xml.ws.WebServiceRef;
+import javax.xml.ws.WebServiceRefs;
+
+import org.jboss.deployers.spi.annotations.AnnotationEnvironment;
+import org.jboss.deployers.spi.annotations.Element;
+import org.jboss.deployers.spi.deployer.DeploymentStages;
+import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
+import org.jboss.virtual.VirtualFile;
+
+/**
+ * A PRE_REAL deployer which generates metadata from annotations.
+ * Alternative option to its super class.
+ *
+ * @author Ales.Justin at jboss.org
+ */
+public class AltAnnotationMetaDataDeployer extends AnnotationMetaDataDeployer
+{
+   private Set<Class<? extends Annotation>> annotationOnClass = new HashSet<Class<? extends Annotation>>();
+   private Set<Class<? extends Annotation>> annotationOnMethod = new HashSet<Class<? extends Annotation>>();
+   private Set<Class<? extends Annotation>> annotationOnField = new HashSet<Class<? extends Annotation>>();
+
+   public AltAnnotationMetaDataDeployer()
+   {
+      super();
+      setStage(DeploymentStages.PRE_REAL);
+      setInput(AnnotationEnvironment.class);
+      // add annotations AnnotationMetaDataDeployer scans
+      addAnnotationClass(Stateful.class);
+      addAnnotationClass(Remove.class);
+      addAnnotationClass(PostActivate.class);
+      addAnnotationClass(PrePassivate.class);
+      addAnnotationClass(Local.class);
+      addAnnotationClass(LocalHome.class);
+      addAnnotationClass(Remote.class);
+      addAnnotationClass(RemoteHome.class);
+      addAnnotationClass(Init.class);
+      addAnnotationClass(Timeout.class);
+      addAnnotationClass(AroundInvoke.class);
+      addAnnotationClass(TransactionManagement.class);
+      addAnnotationClass(TransactionAttribute.class);
+      addAnnotationClass(RunAs.class);
+      addAnnotationClass(DeclareRoles.class);
+      addAnnotationClass(DenyAll.class);
+      addAnnotationClass(RolesAllowed.class);
+      addAnnotationClass(PermitAll.class);
+      addAnnotationClass(Interceptors.class);
+      addAnnotationClass(ExcludeClassInterceptors.class);
+      addAnnotationClass(ExcludeDefaultInterceptors.class);
+      addAnnotationClass(Resource.class);
+      addAnnotationClass(Resources.class);
+      addAnnotationClass(EJB.class);
+      addAnnotationClass(EJBs.class);
+      addAnnotationClass(PersistenceContext.class);
+      addAnnotationClass(PersistenceContexts.class);
+      addAnnotationClass(PostConstruct.class);
+      addAnnotationClass(PreDestroy.class);
+      addAnnotationClass(WebServiceRef.class);
+      addAnnotationClass(WebServiceRefs.class);
+      addAnnotationClass(Stateless.class);
+      addAnnotationClass(MessageDriven.class);
+      addAnnotationClass(Entity.class);
+      addAnnotationClass(ApplicationException.class);
+   }
+
+   @SuppressWarnings("unchecked")
+   protected Collection<Class<?>> getClasses(VFSDeploymentUnit unit, String mainClassName, List<VirtualFile> classpath) throws IOException
+   {
+      AnnotationEnvironment env = unit.getAttachment(AnnotationEnvironment.class);
+      if (env == null)
+         return Collections.emptySet();
+
+      Set<Class<?>> classes = new HashSet<Class<?>>();
+      for(Class<? extends Annotation> annotation : annotationOnClass)
+         classes.addAll(env.classIsAnnotatedWith(annotation));
+      for(Class<? extends Annotation> annotation : annotationOnMethod)
+      {
+         Class<Annotation> annotationClass = (Class<Annotation>)annotation;
+         Set<Element<Annotation, Method>> elements = env.classHasMethodAnnotatedWith(annotationClass);
+         for(Element<Annotation, Method> elt : elements)
+            classes.add(elt.getOwner());
+      }
+      for(Class<? extends Annotation> annotation : annotationOnField)
+      {
+         Class<Annotation> annotationClass = (Class<Annotation>)annotation;
+         Set<Element<Annotation, Field>> elements = env.classHasFieldAnnotatedWith(annotationClass);
+         for(Element<Annotation, Field> elt : elements)
+            classes.add(elt.getOwner());
+      }
+
+      if(log.isTraceEnabled() && classes.isEmpty() == false)
+         log.trace("Annotated classes: " + classes);
+
+      return classes;
+   }
+
+   /**
+    * Cleanup annotation classes.
+    */
+   public void stop()
+   {
+      annotationOnClass.clear();
+      annotationOnMethod.clear();
+      annotationOnField.clear();
+   }
+
+   /**
+    * Add annotation to matching set.
+    *
+    * @param annotation the annotation class
+    */
+   public void addAnnotationClass(Class<? extends Annotation> annotation)
+   {
+      Target target = annotation.getAnnotation(Target.class);
+      if (target == null)
+      {
+         log.info("Annotation " + annotation + " has not @Target.");
+         return;
+      }
+      ElementType[] types = target.value();
+      if (types == null || types.length == 0)
+      {
+         log.info("Null or empty types on annotation's @Target: " + annotation);
+         return;
+      }
+      for(ElementType type : types)
+      {
+         boolean used = false; // no need for duplicates
+         if (type == ElementType.TYPE)
+         {
+            annotationOnClass.add(annotation);
+            used = true;
+         }
+         if (used == false && type == ElementType.METHOD)
+         {
+            annotationOnMethod.add(annotation);
+            used = true;
+         }
+         if (used == false && type == ElementType.FIELD)
+         {
+            annotationOnField.add(annotation);
+         }
+      }
+   }
+}
\ No newline at end of file


Property changes on: trunk/server/src/main/org/jboss/deployment/AltAnnotationMetaDataDeployer.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision

Modified: trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java
===================================================================
--- trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java	2008-06-17 12:03:08 UTC (rev 74680)
+++ trunk/server/src/main/org/jboss/deployment/AnnotationMetaDataDeployer.java	2008-06-17 12:04:02 UTC (rev 74681)
@@ -23,6 +23,7 @@
 
 import java.io.IOException;
 import java.lang.reflect.AnnotatedElement;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -140,6 +141,7 @@
       ApplicationClientMetaData clientMetaData = unit.getAttachment(ApplicationClientMetaData.class);
       if(clientMetaData != null)
          isComplete |= clientMetaData.isMetadataComplete();
+
       if(isComplete)
       {
          log.debug("Deployment is metadata-complete, skipping annotation processing"
@@ -163,7 +165,6 @@
       if(isLeaf == true)
          return;
 
-      ClassLoader loader = unit.getClassLoader();
       List<VirtualFile> classpath = unit.getClassPath();
       if(classpath == null || classpath.isEmpty())
          return;
@@ -175,29 +176,16 @@
       try
       {
          String mainClassName = getMainClassName(unit);
-         Map<VirtualFile, Class<?>> classpathClasses = new HashMap<VirtualFile, Class<?>>();
-         for(VirtualFile path : classpath)
+         Collection<Class<?>> classes = getClasses(unit, mainClassName, classpath);
+         if(classes.size() > 0)
          {
-            AnnotatedClassFilter classVisitor = new AnnotatedClassFilter(unit, loader, path, mainClassName);
-            path.visit(classVisitor);
-            Map<VirtualFile, Class<?>> classes = classVisitor.getAnnotatedClasses();
-            if(classes != null && classes.size() > 0)
-            {
-               if(trace)
-                  log.trace("Annotated classes: "+classes);
-               classpathClasses.putAll(classes);
-            }
-         }
-
-         if(classpathClasses.size() > 0)
-         {
             AnnotationFinder<AnnotatedElement> finder = new DefaultAnnotationFinder<AnnotatedElement>();
             if(webMetaData != null)
-               processJBossWebMetaData(unit, finder, classpathClasses);
+               processJBossWebMetaData(unit, finder, classes);
             else if(clientMetaData != null || mainClassName != null)
-               processJBossClientMetaData(unit, finder, classpathClasses);
+               processJBossClientMetaData(unit, finder, classes);
             else
-               processJBossMetaData(unit, finder, classpathClasses);
+               processJBossMetaData(unit, finder, classes);
          }
       }
       catch(IOException e)
@@ -207,6 +195,33 @@
    }
 
    /**
+    * Get the classes we want to scan.
+    *
+    * @param unit the deployment unit
+    * @param mainClassName the main class name
+    * @param classpath the classpath
+    * @return possible classes containing metadata annotations
+    * @throws IOException for any error
+    */
+   protected Collection<Class<?>> getClasses(VFSDeploymentUnit unit, String mainClassName, List<VirtualFile> classpath) throws IOException
+   {
+      Map<VirtualFile, Class<?>> classpathClasses = new HashMap<VirtualFile, Class<?>>();
+      for(VirtualFile path : classpath)
+      {
+         AnnotatedClassFilter classVisitor = new AnnotatedClassFilter(unit, unit.getClassLoader(), path, mainClassName);
+         path.visit(classVisitor);
+         Map<VirtualFile, Class<?>> classes = classVisitor.getAnnotatedClasses();
+         if(classes != null && classes.size() > 0)
+         {
+            if(log.isTraceEnabled())
+               log.trace("Annotated classes: " + classes);
+            classpathClasses.putAll(classes);
+         }
+      }
+      return classpathClasses.values();
+   }
+
+   /**
     * Undeploy a vfs deployment
     * 
     * @param unit the unit
@@ -224,20 +239,28 @@
     * @param classes the candidate classes
     */
    protected void processJBossMetaData(VFSDeploymentUnit unit,
-         AnnotationFinder<AnnotatedElement> finder, Map<VirtualFile, Class<?>> classes)
+         AnnotationFinder<AnnotatedElement> finder, Collection<Class<?>> classes)
    {
       // Create the metadata model from the annotations
       EjbJar30Creator creator = new EjbJar30Creator(finder);
-      EjbJar30MetaData annotationMetaData = creator.create(classes.values());
+      EjbJar30MetaData annotationMetaData = creator.create(classes);
       if(annotationMetaData != null)
          unit.addAttachment(EJB_ANNOTATED_ATTACHMENT_NAME, annotationMetaData, EjbJarMetaData.class);
    }
 
+   /**
+    * Get main class from manifest.
+    *
+    * @param unit the deployment unit
+    * @return main class name
+    * @throws IOException for any error
+    */
    protected String getMainClassName(VFSDeploymentUnit unit)
       throws IOException
    {
       VirtualFile file = unit.getMetaDataFile("MANIFEST.MF");
-      log.trace("parsing " + file);
+      if (log.isTraceEnabled())
+         log.trace("parsing " + file);
 
       if(file == null)
       {
@@ -248,8 +271,7 @@
       {
          Manifest mf = VFSUtils.readManifest(file);
          Attributes attrs = mf.getMainAttributes();
-         String className = attrs.getValue(Attributes.Name.MAIN_CLASS);
-         return className;
+         return attrs.getValue(Attributes.Name.MAIN_CLASS);
       }
       finally
       {
@@ -265,10 +287,10 @@
     * @param classes the candidate classes
     */
    protected void processJBossWebMetaData(VFSDeploymentUnit unit,
-         AnnotationFinder<AnnotatedElement> finder, Map<VirtualFile, Class<?>> classes)
+         AnnotationFinder<AnnotatedElement> finder, Collection<Class<?>> classes)
    {
       Web25MetaDataCreator creator = new Web25MetaDataCreator(finder);
-      Web25MetaData annotationMetaData = creator.create(classes.values());
+      Web25MetaData annotationMetaData = creator.create(classes);
       if(annotationMetaData != null)
          unit.addAttachment(WEB_ANNOTATED_ATTACHMENT_NAME, annotationMetaData, WebMetaData.class);
    }
@@ -281,10 +303,10 @@
     * @param classes the candidate classes
     */
    protected void processJBossClientMetaData(VFSDeploymentUnit unit,
-         AnnotationFinder<AnnotatedElement> finder, Map<VirtualFile, Class<?>> classes)
+         AnnotationFinder<AnnotatedElement> finder, Collection<Class<?>> classes)
    {
       ApplicationClient5MetaDataCreator creator = new ApplicationClient5MetaDataCreator(finder);
-      ApplicationClient5MetaData annotationMetaData = creator.create(classes.values());
+      ApplicationClient5MetaData annotationMetaData = creator.create(classes);
       if(annotationMetaData != null)
          unit.addAttachment(CLIENT_ANNOTATED_ATTACHMENT_NAME, annotationMetaData, ApplicationClientMetaData.class);      
    }




More information about the jboss-cvs-commits mailing list