001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.security;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.ObjectStreamField;
024: import java.util.Enumeration;
025: import java.util.NoSuchElementException;
026:
027: import org.apache.harmony.security.internal.nls.Messages;
028:
029: /**
030: * Specific PermissionCollection for storing AllPermissions. All instances of
031: * AllPermission are equivalent, so it is enough to store a single added
032: * instance.
033: *
034: */
035: final class AllPermissionCollection extends PermissionCollection {
036:
037: /**
038: * @com.intel.drl.spec_ref
039: */
040: private static final long serialVersionUID = -4023755556366636806L;
041:
042: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
043: "all_allowed", Boolean.TYPE), }; //$NON-NLS-1$
044:
045: // Single element of collection.
046: private transient Permission all;
047:
048: /**
049: * Adds an AllPermission to the collection.
050: */
051: public void add(Permission permission) {
052: if (isReadOnly()) {
053: throw new SecurityException(Messages
054: .getString("security.15")); //$NON-NLS-1$
055: }
056: if (!(permission instanceof AllPermission)) {
057: throw new IllegalArgumentException(Messages.getString(
058: "security.16", //$NON-NLS-1$
059: permission));
060: }
061: all = permission;
062: }
063:
064: /**
065: * Returns enumeration of the collection.
066: */
067: public Enumeration<Permission> elements() {
068: return new SingletonEnumeration<Permission>(all);
069: }
070:
071: /**
072: * An auxiliary implementation for enumerating a single object.
073: *
074: */
075: final static class SingletonEnumeration<E> implements
076: Enumeration<E> {
077:
078: private E element;
079:
080: /**
081: * Constructor taking the single element.
082: */
083: public SingletonEnumeration(E single) {
084: element = single;
085: }
086:
087: /**
088: * Returns true if the element is not enumerated yet.
089: */
090: public boolean hasMoreElements() {
091: return element != null;
092: }
093:
094: /**
095: * Returns the element and clears internal reference to it.
096: */
097: public E nextElement() {
098: if (element == null) {
099: throw new NoSuchElementException(Messages
100: .getString("security.17")); //$NON-NLS-1$
101: }
102: E last = element;
103: element = null;
104: return last;
105: }
106: }
107:
108: /**
109: * Indicates whether the argument permission is implied by the receiver.
110: * AllPermission objects imply all other permissions.
111: *
112: * @return boolean <code>true</code> if the argument permission is implied
113: * by the receiver, and <code>false</code> if it is not.
114: * @param permission
115: * java.security.Permission the permission to check
116: */
117: public boolean implies(Permission permission) {
118: return all != null;
119: }
120:
121: /**
122: * Writes accordingly to expected format:
123: * <dl>
124: * <dt>boolean all_allowed
125: * <dd>This is set to true if this collection is not empty
126: * </dl>
127: */
128: private void writeObject(java.io.ObjectOutputStream out)
129: throws IOException {
130: ObjectOutputStream.PutField fields = out.putFields();
131: fields.put("all_allowed", all != null); //$NON-NLS-1$
132: out.writeFields();
133: }
134:
135: /**
136: * Restores internal state.
137: */
138: private void readObject(java.io.ObjectInputStream in)
139: throws IOException, ClassNotFoundException {
140: ObjectInputStream.GetField fields = in.readFields();
141: if (fields.get("all_allowed", false)) { //$NON-NLS-1$
142: all = new AllPermission();
143: }
144: }
145: }
|