author | coleenp |
Wed, 30 Aug 2017 19:18:22 -0400 | |
changeset 47098 | e704f55561c3 |
parent 45434 | 4582657c7260 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
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 |
||
28 |
import java.util.*; |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
29 |
import java.util.stream.Stream; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
30 |
import java.util.stream.StreamSupport; |
2 | 31 |
|
32 |
/** |
|
33 |
* Abstract class representing a collection of Permission objects. |
|
34 |
* |
|
35 |
* <p>With a PermissionCollection, you can: |
|
36 |
* <UL> |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
37 |
* <LI> add a permission to the collection using the {@code add} method. |
2 | 38 |
* <LI> check to see if a particular permission is implied in the |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
39 |
* collection, using the {@code implies} method. |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
40 |
* <LI> enumerate all the permissions, using the {@code elements} method. |
2 | 41 |
* </UL> |
42 |
* |
|
43 |
* <p>When it is desirable to group together a number of Permission objects |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
44 |
* of the same type, the {@code newPermissionCollection} method on that |
2 | 45 |
* particular type of Permission object should first be called. The default |
46 |
* behavior (from the Permission class) is to simply return null. |
|
47 |
* Subclasses of class Permission override the method if they need to store |
|
48 |
* their permissions in a particular PermissionCollection object in order |
|
49 |
* to provide the correct semantics when the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
50 |
* {@code PermissionCollection.implies} method is called. |
2 | 51 |
* If a non-null value is returned, that PermissionCollection must be used. |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
52 |
* If null is returned, then the caller of {@code newPermissionCollection} |
2 | 53 |
* is free to store permissions of the |
54 |
* given type in any PermissionCollection they choose |
|
55 |
* (one that uses a Hashtable, one that uses a Vector, etc). |
|
56 |
* |
|
57 |
* <p>The PermissionCollection returned by the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
58 |
* {@code Permission.newPermissionCollection} |
2 | 59 |
* method is a homogeneous collection, which stores only Permission objects |
60 |
* for a given Permission type. A PermissionCollection may also be |
|
61 |
* heterogeneous. For example, Permissions is a PermissionCollection |
|
62 |
* subclass that represents a collection of PermissionCollections. |
|
63 |
* That is, its members are each a homogeneous PermissionCollection. |
|
64 |
* For example, a Permissions object might have a FilePermissionCollection |
|
65 |
* for all the FilePermission objects, a SocketPermissionCollection for all the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
66 |
* SocketPermission objects, and so on. Its {@code add} method adds a |
2 | 67 |
* permission to the appropriate collection. |
68 |
* |
|
69 |
* <p>Whenever a permission is added to a heterogeneous PermissionCollection |
|
70 |
* such as Permissions, and the PermissionCollection doesn't yet contain a |
|
71 |
* PermissionCollection of the specified permission's type, the |
|
72 |
* PermissionCollection should call |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
73 |
* the {@code newPermissionCollection} method on the permission's class |
2 | 74 |
* to see if it requires a special PermissionCollection. If |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
75 |
* {@code newPermissionCollection} |
2 | 76 |
* returns null, the PermissionCollection |
77 |
* is free to store the permission in any type of PermissionCollection it |
|
78 |
* desires (one using a Hashtable, one using a Vector, etc.). For example, |
|
79 |
* the Permissions object uses a default PermissionCollection implementation |
|
80 |
* that stores the permission objects in a Hashtable. |
|
81 |
* |
|
82 |
* <p> Subclass implementations of PermissionCollection should assume |
|
83 |
* that they may be called simultaneously from multiple threads, |
|
84 |
* and therefore should be synchronized properly. Furthermore, |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
85 |
* Enumerations returned via the {@code elements} method are |
2 | 86 |
* not <em>fail-fast</em>. Modifications to a collection should not be |
87 |
* performed while enumerating over that collection. |
|
88 |
* |
|
89 |
* @see Permission |
|
90 |
* @see Permissions |
|
91 |
* |
|
92 |
* |
|
93 |
* @author Roland Schemers |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
35302
diff
changeset
|
94 |
* @since 1.2 |
2 | 95 |
*/ |
96 |
||
97 |
public abstract class PermissionCollection implements java.io.Serializable { |
|
98 |
||
99 |
private static final long serialVersionUID = -6727011328946861783L; |
|
100 |
||
101 |
// when set, add will throw an exception. |
|
102 |
private volatile boolean readOnly; |
|
103 |
||
104 |
/** |
|
105 |
* Adds a permission object to the current collection of permission objects. |
|
106 |
* |
|
107 |
* @param permission the Permission object to add. |
|
108 |
* |
|
109 |
* @exception SecurityException - if this PermissionCollection object |
|
110 |
* has been marked readonly |
|
111 |
* @exception IllegalArgumentException - if this PermissionCollection |
|
112 |
* object is a homogeneous collection and the permission |
|
113 |
* is not of the correct type. |
|
114 |
*/ |
|
115 |
public abstract void add(Permission permission); |
|
116 |
||
117 |
/** |
|
118 |
* Checks to see if the specified permission is implied by |
|
119 |
* the collection of Permission objects held in this PermissionCollection. |
|
120 |
* |
|
121 |
* @param permission the Permission object to compare. |
|
122 |
* |
|
123 |
* @return true if "permission" is implied by the permissions in |
|
124 |
* the collection, false if not. |
|
125 |
*/ |
|
126 |
public abstract boolean implies(Permission permission); |
|
127 |
||
128 |
/** |
|
129 |
* Returns an enumeration of all the Permission objects in the collection. |
|
130 |
* |
|
131 |
* @return an enumeration of all the Permissions. |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
132 |
* @see #elementsAsStream() |
2 | 133 |
*/ |
134 |
public abstract Enumeration<Permission> elements(); |
|
135 |
||
136 |
/** |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
137 |
* Returns a stream of all the Permission objects in the collection. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
138 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
139 |
* <p> The collection should not be modified (see {@link #add}) during the |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
140 |
* execution of the terminal stream operation. Otherwise, the result of the |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
141 |
* terminal stream operation is undefined. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
142 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
143 |
* @implSpec |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
144 |
* The default implementation creates a stream whose source is derived from |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
145 |
* the enumeration returned from a call to {@link #elements()}. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
146 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
147 |
* @return a stream of all the Permissions. |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
31469
diff
changeset
|
148 |
* @since 9 |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
149 |
*/ |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
150 |
public Stream<Permission> elementsAsStream() { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
151 |
int characteristics = isReadOnly() |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
152 |
? Spliterator.NONNULL | Spliterator.IMMUTABLE |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
153 |
: Spliterator.NONNULL; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
154 |
return StreamSupport.stream( |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
155 |
Spliterators.spliteratorUnknownSize( |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
156 |
elements().asIterator(), characteristics), |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
157 |
false); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
158 |
} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
159 |
|
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
25859
diff
changeset
|
160 |
/** |
2 | 161 |
* Marks this PermissionCollection object as "readonly". After |
162 |
* a PermissionCollection object |
|
163 |
* is marked as readonly, no new Permission objects can be added to it |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
164 |
* using {@code add}. |
2 | 165 |
*/ |
166 |
public void setReadOnly() { |
|
167 |
readOnly = true; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Returns true if this PermissionCollection object is marked as readonly. |
|
172 |
* If it is readonly, no new Permission objects can be added to it |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
173 |
* using {@code add}. |
2 | 174 |
* |
175 |
* <p>By default, the object is <i>not</i> readonly. It can be set to |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
176 |
* readonly by a call to {@code setReadOnly}. |
2 | 177 |
* |
178 |
* @return true if this PermissionCollection object is marked as readonly, |
|
179 |
* false otherwise. |
|
180 |
*/ |
|
181 |
public boolean isReadOnly() { |
|
182 |
return readOnly; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Returns a string describing this PermissionCollection object, |
|
187 |
* providing information about all the permissions it contains. |
|
188 |
* The format is: |
|
189 |
* <pre> |
|
190 |
* super.toString() ( |
|
191 |
* // enumerate all the Permission |
|
192 |
* // objects and call toString() on them, |
|
193 |
* // one per line.. |
|
194 |
* )</pre> |
|
195 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
5506
diff
changeset
|
196 |
* {@code super.toString} is a call to the {@code toString} |
2 | 197 |
* method of this |
198 |
* object's superclass, which is Object. The result is |
|
199 |
* this PermissionCollection's type name followed by this object's |
|
200 |
* hashcode, thus enabling clients to differentiate different |
|
201 |
* PermissionCollections object, even if they contain the same permissions. |
|
202 |
* |
|
203 |
* @return information about this PermissionCollection object, |
|
204 |
* as described above. |
|
205 |
* |
|
206 |
*/ |
|
207 |
public String toString() { |
|
208 |
Enumeration<Permission> enum_ = elements(); |
|
209 |
StringBuilder sb = new StringBuilder(); |
|
210 |
sb.append(super.toString()+" (\n"); |
|
211 |
while (enum_.hasMoreElements()) { |
|
212 |
try { |
|
213 |
sb.append(" "); |
|
214 |
sb.append(enum_.nextElement().toString()); |
|
215 |
sb.append("\n"); |
|
216 |
} catch (NoSuchElementException e){ |
|
217 |
// ignore |
|
218 |
} |
|
219 |
} |
|
220 |
sb.append(")\n"); |
|
221 |
return sb.toString(); |
|
222 |
} |
|
223 |
} |