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();
}
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;
}
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;
}
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());
}
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());
}
}
Aggregations