1 /* 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 */ 17 18 package java.security; 19 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; 26 27 /** 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. 31 * 32 * @see AllPermission 33 */ 34 final class AllPermissionCollection extends PermissionCollection { 35 36 private static final long serialVersionUID = -4023755556366636806L; 37 38 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 39 "all_allowed", Boolean.TYPE), }; 40 41 // Single element of collection. 42 private transient Permission all; 43 44 /** 45 * Adds an {@code AllPermission} to the collection. 46 */ 47 @Override 48 public void add(Permission permission) { 49 if (isReadOnly()) { 50 throw new SecurityException("collection is read-only"); 51 } 52 if (!(permission instanceof AllPermission)) { 53 throw new IllegalArgumentException("Invalid permission: " + permission); 54 } 55 all = permission; 56 } 57 58 /** 59 * Returns the enumeration of the collection. 60 */ 61 @Override 62 public Enumeration<Permission> elements() { 63 return new SingletonEnumeration<Permission>(all); 64 } 65 66 /** 67 * An auxiliary implementation for enumerating a single object. 68 * 69 */ 70 final static class SingletonEnumeration<E> implements Enumeration<E> { 71 72 private E element; 73 74 /** 75 * Constructor taking the single element. 76 * @param single the element 77 */ 78 public SingletonEnumeration(E single) { 79 element = single; 80 } 81 82 /** 83 * Returns true if the element is not enumerated yet. 84 */ 85 public boolean hasMoreElements() { 86 return element != null; 87 } 88 89 /** 90 * Returns the element and clears internal reference to it. 91 */ 92 public E nextElement() { 93 if (element == null) { 94 throw new NoSuchElementException(); 95 } 96 E last = element; 97 element = null; 98 return last; 99 } 100 } 101 102 /** 103 * Indicates whether the argument permission is implied by the receiver. 104 * {@code AllPermission} objects imply all other permissions. 105 * 106 * @return boolean {@code true} if the argument permission is implied by the 107 * receiver, and {@code false} if it is not. 108 * @param permission 109 * the permission to check. 110 */ 111 @Override 112 public boolean implies(Permission permission) { 113 return all != null; 114 } 115 116 /** 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 119 * empty. 120 */ 121 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 122 ObjectOutputStream.PutField fields = out.putFields(); 123 fields.put("all_allowed", all != null); 124 out.writeFields(); 125 } 126 127 /** 128 * Restores internal state. 129 */ 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(); 135 } 136 } 137 } 138