KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > handlers > PermitAllHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.annotation.handlers;
24
25 import java.lang.annotation.Annotation JavaDoc;
26 import java.lang.annotation.ElementType JavaDoc;
27 import java.lang.reflect.AnnotatedElement JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.logging.Level JavaDoc;
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 /**
47  * This handler is responsible for handling the
48  * javax.annotation.security.PermitAll.
49  *
50  * @author Shing Wai Chan
51  */

52 public class PermitAllHandler extends AbstractAttributeHandler implements PostProcessor {
53     
54     public PermitAllHandler() {
55     }
56     
57     /**
58      * @return the annoation type this annotation handler is handling
59      */

60     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
61         return PermitAll.class;
62     }
63
64     /**
65      * Process Annotation with given EjbContexts.
66      * @param ainfo
67      * @param ejbContexts
68      * @return HandlerProcessingResult
69      */

70     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
71             EjbContext[] ejbContexts) throws AnnotationProcessorException {
72
73         AnnotatedElement JavaDoc ae = (AnnotatedElement JavaDoc)ainfo.getAnnotatedElement();
74         if (ae.isAnnotationPresent(DenyAll.class) ||
75                 ae.isAnnotationPresent(RolesAllowed.class)) {
76             log(Level.SEVERE, ainfo,
77                 localStrings.getLocalString(
78                 "enterprise.deployment.annotation.handlers.inconsistentsecannotation",
79                 "This annotation is not consistent with other annotations. One cannot have more than one of @RolesAllowed, @PermitAll, @DenyAll in the same AnnotatedElement."));
80             return getDefaultFailedResult();
81         }
82
83         for (EjbContext ejbContext : ejbContexts) {
84             EjbDescriptor ejbDesc = ejbContext.getDescriptor();
85
86             if (ElementType.TYPE.equals(ainfo.getElementType())) {
87                 // postpone the processing at the end
88
ejbContext.addPostProcessInfo(ainfo, this);
89             } else { // METHOD
90
Method JavaDoc annMethod = (Method JavaDoc)ainfo.getAnnotatedElement();
91                 for (Object JavaDoc next : ejbDesc.getSecurityBusinessMethodDescriptors()) {
92                     MethodDescriptor md = (MethodDescriptor)next;
93                     Method JavaDoc m = md.getMethod(ejbDesc);
94                     if (TypeUtil.sameMethodSignature(m, annMethod)) {
95                         // override by xml
96
if (!hasMethodPermissionsFromDD(md, ejbDesc)) {
97                             ejbDesc.addPermissionedMethod(
98                                 MethodPermission.getUncheckedMethodPermission(),
99                                 md);
100                         }
101                     }
102                 }
103             }
104         }
105
106         return getDefaultProcessedResult();
107     }
108
109     /**
110      * @return an array of annotation types this annotation handler would
111      * require to be processed (if present) before it processes it's own
112      * annotation type.
113      */

114     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
115         return getEjbAnnotationTypes();
116     }
117
118     protected boolean supportTypeInheritance() {
119         return true;
120     }
121
122     public void postProcessAnnotation(AnnotationInfo ainfo,
123             AnnotatedElementHandler aeHandler)
124             throws AnnotationProcessorException {
125         EjbContext ejbContext = (EjbContext)aeHandler;
126         EjbDescriptor ejbDesc = ejbContext.getDescriptor();
127         if (!ejbContext.isInherited() &&
128                 (ejbDesc.getMethodPermissionsFromDD() == null ||
129                 ejbDesc.getMethodPermissionsFromDD().size() == 0)) {
130             for (MethodDescriptor md : getMethodAllDescriptors(ejbDesc)) {
131                 ejbDesc.addPermissionedMethod(
132                     MethodPermission.getUncheckedMethodPermission(), md);
133             }
134         } else {
135             Class JavaDoc classAn = (Class JavaDoc)ainfo.getAnnotatedElement();
136             for (Object JavaDoc next : ejbDesc.getSecurityBusinessMethodDescriptors()) {
137                 MethodDescriptor md = (MethodDescriptor)next;
138                 if (classAn.equals(ejbContext.getDeclaringClass(md)) &&
139                         !hasMethodPermissionsFromDD(md, ejbDesc)) {
140                     ejbDesc.addPermissionedMethod(
141                         MethodPermission.getUncheckedMethodPermission(),
142                         md);
143                 }
144             }
145         }
146     }
147 }
148
Popular Tags