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 18package java.io; 19 20import java.security.Permission; 21import java.security.PermissionCollection; 22import java.util.Enumeration; 23import java.util.Vector; 24 25/** 26 * Collects {@link FilePermission} objects and allows to query whether a 27 * particular permission is implied by it. 28 */ 29final class FilePermissionCollection extends PermissionCollection implements 30 Serializable { 31 32 private static final long serialVersionUID = 2202956749081564585L; 33 34 Vector<Permission> permissions = new Vector<Permission>(); 35 36 /** 37 * Construct a new FilePermissionCollection 38 */ 39 public FilePermissionCollection() { 40 super(); 41 } 42 43 /** 44 * Add a permission object to the permission collection. 45 * 46 * @param permission 47 * the FilePermission object to add to the collection. 48 * @throws IllegalArgumentException 49 * if {@code permission} is not an instance of 50 * {@code FilePermission}. 51 * @throws IllegalStateException 52 * if this collection is read-only. 53 * @see java.security.PermissionCollection#add(java.security.Permission) 54 */ 55 @Override 56 public void add(Permission permission) { 57 if (isReadOnly()) { 58 throw new IllegalStateException(); 59 } 60 if (permission instanceof FilePermission) { 61 permissions.addElement(permission); 62 } else { 63 throw new IllegalArgumentException(permission.toString()); 64 } 65 } 66 67 /** 68 * Returns an enumeration for the collection of permissions. 69 * 70 * @return a permission enumeration for this permission collection. 71 * @see java.security.PermissionCollection#elements() 72 */ 73 @Override 74 public Enumeration<Permission> elements() { 75 return permissions.elements(); 76 } 77 78 /** 79 * Indicates whether this permissions collection implies a specific 80 * {@code permission}. 81 * 82 * @param permission 83 * the permission to check. 84 * @see java.security.PermissionCollection#implies(java.security.Permission) 85 */ 86 @Override 87 public boolean implies(Permission permission) { 88 if (permission instanceof FilePermission) { 89 FilePermission fp = (FilePermission) permission; 90 int matchedMask = 0; 91 int i = 0; 92 while (i < permissions.size() 93 && ((matchedMask & fp.mask) != fp.mask)) { 94 // Cast will not fail since we added it 95 matchedMask |= ((FilePermission) permissions.elementAt(i)) 96 .impliesMask(permission); 97 i++; 98 } 99 return ((matchedMask & fp.mask) == fp.mask); 100 } 101 return false; 102 } 103} 104