KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > UnresolvedPermissionCollection


1 /*
2  * @(#)UnresolvedPermissionCollection.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.security;
9
10 import java.util.*;
11 import java.io.ObjectStreamField JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15
16 /**
17  * A UnresolvedPermissionCollection stores a collection
18  * of UnresolvedPermission permissions.
19  *
20  * @see java.security.Permission
21  * @see java.security.Permissions
22  * @see java.security.UnresolvedPermission
23  *
24  * @version 1.14 03/12/19
25  *
26  * @author Roland Schemers
27  *
28  * @serial include
29  */

30
31 final class UnresolvedPermissionCollection
32 extends PermissionCollection JavaDoc
33 implements java.io.Serializable JavaDoc
34 {
35     /**
36      * Key is permission type, value is a list of the UnresolvedPermissions
37      * of the same type.
38      * Not serialized; see serialization section at end of class.
39      */

40     private transient Map perms;
41
42     /**
43      * Create an empty UnresolvedPermissionCollection object.
44      *
45      */

46     public UnresolvedPermissionCollection() {
47     perms = new HashMap(11);
48     }
49
50     /**
51      * Adds a permission to this UnresolvedPermissionCollection.
52      * The key for the hash is the unresolved permission's type (class) name.
53      *
54      * @param permission the Permission object to add.
55      */

56
57     public void add(Permission JavaDoc permission)
58     {
59     if (! (permission instanceof UnresolvedPermission JavaDoc))
60         throw new IllegalArgumentException JavaDoc("invalid permission: "+
61                            permission);
62     UnresolvedPermission JavaDoc up = (UnresolvedPermission JavaDoc) permission;
63
64     List v;
65     synchronized (this) {
66         v = (List) perms.get(up.getName());
67         if (v == null) {
68         v = new ArrayList();
69         perms.put(up.getName(), v);
70         }
71     }
72     synchronized (v) {
73         v.add(up);
74     }
75     }
76
77     /**
78      * get any unresolved permissions of the same type as p,
79      * and return the List containing them.
80      */

81     List getUnresolvedPermissions(Permission JavaDoc p) {
82     synchronized (this) {
83         return (List) perms.get(p.getClass().getName());
84     }
85     }
86
87     /**
88      * always returns false for unresolved permissions
89      *
90      */

91     public boolean implies(Permission JavaDoc permission)
92     {
93     return false;
94     }
95
96     /**
97      * Returns an enumeration of all the UnresolvedPermission lists in the
98      * container.
99      *
100      * @return an enumeration of all the UnresolvedPermission objects.
101      */

102
103     public Enumeration elements() {
104     List results = new ArrayList(); // where results are stored
105

106     // Get iterator of Map values (which are lists of permissions)
107
synchronized (this) {
108         for (Iterator iter = perms.values().iterator(); iter.hasNext();) {
109         List l = (List) iter.next();
110         synchronized (l) {
111             results.addAll(l);
112         }
113         }
114     }
115
116     return Collections.enumeration(results);
117     }
118
119     private static final long serialVersionUID = -7176153071733132400L;
120
121     // Need to maintain serialization interoperability with earlier releases,
122
// which had the serializable field:
123
// private Hashtable permissions; // keyed on type
124

125     /**
126      * @serialField permissions java.util.Hashtable
127      * A table of the UnresolvedPermissions keyed on type, value is Vector
128      * of permissions
129      */

130     private static final ObjectStreamField JavaDoc[] serialPersistentFields = {
131         new ObjectStreamField JavaDoc("permissions", Hashtable.class),
132     };
133
134     /**
135      * @serialData Default field.
136      */

137     /*
138      * Writes the contents of the perms field out as a Hashtable
139      * in which the values are Vectors for
140      * serialization compatibility with earlier releases.
141      */

142     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
143     // Don't call out.defaultWriteObject()
144

145     // Copy perms into a Hashtable
146
Hashtable permissions = new Hashtable(perms.size()*2);
147
148     // Convert each entry (List) into a Vector
149
synchronized (this) {
150         Set set = perms.entrySet();
151         for (Iterator iter = set.iterator(); iter.hasNext(); ) {
152         Map.Entry e = (Map.Entry)iter.next();
153
154         // Convert list into Vector
155
List list = (List) e.getValue();
156         Vector vec = new Vector(list.size());
157         synchronized (list) {
158             vec.addAll(list);
159         }
160
161         // Add to Hashtable being serialized
162
permissions.put(e.getKey(), vec);
163         }
164     }
165
166     // Write out serializable fields
167
ObjectOutputStream.PutField JavaDoc pfields = out.putFields();
168         pfields.put("permissions", permissions);
169         out.writeFields();
170     }
171
172     /*
173      * Reads in a Hashtable in which the values are Vectors of
174      * UnresolvedPermissions and saves them in the perms field.
175      */

176     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc,
177     ClassNotFoundException JavaDoc {
178     // Don't call defaultReadObject()
179

180     // Read in serialized fields
181
ObjectInputStream.GetField JavaDoc gfields = in.readFields();
182
183     // Get permissions
184
Hashtable permissions = (Hashtable)gfields.get("permissions", null);
185     perms = new HashMap(permissions.size()*2);
186
187     // Convert each entry (Vector) into a List
188
Set set = permissions.entrySet();
189     for (Iterator iter = set.iterator(); iter.hasNext(); ) {
190         Map.Entry e = (Map.Entry)iter.next();
191
192         // Convert Vector into ArrayList
193
Vector vec = (Vector) e.getValue();
194         List list = new ArrayList(vec.size());
195         list.addAll(vec);
196
197         // Add to Hashtable being serialized
198
perms.put(e.getKey(), list);
199     }
200     }
201 }
202
Popular Tags