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 DelegationPermissions
035: *
036: */
037: class KrbDelegationPermissionCollection extends PermissionCollection
038: implements Serializable {
039:
040: private static final long serialVersionUID = -3383936936589966948L;
041:
042: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
043: "permissions", Vector.class) }; //$NON-NLS-1$
044:
045: private transient DelegationPermission[] items = new DelegationPermission[10];
046:
047: private transient int offset;
048:
049: //initialization of a collection
050: KrbDelegationPermissionCollection() {
051: super ();
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 DelegationPermission)) {
066: throw new IllegalArgumentException(Messages.getString(
067: "auth.22", permission)); //$NON-NLS-1$
068: }
069: synchronized (this ) {
070: if (offset == items.length) {
071: DelegationPermission[] dp = new DelegationPermission[items.length * 2];
072: System.arraycopy(items, 0, dp, 0, offset);
073: items = dp;
074: }
075: items[offset++] = (DelegationPermission) 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;
086:
087: public boolean hasMoreElements() {
088: return index < offset;
089: }
090:
091: public DelegationPermission 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: if (permission == null
106: || !(permission instanceof DelegationPermission)) {
107: return false;
108: }
109:
110: synchronized (this ) {
111: for (int i = 0; i < offset; i++) {
112: if (items[i].implies(permission)) {
113: return true;
114: }
115: }
116: }
117: return false;
118: }
119:
120: // white a collection to stream
121: private void writeObject(ObjectOutputStream out) throws IOException {
122: Vector<DelegationPermission> permissions;
123: permissions = new Vector<DelegationPermission>(offset);
124: for (int i = 0; i < offset; permissions.add(items[i++])) {
125: }
126: ObjectOutputStream.PutField fields = out.putFields();
127: fields.put("permissions", permissions); //$NON-NLS-1$
128: out.writeFields();
129: }
130:
131: // read a collection from stream
132: private void readObject(ObjectInputStream in) throws IOException,
133: ClassNotFoundException {
134: ObjectInputStream.GetField fields = in.readFields();
135: Vector<?> permissions = (Vector<?>) fields.get(
136: "permissions", null); //$NON-NLS-1$
137: items = new DelegationPermission[permissions.size() * 2];
138: for (offset = 0; offset < items.length / 2;) {
139: Object obj = permissions.get(offset);
140: if (obj == null || !(obj instanceof DelegationPermission)) {
141: throw new IllegalArgumentException(Messages.getString(
142: "auth.22", obj)); //$NON-NLS-1$
143: }
144: items[offset++] = (DelegationPermission) obj;
145: }
146: }
147: }
|