author | coleenp |
Wed, 30 Aug 2017 19:18:22 -0400 | |
changeset 47098 | e704f55561c3 |
parent 45434 | 4582657c7260 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.security; |
|
27 |
||
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
28 |
import java.io.IOException; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
29 |
import java.io.ObjectInputStream; |
2 | 30 |
import java.io.ObjectOutputStream; |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
31 |
import java.io.ObjectStreamField; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
32 |
import java.util.*; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
33 |
import java.util.concurrent.ConcurrentHashMap; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
34 |
import java.util.concurrent.CopyOnWriteArrayList; |
2 | 35 |
|
36 |
/** |
|
37 |
* A UnresolvedPermissionCollection stores a collection |
|
38 |
* of UnresolvedPermission permissions. |
|
39 |
* |
|
40 |
* @see java.security.Permission |
|
41 |
* @see java.security.Permissions |
|
42 |
* @see java.security.UnresolvedPermission |
|
43 |
* |
|
44 |
* |
|
45 |
* @author Roland Schemers |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
31080
diff
changeset
|
46 |
* @since 1.2 |
2 | 47 |
* |
48 |
* @serial include |
|
49 |
*/ |
|
50 |
||
51 |
final class UnresolvedPermissionCollection |
|
52 |
extends PermissionCollection |
|
53 |
implements java.io.Serializable |
|
54 |
{ |
|
55 |
/** |
|
56 |
* Key is permission type, value is a list of the UnresolvedPermissions |
|
57 |
* of the same type. |
|
58 |
* Not serialized; see serialization section at end of class. |
|
59 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
60 |
private transient ConcurrentHashMap<String, List<UnresolvedPermission>> perms; |
2 | 61 |
|
62 |
/** |
|
63 |
* Create an empty UnresolvedPermissionCollection object. |
|
64 |
* |
|
65 |
*/ |
|
66 |
public UnresolvedPermissionCollection() { |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
67 |
perms = new ConcurrentHashMap<>(11); |
2 | 68 |
} |
69 |
||
70 |
/** |
|
71 |
* Adds a permission to this UnresolvedPermissionCollection. |
|
72 |
* The key for the hash is the unresolved permission's type (class) name. |
|
73 |
* |
|
74 |
* @param permission the Permission object to add. |
|
75 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
76 |
@Override |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
77 |
public void add(Permission permission) { |
2 | 78 |
if (! (permission instanceof UnresolvedPermission)) |
79 |
throw new IllegalArgumentException("invalid permission: "+ |
|
80 |
permission); |
|
81 |
UnresolvedPermission up = (UnresolvedPermission) permission; |
|
82 |
||
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
83 |
// Add permission to map. NOTE: cannot use lambda for |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
84 |
// remappingFunction parameter until JDK-8076596 is fixed. |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
85 |
perms.compute(up.getName(), |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
86 |
new java.util.function.BiFunction<>() { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
87 |
@Override |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
88 |
public List<UnresolvedPermission> apply(String key, |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
89 |
List<UnresolvedPermission> oldValue) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
90 |
if (oldValue == null) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
91 |
List<UnresolvedPermission> v = |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
92 |
new CopyOnWriteArrayList<>(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
93 |
v.add(up); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
94 |
return v; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
95 |
} else { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
96 |
oldValue.add(up); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
97 |
return oldValue; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
98 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
99 |
} |
2 | 100 |
} |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
101 |
); |
2 | 102 |
} |
103 |
||
104 |
/** |
|
105 |
* get any unresolved permissions of the same type as p, |
|
106 |
* and return the List containing them. |
|
107 |
*/ |
|
108 |
List<UnresolvedPermission> getUnresolvedPermissions(Permission p) { |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
109 |
return perms.get(p.getClass().getName()); |
2 | 110 |
} |
111 |
||
112 |
/** |
|
113 |
* always returns false for unresolved permissions |
|
114 |
* |
|
115 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
116 |
@Override |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
117 |
public boolean implies(Permission permission) { |
2 | 118 |
return false; |
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Returns an enumeration of all the UnresolvedPermission lists in the |
|
123 |
* container. |
|
124 |
* |
|
125 |
* @return an enumeration of all the UnresolvedPermission objects. |
|
126 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
127 |
@Override |
2 | 128 |
public Enumeration<Permission> elements() { |
129 |
List<Permission> results = |
|
7970
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
5506
diff
changeset
|
130 |
new ArrayList<>(); // where results are stored |
2 | 131 |
|
132 |
// Get iterator of Map values (which are lists of permissions) |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
133 |
for (List<UnresolvedPermission> l : perms.values()) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
134 |
results.addAll(l); |
2 | 135 |
} |
136 |
||
137 |
return Collections.enumeration(results); |
|
138 |
} |
|
139 |
||
140 |
private static final long serialVersionUID = -7176153071733132400L; |
|
141 |
||
142 |
// Need to maintain serialization interoperability with earlier releases, |
|
143 |
// which had the serializable field: |
|
144 |
// private Hashtable permissions; // keyed on type |
|
145 |
||
146 |
/** |
|
147 |
* @serialField permissions java.util.Hashtable |
|
148 |
* A table of the UnresolvedPermissions keyed on type, value is Vector |
|
149 |
* of permissions |
|
150 |
*/ |
|
151 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
152 |
new ObjectStreamField("permissions", Hashtable.class), |
|
153 |
}; |
|
154 |
||
155 |
/** |
|
156 |
* @serialData Default field. |
|
157 |
*/ |
|
158 |
/* |
|
159 |
* Writes the contents of the perms field out as a Hashtable |
|
160 |
* in which the values are Vectors for |
|
161 |
* serialization compatibility with earlier releases. |
|
162 |
*/ |
|
163 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
164 |
// Don't call out.defaultWriteObject() |
|
165 |
||
166 |
// Copy perms into a Hashtable |
|
167 |
Hashtable<String, Vector<UnresolvedPermission>> permissions = |
|
7970
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
5506
diff
changeset
|
168 |
new Hashtable<>(perms.size()*2); |
2 | 169 |
|
170 |
// Convert each entry (List) into a Vector |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
171 |
Set<Map.Entry<String, List<UnresolvedPermission>>> set = perms.entrySet(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
172 |
for (Map.Entry<String, List<UnresolvedPermission>> e : set) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
173 |
// Convert list into Vector |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
174 |
List<UnresolvedPermission> list = e.getValue(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
175 |
Vector<UnresolvedPermission> vec = new Vector<>(list); |
2 | 176 |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
177 |
// Add to Hashtable being serialized |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
178 |
permissions.put(e.getKey(), vec); |
2 | 179 |
} |
180 |
||
181 |
// Write out serializable fields |
|
182 |
ObjectOutputStream.PutField pfields = out.putFields(); |
|
183 |
pfields.put("permissions", permissions); |
|
184 |
out.writeFields(); |
|
185 |
} |
|
186 |
||
187 |
/* |
|
188 |
* Reads in a Hashtable in which the values are Vectors of |
|
189 |
* UnresolvedPermissions and saves them in the perms field. |
|
190 |
*/ |
|
191 |
private void readObject(ObjectInputStream in) throws IOException, |
|
192 |
ClassNotFoundException { |
|
193 |
// Don't call defaultReadObject() |
|
194 |
||
195 |
// Read in serialized fields |
|
196 |
ObjectInputStream.GetField gfields = in.readFields(); |
|
197 |
||
198 |
// Get permissions |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
199 |
@SuppressWarnings("unchecked") |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
200 |
// writeObject writes a Hashtable<String, Vector<UnresolvedPermission>> |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
201 |
// for the permissions key, so this cast is safe, unless the data is corrupt. |
2 | 202 |
Hashtable<String, Vector<UnresolvedPermission>> permissions = |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
203 |
(Hashtable<String, Vector<UnresolvedPermission>>) |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
204 |
gfields.get("permissions", null); |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
205 |
perms = new ConcurrentHashMap<>(permissions.size()*2); |
2 | 206 |
|
207 |
// Convert each entry (Vector) into a List |
|
208 |
Set<Map.Entry<String, Vector<UnresolvedPermission>>> set = permissions.entrySet(); |
|
209 |
for (Map.Entry<String, Vector<UnresolvedPermission>> e : set) { |
|
210 |
// Convert Vector into ArrayList |
|
211 |
Vector<UnresolvedPermission> vec = e.getValue(); |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
30033
diff
changeset
|
212 |
List<UnresolvedPermission> list = new CopyOnWriteArrayList<>(vec); |
2 | 213 |
|
214 |
// Add to Hashtable being serialized |
|
215 |
perms.put(e.getKey(), list); |
|
216 |
} |
|
217 |
} |
|
218 |
} |