View Javadoc

1   /***
2    * Role.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.permissioning;
27  
28  import java.util.HashSet;
29  import java.util.Hashtable;
30  import java.util.Iterator;
31  
32  import org.jcreme.enumerations.Enumeration;
33  
34  /***
35   * Role defines the business Roles available to Users. These Roles are
36   * associated to business Tasks that can be legitimately carried out.
37   * 
38   * @author $Author: dbregeon $
39   * @version $Revision: 1.1 $
40   */
41  public class Role extends Enumeration {
42  
43      protected static final Hashtable fromName = new Hashtable();
44  
45      protected HashSet tasks = new HashSet();
46  
47      protected HashSet subRoles = new HashSet();
48  
49      protected Domain domain = null;
50  
51      protected Role(String name) {
52          super(name);
53      }
54  
55      public void addSubRole(Role r) throws IllegalArgumentException {
56          if (r != null) {
57              if (r.isSubRole(this)) {
58                  throw new IllegalArgumentException("Cycle detected.");
59              }
60              this.subRoles.add(r);
61          }
62      }
63  
64      public void removeSubRole(Role r) {
65          if (r != null) {
66              this.subRoles.remove(r);
67          }
68      }
69  
70      protected boolean isSubRole(Role r) {
71          boolean result = this.subRoles.contains(r);
72          if (!result) {
73              Iterator iter = this.subRoles.iterator();
74              while ((iter.hasNext()) && (!result)) {
75                  result = ((Role) iter.next()).isSubRole(r);
76              }
77          }
78          return result;
79      }
80  
81      public void addTask(Task t) {
82          if (t != null) {
83              this.tasks.add(t);
84              if (t.getRole() != this) {
85                  t.setRole(this);
86              }
87          }
88      }
89  
90      public void removeTask(Task t) {
91          if (t != null) {
92              this.tasks.remove(t);
93              if (t.getRole() == this) {
94                  t.setRole(null);
95              }
96          }
97      }
98  
99      public Task[] getTasks() {
100         return (Task[]) this.tasks.toArray(new Task[0]);
101     }
102 
103     /***
104      * Checks if the Role or one of its sub roles is authorized to perform that
105      * Task. This applies to a business Task so the Task has to be directly
106      * authorized for the Role.
107      * 
108      * @param t
109      *            the Task to perform.
110      * @return true if the Role is authorized directly or indirectly to perform
111      *         the Task.
112      */
113     public boolean hasTask(Task t) {
114         boolean result = (t != null) && (this.tasks.contains(t));
115         if (!result) {
116             Iterator iter = this.subRoles.iterator();
117             while ((iter.hasNext()) && (!result)) {
118                 result = ((Role) iter.next()).hasTask(t);
119             }
120         }
121         return result;
122     }
123 
124     /***
125      * This method provides the purpose that the role has to carry out the given
126      * Task. There may be multiple purposes as Role may be authorized to carry
127      * 
128      * @param t
129      * @return
130      */
131     public Purpose getPurpose(Task t) {
132         return null;
133     }
134 
135     public void setDomain(Domain d) {
136         this.domain = d;
137     }
138 
139     public Domain getDomain() {
140         return this.domain;
141     }
142 
143     public static Role get(String name) {
144         Role result = null;
145         if (name != null) {
146             result = (Role) fromName.get(name);
147             if (result == null) {
148                 result = new Role(name);
149             }
150         }
151         return result;
152     }
153 
154     public static boolean contains(String name) {
155         return (name != null) && (fromName.containsKey(name));
156     }
157 
158     public static void remove(String name) {
159         if (name != null) {
160             fromName.remove(name);
161         }
162     }
163 
164     /***
165      * @see org.jcreme.enumerations.Enumeration#getFromName()
166      */
167     protected Hashtable getFromName() {
168         return fromName;
169     }
170 
171     public static Role[] getRoles() {
172         return (Role[]) fromName.values().toArray(new Role[0]);
173     }
174 
175     public static Enumeration[] getValues() {
176         return (Enumeration[]) fromName.values().toArray(new Enumeration[0]);
177     }
178 
179 }