Search in sources :

Example 21 with AnnotationValue

use of org.jboss.jandex.AnnotationValue in project wildfly-swarm by wildfly-swarm.

the class JandexUtil method getRepeatableAnnotation.

/**
 * Many OAI annotations can either be found singly or as a wrapped array.  This method will
 * look for both and return a list of all found.  Both the single and wrapper annotation names
 * must be provided.
 * @param method
 * @param singleAnnotationName
 * @param repeatableAnnotationName
 */
public static List<AnnotationInstance> getRepeatableAnnotation(MethodInfo method, DotName singleAnnotationName, DotName repeatableAnnotationName) {
    List<AnnotationInstance> annotations = new ArrayList<>(method.annotations());
    CollectionUtils.filter(annotations, new Predicate() {

        @Override
        public boolean evaluate(Object object) {
            AnnotationInstance annotation = (AnnotationInstance) object;
            return annotation.name().equals(singleAnnotationName);
        }
    });
    if (repeatableAnnotationName != null && method.hasAnnotation(repeatableAnnotationName)) {
        AnnotationInstance annotation = method.annotation(repeatableAnnotationName);
        AnnotationValue annotationValue = annotation.value();
        if (annotationValue != null) {
            AnnotationInstance[] nestedArray = annotationValue.asNestedArray();
            annotations.addAll(Arrays.asList(nestedArray));
        }
    }
    return annotations;
}
Also used : ArrayList(java.util.ArrayList) AnnotationValue(org.jboss.jandex.AnnotationValue) AnnotationInstance(org.jboss.jandex.AnnotationInstance) Predicate(org.apache.commons.collections.Predicate)

Example 22 with AnnotationValue

use of org.jboss.jandex.AnnotationValue in project wildfly-swarm by wildfly-swarm.

the class JandexUtil method getRepeatableAnnotation.

/**
 * Many OAI annotations can either be found singly or as a wrapped array.  This method will
 * look for both and return a list of all found.  Both the single and wrapper annotation names
 * must be provided.
 * @param clazz
 * @param singleAnnotationName
 * @param repeatableAnnotationName
 */
public static List<AnnotationInstance> getRepeatableAnnotation(ClassInfo clazz, DotName singleAnnotationName, DotName repeatableAnnotationName) {
    List<AnnotationInstance> annotations = new ArrayList<>();
    AnnotationInstance single = JandexUtil.getClassAnnotation(clazz, singleAnnotationName);
    AnnotationInstance repeatable = JandexUtil.getClassAnnotation(clazz, repeatableAnnotationName);
    if (single != null) {
        annotations.add(single);
    }
    if (repeatable != null) {
        AnnotationValue annotationValue = repeatable.value();
        if (annotationValue != null) {
            AnnotationInstance[] nestedArray = annotationValue.asNestedArray();
            annotations.addAll(Arrays.asList(nestedArray));
        }
    }
    return annotations;
}
Also used : ArrayList(java.util.ArrayList) AnnotationValue(org.jboss.jandex.AnnotationValue) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 23 with AnnotationValue

use of org.jboss.jandex.AnnotationValue in project wildfly-swarm by wildfly-swarm.

the class MPJWTAuthExtensionArchivePreparer method process.

@Override
public void process() throws Exception {
    WARArchive war = archive.as(WARArchive.class);
    // Check for LoginConfig annotation
    Collection<AnnotationInstance> lcAnnotations = index.getAnnotations(LOGIN_CONFIG);
    for (AnnotationInstance lc : lcAnnotations) {
        AnnotationValue authMethod = lc.value("authMethod");
        AnnotationValue realmName = lc.value("realmName");
        String realm = realmName != null ? realmName.asString() : "";
        // Set the web.xml login-config auth-method and jboss-web.xml security domain
        if (authMethod != null) {
            WebXmlAsset webXml = war.findWebXmlAsset();
            webXml.setLoginConfig(authMethod.asString(), realm);
        }
        if (realm.length() > 0) {
            JBossWebAsset jBossWeb = war.findJbossWebAsset();
            jBossWeb.setSecurityDomain(realm);
        }
    }
    // Get the @ApplicationPath setting
    WebXmlAsset webXml = war.findWebXmlAsset();
    String appPath = "/";
    Collection<AnnotationInstance> appPaths = index.getAnnotations(APP_PATH);
    if (!appPaths.isEmpty()) {
        appPath = appPaths.iterator().next().value().asString();
    }
    // Process the @RolesAllowed, @PermitAll and @DenyAll annotations
    Collection<AnnotationInstance> rolesAnnotations = index.getAnnotations(ROLES_ALLOWED);
    for (AnnotationInstance annotation : rolesAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            // Process the root resource
            String[] roles = annotation.value().asStringArray();
            ClassInfo classInfo = annotation.target().asClass();
            if (!scannedClasses.contains(classInfo.name())) {
                generateSecurityConstraints(webXml, classInfo, roles, appPath);
            }
        } else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) {
            // Process the containing root resource if it has not been already
            MethodInfo methodInfo = annotation.target().asMethod();
            ClassInfo classInfo = methodInfo.declaringClass();
            if (!scannedClasses.contains(classInfo.name())) {
                String[] roles = {};
                generateSecurityConstraints(webXml, classInfo, roles, appPath);
            }
        }
    }
    // Handle the verification configuration on the fraction
    if (fraction.getTokenIssuer().isPresent()) {
        log.debugf("Issuer: %s", fraction.getTokenIssuer().get());
        war.addAsManifestResource(new StringAsset(fraction.getTokenIssuer().get()), "MP-JWT-ISSUER");
    }
    if (fraction.getPublicKey() != null) {
        log.debugf("PublicKey: %s", fraction.getPublicKey());
        war.addAsManifestResource(new StringAsset(fraction.getPublicKey()), "MP-JWT-SIGNER");
    }
    if (log.isTraceEnabled()) {
        log.trace("war: " + war.toString(true));
    }
}
Also used : JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) AnnotationValue(org.jboss.jandex.AnnotationValue) MethodInfo(org.jboss.jandex.MethodInfo) WARArchive(org.wildfly.swarm.undertow.WARArchive) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 24 with AnnotationValue

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

the class MessageDrivenComponentDescriptionFactory method getMessageListenerInterface.

private String getMessageListenerInterface(final CompositeIndex compositeIndex, final AnnotationInstance messageBeanAnnotation, final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    final AnnotationValue value = messageBeanAnnotation.value("messageListenerInterface");
    if (value != null)
        return value.asClass().name().toString();
    final ClassInfo beanClass = (ClassInfo) messageBeanAnnotation.target();
    final Set<DotName> interfaces = new HashSet<DotName>(getPotentialViewInterfaces(beanClass));
    // check super class(es) of the bean
    DotName superClassDotName = beanClass.superName();
    while (interfaces.isEmpty() && superClassDotName != null && !superClassDotName.toString().equals(Object.class.getName())) {
        ClassInfo superClass = compositeIndex.getClassByName(superClassDotName);
        if (superClass == null) {
            final DeploymentUnit parent = deploymentUnit.getParent();
            if (parent != null) {
                final CompositeIndex parentIndex = parent.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
                if (parentIndex != null) {
                    superClass = parentIndex.getClassByName(superClassDotName);
                }
            }
        }
        if (superClass == null) {
            break;
        }
        interfaces.addAll(getPotentialViewInterfaces(superClass));
        // move to next super class
        superClassDotName = superClass.superName();
    }
    if (interfaces.size() != 1)
        throw EjbLogger.ROOT_LOGGER.mdbDoesNotImplementNorSpecifyMessageListener(beanClass);
    return interfaces.iterator().next().toString();
}
Also used : CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) AnnotationValue(org.jboss.jandex.AnnotationValue) DotName(org.jboss.jandex.DotName) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) ClassInfo(org.jboss.jandex.ClassInfo) HashSet(java.util.HashSet)

Example 25 with AnnotationValue

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

the class SessionBeanComponentDescriptionFactory method processSessionBeans.

private void processSessionBeans(final DeploymentUnit deploymentUnit, final List<AnnotationInstance> sessionBeanAnnotations, final SessionBeanComponentDescription.SessionBeanType annotatedSessionBeanType) {
    final EjbJarDescription ejbJarDescription = getEjbJarDescription(deploymentUnit);
    final ServiceName deploymentUnitServiceName = deploymentUnit.getServiceName();
    PropertyReplacer propertyReplacer = EJBAnnotationPropertyReplacement.propertyReplacer(deploymentUnit);
    // process these session bean annotations and create component descriptions out of it
    for (final AnnotationInstance sessionBeanAnnotation : sessionBeanAnnotations) {
        final AnnotationTarget target = sessionBeanAnnotation.target();
        if (!(target instanceof ClassInfo)) {
            // Let's just WARN and move on. No need to throw an error
            EjbLogger.DEPLOYMENT_LOGGER.warn(EjbLogger.ROOT_LOGGER.annotationOnlyAllowedOnClass(sessionBeanAnnotation.name().toString(), target).getMessage());
            continue;
        }
        final ClassInfo sessionBeanClassInfo = (ClassInfo) target;
        // skip if it's not a valid class for session bean
        if (!assertSessionBeanClassValidity(sessionBeanClassInfo)) {
            continue;
        }
        final String ejbName = sessionBeanClassInfo.name().local();
        final AnnotationValue nameValue = sessionBeanAnnotation.value("name");
        final String beanName = (nameValue == null || nameValue.asString().isEmpty()) ? ejbName : propertyReplacer.replaceProperties(nameValue.asString());
        final SessionBeanMetaData beanMetaData = getEnterpriseBeanMetaData(deploymentUnit, beanName, SessionBeanMetaData.class);
        final SessionBeanComponentDescription.SessionBeanType sessionBeanType;
        final String beanClassName;
        if (beanMetaData != null) {
            beanClassName = override(sessionBeanClassInfo.name().toString(), beanMetaData.getEjbClass());
            sessionBeanType = override(annotatedSessionBeanType, descriptionOf(((SessionBeanMetaData) beanMetaData).getSessionType()));
        } else {
            beanClassName = sessionBeanClassInfo.name().toString();
            sessionBeanType = annotatedSessionBeanType;
        }
        final SessionBeanComponentDescription sessionBeanDescription;
        switch(sessionBeanType) {
            case STATELESS:
                sessionBeanDescription = new StatelessComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, beanMetaData, defaultSlsbPoolAvailable);
                break;
            case STATEFUL:
                sessionBeanDescription = new StatefulComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, beanMetaData);
                // If passivation is disabled for the SFSB, either via annotation or via DD, then setup the component
                // description appropriately
                final boolean passivationCapableAnnotationValue = sessionBeanAnnotation.value("passivationCapable") == null ? true : sessionBeanAnnotation.value("passivationCapable").asBoolean();
                final Boolean passivationCapableDeploymentDescriptorValue;
                if ((beanMetaData instanceof SessionBean32MetaData)) {
                    passivationCapableDeploymentDescriptorValue = ((SessionBean32MetaData) beanMetaData).isPassivationCapable();
                } else {
                    passivationCapableDeploymentDescriptorValue = null;
                }
                final boolean passivationApplicable = override(passivationCapableDeploymentDescriptorValue, passivationCapableAnnotationValue);
                ((StatefulComponentDescription) sessionBeanDescription).setPassivationApplicable(passivationApplicable);
                break;
            case SINGLETON:
                if (sessionBeanClassInfo.interfaceNames().contains(SESSION_BEAN_INTERFACE)) {
                    EjbLogger.ROOT_LOGGER.singletonCantImplementSessionBean(beanClassName);
                }
                sessionBeanDescription = new SingletonComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, beanMetaData);
                break;
            default:
                throw EjbLogger.ROOT_LOGGER.unknownSessionBeanType(sessionBeanType.name());
        }
        addComponent(deploymentUnit, sessionBeanDescription);
        final AnnotationValue mappedNameValue = sessionBeanAnnotation.value("mappedName");
        if (mappedNameValue != null && !mappedNameValue.asString().isEmpty()) {
            EjbLogger.ROOT_LOGGER.mappedNameNotSupported(mappedNameValue != null ? mappedNameValue.asString() : "", ejbName);
        }
    }
    EjbDeploymentMarker.mark(deploymentUnit);
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) SessionBeanMetaData(org.jboss.metadata.ejb.spec.SessionBeanMetaData) ServiceName(org.jboss.msc.service.ServiceName) StatelessComponentDescription(org.jboss.as.ejb3.component.stateless.StatelessComponentDescription) StatefulComponentDescription(org.jboss.as.ejb3.component.stateful.StatefulComponentDescription) AnnotatedEJBComponentDescriptionDeploymentUnitProcessor.getEjbJarDescription(org.jboss.as.ejb3.deployment.processors.AnnotatedEJBComponentDescriptionDeploymentUnitProcessor.getEjbJarDescription) EjbJarDescription(org.jboss.as.ejb3.deployment.EjbJarDescription) SessionBean32MetaData(org.jboss.metadata.ejb.spec.SessionBean32MetaData) AnnotationValue(org.jboss.jandex.AnnotationValue) PropertyReplacer(org.jboss.metadata.property.PropertyReplacer) SingletonComponentDescription(org.jboss.as.ejb3.component.singleton.SingletonComponentDescription) SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

AnnotationValue (org.jboss.jandex.AnnotationValue)32 AnnotationInstance (org.jboss.jandex.AnnotationInstance)20 ClassInfo (org.jboss.jandex.ClassInfo)12 AnnotationTarget (org.jboss.jandex.AnnotationTarget)10 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)8 CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)8 PropertyReplacer (org.jboss.metadata.property.PropertyReplacer)6 HashSet (java.util.HashSet)5 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)4 InjectionSource (org.jboss.as.ee.component.InjectionSource)4 LookupInjectionSource (org.jboss.as.ee.component.LookupInjectionSource)4 PersistenceContextInjectionSource (org.jboss.as.jpa.injectors.PersistenceContextInjectionSource)4 PersistenceUnitInjectionSource (org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource)4 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)4 MethodInfo (org.jboss.jandex.MethodInfo)4 ServiceName (org.jboss.msc.service.ServiceName)4 ArrayList (java.util.ArrayList)3 TimeUnit (java.util.concurrent.TimeUnit)3 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)3 ResourceInjectionConfiguration (org.jboss.as.ee.component.ResourceInjectionConfiguration)3