KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > RunAsIdentity


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.security;
9
10 import java.security.Principal JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14
15 /**
16  * The RunAsIdentity is a Principal that associates the run-as principal
17  * with his run-as role(s).
18  *
19  * @author Thomas.Diesler@jboss.org
20  * @version $Revision: 1.5 $
21  */

22 public class RunAsIdentity extends CallerIdentity
23 {
24    /** The run-as role principals */
25    private Set JavaDoc runAsRoles = new HashSet JavaDoc();
26
27    private static final String JavaDoc ANOYMOUS_PRINCIPAL = "anonymous";
28
29    /**
30     * Construct an inmutable instance of a RunAsIdentity
31     */

32    public RunAsIdentity(String JavaDoc roleName, String JavaDoc principalName)
33    {
34       // we don't support run-as credetials
35
super(principalName != null ? principalName : ANOYMOUS_PRINCIPAL, null);
36
37       if (roleName == null)
38          throw new IllegalArgumentException JavaDoc("The run-as identity must have at least one role");
39
40       runAsRoles.add(new SimplePrincipal(roleName));
41    }
42
43    /**
44     * Construct an inmutable instance of a RunAsIdentity
45     */

46    public RunAsIdentity(String JavaDoc roleName, String JavaDoc principalName, Set JavaDoc extraRoleNames)
47    {
48       this(roleName, principalName);
49
50       // these come from the assembly-descriptor
51
if (extraRoleNames != null)
52       {
53          Iterator JavaDoc it = extraRoleNames.iterator();
54          while (it.hasNext())
55          {
56             String JavaDoc extraRoleName = (String JavaDoc) it.next();
57             runAsRoles.add(new SimplePrincipal(extraRoleName));
58          }
59       }
60    }
61
62    public Set JavaDoc getRunAsRoles()
63    {
64       return new HashSet JavaDoc(runAsRoles);
65    }
66
67    public boolean doesUserHaveRole(Principal JavaDoc role)
68    {
69       return runAsRoles.contains(role);
70    }
71
72    /**
73     * True if the run-as principal has any of the method roles
74     */

75    public boolean doesUserHaveRole(Set JavaDoc methodRoles)
76    {
77       Iterator JavaDoc it = methodRoles.iterator();
78       while (it.hasNext())
79       {
80          Principal JavaDoc role = (Principal JavaDoc) it.next();
81          if (doesUserHaveRole(role))
82             return true;
83       }
84       return false;
85    }
86
87    /**
88     * Returns a string representation of the object.
89     * @return a string representation of the object.
90     */

91    public String JavaDoc toString()
92    {
93       return "[roles=" + runAsRoles + ",principal=" + getName() + "]";
94    }
95 }
96
Popular Tags