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 javax.security.auth.kerberos;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.ObjectStreamField;
024: import java.io.Serializable;
025: import java.security.Permission;
026: import java.security.PermissionCollection;
027: import java.util.Enumeration;
028: import java.util.NoSuchElementException;
029: import java.util.Vector;
030:
031: import org.apache.harmony.auth.internal.nls.Messages;
032:
033: /**
034: * Specific PermissionCollection for storing ServicePermissions
035: *
036: */
037:
038: final class KrbServicePermissionCollection extends PermissionCollection
039: implements Serializable {
040:
041: private static final long serialVersionUID = -4118834211490102011L;
042:
043: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
044: "permissions", Vector.class) }; //$NON-NLS-1$
045:
046: private transient ServicePermission[] items = new ServicePermission[10];
047:
048: private transient int offset;
049:
050: // initialization of a collection
051: KrbServicePermissionCollection() {
052: }
053:
054: /**
055: * Adds a ServicePermission to the collection.
056: */
057: @Override
058: public void add(Permission permission) {
059:
060: if (isReadOnly()) {
061: throw new SecurityException(Messages.getString("auth.21")); //$NON-NLS-1$
062: }
063:
064: if (permission == null
065: || !(permission instanceof ServicePermission)) {
066: throw new IllegalArgumentException(Messages.getString(
067: "auth.22", permission)); //$NON-NLS-1$
068: }
069: synchronized (this ) {
070: if (offset == items.length) {
071: ServicePermission[] sp = new ServicePermission[items.length * 2];
072: System.arraycopy(items, 0, sp, 0, offset);
073: items = sp;
074: }
075: items[offset++] = (ServicePermission) permission;
076: }
077: }
078:
079: /**
080: * Returns enumeration of the collection.
081: */
082: @Override
083: public Enumeration<Permission> elements() {
084: return new Enumeration<Permission>() {
085: private int index = 0;
086:
087: public boolean hasMoreElements() {
088: return index < offset;
089: }
090:
091: public Permission nextElement() {
092: if (index == offset) {
093: throw new NoSuchElementException();
094: }
095: return items[index++];
096: }
097: };
098: }
099:
100: /**
101: * Returns true if this collection implies the specified permission.
102: */
103: @Override
104: public boolean implies(Permission permission) {
105:
106: if (permission == null
107: || !(permission instanceof ServicePermission)) {
108: return false;
109: }
110:
111: synchronized (this ) {
112: for (int i = 0; i < offset; i++) {
113: if (items[i].implies(permission)) {
114: return true;
115: }
116: }
117: }
118:
119: return false;
120: }
121:
122: // white collection to stream
123: private void writeObject(java.io.ObjectOutputStream out)
124: throws IOException {
125: Vector<ServicePermission> permissions;
126: permissions = new Vector<ServicePermission>(offset);
127: for (int i = 0; i < offset; permissions.add(items[i++])) {
128: }
129: ObjectOutputStream.PutField fields = out.putFields();
130: fields.put("permissions", permissions); //$NON-NLS-1$
131: out.writeFields();
132: }
133:
134: // read collection from stream
135: private void readObject(java.io.ObjectInputStream in)
136: throws IOException, ClassNotFoundException {
137: ObjectInputStream.GetField fields = in.readFields();
138: Vector<?> permissions = (Vector<?>) fields.get(
139: "permissions", null); //$NON-NLS-1$
140: items = new ServicePermission[permissions.size() * 2];
141: for (offset = 0; offset < items.length / 2;) {
142: Object obj = permissions.get(offset);
143: if (obj == null || !(obj instanceof ServicePermission)) {
144: throw new IllegalArgumentException(Messages.getString(
145: "auth.22", obj)); //$NON-NLS-1$
146: }
147: items[offset++] = (ServicePermission) obj;
148: }
149: }
150: }
|