Search in sources :

Example 1 with AnnotatedMethod

use of javax.enterprise.inject.spi.AnnotatedMethod in project deltaspike by apache.

the class SecurityExtension method validateBindings.

public void validateBindings(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
    if (!isActivated) {
        return;
    }
    SecurityMetaDataStorage metaDataStorage = getMetaDataStorage();
    SecurityExtension parentExtension = ParentExtensionStorage.getParentExtension(this);
    if (parentExtension != null) {
        // also add the authorizers from the parent extension
        Set<Authorizer> parentAuthorizers = parentExtension.getMetaDataStorage().getAuthorizers();
        for (Authorizer parentAuthorizer : parentAuthorizers) {
            metaDataStorage.addAuthorizer(parentAuthorizer);
        }
    }
    metaDataStorage.registerSecuredMethods();
    for (final AnnotatedMethod<?> method : metaDataStorage.getSecuredMethods()) {
        // Here we simply want to validate that each method that is annotated with
        // one or more security bindings has a valid authorizer for each binding
        Class<?> targetClass = method.getDeclaringType().getJavaClass();
        Method targetMethod = method.getJavaMember();
        for (final Annotation annotation : SecurityUtils.getSecurityBindingTypes(targetClass, targetMethod)) {
            boolean found = false;
            Set<AuthorizationParameter> authorizationParameters = new HashSet<AuthorizationParameter>();
            for (AnnotatedParameter<?> parameter : (List<AnnotatedParameter<?>>) (List<?>) method.getParameters()) {
                Set<Annotation> securityParameterBindings = null;
                for (Annotation a : parameter.getAnnotations()) {
                    if (SecurityUtils.isMetaAnnotatedWithSecurityParameterBinding(a)) {
                        if (securityParameterBindings == null) {
                            securityParameterBindings = new HashSet<Annotation>();
                        }
                        securityParameterBindings.add(a);
                    }
                }
                if (securityParameterBindings != null) {
                    AuthorizationParameter authorizationParameter = new AuthorizationParameter(parameter.getBaseType(), securityParameterBindings);
                    authorizationParameters.add(authorizationParameter);
                }
            }
            // Validate the authorizer
            for (Authorizer auth : metaDataStorage.getAuthorizers()) {
                if (auth.matchesBindings(annotation, authorizationParameters, targetMethod.getReturnType())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                event.addDefinitionError(new SecurityDefinitionException("Secured type " + method.getDeclaringType().getJavaClass().getName() + " has no matching authorizer method for security binding @" + annotation.annotationType().getName()));
            }
        }
        for (final Annotation annotation : method.getAnnotations()) {
            if (SecurityUtils.isMetaAnnotatedWithSecurityBindingType(annotation)) {
                metaDataStorage.registerSecuredMethod(targetClass, targetMethod);
                break;
            }
        }
    }
    // Clear securedTypes, we don't require it any more
    metaDataStorage.resetSecuredMethods();
}
Also used : Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Annotation(java.lang.annotation.Annotation) SecurityDefinitionException(org.apache.deltaspike.security.api.authorization.SecurityDefinitionException) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with AnnotatedMethod

use of javax.enterprise.inject.spi.AnnotatedMethod in project deltaspike by apache.

the class DefaultFutureableStrategy method getOrCreatePool.

protected ExecutorService getOrCreatePool(final InvocationContext ic) {
    final Method method = ic.getMethod();
    ExecutorService executorService = configByMethod.get(method);
    if (executorService == null) {
        final AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(method.getDeclaringClass());
        final AnnotatedMethod<?> annotatedMethod = AnnotatedMethods.findMethod(annotatedType, method);
        final Futureable methodConfig = annotatedMethod.getAnnotation(Futureable.class);
        final ExecutorService instance = manager.find((methodConfig == null ? annotatedType.getAnnotation(Futureable.class) : methodConfig).value());
        configByMethod.putIfAbsent(method, instance);
        executorService = instance;
    }
    return executorService;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Futureable(org.apache.deltaspike.core.api.future.Futureable)

Example 3 with AnnotatedMethod

use of javax.enterprise.inject.spi.AnnotatedMethod in project tomee by apache.

the class CdiPlugin method defineSessionBean.

@Override
public <T> Bean<T> defineSessionBean(final Class<T> clazz, final BeanAttributes<T> attributes, final AnnotatedType<T> annotatedType) {
    final BeanContext bc = findBeanContext(webBeansContext, clazz);
    final Class<?> superClass = bc.getManagedClass().getSuperclass();
    if (annotatedType.isAnnotationPresent(Specializes.class)) {
        if (superClass != Object.class && !isSessionBean(superClass)) {
            throw new DefinitionException("You can only specialize another EJB: " + clazz);
        }
        final BeanContext parentBc = findBeanContext(webBeansContext, superClass);
        final List<Class> businessLocalInterfaces = new ArrayList<>(parentBc.getBusinessLocalInterfaces());
        for (final Class<?> api : bc.getBusinessLocalInterfaces()) {
            businessLocalInterfaces.removeAll(GenericsUtil.getTypeClosure(api));
        }
        if (!businessLocalInterfaces.isEmpty()) {
            throw new DefinitionException("You can only specialize another EJB with at least the same API: " + clazz);
        }
    }
    final CdiEjbBean<T> bean = new OpenEJBBeanBuilder<T>(bc, webBeansContext, annotatedType, attributes).createBean(clazz, !annotatedType.isAnnotationPresent(Vetoed.class));
    bc.set(CdiEjbBean.class, bean);
    bc.set(CurrentCreationalContext.class, new CurrentCreationalContext());
    validateDisposeMethods(bean);
    validateScope(bean);
    final Set<ObserverMethod<?>> observerMethods;
    if (bean.isEnabled()) {
        observerMethods = new ObserverMethodsBuilder<T>(webBeansContext, bean.getAnnotatedType()).defineObserverMethods(bean);
    } else {
        observerMethods = new HashSet<>();
    }
    final WebBeansUtil webBeansUtil = webBeansContext.getWebBeansUtil();
    final Set<ProducerFieldBean<?>> producerFields = new ProducerFieldBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerFields(bean);
    final Set<ProducerMethodBean<?>> producerMethods = new ProducerMethodBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerMethods(bean, producerFields);
    final Map<ProducerMethodBean<?>, AnnotatedMethod<?>> annotatedMethods = new HashMap<>();
    for (final ProducerMethodBean<?> producerMethod : producerMethods) {
        final AnnotatedMethod<?> method = webBeansContext.getAnnotatedElementFactory().newAnnotatedMethod(producerMethod.getCreatorMethod(), annotatedType);
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for " + "ProducerMethods. Look at logs for further details");
        annotatedMethods.put(producerMethod, method);
    }
    final Map<ProducerFieldBean<?>, AnnotatedField<?>> annotatedFields = new HashMap<>();
    for (final ProducerFieldBean<?> producerField : producerFields) {
        if (!Modifier.isStatic(producerField.getCreatorField().getModifiers())) {
            throw new DefinitionException("In an EJB all producer fields should be static");
        }
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for" + " ProducerFields. Look at logs for further details");
        annotatedFields.put(producerField, webBeansContext.getAnnotatedElementFactory().newAnnotatedField(producerField.getCreatorField(), webBeansContext.getAnnotatedElementFactory().newAnnotatedType(producerField.getBeanClass())));
    }
    final Map<ObserverMethod<?>, AnnotatedMethod<?>> observerMethodsMap = new HashMap<>();
    for (final ObserverMethod<?> observerMethod : observerMethods) {
        final ObserverMethodImpl<?> impl = (ObserverMethodImpl<?>) observerMethod;
        final AnnotatedMethod<?> method = impl.getObserverMethod();
        observerMethodsMap.put(observerMethod, method);
    }
    validateProduceMethods(bean, producerMethods);
    validateObserverMethods(bean, observerMethodsMap);
    final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
    //Fires ProcessManagedBean
    final GProcessSessionBean event = new GProcessSessionBean(Bean.class.cast(bean), annotatedType, bc.getEjbName(), bean.getEjbType());
    beanManager.fireEvent(event, true);
    event.setStarted();
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessSessionBean event observers for managed beans. Look at logs for further details");
    //Fires ProcessProducerMethod
    webBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods, annotatedType);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerMethod event observers for producer method beans. Look at logs for further details");
    //Fires ProcessProducerField
    webBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerField event observers for producer field beans. Look at logs for further details");
    //Fire ObservableMethods
    webBeansUtil.fireProcessObservableMethodBeanEvent(observerMethodsMap);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessObserverMethod event observers for observer methods. Look at logs for further details");
    if (!webBeansUtil.isAnnotatedTypeDecoratorOrInterceptor(annotatedType)) {
        for (final ProducerMethodBean<?> producerMethod : producerMethods) {
            beanManager.addBean(producerMethod);
        }
        for (final ProducerFieldBean<?> producerField : producerFields) {
            beanManager.addBean(producerField);
        }
    }
    beanManager.addBean(bean);
    return bean;
}
Also used : AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ArrayList(java.util.ArrayList) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) Bean(javax.enterprise.inject.spi.Bean) OwbBean(org.apache.webbeans.component.OwbBean) AbstractOwbBean(org.apache.webbeans.component.AbstractOwbBean) ProducerFieldBeansBuilder(org.apache.webbeans.component.creation.ProducerFieldBeansBuilder) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ObserverMethodsBuilder(org.apache.webbeans.component.creation.ObserverMethodsBuilder) ObserverMethodImpl(org.apache.webbeans.event.ObserverMethodImpl) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) BeanContext(org.apache.openejb.BeanContext) WebBeansUtil(org.apache.webbeans.util.WebBeansUtil) ProducerMethodBeansBuilder(org.apache.webbeans.component.creation.ProducerMethodBeansBuilder) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 4 with AnnotatedMethod

use of javax.enterprise.inject.spi.AnnotatedMethod in project deltaspike by apache.

the class DefaultMockFilter method isMockedImplementationSupported.

@Override
public boolean isMockedImplementationSupported(BeanManager beanManager, Annotated annotated) {
    if (!isMockSupportEnabled(annotated)) {
        return false;
    }
    Class origin = null;
    if (annotated instanceof AnnotatedType) {
        origin = ((AnnotatedType) annotated).getJavaClass();
        Set<Annotation> annotations = new HashSet<Annotation>();
        annotations.addAll(annotated.getAnnotations());
        for (AnnotatedMethod annotatedMethod : (Set<javax.enterprise.inject.spi.AnnotatedMethod>) ((AnnotatedType) annotated).getMethods()) {
            annotations.addAll(annotatedMethod.getAnnotations());
        }
        if (isEjbOrAnnotatedTypeWithInterceptorAnnotation(beanManager, annotations, origin.getName())) {
            return false;
        }
    } else if (annotated instanceof AnnotatedMember) {
        Member member = ((AnnotatedMember) annotated).getJavaMember();
        origin = member.getDeclaringClass();
        if (isEjbOrAnnotatedTypeWithInterceptorAnnotation(beanManager, annotated.getAnnotations(), member.toString())) {
            return false;
        }
    }
    if (origin != null && origin.getPackage() == null) {
        LOG.warning("Please don't use the default-package for " + origin.getName());
        return true;
    }
    return origin != null && !isInternalPackage(origin.getPackage().getName());
}
Also used : AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Set(java.util.Set) HashSet(java.util.HashSet) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) AnnotatedMember(javax.enterprise.inject.spi.AnnotatedMember) AnnotatedMember(javax.enterprise.inject.spi.AnnotatedMember) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 5 with AnnotatedMethod

use of javax.enterprise.inject.spi.AnnotatedMethod in project deltaspike by apache.

the class AnnotatedTypeBuilderTest method testTypeLevelAnnotationRedefinition.

@Test
public void testTypeLevelAnnotationRedefinition() {
    AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
    builder.readFromType(Cat.class);
    AnnotatedType<Cat> cat = builder.create();
    assertNotNull(cat);
    assertNotNull(cat.getAnnotation(Named.class));
    assertEquals("cat", cat.getAnnotation(Named.class).value());
    builder.addToClass(new AlternativeLiteral()).addToClass(new ApplicationScopedLiteral()).removeFromClass(Named.class).addToClass(new NamedLiteral("tomcat"));
    cat = builder.create();
    assertNotNull(cat);
    assertEquals(3, cat.getAnnotations().size());
    assertTrue(cat.isAnnotationPresent(Named.class));
    assertTrue(cat.isAnnotationPresent(Alternative.class));
    assertTrue(cat.isAnnotationPresent(ApplicationScoped.class));
    assertEquals("tomcat", cat.getAnnotation(Named.class).value());
    AnnotatedMethod observerMethod = null;
    for (AnnotatedMethod m : cat.getMethods()) {
        if ("doSomeObservation".equals(m.getJavaMember().getName())) {
            observerMethod = m;
            break;
        }
    }
    assertNotNull(observerMethod);
    observerMethod.isAnnotationPresent(Observes.class);
    {
        // test reading from an AnnotatedType
        AnnotatedTypeBuilder<Cat> builder2 = new AnnotatedTypeBuilder<Cat>();
        builder2.readFromType(cat);
        builder2.removeFromAll(Named.class);
        final AnnotatedType<Cat> noNameCat = builder2.create();
        assertFalse(noNameCat.isAnnotationPresent(Named.class));
        assertEquals(2, noNameCat.getAnnotations().size());
    }
    {
        // test reading from an AnnotatedType in non-overwrite mode
        AnnotatedTypeBuilder<Cat> builder3 = new AnnotatedTypeBuilder<Cat>();
        builder3.readFromType(cat, true);
        builder3.removeFromAll(Named.class);
        builder3.readFromType(cat, false);
        final AnnotatedType<Cat> namedCat = builder3.create();
        assertTrue(namedCat.isAnnotationPresent(Named.class));
        assertEquals(3, namedCat.getAnnotations().size());
    }
}
Also used : Named(javax.inject.Named) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) AnnotatedTypeBuilder(org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder) Alternative(javax.enterprise.inject.Alternative) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) AlternativeLiteral(org.apache.deltaspike.core.api.literal.AlternativeLiteral) ApplicationScoped(javax.enterprise.context.ApplicationScoped) NamedLiteral(org.apache.deltaspike.core.api.literal.NamedLiteral) ApplicationScopedLiteral(org.apache.deltaspike.core.api.literal.ApplicationScopedLiteral) Test(org.junit.Test)

Aggregations

AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)9 Method (java.lang.reflect.Method)6 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)4 Annotation (java.lang.annotation.Annotation)3 HashSet (java.util.HashSet)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 HashMap (java.util.HashMap)2 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 WeakHashMap (java.util.WeakHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 Lock (java.util.concurrent.locks.Lock)1