Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae0a58bb4dcd7da57686c5fede5235055039761f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2008, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.internal.permadmin;

import java.security.Permission;
import java.security.PermissionCollection;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * A simple Hashtable based collection of Permission objects.
 * <p>
 * The class' .implies method simply scans each permission
 * individually and asks if the permission should be granted.
 * No addition semantics is provided by the collection, so it is
 * not possible to grant permissions whose "grantedness" is
 * split across multiple stored Permissions.
 * <p>
 * Instances of this class can be used to store heterogeneous
 * collections of permissions, as long as it is not necessary
 * to remember when multiple occurances of .equal permissions
 * are added.
 *
 */
class PermissionsHash extends PermissionCollection {
	private static final long serialVersionUID = 3258408426341284153L;
	/**
	 * A hashtable to store the elements of the collection.
	 */
	Hashtable<Permission, Permission> perms = new Hashtable<>(8);

	/**
	 * Constructs a new instance of this class.
	 *
	 */
	public PermissionsHash() {
		super();
	}

	/**
	 * Adds the argument to the collection.
	 *
	 * @param		perm java.security.Permission
	 *					the permission to add to the collection.
	 * @exception	IllegalStateException
	 *					if the collection is read only.
	 */
	public void add(Permission perm) {
		if (isReadOnly()) {
			throw new SecurityException();
		}

		perms.put(perm, perm);
	}

	/**
	 * Answers an enumeration of the permissions
	 * in the receiver.
	 *
	 * @return		Enumeration
	 *					the permissions in the receiver.
	 */
	public Enumeration<Permission> elements() {
		return perms.keys();
	}

	/**
	 * Indicates whether the argument permission is implied
	 * by the permissions contained in the receiver.
	 *
	 * @return		boolean
	 *					<code>true</code> if the argument permission
	 *					is implied by the permissions in the receiver,
	 *					and <code>false</code> if it is not.
	 * @param		perm java.security.Permission
	 *					the permission to check
	 */
	public boolean implies(Permission perm) {
		Permission p = perms.get(perm);

		if ((p != null) && p.implies(perm)) {
			return true;
		}

		Enumeration<Permission> permsEnum = elements();

		while (permsEnum.hasMoreElements()) {
			if (permsEnum.nextElement().implies(perm)) {
				return true;
			}
		}

		return false;
	}
}

Back to the top