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.net;
19
20import java.security.Permission;
21import java.security.PermissionCollection;
22import java.util.Enumeration;
23import java.util.Vector;
24
25/**
26 * This class represents a list of {@code SocketPermission} objects and provides
27 * a method to check whether or not a specific permission is implied by this
28 * {@code SocketPermissionCollection}.
29 */
30final 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    // Returns an enumeration of the permissions
54    @Override
55    public Enumeration<Permission> elements() {
56        return permissions.elements();
57    }
58
59    /**
60     * Returns whether this permission collection implies {@code permission}.
61     * Basically it tests whether {@code permission} 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}
99