KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > NamespacePermissionCollection


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

7 package org.jboss.test;
8
9 import java.security.Permission JavaDoc;
10 import java.security.PermissionCollection JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.NoSuchElementException JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.SortedMap JavaDoc;
18 import java.util.TreeMap JavaDoc;
19
20 /** The PermissionCollection object for NamespacePermissions.
21
22 @author Scott.Stark@jboss.org
23 @version $Revision: 1.5.6.1 $
24 */

25 public class NamespacePermissionCollection extends PermissionCollection JavaDoc
26 {
27     private TreeMap JavaDoc namespacePerms = new TreeMap JavaDoc();
28     private TreeMap JavaDoc namespaceKeys = new TreeMap JavaDoc(new PermissionName.NameLengthComparator());
29
30     /** Creates new NamespacePermission */
31     public NamespacePermissionCollection()
32     {
33     }
34
35     public void add(Permission JavaDoc permission)
36     {
37         if( this.isReadOnly() )
38             throw new SecurityException JavaDoc("Cannot add permission to read-only collection");
39         if( (permission instanceof NamespacePermission) == false )
40             throw new IllegalArgumentException JavaDoc("Only NamespacePermission can be added, invalid="+permission);
41         NamespacePermission np = (NamespacePermission) permission;
42         PermissionName key = np.getFullName();
43         ArrayList JavaDoc tmp = (ArrayList JavaDoc) namespacePerms.get(key);
44         if( tmp == null )
45         {
46             tmp = new ArrayList JavaDoc();
47             namespacePerms.put(key, tmp);
48             namespaceKeys.put(key, key);
49         }
50         tmp.add(np);
51     }
52
53     /** Locate the closest permissions assigned to the namespace. This is based
54      *on the viewing the permission name as a heirarchical PermissionName and
55      */

56     public boolean implies(Permission JavaDoc permission)
57     {
58         boolean implies = false;
59         if( namespacePerms.isEmpty() == true )
60             return false;
61
62         NamespacePermission np = (NamespacePermission) permission;
63         // See if there is an exact permission for the name
64
PermissionName key = np.getFullName();
65         ArrayList JavaDoc tmp = (ArrayList JavaDoc) namespacePerms.get(key);
66         if( tmp == null )
67         { // Find the closest parent position.
68
SortedMap JavaDoc headMap = namespacePerms.headMap(key);
69             try
70             {
71                 PermissionName lastKey = (PermissionName) headMap.lastKey();
72                 if( lastKey.isParent(key) == true )
73                     tmp = (ArrayList JavaDoc) namespacePerms.get(lastKey);
74                 else
75                 {
76                     PermissionName[] keys = {};
77                     keys = (PermissionName[]) headMap.keySet().toArray(keys);
78                     for(int k = keys.length-1; k >= 0; k --)
79                     {
80                         lastKey = keys[k];
81                         if( lastKey.isParent(key) == true )
82                         {
83                             tmp = (ArrayList JavaDoc) namespacePerms.get(lastKey);
84                             break;
85                         }
86                     }
87                 }
88             }
89             catch(NoSuchElementException JavaDoc e)
90             { /* Assign the first permission
91                 Object firstKey = namespacePerms.firstKey();
92                 tmp = (ArrayList) namespacePerms.get(firstKey);
93         */

94             }
95         }
96
97         // See if the permission is implied by any we found
98
if( tmp != null )
99             implies = isImplied(tmp, np);
100 //System.out.println("NPC["+this+"].implies("+np+") -> "+implies);
101
return implies;
102     }
103
104     public Enumeration JavaDoc elements()
105     {
106         Set JavaDoc s = namespaceKeys.keySet();
107         final Iterator JavaDoc iter = s.iterator();
108         Enumeration JavaDoc elements = new Enumeration JavaDoc()
109         {
110             ArrayList JavaDoc activeEntry;
111             int index;
112             public boolean hasMoreElements()
113             {
114                 boolean hasMoreElements = true;
115                 if( activeEntry == null || index >= activeEntry.size() )
116                 {
117                     hasMoreElements = iter.hasNext();
118                     activeEntry = null;
119                 }
120                 return hasMoreElements;
121             }
122             public Object JavaDoc nextElement()
123             {
124                 Object JavaDoc next = null;
125                 if( activeEntry == null )
126                 {
127                     Object JavaDoc key = iter.next();
128                     activeEntry = (ArrayList JavaDoc) namespacePerms.get(key);
129                     index = 0;
130                     next = activeEntry.get(index ++);
131                 }
132                 else
133                 {
134                     next = activeEntry.get(index ++);
135                 }
136                 return next;
137             }
138         };
139         return elements;
140     }
141
142
143     private boolean isImplied(ArrayList JavaDoc permissions, NamespacePermission np)
144     {
145         boolean isImplied = false;
146         for(int p = 0; p < permissions.size(); p ++)
147         {
148             Permission JavaDoc perm = (Permission JavaDoc) permissions.get(p);
149             isImplied |= perm.implies(np);
150             if( isImplied == true )
151                 break;
152         }
153         return isImplied;
154     }
155 }
156
Popular Tags