KickJava   Java API By Example, From Geeks To Geeks.

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


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
30 import javax.annotation.security.DeclareRoles;
31
32 import com.sun.enterprise.deployment.EjbDescriptor;
33 import com.sun.enterprise.deployment.Role;
34 import com.sun.enterprise.deployment.RoleReference;
35 import com.sun.enterprise.deployment.SecurityRoleDescriptor;
36 import com.sun.enterprise.deployment.WebBundleDescriptor;
37 import com.sun.enterprise.deployment.WebComponentDescriptor;
38 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
39 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
40 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
41 import com.sun.enterprise.deployment.annotation.context.EjbContext;
42 import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
43 import com.sun.enterprise.deployment.annotation.context.WebComponentContext;
44
45 /**
46  * This handler is responsible for handling the
47  * javax.annotation.security.DeclareRoles.
48  *
49  * @author Shing Wai Chan
50  */

51 public class DeclareRolesHandler extends AbstractCommonAttributeHandler {
52     
53     public DeclareRolesHandler() {
54     }
55     
56     /**
57      * @return the annoation type this annotation handler is handling
58      */

59     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
60         return DeclareRoles.class;
61     }
62         
63     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
64             EjbContext[] ejbContexts) throws AnnotationProcessorException {
65         
66         DeclareRoles rolesRefAn = (DeclareRoles)ainfo.getAnnotation();
67
68         for (EjbContext ejbContext : ejbContexts) {
69             EjbDescriptor ejbDescriptor = ejbContext.getDescriptor();
70             for (String JavaDoc roleName : rolesRefAn.value()) {
71                 if (ejbDescriptor.getRoleReferenceByName(roleName) == null) {
72                     RoleReference roleRef = new RoleReference(roleName, "");
73                     roleRef.setRolename(roleName);
74                     roleRef.setSecurityRoleLink(
75                            new SecurityRoleDescriptor(roleName, ""));
76                     ejbDescriptor.addRoleReference(roleRef);
77                 }
78
79                 Role role = new Role(roleName);
80                 ejbDescriptor.getEjbBundleDescriptor().addRole(role);
81             }
82         }
83         return getDefaultProcessedResult();
84     }
85
86     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
87             WebComponentContext[] webCompContexts)
88             throws AnnotationProcessorException {
89         WebBundleDescriptor webBundleDesc =
90             webCompContexts[0].getDescriptor().getWebBundleDescriptor();
91         return processAnnotation(ainfo, webBundleDesc);
92     }
93
94     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
95              WebBundleContext webBundleContext)
96              throws AnnotationProcessorException {
97         WebBundleDescriptor webBundleDesc = webBundleContext.getDescriptor();
98         return processAnnotation(ainfo, webBundleDesc);
99     }
100
101     private HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
102              WebBundleDescriptor webBundleDesc) {
103         DeclareRoles rolesRefAn = (DeclareRoles)ainfo.getAnnotation();
104         for (String JavaDoc roleName : rolesRefAn.value()) {
105             Role role = new Role(roleName);
106             webBundleDesc.addRole(role);
107         }
108         return getDefaultProcessedResult();
109     }
110
111     /**
112      * @return an array of annotation types this annotation handler would
113      * require to be processed (if present) before it processes it's own
114      * annotation type.
115      */

116     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
117         return getEjbAnnotationTypes();
118     }
119
120     protected boolean supportTypeInheritance() {
121         return true;
122     }
123 }
124
Popular Tags