001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security;
027
028 import java.util.*;
029 import java.io.ObjectStreamField;
030 import java.io.ObjectOutputStream;
031 import java.io.ObjectInputStream;
032 import java.io.IOException;
033
034 /**
035 * A UnresolvedPermissionCollection stores a collection
036 * of UnresolvedPermission permissions.
037 *
038 * @see java.security.Permission
039 * @see java.security.Permissions
040 * @see java.security.UnresolvedPermission
041 *
042 * @version 1.22 07/05/05
043 *
044 * @author Roland Schemers
045 *
046 * @serial include
047 */
048
049 final class UnresolvedPermissionCollection extends PermissionCollection
050 implements java.io.Serializable {
051 /**
052 * Key is permission type, value is a list of the UnresolvedPermissions
053 * of the same type.
054 * Not serialized; see serialization section at end of class.
055 */
056 private transient Map<String, List<UnresolvedPermission>> perms;
057
058 /**
059 * Create an empty UnresolvedPermissionCollection object.
060 *
061 */
062 public UnresolvedPermissionCollection() {
063 perms = new HashMap<String, List<UnresolvedPermission>>(11);
064 }
065
066 /**
067 * Adds a permission to this UnresolvedPermissionCollection.
068 * The key for the hash is the unresolved permission's type (class) name.
069 *
070 * @param permission the Permission object to add.
071 */
072
073 public void add(Permission permission) {
074 if (!(permission instanceof UnresolvedPermission))
075 throw new IllegalArgumentException("invalid permission: "
076 + permission);
077 UnresolvedPermission up = (UnresolvedPermission) permission;
078
079 List<UnresolvedPermission> v;
080 synchronized (this ) {
081 v = perms.get(up.getName());
082 if (v == null) {
083 v = new ArrayList<UnresolvedPermission>();
084 perms.put(up.getName(), v);
085 }
086 }
087 synchronized (v) {
088 v.add(up);
089 }
090 }
091
092 /**
093 * get any unresolved permissions of the same type as p,
094 * and return the List containing them.
095 */
096 List<UnresolvedPermission> getUnresolvedPermissions(Permission p) {
097 synchronized (this ) {
098 return perms.get(p.getClass().getName());
099 }
100 }
101
102 /**
103 * always returns false for unresolved permissions
104 *
105 */
106 public boolean implies(Permission permission) {
107 return false;
108 }
109
110 /**
111 * Returns an enumeration of all the UnresolvedPermission lists in the
112 * container.
113 *
114 * @return an enumeration of all the UnresolvedPermission objects.
115 */
116
117 public Enumeration<Permission> elements() {
118 List<Permission> results = new ArrayList<Permission>(); // where results are stored
119
120 // Get iterator of Map values (which are lists of permissions)
121 synchronized (this ) {
122 for (List<UnresolvedPermission> l : perms.values()) {
123 synchronized (l) {
124 results.addAll(l);
125 }
126 }
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<String, Vector<UnresolvedPermission>> permissions = new Hashtable<String, Vector<UnresolvedPermission>>(
159 perms.size() * 2);
160
161 // Convert each entry (List) into a Vector
162 synchronized (this ) {
163 Set<Map.Entry<String, List<UnresolvedPermission>>> set = perms
164 .entrySet();
165 for (Map.Entry<String, List<UnresolvedPermission>> e : set) {
166 // Convert list into Vector
167 List<UnresolvedPermission> list = e.getValue();
168 Vector<UnresolvedPermission> vec = new Vector<UnresolvedPermission>(
169 list.size());
170 synchronized (list) {
171 vec.addAll(list);
172 }
173
174 // Add to Hashtable being serialized
175 permissions.put(e.getKey(), vec);
176 }
177 }
178
179 // Write out serializable fields
180 ObjectOutputStream.PutField pfields = out.putFields();
181 pfields.put("permissions", permissions);
182 out.writeFields();
183 }
184
185 /*
186 * Reads in a Hashtable in which the values are Vectors of
187 * UnresolvedPermissions and saves them in the perms field.
188 */
189 private void readObject(ObjectInputStream in) throws IOException,
190 ClassNotFoundException {
191 // Don't call defaultReadObject()
192
193 // Read in serialized fields
194 ObjectInputStream.GetField gfields = in.readFields();
195
196 // Get permissions
197 Hashtable<String, Vector<UnresolvedPermission>> permissions = (Hashtable<String, Vector<UnresolvedPermission>>) gfields
198 .get("permissions", null);
199 perms = new HashMap<String, List<UnresolvedPermission>>(
200 permissions.size() * 2);
201
202 // Convert each entry (Vector) into a List
203 Set<Map.Entry<String, Vector<UnresolvedPermission>>> set = permissions
204 .entrySet();
205 for (Map.Entry<String, Vector<UnresolvedPermission>> e : set) {
206 // Convert Vector into ArrayList
207 Vector<UnresolvedPermission> vec = e.getValue();
208 List<UnresolvedPermission> list = new ArrayList<UnresolvedPermission>(
209 vec.size());
210 list.addAll(vec);
211
212 // Add to Hashtable being serialized
213 perms.put(e.getKey(), list);
214 }
215 }
216 }
|