1 23 package com.sun.enterprise.deployment.annotation.handlers; 24 25 import java.lang.annotation.Annotation ; 26 import java.lang.annotation.ElementType ; 27 import java.lang.reflect.AnnotatedElement ; 28 import java.lang.reflect.Method ; 29 import java.util.logging.Level ; 30 31 import javax.annotation.security.DenyAll; 32 import javax.annotation.security.PermitAll; 33 import javax.annotation.security.RolesAllowed; 34 35 import com.sun.enterprise.deployment.EjbDescriptor; 36 import com.sun.enterprise.deployment.MethodDescriptor; 37 import com.sun.enterprise.deployment.MethodPermission; 38 import com.sun.enterprise.deployment.Role; 39 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 40 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 41 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 42 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 43 import com.sun.enterprise.deployment.annotation.context.EjbContext; 44 import com.sun.enterprise.util.TypeUtil; 45 46 52 public class RolesAllowedHandler extends AbstractAttributeHandler implements PostProcessor { 53 54 public RolesAllowedHandler() { 55 } 56 57 60 public Class <? extends Annotation > getAnnotationType() { 61 return RolesAllowed.class; 62 } 63 64 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 65 EjbContext[] ejbContexts) throws AnnotationProcessorException { 66 67 AnnotatedElement ae = (AnnotatedElement )ainfo.getAnnotatedElement(); 68 69 if (ae.isAnnotationPresent(DenyAll.class) || 70 ae.isAnnotationPresent(PermitAll.class)) { 71 log(Level.SEVERE, ainfo, 72 localStrings.getLocalString( 73 "enterprise.deployment.annotation.handlers.inconsistentsecannotation", 74 "This annotation is not consistent with other annotations. One cannot have more than one of @RolesAllowed, @PermitAll, @DenyAll in the same AnnotatedElement.")); 75 return getDefaultFailedResult(); 76 } 77 78 RolesAllowed rolesAllowedAn = (RolesAllowed)ainfo.getAnnotation(); 79 80 for (EjbContext ejbContext : ejbContexts) { 81 EjbDescriptor ejbDesc = ejbContext.getDescriptor(); 82 if (ElementType.TYPE.equals(ainfo.getElementType())) { 83 ejbContext.addPostProcessInfo(ainfo, this); 85 } else { 86 Method annMethod = (Method ) ainfo.getAnnotatedElement(); 87 88 for (Object next : ejbDesc.getSecurityBusinessMethodDescriptors()) { 89 MethodDescriptor md = (MethodDescriptor)next; 90 Method m = md.getMethod(ejbDesc); 91 if (TypeUtil.sameMethodSignature(m, annMethod)) { 92 if (!hasMethodPermissionsFromDD(md, ejbDesc)) { 94 addMethodPermissions(rolesAllowedAn, ejbDesc, md); 95 } 96 } 97 } 98 } 99 } 100 101 return getDefaultProcessedResult(); 102 } 103 104 109 public Class <? extends Annotation >[] getTypeDependencies() { 110 return getEjbAnnotationTypes(); 111 } 112 113 protected boolean supportTypeInheritance() { 114 return true; 115 } 116 117 public void postProcessAnnotation(AnnotationInfo ainfo, 118 AnnotatedElementHandler aeHandler) 119 throws AnnotationProcessorException { 120 EjbContext ejbContext = (EjbContext)aeHandler; 121 EjbDescriptor ejbDesc = ejbContext.getDescriptor(); 122 RolesAllowed rolesAllowedAn = (RolesAllowed)ainfo.getAnnotation(); 123 if (!ejbContext.isInherited() && 124 (ejbDesc.getMethodPermissionsFromDD() == null || 125 ejbDesc.getMethodPermissionsFromDD().size() == 0)) { 126 for (MethodDescriptor md : getMethodAllDescriptors(ejbDesc)) { 127 addMethodPermissions(rolesAllowedAn, ejbDesc, md); 128 } 129 } else { 130 Class classAn = (Class )ainfo.getAnnotatedElement(); 131 for (Object next : ejbDesc.getSecurityBusinessMethodDescriptors()) { 132 MethodDescriptor md = (MethodDescriptor)next; 133 Method m = md.getMethod(ejbDesc); 134 if (classAn.equals(ejbContext.getDeclaringClass(md)) && 136 !hasMethodPermissionsFromDD(md, ejbDesc)) { 137 addMethodPermissions(rolesAllowedAn, ejbDesc, md); 138 } 139 } 140 } 141 } 142 143 149 private void addMethodPermissions(RolesAllowed rolesAllowedAn, 150 EjbDescriptor ejbDesc, MethodDescriptor md) { 151 for (String roleName : rolesAllowedAn.value()) { 152 Role role = new Role(roleName); 153 ejbDesc.getEjbBundleDescriptor().addRole(role); 155 ejbDesc.addPermissionedMethod(new MethodPermission(role), md); 156 } 157 } 158 159 } 160 | Popular Tags |