Search in sources :

Example 11 with CompositeIndex

use of org.jboss.as.server.deployment.annotation.CompositeIndex in project wildfly by wildfly.

the class ServletContainerInitializerDeploymentProcessor method deploy.

/**
     * Process SCIs.
     */
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final ModuleSpecification moduleSpecification = deploymentUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
    final ServiceModuleLoader loader = deploymentUnit.getAttachment(Attachments.SERVICE_MODULE_LOADER);
    if (!DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
        // Skip non web deployments
        return;
    }
    WarMetaData warMetaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
    assert warMetaData != null;
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    if (module == null) {
        throw UndertowLogger.ROOT_LOGGER.failedToResolveModule(deploymentUnit);
    }
    final ClassLoader classLoader = module.getClassLoader();
    ScisMetaData scisMetaData = deploymentUnit.getAttachment(ScisMetaData.ATTACHMENT_KEY);
    if (scisMetaData == null) {
        scisMetaData = new ScisMetaData();
        deploymentUnit.putAttachment(ScisMetaData.ATTACHMENT_KEY, scisMetaData);
    }
    Set<ServletContainerInitializer> scis = scisMetaData.getScis();
    Set<Class<? extends ServletContainerInitializer>> sciClasses = new HashSet<>();
    if (scis == null) {
        scis = new HashSet<ServletContainerInitializer>();
        scisMetaData.setScis(scis);
    }
    Map<ServletContainerInitializer, Set<Class<?>>> handlesTypes = scisMetaData.getHandlesTypes();
    if (handlesTypes == null) {
        handlesTypes = new HashMap<ServletContainerInitializer, Set<Class<?>>>();
        scisMetaData.setHandlesTypes(handlesTypes);
    }
    // Find the SCIs from shared modules
    for (ModuleDependency dependency : moduleSpecification.getAllDependencies()) {
        try {
            Module depModule = loader.loadModule(dependency.getIdentifier());
            ServiceLoader<ServletContainerInitializer> serviceLoader = depModule.loadService(ServletContainerInitializer.class);
            for (ServletContainerInitializer service : serviceLoader) {
                if (sciClasses.add(service.getClass())) {
                    scis.add(service);
                }
            }
        } catch (ModuleLoadException e) {
            if (dependency.isOptional() == false) {
                throw UndertowLogger.ROOT_LOGGER.errorLoadingSCIFromModule(dependency.getIdentifier(), e);
            }
        }
    }
    // Find local ServletContainerInitializer services
    List<String> order = warMetaData.getOrder();
    Map<String, VirtualFile> localScis = warMetaData.getScis();
    if (order != null && localScis != null) {
        for (String jar : order) {
            VirtualFile sci = localScis.get(jar);
            if (sci != null) {
                scis.addAll(loadSci(classLoader, sci, jar, true, sciClasses));
            }
        }
    }
    // Process HandlesTypes for ServletContainerInitializer
    Map<Class<?>, Set<ServletContainerInitializer>> typesMap = new HashMap<Class<?>, Set<ServletContainerInitializer>>();
    for (ServletContainerInitializer service : scis) {
        if (service.getClass().isAnnotationPresent(HandlesTypes.class)) {
            HandlesTypes handlesTypesAnnotation = service.getClass().getAnnotation(HandlesTypes.class);
            Class<?>[] typesArray = handlesTypesAnnotation.value();
            if (typesArray != null) {
                for (Class<?> type : typesArray) {
                    Set<ServletContainerInitializer> servicesSet = typesMap.get(type);
                    if (servicesSet == null) {
                        servicesSet = new HashSet<ServletContainerInitializer>();
                        typesMap.put(type, servicesSet);
                    }
                    servicesSet.add(service);
                    handlesTypes.put(service, new HashSet<Class<?>>());
                }
            }
        }
    }
    Class<?>[] typesArray = typesMap.keySet().toArray(new Class<?>[0]);
    final CompositeIndex index = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
    if (index == null) {
        throw UndertowLogger.ROOT_LOGGER.unableToResolveAnnotationIndex(deploymentUnit);
    }
    //WFLY-4205, look in the parent as well as the war
    CompositeIndex parentIndex = deploymentUnit.getParent() == null ? null : deploymentUnit.getParent().getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
    // Find classes which extend, implement, or are annotated by HandlesTypes
    for (Class<?> type : typesArray) {
        DotName className = DotName.createSimple(type.getName());
        Set<ClassInfo> classInfos = new HashSet<>();
        classInfos.addAll(processHandlesType(className, type, index));
        if (parentIndex != null) {
            classInfos.addAll(processHandlesType(className, type, parentIndex));
        }
        Set<Class<?>> classes = loadClassInfoSet(classInfos, classLoader);
        Set<ServletContainerInitializer> sciSet = typesMap.get(type);
        for (ServletContainerInitializer sci : sciSet) {
            handlesTypes.get(sci).addAll(classes);
        }
    }
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) VirtualFile(org.jboss.vfs.VirtualFile) HashSet(java.util.HashSet) Set(java.util.Set) ModuleDependency(org.jboss.as.server.deployment.module.ModuleDependency) HashMap(java.util.HashMap) WarMetaData(org.jboss.as.web.common.WarMetaData) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) DotName(org.jboss.jandex.DotName) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) HandlesTypes(javax.servlet.annotation.HandlesTypes) HashSet(java.util.HashSet) ServiceModuleLoader(org.jboss.as.server.moduleservice.ServiceModuleLoader) ModuleSpecification(org.jboss.as.server.deployment.module.ModuleSpecification) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) ClassInfo(org.jboss.jandex.ClassInfo)

Example 12 with CompositeIndex

use of org.jboss.as.server.deployment.annotation.CompositeIndex in project wildfly by wildfly.

the class WebComponentProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
        return;
    }
    final Map<String, ComponentDescription> componentByClass = new HashMap<String, ComponentDescription>();
    final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final EEApplicationClasses applicationClassesDescription = deploymentUnit.getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
    final CompositeIndex compositeIndex = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
    if (moduleDescription == null) {
        //not an EE deployment
        return;
    }
    for (ComponentDescription component : moduleDescription.getComponentDescriptions()) {
        componentByClass.put(component.getComponentClassName(), component);
    }
    final WarMetaData warMetaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
    final TldsMetaData tldsMetaData = deploymentUnit.getAttachment(TldsMetaData.ATTACHMENT_KEY);
    final Set<String> classes = getAllComponentClasses(deploymentUnit, compositeIndex, warMetaData, tldsMetaData);
    for (String clazz : classes) {
        if (clazz == null || clazz.trim().isEmpty()) {
            continue;
        }
        ComponentDescription description = componentByClass.get(clazz);
        if (description != null) {
            //TODO: make sure the component is a managed bean
            if (!(description.getViews().size() == 1)) {
                throw UndertowLogger.ROOT_LOGGER.wrongComponentType(clazz);
            }
        } else {
            //we do not make the standard tags into components, as there is no need
            if (compositeIndex.getClassByName(DotName.createSimple(clazz)) == null) {
                boolean found = false;
                for (String pack : BUILTIN_TAGLIBS) {
                    if (clazz.startsWith(pack)) {
                        found = true;
                        break;
                    }
                }
                if (found) {
                    continue;
                }
            }
            description = new WebComponentDescription(clazz, clazz, moduleDescription, deploymentUnit.getServiceName(), applicationClassesDescription);
            moduleDescription.addComponent(description);
            deploymentUnit.addToAttachmentList(WebComponentDescription.WEB_COMPONENTS, description.getStartServiceName());
        }
    }
}
Also used : ComponentDescription(org.jboss.as.ee.component.ComponentDescription) WebComponentDescription(org.jboss.as.web.common.WebComponentDescription) HashMap(java.util.HashMap) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) WarMetaData(org.jboss.as.web.common.WarMetaData) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) WebComponentDescription(org.jboss.as.web.common.WebComponentDescription) EEApplicationClasses(org.jboss.as.ee.component.EEApplicationClasses) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 13 with CompositeIndex

use of org.jboss.as.server.deployment.annotation.CompositeIndex in project wildfly by wildfly.

the class UndertowJSRWebSocketDeploymentProcessor method deploy.

@Override
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    if (module == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(module.getClassLoader());
        WarMetaData metaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
        if (metaData == null || metaData.getMergedJBossWebMetaData() == null) {
            return;
        }
        if (!metaData.getMergedJBossWebMetaData().isEnableWebSockets()) {
            return;
        }
        final Set<Class<?>> annotatedEndpoints = new HashSet<>();
        final Set<Class<? extends Endpoint>> endpoints = new HashSet<>();
        final Set<Class<? extends ServerApplicationConfig>> config = new HashSet<>();
        final CompositeIndex index = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
        final List<AnnotationInstance> serverEndpoints = index.getAnnotations(SERVER_ENDPOINT);
        if (serverEndpoints != null) {
            for (AnnotationInstance endpoint : serverEndpoints) {
                if (endpoint.target() instanceof ClassInfo) {
                    ClassInfo clazz = (ClassInfo) endpoint.target();
                    try {
                        Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                        if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                            annotatedEndpoints.add(moduleClass);
                        }
                    } catch (ClassNotFoundException e) {
                        UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketEndpoint(clazz.name().toString(), e);
                    }
                }
            }
        }
        final List<AnnotationInstance> clientEndpoints = index.getAnnotations(CLIENT_ENDPOINT);
        if (clientEndpoints != null) {
            for (AnnotationInstance endpoint : clientEndpoints) {
                if (endpoint.target() instanceof ClassInfo) {
                    ClassInfo clazz = (ClassInfo) endpoint.target();
                    try {
                        Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                        if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                            annotatedEndpoints.add(moduleClass);
                        }
                    } catch (ClassNotFoundException e) {
                        UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketEndpoint(clazz.name().toString(), e);
                    }
                }
            }
        }
        final Set<ClassInfo> subclasses = index.getAllKnownImplementors(SERVER_APPLICATION_CONFIG);
        if (subclasses != null) {
            for (final ClassInfo clazz : subclasses) {
                try {
                    Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                    if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                        config.add((Class) moduleClass);
                    }
                } catch (ClassNotFoundException e) {
                    UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketConfig(clazz.name().toString(), e);
                }
            }
        }
        final Set<ClassInfo> epClasses = index.getAllKnownSubclasses(ENDPOINT);
        if (epClasses != null) {
            for (final ClassInfo clazz : epClasses) {
                try {
                    Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                    if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                        endpoints.add((Class) moduleClass);
                    }
                } catch (ClassNotFoundException e) {
                    UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketConfig(clazz.name().toString(), e);
                }
            }
        }
        WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
        doDeployment(deploymentUnit, webSocketDeploymentInfo, annotatedEndpoints, config, endpoints);
        installWebsockets(phaseContext, webSocketDeploymentInfo);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
    }
}
Also used : WarMetaData(org.jboss.as.web.common.WarMetaData) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) AnnotationInstance(org.jboss.jandex.AnnotationInstance) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 14 with CompositeIndex

use of org.jboss.as.server.deployment.annotation.CompositeIndex in project wildfly by wildfly.

the class WarAnnotationDeploymentProcessor method deploy.

/**
     * Process web annotations.
     */
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
        // Skip non web deployments
        return;
    }
    WarMetaData warMetaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
    assert warMetaData != null;
    Map<String, WebMetaData> annotationsMetaData = warMetaData.getAnnotationsMetaData();
    if (annotationsMetaData == null) {
        annotationsMetaData = new HashMap<String, WebMetaData>();
        warMetaData.setAnnotationsMetaData(annotationsMetaData);
    }
    Map<ResourceRoot, Index> indexes = AnnotationIndexUtils.getAnnotationIndexes(deploymentUnit);
    // Process lib/*.jar
    for (final Entry<ResourceRoot, Index> entry : indexes.entrySet()) {
        final Index jarIndex = entry.getValue();
        annotationsMetaData.put(entry.getKey().getRootName(), processAnnotations(jarIndex));
    }
    Map<ModuleIdentifier, CompositeIndex> additionalModelAnnotations = deploymentUnit.getAttachment(Attachments.ADDITIONAL_ANNOTATION_INDEXES_BY_MODULE);
    if (additionalModelAnnotations != null) {
        final List<WebMetaData> additional = new ArrayList<WebMetaData>();
        for (Entry<ModuleIdentifier, CompositeIndex> entry : additionalModelAnnotations.entrySet()) {
            for (Index index : entry.getValue().getIndexes()) {
                additional.add(processAnnotations(index));
            }
        }
        warMetaData.setAdditionalModuleAnnotationsMetadata(additional);
    }
}
Also used : WarMetaData(org.jboss.as.web.common.WarMetaData) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) ArrayList(java.util.ArrayList) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) Index(org.jboss.jandex.Index) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) WebMetaData(org.jboss.metadata.web.spec.WebMetaData)

Example 15 with CompositeIndex

use of org.jboss.as.server.deployment.annotation.CompositeIndex in project wildfly by wildfly.

the class WSClassVerificationProcessor method verifyApacheCXFModuleDependencyRequirement.

private void verifyApacheCXFModuleDependencyRequirement(DeploymentUnit unit) {
    if (!hasCxfModuleDependency(unit)) {
        //notify user if he clearly forgot the CXF module dependency
        final CompositeIndex index = unit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
        final DotName[] dotNames = { WEB_SERVICE_ANNOTATION, WEB_SERVICE_PROVIDER_ANNOTATION };
        for (final DotName dotName : dotNames) {
            for (AnnotationInstance ai : index.getAnnotations(dotName)) {
                AnnotationTarget at = ai.target();
                if (at instanceof ClassInfo) {
                    final ClassInfo clazz = (ClassInfo) ai.target();
                    for (DotName dn : clazz.annotations().keySet()) {
                        if (dn.toString().startsWith("org.apache.cxf")) {
                            WSLogger.ROOT_LOGGER.missingModuleDependency(dn.toString(), clazz.name().toString(), "org.apache.cxf");
                        }
                    }
                }
            }
        }
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) DotName(org.jboss.jandex.DotName) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)38 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)29 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)17 AnnotationInstance (org.jboss.jandex.AnnotationInstance)17 ClassInfo (org.jboss.jandex.ClassInfo)12 HashSet (java.util.HashSet)6 EEApplicationClasses (org.jboss.as.ee.component.EEApplicationClasses)6 AnnotationTarget (org.jboss.jandex.AnnotationTarget)6 DotName (org.jboss.jandex.DotName)6 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)5 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)5 PropertyReplacer (org.jboss.metadata.property.PropertyReplacer)5 Module (org.jboss.modules.Module)5 HashMap (java.util.HashMap)4 WarMetaData (org.jboss.as.web.common.WarMetaData)4 AnnotationValue (org.jboss.jandex.AnnotationValue)4 ModuleDependency (org.jboss.as.server.deployment.module.ModuleDependency)3 MethodInfo (org.jboss.jandex.MethodInfo)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2