Search in sources :

Example 6 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class EjbResourceInjectionAnnotationProcessor method deploy.

public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final CompositeIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
    final List<AnnotationInstance> resourceAnnotations = index.getAnnotations(EJB_ANNOTATION_NAME);
    PropertyReplacer propertyReplacer = EJBAnnotationPropertyReplacement.propertyReplacer(deploymentUnit);
    for (AnnotationInstance annotation : resourceAnnotations) {
        final AnnotationTarget annotationTarget = annotation.target();
        final EJBResourceWrapper annotationWrapper = new EJBResourceWrapper(annotation, propertyReplacer);
        if (annotationTarget instanceof FieldInfo) {
            processField(deploymentUnit, annotationWrapper, (FieldInfo) annotationTarget, moduleDescription);
        } else if (annotationTarget instanceof MethodInfo) {
            processMethod(deploymentUnit, annotationWrapper, (MethodInfo) annotationTarget, moduleDescription);
        } else if (annotationTarget instanceof ClassInfo) {
            processClass(deploymentUnit, annotationWrapper, (ClassInfo) annotationTarget, moduleDescription);
        }
    }
    final List<AnnotationInstance> ejbsAnnotations = index.getAnnotations(EJBS_ANNOTATION_NAME);
    for (AnnotationInstance annotation : ejbsAnnotations) {
        final AnnotationTarget annotationTarget = annotation.target();
        if (annotationTarget instanceof ClassInfo) {
            final AnnotationValue annotationValue = annotation.value();
            final AnnotationInstance[] ejbAnnotations = annotationValue.asNestedArray();
            for (AnnotationInstance ejbAnnotation : ejbAnnotations) {
                final EJBResourceWrapper annotationWrapper = new EJBResourceWrapper(ejbAnnotation, propertyReplacer);
                processClass(deploymentUnit, annotationWrapper, (ClassInfo) annotationTarget, moduleDescription);
            }
        } else {
            throw EjbLogger.ROOT_LOGGER.annotationOnlyAllowedOnClass(EJBs.class.getName(), annotation.target());
        }
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) EJBs(javax.ejb.EJBs) AnnotationValue(org.jboss.jandex.AnnotationValue) PropertyReplacer(org.jboss.metadata.property.PropertyReplacer) MethodInfo(org.jboss.jandex.MethodInfo) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) AnnotationInstance(org.jboss.jandex.AnnotationInstance) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Example 7 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class MessageDrivenComponentDescriptionFactory method processMessageBeans.

private void processMessageBeans(final DeploymentUnit deploymentUnit, final Collection<AnnotationInstance> messageBeanAnnotations, final CompositeIndex compositeIndex) throws DeploymentUnitProcessingException {
    if (messageBeanAnnotations.isEmpty())
        return;
    final EjbJarDescription ejbJarDescription = getEjbJarDescription(deploymentUnit);
    final PropertyReplacer propertyReplacer = EJBAnnotationPropertyReplacement.propertyReplacer(deploymentUnit);
    final ServiceName deploymentUnitServiceName = deploymentUnit.getServiceName();
    DeploymentDescriptorEnvironment deploymentDescriptorEnvironment = null;
    for (final AnnotationInstance messageBeanAnnotation : messageBeanAnnotations) {
        final AnnotationTarget target = messageBeanAnnotation.target();
        final ClassInfo beanClassInfo = (ClassInfo) target;
        if (!assertMDBClassValidity(beanClassInfo)) {
            continue;
        }
        final String ejbName = beanClassInfo.name().local();
        final AnnotationValue nameValue = messageBeanAnnotation.value("name");
        final String beanName = (nameValue == null || nameValue.asString().isEmpty()) ? ejbName : propertyReplacer.replaceProperties(nameValue.asString());
        final MessageDrivenBeanMetaData beanMetaData = getEnterpriseBeanMetaData(deploymentUnit, beanName, MessageDrivenBeanMetaData.class);
        final String beanClassName;
        final String messageListenerInterfaceName;
        final Properties activationConfigProperties = getActivationConfigProperties(messageBeanAnnotation, propertyReplacer);
        final String messagingType;
        if (beanMetaData != null) {
            beanClassName = override(beanClassInfo.name().toString(), beanMetaData.getEjbClass());
            deploymentDescriptorEnvironment = new DeploymentDescriptorEnvironment("java:comp/env/", beanMetaData);
            if (beanMetaData instanceof MessageDrivenBeanMetaData) {
                //It may actually be GenericBeanMetadata instance
                final MessageDrivenBeanMetaData mdb = (MessageDrivenBeanMetaData) beanMetaData;
                messagingType = mdb.getMessagingType();
                final ActivationConfigMetaData activationConfigMetaData = mdb.getActivationConfig();
                if (activationConfigMetaData != null) {
                    final ActivationConfigPropertiesMetaData propertiesMetaData = activationConfigMetaData.getActivationConfigProperties();
                    if (propertiesMetaData != null) {
                        for (final ActivationConfigPropertyMetaData propertyMetaData : propertiesMetaData) {
                            activationConfigProperties.put(propertyMetaData.getKey(), propertyMetaData.getValue());
                        }
                    }
                }
            } else if (beanMetaData instanceof JBossGenericBeanMetaData) {
                //TODO: fix the hierarchy so this is not needed
                final JBossGenericBeanMetaData mdb = (JBossGenericBeanMetaData) beanMetaData;
                messagingType = mdb.getMessagingType();
                final ActivationConfigMetaData activationConfigMetaData = mdb.getActivationConfig();
                if (activationConfigMetaData != null) {
                    final ActivationConfigPropertiesMetaData propertiesMetaData = activationConfigMetaData.getActivationConfigProperties();
                    if (propertiesMetaData != null) {
                        for (final ActivationConfigPropertyMetaData propertyMetaData : propertiesMetaData) {
                            activationConfigProperties.put(propertyMetaData.getKey(), propertyMetaData.getValue());
                        }
                    }
                }
            } else {
                messagingType = null;
            }
            messageListenerInterfaceName = messagingType != null ? messagingType : getMessageListenerInterface(compositeIndex, messageBeanAnnotation);
        } else {
            beanClassName = beanClassInfo.name().toString();
            messageListenerInterfaceName = getMessageListenerInterface(compositeIndex, messageBeanAnnotation);
        }
        final String defaultResourceAdapterName = this.getDefaultResourceAdapterName(deploymentUnit.getServiceRegistry());
        final MessageDrivenComponentDescription beanDescription = new MessageDrivenComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnitServiceName, messageListenerInterfaceName, activationConfigProperties, defaultResourceAdapterName, beanMetaData);
        beanDescription.setDeploymentDescriptorEnvironment(deploymentDescriptorEnvironment);
        addComponent(deploymentUnit, beanDescription);
    }
    EjbDeploymentMarker.mark(deploymentUnit);
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) MessageDrivenBeanMetaData(org.jboss.metadata.ejb.spec.MessageDrivenBeanMetaData) ActivationConfigPropertyMetaData(org.jboss.metadata.ejb.spec.ActivationConfigPropertyMetaData) ActivationConfigMetaData(org.jboss.metadata.ejb.spec.ActivationConfigMetaData) Properties(java.util.Properties) ActivationConfigPropertiesMetaData(org.jboss.metadata.ejb.spec.ActivationConfigPropertiesMetaData) JBossGenericBeanMetaData(org.jboss.metadata.ejb.jboss.ejb3.JBossGenericBeanMetaData) ServiceName(org.jboss.msc.service.ServiceName) MessageDrivenComponentDescription(org.jboss.as.ejb3.component.messagedriven.MessageDrivenComponentDescription) DeploymentDescriptorEnvironment(org.jboss.as.ee.component.DeploymentDescriptorEnvironment) EjbJarDescription(org.jboss.as.ejb3.deployment.EjbJarDescription) AbstractDeploymentUnitProcessor.getEjbJarDescription(org.jboss.as.ejb3.deployment.processors.AbstractDeploymentUnitProcessor.getEjbJarDescription) AnnotationValue(org.jboss.jandex.AnnotationValue) PropertyReplacer(org.jboss.metadata.property.PropertyReplacer) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 8 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class MessageDrivenComponentDescriptionFactory method getActivationConfigProperties.

private Properties getActivationConfigProperties(final AnnotationInstance messageBeanAnnotation, PropertyReplacer propertyReplacer) {
    final Properties props = new Properties();
    final AnnotationValue activationConfig = messageBeanAnnotation.value("activationConfig");
    if (activationConfig == null)
        return props;
    for (final AnnotationInstance propAnnotation : activationConfig.asNestedArray()) {
        String propertyName = propAnnotation.value("propertyName").asString();
        String propertyValue = propAnnotation.value("propertyValue").asString();
        props.put(propertyReplacer.replaceProperties(propertyName), propertyReplacer.replaceProperties(propertyValue));
    }
    return props;
}
Also used : AnnotationValue(org.jboss.jandex.AnnotationValue) Properties(java.util.Properties) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 9 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance in project wildfly by wildfly.

the class HibernateAnnotationScanner method getClassesInJar.

@Override
public Set<Class<?>> getClassesInJar(URL jarToScan, Set<Class<? extends Annotation>> annotationsToLookFor) {
    if (jarToScan == null) {
        throw JPA_LOGGER.nullVar("jarToScan");
    }
    JPA_LOGGER.tracef("getClassesInJar url=%s annotations=%s", jarToScan.getPath(), annotationsToLookFor);
    PersistenceUnitMetadata pu = PERSISTENCE_UNIT_METADATA_TLS.get();
    if (pu == null) {
        throw JPA_LOGGER.missingPersistenceUnitMetadata();
    }
    if (pu.getAnnotationIndex() != null) {
        Index index = getJarFileIndex(jarToScan, pu);
        if (index == null) {
            JPA_LOGGER.tracef("No classes to scan for annotations in jar '%s' (jars with classes '%s')", jarToScan, pu.getAnnotationIndex().keySet());
            return new HashSet<Class<?>>();
        }
        if (annotationsToLookFor == null) {
            throw JPA_LOGGER.nullVar("annotationsToLookFor");
        }
        if (annotationsToLookFor.size() == 0) {
            throw JPA_LOGGER.emptyParameter("annotationsToLookFor");
        }
        Set<Class<?>> result = new HashSet<Class<?>>();
        for (Class<? extends Annotation> annClass : annotationsToLookFor) {
            DotName annotation = DotName.createSimple(annClass.getName());
            List<AnnotationInstance> classesWithAnnotation = index.getAnnotations(annotation);
            Set<Class<?>> classesForAnnotation = new HashSet<Class<?>>();
            for (AnnotationInstance annotationInstance : classesWithAnnotation) {
                // may generate bytecode with annotations placed on methods (see AS7-2559)
                if (annotationInstance.target() instanceof ClassInfo) {
                    String className = annotationInstance.target().toString();
                    try {
                        JPA_LOGGER.tracef("getClassesInJar found class %s with annotation %s", className, annClass.getName());
                        Class<?> clazz = pu.cacheTempClassLoader().loadClass(className);
                        result.add(clazz);
                        classesForAnnotation.add(clazz);
                    } catch (ClassNotFoundException e) {
                        JPA_LOGGER.cannotLoadEntityClass(e, className);
                    } catch (NoClassDefFoundError e) {
                        JPA_LOGGER.cannotLoadEntityClass(e, className);
                    }
                }
            }
            cacheClasses(pu, jarToScan, annClass, classesForAnnotation);
        }
        return result;
    } else {
        return getCachedClasses(pu, jarToScan, annotationsToLookFor);
    }
}
Also used : Index(org.jboss.jandex.Index) DotName(org.jboss.jandex.DotName) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) AnnotationInstance(org.jboss.jandex.AnnotationInstance) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 10 with AnnotationInstance

use of org.jboss.jandex.AnnotationInstance 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)

Aggregations

AnnotationInstance (org.jboss.jandex.AnnotationInstance)40 ClassInfo (org.jboss.jandex.ClassInfo)26 CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)17 AnnotationTarget (org.jboss.jandex.AnnotationTarget)16 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)15 AnnotationValue (org.jboss.jandex.AnnotationValue)11 MethodInfo (org.jboss.jandex.MethodInfo)11 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)9 HashSet (java.util.HashSet)8 DotName (org.jboss.jandex.DotName)7 FieldInfo (org.jboss.jandex.FieldInfo)7 ArrayList (java.util.ArrayList)5 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)5 PropertyReplacer (org.jboss.metadata.property.PropertyReplacer)5 HashMap (java.util.HashMap)4 EEApplicationClasses (org.jboss.as.ee.component.EEApplicationClasses)4 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)3 Module (org.jboss.modules.Module)3 PersistenceUnitMetadata (org.jipijapa.plugin.spi.PersistenceUnitMetadata)3 URL (java.net.URL)2