001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.security.Permission;
025: import java.security.PermissionCollection;
026: import java.util.ArrayList;
027: import java.util.Enumeration;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.NoSuchElementException;
031: import java.util.Set;
032: import java.util.SortedMap;
033: import java.util.TreeMap;
034:
035: /** The PermissionCollection object for NamespacePermissions.
036:
037: @author Scott.Stark@jboss.org
038: @version $Revision: 37390 $
039: */
040: public class NamespacePermissionCollection extends PermissionCollection {
041: private TreeMap namespacePerms = new TreeMap();
042: private TreeMap namespaceKeys = new TreeMap(
043: new PermissionName.NameLengthComparator());
044:
045: /** Creates new NamespacePermission */
046: public NamespacePermissionCollection() {
047: }
048:
049: public void add(Permission permission) {
050: if (this .isReadOnly())
051: throw new SecurityException(
052: "Cannot add permission to read-only collection");
053: if ((permission instanceof NamespacePermission) == false)
054: throw new IllegalArgumentException(
055: "Only NamespacePermission can be added, invalid="
056: + permission);
057: NamespacePermission np = (NamespacePermission) permission;
058: PermissionName key = np.getFullName();
059: ArrayList tmp = (ArrayList) namespacePerms.get(key);
060: if (tmp == null) {
061: tmp = new ArrayList();
062: namespacePerms.put(key, tmp);
063: namespaceKeys.put(key, key);
064: }
065: tmp.add(np);
066: }
067:
068: /** Locate the closest permissions assigned to the namespace. This is based
069: *on the viewing the permission name as a heirarchical PermissionName and
070: */
071: public boolean implies(Permission permission) {
072: boolean implies = false;
073: if (namespacePerms.isEmpty() == true)
074: return false;
075:
076: NamespacePermission np = (NamespacePermission) permission;
077: // See if there is an exact permission for the name
078: PermissionName key = np.getFullName();
079: ArrayList tmp = (ArrayList) namespacePerms.get(key);
080: if (tmp == null) { // Find the closest parent position.
081: SortedMap headMap = namespacePerms.headMap(key);
082: try {
083: PermissionName lastKey = (PermissionName) headMap
084: .lastKey();
085: if (lastKey.isParent(key) == true)
086: tmp = (ArrayList) namespacePerms.get(lastKey);
087: else {
088: PermissionName[] keys = {};
089: keys = (PermissionName[]) headMap.keySet().toArray(
090: keys);
091: for (int k = keys.length - 1; k >= 0; k--) {
092: lastKey = keys[k];
093: if (lastKey.isParent(key) == true) {
094: tmp = (ArrayList) namespacePerms
095: .get(lastKey);
096: break;
097: }
098: }
099: }
100: } catch (NoSuchElementException e) { /* Assign the first permission
101: Object firstKey = namespacePerms.firstKey();
102: tmp = (ArrayList) namespacePerms.get(firstKey);
103: */
104: }
105: }
106:
107: // See if the permission is implied by any we found
108: if (tmp != null)
109: implies = isImplied(tmp, np);
110: //System.out.println("NPC["+this+"].implies("+np+") -> "+implies);
111: return implies;
112: }
113:
114: public Enumeration elements() {
115: Set s = namespaceKeys.keySet();
116: final Iterator iter = s.iterator();
117: Enumeration elements = new Enumeration() {
118: ArrayList activeEntry;
119: int index;
120:
121: public boolean hasMoreElements() {
122: boolean hasMoreElements = true;
123: if (activeEntry == null || index >= activeEntry.size()) {
124: hasMoreElements = iter.hasNext();
125: activeEntry = null;
126: }
127: return hasMoreElements;
128: }
129:
130: public Object nextElement() {
131: Object next = null;
132: if (activeEntry == null) {
133: Object key = iter.next();
134: activeEntry = (ArrayList) namespacePerms.get(key);
135: index = 0;
136: next = activeEntry.get(index++);
137: } else {
138: next = activeEntry.get(index++);
139: }
140: return next;
141: }
142: };
143: return elements;
144: }
145:
146: private boolean isImplied(ArrayList permissions,
147: NamespacePermission np) {
148: boolean isImplied = false;
149: for (int p = 0; p < permissions.size(); p++) {
150: Permission perm = (Permission) permissions.get(p);
151: isImplied |= perm.implies(np);
152: if (isImplied == true)
153: break;
154: }
155: return isImplied;
156: }
157: }
|