KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test;
23
24 import java.security.Permission JavaDoc;
25 import java.security.PermissionCollection JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.SortedMap JavaDoc;
33 import java.util.TreeMap JavaDoc;
34
35 /** The PermissionCollection object for NamespacePermissions.
36
37 @author Scott.Stark@jboss.org
38 @version $Revision: 37406 $
39 */

40 public class NamespacePermissionCollection extends PermissionCollection JavaDoc
41 {
42     private TreeMap JavaDoc namespacePerms = new TreeMap JavaDoc();
43     private TreeMap JavaDoc namespaceKeys = new TreeMap JavaDoc(new PermissionName.NameLengthComparator());
44
45     /** Creates new NamespacePermission */
46     public NamespacePermissionCollection()
47     {
48     }
49
50     public void add(Permission JavaDoc permission)
51     {
52         if( this.isReadOnly() )
53             throw new SecurityException JavaDoc("Cannot add permission to read-only collection");
54         if( (permission instanceof NamespacePermission) == false )
55             throw new IllegalArgumentException JavaDoc("Only NamespacePermission can be added, invalid="+permission);
56         NamespacePermission np = (NamespacePermission) permission;
57         PermissionName key = np.getFullName();
58         ArrayList JavaDoc tmp = (ArrayList JavaDoc) namespacePerms.get(key);
59         if( tmp == null )
60         {
61             tmp = new ArrayList JavaDoc();
62             namespacePerms.put(key, tmp);
63             namespaceKeys.put(key, key);
64         }
65         tmp.add(np);
66     }
67
68     /** Locate the closest permissions assigned to the namespace. This is based
69      *on the viewing the permission name as a heirarchical PermissionName and
70      */

71     public boolean implies(Permission JavaDoc permission)
72     {
73         boolean implies = false;
74         if( namespacePerms.isEmpty() == true )
75             return false;
76
77         NamespacePermission np = (NamespacePermission) permission;
78         // See if there is an exact permission for the name
79
PermissionName key = np.getFullName();
80         ArrayList JavaDoc tmp = (ArrayList JavaDoc) namespacePerms.get(key);
81         if( tmp == null )
82         { // Find the closest parent position.
83
SortedMap JavaDoc headMap = namespacePerms.headMap(key);
84             try
85             {
86                 PermissionName lastKey = (PermissionName) headMap.lastKey();
87                 if( lastKey.isParent(key) == true )
88                     tmp = (ArrayList JavaDoc) namespacePerms.get(lastKey);
89                 else
90                 {
91                     PermissionName[] keys = {};
92                     keys = (PermissionName[]) headMap.keySet().toArray(keys);
93                     for(int k = keys.length-1; k >= 0; k --)
94                     {
95                         lastKey = keys[k];
96                         if( lastKey.isParent(key) == true )
97                         {
98                             tmp = (ArrayList JavaDoc) namespacePerms.get(lastKey);
99                             break;
100                         }
101                     }
102                 }
103             }
104             catch(NoSuchElementException JavaDoc e)
105             { /* Assign the first permission
106                 Object firstKey = namespacePerms.firstKey();
107                 tmp = (ArrayList) namespacePerms.get(firstKey);
108         */

109             }
110         }
111
112         // See if the permission is implied by any we found
113
if( tmp != null )
114             implies = isImplied(tmp, np);
115 //log.debug("NPC["+this+"].implies("+np+") -> "+implies);
116
return implies;
117     }
118
119     public Enumeration JavaDoc elements()
120     {
121         Set JavaDoc s = namespaceKeys.keySet();
122         final Iterator JavaDoc iter = s.iterator();
123         Enumeration JavaDoc elements = new Enumeration JavaDoc()
124         {
125             ArrayList JavaDoc activeEntry;
126             int index;
127             public boolean hasMoreElements()
128             {
129                 boolean hasMoreElements = true;
130                 if( activeEntry == null || index >= activeEntry.size() )
131                 {
132                     hasMoreElements = iter.hasNext();
133                     activeEntry = null;
134                 }
135                 return hasMoreElements;
136             }
137             public Object JavaDoc nextElement()
138             {
139                 Object JavaDoc next = null;
140                 if( activeEntry == null )
141                 {
142                     Object JavaDoc key = iter.next();
143                     activeEntry = (ArrayList JavaDoc) namespacePerms.get(key);
144                     index = 0;
145                     next = activeEntry.get(index ++);
146                 }
147                 else
148                 {
149                     next = activeEntry.get(index ++);
150                 }
151                 return next;
152             }
153         };
154         return elements;
155     }
156
157
158     private boolean isImplied(ArrayList JavaDoc permissions, NamespacePermission np)
159     {
160         boolean isImplied = false;
161         for(int p = 0; p < permissions.size(); p ++)
162         {
163             Permission JavaDoc perm = (Permission JavaDoc) permissions.get(p);
164             isImplied |= perm.implies(np);
165             if( isImplied == true )
166                 break;
167         }
168         return isImplied;
169     }
170 }
171
Popular Tags