KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Level JavaDoc;
27 import javax.annotation.security.RunAs;
28
29 import com.sun.enterprise.deployment.EjbDescriptor;
30 import com.sun.enterprise.deployment.Role;
31 import com.sun.enterprise.deployment.RunAsIdentityDescriptor;
32 import com.sun.enterprise.deployment.WebComponentDescriptor;
33 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
34 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
35 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
36 import com.sun.enterprise.deployment.annotation.context.EjbContext;
37 import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
38 import com.sun.enterprise.deployment.annotation.context.WebComponentContext;
39
40 /**
41  * This handler is responsible for handling the
42  * javax.annotation.security.RunAs.
43  * @author Shing Wai Chan
44  */

45 public class RunAsHandler extends AbstractCommonAttributeHandler {
46     
47     public RunAsHandler() {
48     }
49     
50     /**
51      * @return the annoation type this annotation handler is handling
52      */

53     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
54         return RunAs.class;
55     }
56
57     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
58             EjbContext[] ejbContexts) throws AnnotationProcessorException {
59         
60         RunAs runAsAn = (RunAs)ainfo.getAnnotation();
61
62         for (EjbContext ejbContext : ejbContexts) {
63             EjbDescriptor ejbDesc = ejbContext.getDescriptor();
64             // override by xml
65
if (ejbDesc.getUsesCallerIdentity() != null) {
66                 continue;
67             }
68             String JavaDoc roleName = runAsAn.value();
69             Role role = new Role(roleName);
70             // add Role if not exists
71
ejbDesc.getEjbBundleDescriptor().addRole(role);
72             RunAsIdentityDescriptor runAsDesc = new RunAsIdentityDescriptor();
73             runAsDesc.setRoleName(roleName);
74             ejbDesc.setUsesCallerIdentity(false);
75             if (ejbDesc.getRunAsIdentity() == null) {
76                 ejbDesc.setRunAsIdentity(runAsDesc);
77             }
78         }
79
80         return getDefaultProcessedResult();
81     }
82
83     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
84             WebComponentContext[] webCompContexts) throws AnnotationProcessorException {
85         RunAs runAsAn = (RunAs)ainfo.getAnnotation();
86
87         for (WebComponentContext webCompContext : webCompContexts) {
88             WebComponentDescriptor webDesc = webCompContext.getDescriptor();
89             // override by xml
90
if (webDesc.getRunAsIdentity() != null) {
91                 continue;
92             }
93             String JavaDoc roleName = runAsAn.value();
94             Role role = new Role(roleName);
95             // add Role if not exists
96
webDesc.getWebBundleDescriptor().addRole(role);
97             RunAsIdentityDescriptor runAsDesc = new RunAsIdentityDescriptor();
98             runAsDesc.setRoleName(roleName);
99             webDesc.setRunAsIdentity(runAsDesc);
100         }
101
102         return getDefaultProcessedResult();
103     }
104
105     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
106              WebBundleContext webBundleContext)
107              throws AnnotationProcessorException {
108         return getInvalidAnnotatedElementHandlerResult(
109             ainfo.getProcessingContext().getHandler(), ainfo);
110     }
111
112
113     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
114         return getEjbAnnotationTypes();
115     }
116 }
117
Popular Tags