001: /*
002: * @(#)UnresolvedPermissionCollection.java 1.17 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.security;
029:
030: import java.util.*;
031: import java.io.ObjectStreamField;
032: import java.io.ObjectOutputStream;
033: import java.io.ObjectInputStream;
034: import java.io.IOException;
035:
036: /**
037: * A UnresolvedPermissionCollection stores a collection
038: * of UnresolvedPermission permissions.
039: *
040: * @see java.security.Permission
041: * @see java.security.Permissions
042: * @see java.security.UnresolvedPermission
043: *
044: * @version 1.9 00/02/02
045: *
046: * @author Roland Schemers
047: *
048: * @serial include
049: */
050:
051: final class UnresolvedPermissionCollection extends PermissionCollection
052: implements java.io.Serializable {
053: /**
054: * Key is permission type, value is a list of the UnresolvedPermissions
055: * of the same type.
056: * Not serialized; see serialization section at end of class.
057: */
058: private transient Map perms;
059:
060: /**
061: * Create an empty UnresolvedPermissionCollection object.
062: *
063: */
064: public UnresolvedPermissionCollection() {
065: perms = new HashMap(11);
066: }
067:
068: /**
069: * Adds a permission to this UnresolvedPermissionCollection.
070: * The key for the hash is the unresolved permission's type (class) name.
071: *
072: * @param permission the Permission object to add.
073: */
074:
075: public void add(Permission permission) {
076: if (!(permission instanceof UnresolvedPermission))
077: throw new IllegalArgumentException("invalid permission: "
078: + permission);
079: UnresolvedPermission up = (UnresolvedPermission) permission;
080:
081: // No need to synchronize because all adds are done sequentially
082: // (by policy provider) before any getUnresolvedPermissions() calls.
083: // If policy provider is multithreaded, it should lock the
084: // collection to allow concurrent adds
085:
086: List v = (List) perms.get(up.getName());
087: if (v == null) {
088: v = new ArrayList();
089: perms.put(up.getName(), v);
090: }
091: v.add(up);
092: }
093:
094: /**
095: * get any unresolved permissions of the same type as p,
096: * and return the List containing them.
097: */
098: // No need to synchronize; list is only gotten for reading
099: List getUnresolvedPermissions(Permission p) {
100: return (List) perms.get(p.getClass().getName());
101: }
102:
103: /**
104: * always returns false for unresolved permissions
105: *
106: */
107: public boolean implies(Permission permission) {
108: return false;
109: }
110:
111: /**
112: * Returns an enumeration of all the UnresolvedPermission lists in the
113: * container.
114: *
115: * @return an enumeration of all the UnresolvedPermission objects.
116: */
117:
118: // No need to synchronize; lists won't change after initial adds
119: // A performance improvement might be to construct the global
120: // list lazily as enumeration proceeds.
121: public Enumeration elements() {
122: List results = new ArrayList(); // where results are stored
123:
124: // Get iterator of Map values (which are lists of permissions)
125: for (Iterator iter = perms.values().iterator(); iter.hasNext();) {
126: results.addAll((List) iter.next());
127: }
128:
129: return Collections.enumeration(results);
130: }
131:
132: private static final long serialVersionUID = -7176153071733132400L;
133:
134: // Need to maintain serialization interoperability with earlier releases,
135: // which had the serializable field:
136: // private Hashtable permissions; // keyed on type
137:
138: /**
139: * @serialField permissions java.util.Hashtable
140: * A table of the UnresolvedPermissions keyed on type, value is Vector
141: * of permissions
142: */
143: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
144: "permissions", Hashtable.class), };
145:
146: /**
147: * @serialData Default field.
148: */
149: /*
150: * Writes the contents of the perms field out as a Hashtable
151: * in which the values are Vectors for
152: * serialization compatibility with earlier releases.
153: */
154: private void writeObject(ObjectOutputStream out) throws IOException {
155: // Don't call out.defaultWriteObject()
156:
157: // Copy perms into a Hashtable
158: Hashtable permissions = new Hashtable(perms.size() * 2);
159:
160: // Convert each entry (List) into a Vector
161: Set set = perms.entrySet();
162: for (Iterator iter = set.iterator(); iter.hasNext();) {
163: Map.Entry e = (Map.Entry) iter.next();
164:
165: // Convert list into Vector
166: List list = (List) e.getValue();
167: Vector vec = new Vector(list.size());
168: vec.addAll(list);
169:
170: // Add to Hashtable being serialized
171: permissions.put(e.getKey(), vec);
172: }
173:
174: // Write out serializable fields
175: ObjectOutputStream.PutField pfields = out.putFields();
176: pfields.put("permissions", permissions);
177: out.writeFields();
178: }
179:
180: /*
181: * Reads in a Hashtable in which the values are Vectors of
182: * UnresolvedPermissions and saves them in the perms field.
183: */
184: private void readObject(ObjectInputStream in) throws IOException,
185: ClassNotFoundException {
186: // Don't call defaultReadObject()
187:
188: // Read in serialized fields
189: ObjectInputStream.GetField gfields = in.readFields();
190:
191: // Get permissions
192: Hashtable permissions = (Hashtable) gfields.get("permissions",
193: null);
194: perms = new HashMap(permissions.size() * 2);
195:
196: // Convert each entry (Vector) into a List
197: Set set = permissions.entrySet();
198: for (Iterator iter = set.iterator(); iter.hasNext();) {
199: Map.Entry e = (Map.Entry) iter.next();
200:
201: // Convert Vector into ArrayList
202: Vector vec = (Vector) e.getValue();
203: List list = new ArrayList(vec.size());
204: list.addAll(vec);
205:
206: // Add to Hashtable being serialized
207: perms.put(e.getKey(), list);
208: }
209: }
210: }
|