2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package java.security;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.io.ObjectStreamField;
24 import java.util.Enumeration;
25 import java.util.NoSuchElementException;
28 * Specific {@code PermissionCollection} for storing {@code AllPermission}s. All
29 * instances of {@code AllPermission} are equivalent, so it is enough to store a
30 * single added instance.
34 final class AllPermissionCollection extends PermissionCollection {
36 private static final long serialVersionUID = -4023755556366636806L;
38 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
39 "all_allowed", Boolean.TYPE), };
41 // Single element of collection.
42 private transient Permission all;
45 * Adds an {@code AllPermission} to the collection.
48 public void add(Permission permission) {
50 throw new SecurityException("collection is read-only");
52 if (!(permission instanceof AllPermission)) {
53 throw new IllegalArgumentException("Invalid permission: " + permission);
59 * Returns the enumeration of the collection.
62 public Enumeration<Permission> elements() {
63 return new SingletonEnumeration<Permission>(all);
67 * An auxiliary implementation for enumerating a single object.
70 final static class SingletonEnumeration<E> implements Enumeration<E> {
75 * Constructor taking the single element.
76 * @param single the element
78 public SingletonEnumeration(E single) {
83 * Returns true if the element is not enumerated yet.
85 public boolean hasMoreElements() {
86 return element != null;
90 * Returns the element and clears internal reference to it.
92 public E nextElement() {
93 if (element == null) {
94 throw new NoSuchElementException();
103 * Indicates whether the argument permission is implied by the receiver.
104 * {@code AllPermission} objects imply all other permissions.
106 * @return boolean {@code true} if the argument permission is implied by the
107 * receiver, and {@code false} if it is not.
109 * the permission to check.
112 public boolean implies(Permission permission) {
117 * Writes the fields according to expected format, adding the boolean field
118 * {@code all_allowed} which is {@code true} if this collection is not
121 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
122 ObjectOutputStream.PutField fields = out.putFields();
123 fields.put("all_allowed", all != null);
128 * Restores internal state.
130 private void readObject(java.io.ObjectInputStream in) throws IOException,
131 ClassNotFoundException {
132 ObjectInputStream.GetField fields = in.readFields();
133 if (fields.get("all_allowed", false)) {
134 all = new AllPermission();