Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat 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.as.jaxrs.deployment;
 
 import static org.jboss.as.jaxrs.JaxrsLogger.JAXRS_LOGGER;
 import static org.jboss.as.jaxrs.JaxrsMessages.MESSAGES;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 import  org.jboss.metadata.javaee.spec.ParamValueMetaData;
 import  org.jboss.metadata.web.jboss.JBossWebMetaData;
 import  org.jboss.metadata.web.spec.FilterMetaData;
 import  org.jboss.metadata.web.spec.ServletMetaData;
Processor that finds jax-rs classes in the deployment

Author(s):
Stuart Douglas
 
 public class JaxrsScanningProcessor implements DeploymentUnitProcessor {
 
     public static final DotName APPLICATION = DotName.createSimple(Application.class.getName());
 
     private static CompositeIndex[] EMPTY_INDEXES = new CompositeIndex[0];
 
     @Override
     public void deploy(DeploymentPhaseContext phaseContextthrows DeploymentUnitProcessingException {
         final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
         if (!JaxrsDeploymentMarker.isJaxrsDeployment(deploymentUnit)) {
             return;
         }
         final DeploymentUnit parent = deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();
         final Map<ModuleIdentifierResteasyDeploymentDatadeploymentData;
         if(deploymentUnit.getParent() == null) {
             deploymentData = Collections.synchronizedMap(new HashMap<ModuleIdentifierResteasyDeploymentData>());
             deploymentUnit.putAttachment(.deploymentData);
         } else {
             deploymentData = parent.getAttachment(.);
         }
 
         final ModuleIdentifier moduleIdentifier = deploymentUnit.getAttachment(.);
 
         ResteasyDeploymentData resteasyDeploymentData = new ResteasyDeploymentData();
         final WarMetaData warMetaData = deploymentUnit.getAttachment(.);
         final Module module = deploymentUnit.getAttachment(.);
 
         try {
 
             if (warMetaData == null) {
                resteasyDeploymentData.setScanAll(true);
                scan(deploymentUnitmodule.getClassLoader(), resteasyDeploymentDataserviceController.getValue(), false);
                deploymentData.put(moduleIdentifierresteasyDeploymentData);
            } else {
                scanWebDeployment(deploymentUnitwarMetaData.getMergedJBossWebMetaData(), module.getClassLoader(), resteasyDeploymentData);
                scan(deploymentUnitmodule.getClassLoader(), resteasyDeploymentDataserviceController.getValue(), true);
            }
            deploymentUnit.putAttachment(.resteasyDeploymentData);
        } catch (ModuleLoadException e) {
            throw new DeploymentUnitProcessingException(e);
        }
    }
    @Override
    public void undeploy(DeploymentUnit context) {
    }
    public static final Set<StringBOOT_CLASSES = new HashSet<String>();
    static {
        for (String clazz : .) {
            .add(clazz);
        }
    }

    
If any servlet/filter classes are declared, then we probably don't want to scan.
    protected boolean hasBootClasses(JBossWebMetaData webdatathrows DeploymentUnitProcessingException {
        if (webdata.getServlets() != null) {
            for (ServletMetaData servlet : webdata.getServlets()) {
                String servletClass = servlet.getServletClass();
                if (.contains(servletClass))
                    return true;
            }
        }
        if (webdata.getFilters() != null) {
            for (FilterMetaData filter : webdata.getFilters()) {
                if (.contains(filter.getFilterClass()))
                    return true;
            }
        }
        return false;
    }
    protected void scanWebDeployment(final DeploymentUnit dufinal JBossWebMetaData webdatafinal ClassLoader classLoaderfinal ResteasyDeploymentData resteasyDeploymentDatathrows DeploymentUnitProcessingException {
        // If there is a resteasy boot class in web.xml, then the default should be to not scan
        // make sure this call happens before checkDeclaredApplicationClassAsServlet!!!
        boolean hasBoot = hasBootClasses(webdata);
        resteasyDeploymentData.setBootClasses(hasBoot);
        Class<?> declaredApplicationClass = checkDeclaredApplicationClassAsServlet(webdataclassLoader);
        // Assume that checkDeclaredApplicationClassAsServlet created the dispatcher
        if (declaredApplicationClass != null) {
            resteasyDeploymentData.setDispatcherCreated(true);
        }
        // set scanning on only if there are no boot classes
        if (!hasBoot && !webdata.isMetadataComplete()) {
            resteasyDeploymentData.setScanAll(true);
            resteasyDeploymentData.setScanProviders(true);
            resteasyDeploymentData.setScanResources(true);
        }
        // check resteasy configuration flags
        List<ParamValueMetaData> contextParams = webdata.getContextParams();
        if (contextParams != null) {
            for (ParamValueMetaData param : contextParams) {
                if (param.getParamName().equals(.)) {
                    resteasyDeploymentData.setScanAll(Boolean.valueOf(param.getParamValue()));
                } else if (param.getParamName().equals(.)) {
                    resteasyDeploymentData.setScanProviders(Boolean.valueOf(param.getParamValue()));
                } else if (param.getParamName().equals(.)) {
                    resteasyDeploymentData.setScanResources(Boolean.valueOf(param.getParamValue()));
                } else if (param.getParamName().equals(.)) {
                    resteasyDeploymentData.setUnwrappedExceptionsParameterSet(true);
                }
            }
        }
    }
    protected void scan(final DeploymentUnit dufinal ClassLoader classLoaderfinal ResteasyDeploymentData resteasyDeploymentDatafinal ModuleIndexService moduleIndexServiceboolean webDeployment)
            throws DeploymentUnitProcessingExceptionModuleLoadException {
        if (!resteasyDeploymentData.shouldScan()) {
            return;
        }
        final Set<ClassInfoapplicationClass = index.getAllKnownSubclasses();
        try {
            if (applicationClass.size() > 1) {
                StringBuilder builder = new StringBuilder();
                Set<ClassInfoaClasses = new HashSet<ClassInfo>();
                for (ClassInfo c : applicationClass) {
                    if (!Modifier.isAbstract(c.flags())) {
                        aClasses.add(c);
                    }
                    builder.append(" ").append(c.name().toString());
                }
                if (aClasses.size() > 1) {
                    throw new DeploymentUnitProcessingException(.onlyOneApplicationClassAllowed(builder));
                } else if (aClasses.size() == 1) {
                    ClassInfo aClass = applicationClass.iterator().next();
                    resteasyDeploymentData.setScannedApplicationClass((Class<? extends Application>) classLoader
                            .loadClass(aClass.name().toString()));
                }
            } else if (applicationClass.size() == 1) {
                ClassInfo aClass = applicationClass.iterator().next();
                resteasyDeploymentData.setScannedApplicationClass((Class<? extends Application>) classLoader
                        .loadClass(aClass.name().toString()));
            }
        } catch (ClassNotFoundException e) {
            throw .cannotLoadApplicationClass(e);
        }
        List<AnnotationInstanceresources = null;
        List<AnnotationInstanceproviders = null;
        if (resteasyDeploymentData.isScanResources()) {
            resources = index.getAnnotations(..getDotName());
        }
        if (resteasyDeploymentData.isScanProviders()) {
            providers = index.getAnnotations(..getDotName());
        }
        if ((resources == null || resources.isEmpty()) && (providers == null || providers.isEmpty()))
            return;
        final Set<ClassInfopathInterfaces = new HashSet<ClassInfo>();
        if (resources != null) {
            for (AnnotationInstance e : resources) {
                final ClassInfo info;
                if (e.target() instanceof ClassInfo) {
                    info = (ClassInfoe.target();
                } else if (e.target() instanceof MethodInfo) {
                    //ignore
                    continue;
                } else {
                    .classOrMethodAnnotationNotFound("@Path"e.target());
                    continue;
                }
                if (!Modifier.isInterface(info.flags())) {
                    resteasyDeploymentData.getScannedResourceClasses().add(info.name().toString());
                } else {
                    pathInterfaces.add(info);
                }
            }
        }
        if (providers != null) {
            for (AnnotationInstance e : providers) {
                if (e.target() instanceof ClassInfo) {
                    ClassInfo info = (ClassInfoe.target();
                    if (!Modifier.isInterface(info.flags())) {
                        resteasyDeploymentData.getScannedProviderClasses().add(info.name().toString());
                    }
                } else {
                    .classAnnotationNotFound("@Provider"e.target());
                }
            }
        }
        // look for all implementations of interfaces annotated @Path
        for (final ClassInfo iface : pathInterfaces) {
            final Set<ClassInfoimplementors = index.getAllKnownImplementors(iface.name());
            for (final ClassInfo implementor : implementors) {
                resteasyDeploymentData.getScannedResourceClasses().add(implementor.name().toString());
            }
        }
    }
    protected Class<?> checkDeclaredApplicationClassAsServlet(JBossWebMetaData webData,
                                                              ClassLoader classLoaderthrows DeploymentUnitProcessingException {
        if (webData.getServlets() == null)
            return null;
        for (ServletMetaData servlet : webData.getServlets()) {
            String servletClass = servlet.getServletClass();
            if (servletClass == null)
                continue;
            Class<?> clazz = null;
            try {
                clazz = classLoader.loadClass(servletClass);
            } catch (ClassNotFoundException e) {
                throw new DeploymentUnitProcessingException(e);
            }
            if (Application.class.isAssignableFrom(clazz)) {
                servlet.setServletClass(HttpServlet30Dispatcher.class.getName());
                servlet.setAsyncSupported(true);
                ParamValueMetaData param = new ParamValueMetaData();
                param.setParamName("javax.ws.rs.Application");
                param.setParamValue(servletClass);
                List<ParamValueMetaData> params = servlet.getInitParam();
                if (params == null) {
                    params = new ArrayList<ParamValueMetaData>();
                    servlet.setInitParam(params);
                }
                params.add(param);
                return clazz;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X