Package org.jboss.web.deployers

Source Code of org.jboss.web.deployers.SpecCompliantWarAnnotationDeployer

/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file 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.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.AsyncListener;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.jsp.tagext.SimpleTag;
import javax.servlet.jsp.tagext.Tag;

import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
import org.jboss.metadata.annotation.creator.AnnotationContext;
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.WebMetaData;
import org.jboss.scanning.web.spi.ResourcesIndex;
import org.jboss.vfs.VirtualFile;

/**
* Servlet spec (and JSP spec) lists only specific set of class types which
* are eligible for Java EE resource injections. Unlike the {@link WarAnnotationMetaDataDeployer},
* which picks up all classes in the classpath for annotation processing and subsequent metadata creation,
* this {@link SpecCompliantWarAnnotationDeployer} attempts to scan only the following types
* of classes for Java EE resource injections.
* <p>
* As determined by the Servlet spec and JSP spec, the following class types will be scanned for resource injection
* annotations:
* <ul>
<li>javax.servlet.Servlet</li>
<li>javax.servlet.Filter</li>
<li>javax.servlet.ServletContextListener</li>
<li>javax.servlet.ServletContextAttributeListener</li>
<li>javax.servlet.ServletRequestListener</li>
<li>javax.servlet.ServletRequestAttributeListener</li>
<li>javax.servlet.http.HttpSessionListener</li>
<li>javax.servlet.http.HttpSessionAttributeListener</li>
<li>javax.servlet.AsyncListener</li>
<li>javax.servlet.jsp.tagext.Tag</li>
<li>javax.servlet.jsp.tagext.SimpleTag</li>
* </ul>
* TODO: This is currently a work-in-progress deployer. The blocker currently, is that the JSF managed bean
* integration implementation assumes that the WarAnnotationDeployer will scan for annotations on a JSF managed bean.
* However, for this {@link SpecCompliantWarAnnotationDeployer} to work correctly, we need to know if a specific class
* has been marked as a JSF managed bean (via faces-config.xml). Currently there's no way to get that information
* during deployment phase of a unit. Needs more thought on how to get this working cleanly.
* @see https://issues.jboss.org/browse/JBAS-8775. Once this deployer is fully functional, it will replace the
* {@link WarAnnotationMetaDataDeployer}
*
* @author Jaikiran Pai
* @version $Revision: $
*/
public class SpecCompliantWarAnnotationDeployer extends WarAnnotationMetaDataDeployer
{

   public SpecCompliantWarAnnotationDeployer()
   {

   }

   @Override
   protected void processMetaData(VFSDeploymentUnit unit, List<VirtualFile> classpath) throws Exception
   {
      ResourcesIndex resourceIndex = unit.getAttachment(ResourcesIndex.class);
      if (resourceIndex == null)
      {
         return;
      }
      AnnotationFinder<AnnotatedElement> finder = new DefaultAnnotationFinder<AnnotatedElement>();
      Web30MetaDataCreator creator = new Web30MetaDataCreator(finder);
      // Merge processed annotations from all scopes
      Set<Class<? extends Annotation>> annotations = new HashSet<Class<? extends Annotation>>();
      AnnotationContext annotationContext = creator.getAnnotationContext();
      if (annotationContext.getTypeAnnotations() != null)
         annotations.addAll(annotationContext.getTypeAnnotations());
      if (annotationContext.getMethodAnnotations() != null)
         annotations.addAll(annotationContext.getMethodAnnotations());
      if (annotationContext.getFieldAnnotations() != null)
         annotations.addAll(annotationContext.getFieldAnnotations());

      Collection<Class<?>> specEligibleResourceInjectionClasses = this.getResourceInjectionEligibleWebAppClasses(resourceIndex, classpath);
      if (specEligibleResourceInjectionClasses.isEmpty())
      {
         return;
      }
      boolean metaData = false;
      for (VirtualFile path : classpath)
      {
         Collection<Class<?>> eligibleAnnotatedClasses = new HashSet<Class<?>>();
         for (Class<? extends Annotation> annotation : annotations)
         {
            Collection<Class<?>> annotatedClasses = resourceIndex.getAnnotatedClasses(path, annotation);
            eligibleAnnotatedClasses.addAll(this.retainResourceInjectionEligibleWebAppClasses(specEligibleResourceInjectionClasses, annotatedClasses));
         }
         WebMetaData annotationMetaData = creator.create(eligibleAnnotatedClasses);
         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);
      }
   }

   protected Collection<Class<?>> getResourceInjectionEligibleWebAppClasses(ResourcesIndex resourceIndex, List<VirtualFile> classpath)
   {
      Set<Class<?>> eligibleClasses = new HashSet<Class<?>>();
     
      for (VirtualFile file : classpath)
      {
         // javax.servlet.Servlet type classes
         Collection<Class<? extends Servlet>> servlets = resourceIndex.getInheritedClasses(file, Servlet.class);
         if (servlets != null)
         {
            eligibleClasses.addAll(servlets);
         }
         // javax.servlet.Filter classes
         Collection<Class<? extends Filter>> filters = resourceIndex.getInheritedClasses(file, Filter.class);
         if (filters != null)
         {
            eligibleClasses.addAll(filters);
         }
         // javax.servlet.ServletContextListener classes
         Collection<Class<? extends ServletContextListener>> servletContextListeners = resourceIndex.getInheritedClasses(file, ServletContextListener.class);
         if (servletContextListeners != null)
         {
            eligibleClasses.addAll(servletContextListeners);
         }
         // javax.servlet.ServletContextAttributeListener classes
         Collection<Class<? extends ServletContextAttributeListener>> servletCtxAttributeListeners = resourceIndex.getInheritedClasses(file, ServletContextAttributeListener.class);
         if (servletCtxAttributeListeners != null)
         {
            eligibleClasses.addAll(servletCtxAttributeListeners);
         }
         // javax.servlet.ServletRequestListener classes
         Collection<Class<? extends ServletRequestListener>> servletRequestListeners = resourceIndex.getInheritedClasses(file, ServletRequestListener.class);
         if (servletRequestListeners != null)
         {
            eligibleClasses.addAll(servletRequestListeners);
         }
         // javax.servlet.ServletRequestAttributeListener classes
         Collection<Class<? extends ServletRequestAttributeListener>> servletRequestAttrListeners = resourceIndex.getInheritedClasses(file, ServletRequestAttributeListener.class);
         if (servletRequestAttrListeners != null)
         {
            eligibleClasses.addAll(servletRequestAttrListeners);
         }
         // javax.servlet.http.HttpSessionListener classes
         Collection<Class<? extends HttpSessionListener>> httpSessionListeners = resourceIndex.getInheritedClasses(file, HttpSessionListener.class);
         if (httpSessionListeners != null)
         {
            eligibleClasses.addAll(httpSessionListeners);
         }
         // javax.servlet.http.HttpSessionAttributeListener classes
         Collection<Class<? extends HttpSessionAttributeListener>> httpSessionAttrListeners = resourceIndex.getInheritedClasses(file, HttpSessionAttributeListener.class);
         if (httpSessionAttrListeners != null)
         {
            eligibleClasses.addAll(httpSessionAttrListeners);
         }
         // javax.servlet.AsyncListener classes
         Collection<Class<? extends AsyncListener>> asyncListeners = resourceIndex.getInheritedClasses(file, AsyncListener.class);
         if (asyncListeners != null)
         {
            eligibleClasses.addAll(asyncListeners);
         }
        
         // JSP resource injection support
         // javax.servlet.jsp.tagext.Tag classes
         Collection<Class<? extends Tag>> jspTags = resourceIndex.getInheritedClasses(file, Tag.class);
         if (jspTags != null)
         {
            eligibleClasses.addAll(jspTags);
         }
        
         // javax.servlet.jsp.tagext.SimpleTag classes
         Collection<Class<? extends SimpleTag>> simpleTags = resourceIndex.getInheritedClasses(file, SimpleTag.class);
         if (simpleTags != null)
         {
            eligibleClasses.addAll(simpleTags);
         }

      }
     
      return eligibleClasses;
   }
  
   private Collection<Class<?>> retainResourceInjectionEligibleWebAppClasses(Collection<Class<?>> specEligibleClasses, Collection<Class<?>> annotatedClasses)
   {
      Set<Class<?>> eligibleClasses = new HashSet<Class<?>>();
     
      for (Class<?> annotatedClass : annotatedClasses)
      {
         for (Class<?> specEligibleClass : specEligibleClasses)
         {
            if (annotatedClass.isAssignableFrom(specEligibleClass))
            {
               eligibleClasses.add(annotatedClass);
               break;
            }
         }
      }
      return eligibleClasses;
   }

}
TOP

Related Classes of org.jboss.web.deployers.SpecCompliantWarAnnotationDeployer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.