KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > dd > SecurityTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.ejb.dd;
6
7 import java.util.*;
8
9 import xjavadoc.*;
10
11 import xdoclet.XDocletException;
12
13 import xdoclet.modules.ejb.EjbTagsHandler;
14 import xdoclet.modules.ejb.intf.InterfaceTagsHandler;
15 import xdoclet.util.DocletUtil;
16
17 /**
18  * @author Ara Abrahamian (ara_e@email.com)
19  * @created Oct 15, 2001
20  * @xdoclet.taghandler namespace="EjbSec"
21  * @version $Revision: 1.7 $
22  */

23 public class SecurityTagsHandler extends EjbTagsHandler
24 {
25     /**
26      * The current security role name, set by forAllSecurityRoles and returned by securityRoleName. It somehow is like
27      * the current index for the forAllSecurityRoles loop.
28      *
29      * @see #forAllSecurityRoles(java.lang.String)
30      * @see #securityRoleName()
31      */

32     protected transient String JavaDoc currentSecurityRoleName;
33
34     /**
35      * Returns current security role name set by the containing forAllSecurityRoles.
36      *
37      * @return Current security role name
38      * @exception XDocletException
39      * @see #forAllSecurityRoles(java.lang.String)
40      * @doc.tag type="content"
41      */

42     public String JavaDoc securityRoleName() throws XDocletException
43     {
44         return currentSecurityRoleName;
45     }
46
47     /**
48      * Evaluates the body block for each ejb:permission defined in class level or method level.
49      *
50      * @param template The body of the block tag
51      * @exception XDocletException
52      * @see #hasPermission(xjavadoc.XDoc)
53      * @see #securityRoleName()
54      * @doc.tag type="block"
55      */

56     public void forAllSecurityRoles(String JavaDoc template) throws XDocletException
57     {
58         Collection classes = getXJavaDoc().getSourceClasses();
59
60         Set roleSet = new HashSet();
61
62         for (Iterator i = classes.iterator(); i.hasNext(); ) {
63             XClass clazz = (XClass) i.next();
64
65             setCurrentClass(clazz);
66
67             // Get roles from class
68
if (hasPermission(getCurrentClass().getDoc())) {
69                 Collection permissions = getCurrentClass().getDoc().getTags("ejb.permission");
70
71                 for (Iterator k = permissions.iterator(); k.hasNext(); ) {
72                     XTag tag = (XTag) k.next();
73                     String JavaDoc roleName = tag.getAttributeValue("role-name");
74
75                     if (roleName != null) {
76                         roleSet.addAll(Arrays.asList(DocletUtil.tokenizeDelimitedToArray(roleName, ",")));
77                     }
78                 }
79                 // Add roles to set
80
}
81
82             // Get roles from methods
83
Collection methods = getCurrentClass().getMethods();
84
85             for (Iterator j = methods.iterator(); j.hasNext(); ) {
86                 XMethod method = (XMethod) j.next();
87
88                 setCurrentMethod(method);
89
90                 if (hasPermission(getCurrentMethod().getDoc()) && InterfaceTagsHandler.isInterfaceMethod(getCurrentMethod())) {
91                     Collection permissions = getCurrentMethod().getDoc().getTags("ejb.permission");
92
93                     for (Iterator k = permissions.iterator(); k.hasNext(); ) {
94                         XTag tag = (XTag) k.next();
95                         String JavaDoc role_name = tag.getAttributeValue("role-name");
96
97                         if (role_name != null) {
98                             roleSet.addAll(Arrays.asList(DocletUtil.tokenizeDelimitedToArray(role_name, ",")));
99                         }
100                     }
101                     // Add role to set
102
}
103             }
104
105             // get roles from finders
106
Collection finders = getCurrentClass().getDoc().getTags("ejb:finder");
107
108             for (Iterator j = finders.iterator(); j.hasNext(); ) {
109                 XTag tag = (XTag) j.next();
110                 String JavaDoc roleName = tag.getAttributeValue("role-name");
111
112                 if (roleName != null) {
113                     roleSet.addAll(Arrays.asList(DocletUtil.tokenizeDelimitedToArray(roleName, ",")));
114                 }
115             }
116
117             // and from pk field ( if any )
118
Collection pk = getCurrentClass().getDoc().getTags("ejb:pk");
119
120             for (Iterator j = pk.iterator(); j.hasNext(); ) {
121                 XTag tag = (XTag) j.next();
122                 String JavaDoc roleName = tag.getAttributeValue("role-name");
123
124                 if (roleName != null) {
125                     roleSet.addAll(Arrays.asList(DocletUtil.tokenizeDelimitedToArray(roleName, ",")));
126                 }
127             }
128         }
129
130         // Output set of roles
131
Iterator roleEnum = roleSet.iterator();
132
133         while (roleEnum.hasNext()) {
134             currentSecurityRoleName = (String JavaDoc) roleEnum.next();
135
136             generate(template);
137         }
138     }
139
140     /**
141      * Returns true if class/method denoted by doc has ejb:permission tag, false otherwise.
142      *
143      * @param doc Description of Parameter
144      * @return Description of the Returned Value
145      * @todo Shouldn't this method rather be in SecurityTagsHandler?
146      */

147     private boolean hasPermission(XDoc doc)
148     {
149         return doc.hasTag("ejb:permission");
150     }
151 }
152
Popular Tags