[jboss-cvs] JBossAS SVN: r93953 - in trunk: server/src/main/org/jboss/web/deployers and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Sep 23 10:48:25 EDT 2009


Author: remy.maucherat at jboss.com
Date: 2009-09-23 10:48:25 -0400 (Wed, 23 Sep 2009)
New Revision: 93953

Added:
   trunk/server/src/main/org/jboss/web/deployers/WarAnnotationMetaDataDeployer.java
Modified:
   trunk/server/src/main/org/jboss/deployment/ServletContainerInitializerDeployer.java
   trunk/tomcat/src/resources/war-deployers-jboss-beans.xml
Log:
- Add a WAR specific deployer for annotations.
- This makes things reasonably fast while waiting for an optimized solution.

Modified: trunk/server/src/main/org/jboss/deployment/ServletContainerInitializerDeployer.java
===================================================================
--- trunk/server/src/main/org/jboss/deployment/ServletContainerInitializerDeployer.java	2009-09-23 12:10:38 UTC (rev 93952)
+++ trunk/server/src/main/org/jboss/deployment/ServletContainerInitializerDeployer.java	2009-09-23 14:48:25 UTC (rev 93953)
@@ -63,6 +63,10 @@
 
    public void deploy(DeploymentUnit unit) throws DeploymentException
    {
+      if (!unit.getSimpleName().endsWith(".war"))
+      {
+         return;
+      }
       // Load the ServletContainerInitializer services
       ServiceLoader<ServletContainerInitializer> serviceLoader = 
          ServiceLoader.load(ServletContainerInitializer.class, unit.getClassLoader());

Added: trunk/server/src/main/org/jboss/web/deployers/WarAnnotationMetaDataDeployer.java
===================================================================
--- trunk/server/src/main/org/jboss/web/deployers/WarAnnotationMetaDataDeployer.java	                        (rev 0)
+++ trunk/server/src/main/org/jboss/web/deployers/WarAnnotationMetaDataDeployer.java	2009-09-23 14:48:25 UTC (rev 93953)
@@ -0,0 +1,257 @@
+/*
+ * 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.web.deployers;
+
+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;
+
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.deployer.DeploymentStages;
+import org.jboss.deployers.spi.deployer.helpers.AbstractDeployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
+import org.jboss.deployment.AnnotatedClassFilter;
+import org.jboss.metadata.annotation.creator.web.Web30MetaDataCreator;
+import org.jboss.metadata.annotation.finder.AnnotationFinder;
+import org.jboss.metadata.annotation.finder.DefaultAnnotationFinder;
+import org.jboss.metadata.web.spec.Web25MetaData;
+import org.jboss.metadata.web.spec.Web30MetaData;
+import org.jboss.metadata.web.spec.WebMetaData;
+import org.jboss.virtual.VirtualFile;
+
+/**
+ * A POST_CLASSLOADER deployer which generates metadata from
+ * annotations
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision: 93949 $
+ */
+public class WarAnnotationMetaDataDeployer extends AbstractDeployer
+{
+   public static final String WEB_ANNOTATED_ATTACHMENT_NAME = "annotated."+WebMetaData.class.getName();
+
+   private boolean metaDataCompleteIsDefault = false;
+
+   public WarAnnotationMetaDataDeployer()
+   {
+      setStage(DeploymentStages.POST_CLASSLOADER);
+      addInput(WebMetaData.class);
+      addOutput(WEB_ANNOTATED_ATTACHMENT_NAME);
+   }
+
+   public boolean isMetaDataCompleteIsDefault()
+   {
+      return metaDataCompleteIsDefault;
+   }
+   public void setMetaDataCompleteIsDefault(boolean metaDataCompleteIsDefault)
+   {
+      this.metaDataCompleteIsDefault = metaDataCompleteIsDefault;
+   }
+
+   public void deploy(DeploymentUnit unit) throws DeploymentException
+   {
+      if (unit instanceof VFSDeploymentUnit == false)
+         return;
+      
+      VFSDeploymentUnit vfsDeploymentUnit = (VFSDeploymentUnit) unit;
+      deploy(vfsDeploymentUnit);
+   }
+
+   public void undeploy(DeploymentUnit unit)
+   {
+      if (unit instanceof VFSDeploymentUnit == false)
+         return;
+      
+      VFSDeploymentUnit vfsDeploymentUnit = (VFSDeploymentUnit) unit;
+      undeploy(vfsDeploymentUnit);
+   }
+
+   /**
+    * Process the 
+    * 
+    * @param unit the unit
+    * @throws DeploymentException for any error
+    */
+   protected void deploy(VFSDeploymentUnit unit)
+      throws DeploymentException
+   {
+      if (!unit.getSimpleName().endsWith(".war"))
+      {
+         return;
+      }
+      /* Ignore any spec metadata complete deployments. This expects that a
+       deployment unit only represents one of the client, ejb or web
+       deployments and its metadata completeness applies to the unit in terms
+       of whether annotations should be scanned for.
+       */
+      boolean isComplete = this.isMetaDataCompleteIsDefault();
+      WebMetaData webMetaData = unit.getAttachment(WebMetaData.class);
+      if(webMetaData != null)
+      {
+         if (webMetaData instanceof Web25MetaData)
+         {
+            isComplete |= ((Web25MetaData)webMetaData).isMetadataComplete();
+         }
+         else if (webMetaData instanceof Web30MetaData)
+         {
+            isComplete |= ((Web30MetaData)webMetaData).isMetadataComplete();
+         }
+         else
+         {
+            // Any web.xml 2.4 or earlier deployment is metadata complete
+            isComplete = true;
+         }
+      }
+      if(isComplete)
+      {
+         log.debug("Deployment is metadata-complete, skipping annotation processing"
+               + ", jbossWebMetaData="+webMetaData
+               + ", metaDataCompleteIsDefault="+metaDataCompleteIsDefault
+               );
+         return;
+      }
+      VirtualFile root = unit.getRoot();
+      boolean isLeaf = true;
+      try
+      {
+         isLeaf = root.isLeaf();
+      }
+      catch(IOException ignore)
+      {
+      }
+      if(isLeaf == true)
+         return;
+
+      List<VirtualFile> classpath = unit.getClassPath();
+      if(classpath == null || classpath.isEmpty())
+         return;
+
+      if (log.isTraceEnabled())
+         log.trace("Deploying annotations for unit: " + unit + ", classpath: " + classpath);
+
+      try
+      {
+         processMetaData(unit, webMetaData, classpath);
+      }
+      catch (Exception e)
+      {
+         throw DeploymentException.rethrowAsDeploymentException("Cannot process metadata", e);
+      }
+   }
+
+   /**
+    * Process metadata.
+    *
+    * @param unit the deployment unit
+    * @param webMetaData the web metadata
+    * @param clientMetaData the client metadata
+    * @param classpath the classpath
+    * @throws DeploymentException for any error
+    */
+   protected void processMetaData(VFSDeploymentUnit unit, WebMetaData webMetaData, List<VirtualFile> classpath) throws Exception
+   {
+      Map<VirtualFile, Collection<Class<?>>> classesPerJar = new HashMap<VirtualFile, Collection<Class<?>>>();
+      boolean foundAnnotations = false;
+      for (VirtualFile path : classpath)
+      {
+         Collection<Class<?>> currentClasses = getClasses(unit, path);
+         classesPerJar.put(path, currentClasses);
+         if (currentClasses.size() > 0)
+         {
+            foundAnnotations = true;
+         }
+      }
+      if (foundAnnotations)
+      {
+         AnnotationFinder<AnnotatedElement> finder = new DefaultAnnotationFinder<AnnotatedElement>();
+         processJBossWebMetaData(unit, finder, classesPerJar);
+      }
+   }
+
+   /**
+    * 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, VirtualFile classpath) throws IOException
+   {
+      AnnotatedClassFilter classVisitor = new AnnotatedClassFilter(unit, unit.getClassLoader(), classpath, null);
+      classpath.visit(classVisitor);
+      Map<VirtualFile, Class<?>> classes = classVisitor.getAnnotatedClasses();
+      if (classes != null && classes.size() > 0)
+      {
+         if(log.isTraceEnabled())
+            log.trace("Annotated classes: " + classes);
+      }
+      else
+      {
+         classes = new HashMap<VirtualFile, Class<?>>();
+      }
+      return classes.values();
+   }
+
+   /**
+    * Undeploy a vfs deployment
+    * 
+    * @param unit the unit
+    */
+   protected void undeploy(VFSDeploymentUnit unit)
+   {
+      // Nothing
+   }
+
+   /**
+    * Process annotations.
+    *
+    * @param unit the deployment unit
+    * @param finder the annotation finder
+    * @param classes the candidate classes
+    */
+   protected void processJBossWebMetaData(VFSDeploymentUnit unit,
+         AnnotationFinder<AnnotatedElement> finder, Map<VirtualFile, Collection<Class<?>>> classes)
+   {
+      Web30MetaDataCreator creator = new Web30MetaDataCreator(finder);
+      boolean metaData = false;
+      for (VirtualFile path : classes.keySet())
+      {
+         WebMetaData annotationMetaData = creator.create(classes.get(path));
+         log.debug("Add annotations: " + WEB_ANNOTATED_ATTACHMENT_NAME + ":" + path.getName());
+         if (annotationMetaData != null)
+         {
+            unit.addAttachment(WEB_ANNOTATED_ATTACHMENT_NAME + ":" + path.getName(), annotationMetaData, WebMetaData.class);
+            metaData = true;
+         }
+      }
+      if (metaData)
+         unit.addAttachment(WEB_ANNOTATED_ATTACHMENT_NAME, Boolean.TRUE);
+   }
+
+}
+

Modified: trunk/tomcat/src/resources/war-deployers-jboss-beans.xml
===================================================================
--- trunk/tomcat/src/resources/war-deployers-jboss-beans.xml	2009-09-23 12:10:38 UTC (rev 93952)
+++ trunk/tomcat/src/resources/war-deployers-jboss-beans.xml	2009-09-23 14:48:25 UTC (rev 93953)
@@ -34,7 +34,7 @@
       <property name="relativeOrder">2004</property>
    </bean>
    <!-- Temporary for Servlet 3.0 annotation support, large performance drop ... -->
-   <bean name="WebAnnotationMetaDataDeployer" class="org.jboss.deployment.AnnotationMetaDataDeployer">
+   <bean name="WebAnnotationMetaDataDeployer" class="org.jboss.web.deployers.WarAnnotationMetaDataDeployer">
       <property name="relativeOrder">2005</property>
       <property name="metaDataCompleteIsDefault">false</property>
    </bean>




More information about the jboss-cvs-commits mailing list