01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: package java.io;
19:
20: import java.security.Permission;
21: import java.security.PermissionCollection;
22: import java.util.Enumeration;
23: import java.util.Vector;
24:
25: /**
26: * FilePermissionCollection is a class which holds a collection of
27: * FilePermission objects and can answer a boolean indicating whether or not a
28: * specific permissions is implied by a FilePermissionCollection.
29: */
30: final class FilePermissionCollection extends PermissionCollection
31: implements Serializable {
32:
33: private static final long serialVersionUID = 2202956749081564585L;
34:
35: Vector<Permission> permissions = new Vector<Permission>();
36:
37: /**
38: * Construct a new FilePermissionCollection.
39: */
40: public FilePermissionCollection() {
41: super ();
42: }
43:
44: /**
45: * Add a permission Object to the permission collection.
46: *
47: * @see java.security.PermissionCollection#add(java.security.Permission)
48: */
49: @Override
50: public void add(Permission permission) {
51: if (isReadOnly()) {
52: throw new IllegalStateException();
53: }
54: if (permission instanceof FilePermission) {
55: permissions.addElement(permission);
56: } else {
57: throw new IllegalArgumentException(permission.toString());
58: }
59: }
60:
61: /**
62: * Answers an enumeration for the collection of permissions.
63: *
64: * @see java.security.PermissionCollection#elements()
65: */
66: @Override
67: public Enumeration<Permission> elements() {
68: return permissions.elements();
69: }
70:
71: /**
72: * Answers a boolean indicating whether or not this permissions collection
73: * implies a specific <code>permission</code>.
74: *
75: * @see java.security.PermissionCollection#implies(java.security.Permission)
76: */
77: @Override
78: public boolean implies(Permission permission) {
79: if (permission instanceof FilePermission) {
80: FilePermission fp = (FilePermission) permission;
81: int matchedMask = 0;
82: int i = 0;
83: while (i < permissions.size()
84: && ((matchedMask & fp.mask) != fp.mask)) {
85: // Cast will not fail since we added it
86: matchedMask |= ((FilePermission) permissions
87: .elementAt(i)).impliesMask(permission);
88: i++;
89: }
90: return ((matchedMask & fp.mask) == fp.mask);
91: }
92: return false;
93: }
94: }
|