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.util;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.ObjectStreamField;
024: import java.security.Permission;
025: import java.security.PermissionCollection;
026:
027: /**
028: * A PermissionCollection for holding PropertyPermissions.
029: */
030: class PropertyPermissionCollection extends PermissionCollection {
031:
032: private static final long serialVersionUID = 7015263904581634791L;
033:
034: Hashtable<String, Permission> permissions = new Hashtable<String, Permission>(
035: 30);
036:
037: @Override
038: public void add(Permission perm) {
039: if (!isReadOnly()) {
040: Permission prev = permissions.put(perm.getName(), perm);
041: /*
042: * If the permission already existed but with only "read" or "write"
043: * set, then replace with both set.
044: */
045: if (prev != null
046: && !prev.getActions().equals(perm.getActions())) {
047: Permission np = new PropertyPermission(perm.getName(),
048: "read,write"); //$NON-NLS-1$
049: permissions.put(perm.getName(), np);
050: }
051: } else {
052: throw new IllegalStateException();
053: }
054: }
055:
056: @Override
057: public Enumeration<Permission> elements() {
058: return permissions.elements();
059: }
060:
061: @Override
062: public boolean implies(Permission perm) {
063: Enumeration<Permission> elemEnum = elements();
064: while (elemEnum.hasMoreElements()) {
065: if ((elemEnum.nextElement()).implies(perm)) {
066: return true;
067: }
068: }
069: /*
070: * At this point, the only way it can succeed is if both read and write
071: * are set, and these are separately granted by two different
072: * permissions with one representing a parent directory.
073: */
074: return perm.getActions().equals("read,write") //$NON-NLS-1$
075: && implies(new PropertyPermission(perm.getName(),
076: "read")) //$NON-NLS-1$
077: && implies(new PropertyPermission(perm.getName(),
078: "write")); //$NON-NLS-1$
079: }
080:
081: private static final ObjectStreamField[] serialPersistentFields = {
082: new ObjectStreamField("permissions", Hashtable.class), //$NON-NLS-1$
083: new ObjectStreamField("all_allowed", Boolean.TYPE) }; //$NON-NLS-1$
084:
085: private void writeObject(ObjectOutputStream stream)
086: throws IOException {
087: ObjectOutputStream.PutField fields = stream.putFields();
088: fields.put("permissions", permissions); //$NON-NLS-1$
089: fields.put("all_allowed", false); //$NON-NLS-1$
090: stream.writeFields();
091: }
092:
093: @SuppressWarnings("unchecked")
094: private void readObject(ObjectInputStream stream)
095: throws IOException, ClassNotFoundException {
096: ObjectInputStream.GetField fields = stream.readFields();
097: permissions = (Hashtable<String, Permission>) fields.get(
098: "permissions", null); //$NON-NLS-1$
099: }
100: }
|