KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > security > context > SecurityContext


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SecurityContext.java,v 1.12 2005/07/26 17:17:46 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.security.context;
27
28 import java.io.Serializable JavaDoc;
29 import java.security.Principal JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.objectweb.jonas.security.jacc.JPolicyUserRoleMapping;
36
37 /**
38  * Implementation of the JOnAS Security Context CAUTION: Don't forget to modify
39  * the Marshalling class of this package used for IIOP protocol
40  * @author Jeff Mesnil (initial developer)
41  * @author Florent Benoit
42  */

43
44 public class SecurityContext implements Serializable JavaDoc {
45
46     /**
47      * Name of the principal
48      */

49     private String JavaDoc principalName = null;
50
51     /**
52      * List of roles of the principal
53      */

54     private final String JavaDoc[] roles;
55
56     /**
57      * run-as stack It uses ArrayList instead of java.util.Stack class This is
58      * for use ArrayList and not Vector as Stack implementation
59      */

60     private List JavaDoc runAsRoleStack = null;
61
62     /**
63      * This stack is used for defining the principal identity for runAs
64      */

65     private List JavaDoc runAsPrincipalStack = null;
66
67     /**
68      * This stack is used for defining the roles of the principal identity for
69      * runAs
70      */

71     private List JavaDoc runAsPrincipalRolesStack = null;
72
73     /**
74      * Constructor SecurityContext use the default principal name
75      */

76     public SecurityContext() {
77         this("ANONYMOUS");
78     }
79
80     /**
81      * Constructor SecurityContext
82      * @param principalName principal name
83      * @param roles the roles of the principal
84      */

85     public SecurityContext(String JavaDoc principalName, String JavaDoc[] roles) {
86         this.principalName = principalName;
87         this.roles = roles;
88     }
89
90     /**
91      * Constructor SecurityContext
92      * @param principalName principal name
93      * @param arrayRoles the list of the roles of the principal
94      */

95     public SecurityContext(String JavaDoc principalName, List JavaDoc arrayRoles) {
96         this(principalName, arrayRoles, null, null, null);
97     }
98
99     /**
100      * Constructor SecurityContext
101      * @param principalName principal name
102      * @param arrayRoles the list of the roles of the principal
103      * @param arrayRunas the RunAs stack
104      * @param arrayRunasPrincipal the RunAs stack for principal of runAs
105      * @param arrayRunasPrincipalRoles the RunAs stack for roles of the principal
106      *
107      */

108     protected SecurityContext(String JavaDoc principalName, List JavaDoc arrayRoles, List JavaDoc arrayRunas, List JavaDoc arrayRunasPrincipal, List JavaDoc arrayRunasPrincipalRoles) {
109         this.principalName = principalName;
110         String JavaDoc[] overridedRoles = JPolicyUserRoleMapping.getGlobalMappingForPrincipal(principalName);
111         if (overridedRoles != null) {
112             this.roles = overridedRoles;
113         } else {
114             if (arrayRoles != null) {
115                 //Convert list into array
116
String JavaDoc[] r = new String JavaDoc[arrayRoles.size()];
117                 r = (String JavaDoc[]) arrayRoles.toArray(r);
118                 this.roles = r;
119             } else {
120                 this.roles = null;
121             }
122         }
123
124         this.runAsRoleStack = arrayRunas;
125         this.runAsPrincipalStack = arrayRunasPrincipal;
126         this.runAsPrincipalRolesStack = arrayRunasPrincipalRoles;
127     }
128
129     /**
130      * Constructor SecurityContext
131      * @param principalName principal name
132      */

133     public SecurityContext(String JavaDoc principalName) {
134         this.principalName = principalName;
135         this.roles = new String JavaDoc[] {"JOnAS"};
136     }
137
138     /**
139      * Method getCallerPrincipal
140      * @param inRunAs is the caller is in a runAs case in RunAs mode this
141      * function must return the caller of the bean and not the run as
142      * identity (EJB 2.1 chapter21.2.5.1)
143      * @return the Principal in the Security Context
144      */

145     public Principal JavaDoc getCallerPrincipal(boolean inRunAs) {
146         return new InternalPrincipal(inRunAs);
147     }
148
149     /**
150      * Return the roles of the principal
151      * @param inRunAs caller is in run-as bean ?
152      * @return roles of this principal
153      */

154     public String JavaDoc[] getCallerPrincipalRoles(boolean inRunAs) {
155         String JavaDoc[] runAsRoles = null;
156         if (inRunAs) {
157             runAsRoles = peekLastRunAsPrincipalRoles();
158         } else {
159             runAsRoles = peekRunAsPrincipalRoles();
160         }
161         if (runAsRoles != null) {
162             return runAsRoles;
163         } else {
164             return roles;
165         }
166     }
167
168     /**
169      * Push : Pushes an item onto the top of this stack.
170      * @param role the role to add on top of the stack
171      */

172     public synchronized void pushRunAsRole(String JavaDoc role) {
173         getRunAsRoleStack().add(role);
174     }
175
176     /**
177      * Push : Pushes an item onto the top of this stack.
178      * @param principalName the name of the principal to add on top of the
179      * stack.
180      * @param roles list of roles of this principal.
181      */

182     public synchronized void pushRunAsPrincipal(String JavaDoc principalName, String JavaDoc[] roles) {
183         getRunAsPrincipalStack().add(principalName);
184         getRunAsPrincipalRolesStack().add(roles);
185     }
186
187     /**
188      * Pop : Removes the object at the top of the run-as stack
189      */

190     public synchronized void popRunAs() {
191         if (!getRunAsRoleStack().isEmpty()) {
192             getRunAsRoleStack().remove(getRunAsRoleStack().size() - 1);
193         }
194         if (!getRunAsPrincipalStack().isEmpty()) {
195             getRunAsPrincipalStack().remove(getRunAsPrincipalStack().size() - 1);
196         }
197         if (!getRunAsPrincipalRolesStack().isEmpty()) {
198             getRunAsPrincipalRolesStack().remove(getRunAsPrincipalRolesStack().size() - 1);
199         }
200     }
201
202     /**
203      * Peek : Looks at the object at the top of this stack without removing it
204      * from the stack.
205      * @return the role at the top of the stack
206      */

207     public synchronized String JavaDoc peekRunAsRole() {
208         if (getRunAsRoleStack().isEmpty()) {
209             return null;
210         } else {
211             return (String JavaDoc) getRunAsRoleStack().get(getRunAsRoleStack().size() - 1);
212         }
213     }
214
215     /**
216      * Peek : Looks at the object at the top of this stack without removing it
217      * from the stack.
218      * @return the principal at the top of the stack
219      */

220     public synchronized String JavaDoc peekRunAsPrincipal() {
221         if (getRunAsPrincipalStack().isEmpty()) {
222             return null;
223         } else {
224             return (String JavaDoc) getRunAsPrincipalStack().get(getRunAsPrincipalStack().size() - 1);
225         }
226     }
227
228     /**
229      * Peek : Looks at the object at the top of this stack without removing it
230      * from the stack.
231      * @return the principal at the top of the stack
232      */

233     public synchronized String JavaDoc peekLastRunAsPrincipal() {
234         if (getRunAsPrincipalStack().size() < 2) {
235             return null;
236         } else {
237             return (String JavaDoc) getRunAsPrincipalStack().get(getRunAsPrincipalStack().size() - 2);
238         }
239     }
240
241     /**
242      * Peek : Looks at the object at the top of this stack without removing it
243      * from the stack.
244      * @return the principal at the top of the stack
245      */

246     public synchronized String JavaDoc[] peekRunAsPrincipalRoles() {
247         if (getRunAsPrincipalRolesStack().isEmpty()) {
248             return null;
249         } else {
250             return (String JavaDoc[]) getRunAsPrincipalRolesStack().get(getRunAsPrincipalRolesStack().size() - 1);
251         }
252     }
253
254     /**
255      * Peek : Looks at the object at the top of this stack without removing it
256      * from the stack.
257      * @return the principal at the top of the stack
258      */

259     public synchronized String JavaDoc[] peekLastRunAsPrincipalRoles() {
260         if (getRunAsPrincipalRolesStack().size() < 2) {
261             return null;
262         } else {
263             return (String JavaDoc[]) getRunAsPrincipalRolesStack().get(getRunAsPrincipalRolesStack().size() - 2);
264         }
265     }
266
267     /**
268      * Gets the stack which manages the run-as
269      * @return the stack which manages the run-as
270      */

271     public synchronized List JavaDoc getRunAsRoleStack() {
272         if (runAsRoleStack == null) {
273             runAsRoleStack = Collections.synchronizedList(new ArrayList JavaDoc());
274         }
275         return runAsRoleStack;
276     }
277
278     /**
279      * Gets the stack which manages the roles of the current run-as principal
280      * @return the stack which manages the roles of the current run-as principal
281      */

282     public synchronized List JavaDoc getRunAsPrincipalRolesStack() {
283         if (runAsPrincipalRolesStack == null) {
284             runAsPrincipalRolesStack = Collections.synchronizedList(new ArrayList JavaDoc());
285         }
286         return runAsPrincipalRolesStack;
287     }
288
289     /**
290      * Gets the stack which manages the run-as principal
291      * @return the stack which manages the run-as principal
292      */

293     public synchronized List JavaDoc getRunAsPrincipalStack() {
294         if (runAsPrincipalStack == null) {
295             runAsPrincipalStack = Collections.synchronizedList(new ArrayList JavaDoc());
296         }
297         return runAsPrincipalStack;
298     }
299
300     /**
301      * Method toString
302      * @return String a string representation of the object
303      */

304     public String JavaDoc toString() {
305         String JavaDoc txt = "principal : name = " + principalName + "\n";
306         if (roles != null) {
307             for (int i = 0; i < roles.length; i++) {
308                 txt += "role[" + i + "] = " + roles[i] + "\n";
309             }
310         }
311         if (runAsRoleStack != null) {
312             Iterator JavaDoc iRunas = runAsRoleStack.iterator();
313             int i = 0;
314             while (iRunas.hasNext()) {
315                 txt += "runas[" + i + "] = " + ((String JavaDoc) iRunas.next()) + "\n";
316             }
317         }
318         return txt;
319     }
320
321     /**
322      * @param runningRunAs bean is currently running with run-as enabled
323      * @return the principal name.
324      */

325     protected String JavaDoc getPrincipalName(boolean runningRunAs) {
326         String JavaDoc principal = null;
327         if (runningRunAs) {
328             principal = peekLastRunAsPrincipal();
329         } else {
330             principal = peekRunAsPrincipal();
331         }
332         if (principal != null) {
333             return principal;
334         } else {
335             return principalName;
336         }
337     }
338
339     /**
340      * @return the principal Name.
341      */

342     public String JavaDoc getPrincipalName() {
343         return principalName;
344     }
345
346     /**
347      * @return the roles.
348      */

349     protected String JavaDoc[] getRoles() {
350         return roles;
351     }
352
353     /**
354      * @author Florent Benoit Defines an implementation of the Principal object
355      */

356     class InternalPrincipal implements Principal JavaDoc {
357
358         /**
359          * When building principal name, use right principal name. This is
360          * possible by knowing the bean making the call is in a run-as bean or
361          * not.
362          */

363         private boolean inRunAs = false;
364
365         /**
366          * Constructor
367          * @param inRunAs caller is in run-as bean ?
368          */

369         public InternalPrincipal(boolean inRunAs) {
370             super();
371             this.inRunAs = inRunAs;
372
373         }
374
375         /**
376          * @return name of this principal
377          */

378         public String JavaDoc getName() {
379             return getPrincipalName(inRunAs);
380         }
381
382         /**
383          * @param o the object to compare
384          * @return true if the given principal has the same name than ours
385          */

386         public boolean equals(Object JavaDoc o) {
387             if (o instanceof Principal JavaDoc) {
388                 return getPrincipalName(inRunAs).equals(((Principal JavaDoc) o).getName());
389             }
390             return false;
391         }
392
393         /**
394          * Hashcode for this principal
395          * @return hashcode of the principal name
396          */

397         public int hashCode() {
398             return getPrincipalName(inRunAs).hashCode();
399         }
400
401         /**
402          * Display this object
403          * @return string representing this object
404          */

405         public String JavaDoc toString() {
406             return "name = " + getPrincipalName(inRunAs);
407         }
408     }
409
410 }
411
412
Popular Tags