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.net;
19:
20: import java.security.Permission;
21: import java.security.PermissionCollection;
22: import java.util.Enumeration;
23: import java.util.Vector;
24:
25: /**
26: * SocketPermissionCollection is a class which holds a collection of
27: * SocketPermission objects and can answer a boolean indicating whether or not a
28: * specific permissions is implied by a SocketPermissionCollection.
29: */
30: final class SocketPermissionCollection extends PermissionCollection {
31:
32: private static final long serialVersionUID = 2787186408602843674L;
33:
34: private Vector<Permission> permissions = new Vector<Permission>();
35:
36: // Constructs a new instance of this class.
37: public SocketPermissionCollection() {
38: super ();
39: }
40:
41: // Adds the argument to the collection.
42: @Override
43: public void add(Permission permission) {
44: if (isReadOnly()) {
45: throw new IllegalStateException();
46: }
47: if (!(permission instanceof SocketPermission)) {
48: throw new IllegalArgumentException(permission.toString());
49: }
50: permissions.addElement(permission);
51: }
52:
53: // Answers an enumeration of the permissions
54: @Override
55: public Enumeration<Permission> elements() {
56: return permissions.elements();
57: }
58:
59: /**
60: * Answers if this permission collection implies <code>permission</code>.
61: * Basically it tests if <code>permission</code> is the subset of this
62: * collection.
63: */
64: @Override
65: public boolean implies(Permission permission) {
66: if (!(permission instanceof SocketPermission)) {
67: return false;
68: }
69: SocketPermission sp, argPerm = (SocketPermission) permission;
70: int pmask = argPerm.actionsMask;
71: int allMask = 0;
72: int i = 0, count = permissions.size();
73: while ((i < count) && ((allMask & pmask) != pmask)) {
74: sp = (SocketPermission) permissions.elementAt(i);
75: if (sp.checkHost(argPerm)) {
76: if ((sp.actionsMask & SocketPermission.SP_RESOLVE) == SocketPermission.SP_RESOLVE) {
77: allMask |= SocketPermission.SP_RESOLVE;
78: }
79: // Only set flags if the port range and host can be implied
80: if ((argPerm.portMin >= sp.portMin)
81: && (argPerm.portMax <= sp.portMax)) {
82: if ((sp.actionsMask & SocketPermission.SP_CONNECT) == SocketPermission.SP_CONNECT) {
83: allMask |= SocketPermission.SP_CONNECT;
84: }
85: if ((sp.actionsMask & SocketPermission.SP_ACCEPT) == SocketPermission.SP_ACCEPT) {
86: allMask |= SocketPermission.SP_ACCEPT;
87: }
88: if ((sp.actionsMask & SocketPermission.SP_LISTEN) == SocketPermission.SP_LISTEN) {
89: allMask |= SocketPermission.SP_LISTEN;
90: }
91: }
92: }
93: ++i;
94: }
95:
96: return (allMask & pmask) == pmask;
97: }
98: }
|