KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > security > Scheme


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.security;
10
11 import org.jboss.portal.common.FQN;
12 import org.jboss.portal.common.util.SetMap;
13
14 import java.util.Set JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
24  * @version $Revision: 1.4 $
25  */

26 public class Scheme implements Serializable JavaDoc
27 {
28
29    /** Version. */
30    static final long serialVersionUID = 6459318089372864576L;
31
32    /** Contains the scheme data. */
33    private Map paths;
34
35    /**
36     * Creates a new empty scheme.
37     */

38    public Scheme()
39    {
40       paths = new HashMap JavaDoc();
41    }
42
43    /**
44     * Copy constructor.
45     *
46     * @throws IllegalArgumentException if the copied scheme argument is null
47     */

48    public Scheme(Scheme other) throws IllegalArgumentException JavaDoc
49    {
50       if (other == null)
51       {
52          throw new IllegalArgumentException JavaDoc("Cannot copy null");
53       }
54
55       //
56
paths = new HashMap JavaDoc();
57       for (Iterator JavaDoc i = other.paths.entrySet().iterator(); i.hasNext();)
58       {
59          Map.Entry entry = (Map.Entry)i.next();
60          FQN key = (FQN)entry.getKey();
61          SetMap value = (SetMap)entry.getValue();
62          paths.put(key, value);
63       }
64    }
65
66    /**
67     * Return a set of the children names.
68     */

69    public Set JavaDoc getChildrenNames(String JavaDoc[] path)
70    {
71       FQN parent = new FQN(path);
72       Set JavaDoc names = new HashSet JavaDoc();
73       for (Iterator JavaDoc i = paths.keySet().iterator(); i.hasNext();)
74       {
75          FQN child = (FQN)i.next();
76          if (child.isChildOf(parent))
77          {
78             names.add(child.getName(child.size() - 1));
79          }
80       }
81       return names;
82    }
83
84    /**
85     *
86     */

87    public Set JavaDoc getRoles(String JavaDoc[] path)
88    {
89       FQN fqn = new FQN(path);
90       SetMap sm = (SetMap)paths.get(fqn);
91       if (sm != null)
92       {
93          return new HashSet JavaDoc(sm.keySet());
94       }
95       else
96       {
97          return Collections.EMPTY_SET;
98       }
99    }
100
101    /**
102     * Get the set of permissions for the given domain, role and path.
103     */

104    public Set JavaDoc get(String JavaDoc[] path, String JavaDoc role)
105    {
106       Set JavaDoc permissions = null;
107       SetMap sm = (SetMap)paths.get(new FQN(path));
108       if (sm != null)
109       {
110          permissions = sm.get(role);
111       }
112       if (permissions == null)
113       {
114          permissions = Collections.EMPTY_SET;
115       }
116       return permissions;
117    }
118
119    /**
120     * Get the set of permissions for the given roles and path.
121     */

122    public Set JavaDoc get(String JavaDoc[] path, Set JavaDoc roles)
123    {
124       Set JavaDoc union = null;
125       for (Iterator JavaDoc i = roles.iterator(); i.hasNext();)
126       {
127          String JavaDoc role = (String JavaDoc)i.next();
128          Set JavaDoc permissions = get(path, role);
129          if (permissions.size() > 0)
130          {
131             if (union == null)
132             {
133                union = new HashSet JavaDoc(permissions);
134             }
135             else
136             {
137                union.addAll(permissions);
138             }
139          }
140       }
141       if (union == null)
142       {
143          union = Collections.EMPTY_SET;
144       }
145       return union;
146    }
147
148    /**
149     * Add a permission.
150     *
151     * @throws UnsupportedOperationException if the scheme is not mutable
152     */

153    public synchronized void add(String JavaDoc[] path, String JavaDoc role, String JavaDoc permission)
154    {
155       FQN key = new FQN(path);
156
157       // Modify setmap
158
SetMap sm = (SetMap)paths.get(key);
159       if (sm == null)
160       {
161          sm = new SetMap();
162       }
163       else
164       {
165          sm = new SetMap(sm);
166       }
167       sm.put(role, permission);
168
169       //
170
HashMap JavaDoc copy = new HashMap JavaDoc(paths);
171       copy.put(key, sm);
172       paths = copy;
173    }
174
175    /**
176     * Remove a permission.
177     *
178     * @throws UnsupportedOperationException if the scheme is not mutable
179     */

180    public synchronized void remove(String JavaDoc[] path, String JavaDoc role, String JavaDoc permission)
181    {
182       FQN key = new FQN(path);
183
184       // Modify setmap
185
SetMap sm = (SetMap)paths.get(key);
186       if (sm != null)
187       {
188          sm = new SetMap(sm);
189          sm.remove(role, permission);
190          HashMap JavaDoc copy = new HashMap JavaDoc(paths);
191          copy.put(key, sm);
192          paths = copy;
193       }
194    }
195
196    /**
197     * Remove an item.
198     *
199     * @throws UnsupportedOperationException if the scheme is not mutable
200     */

201    public synchronized void remove(String JavaDoc[] path)
202    {
203       FQN key = new FQN(path);
204       Map copy = new HashMap JavaDoc(paths);
205       copy.remove(key);
206       paths = copy;
207    }
208
209    /**
210     * Clear the scheme.
211     *
212     * @throws UnsupportedOperationException if the scheme is not mutable
213     */

214    public synchronized void clear()
215    {
216       paths = new HashMap JavaDoc();
217    }
218 }
219
Popular Tags