Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4921f04a2a9d194c1d3347a6aa13b56b5396431d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*******************************************************************************
 * Copyright (c) 2003, 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.framework.internal.core;

import java.security.Permission;
import java.util.*;

/**
 * A combination of two BundlePermissionCollection classes.
 *
 */
final class BundleCombinedPermissions extends BundlePermissionCollection {
	private static final long serialVersionUID = 4049357526208360496L;
	private BundlePermissionCollection assigned;
	private BundlePermissionCollection implied;
	private ConditionalPermissions conditional;
	private ConditionalPermissionSet restrictedPermissions;
	private boolean isDefault;

	/**
	 * Create a permission combiner class.
	 *
	 * @param implied The permissions a bundle always has.
	 */
	BundleCombinedPermissions(BundlePermissionCollection implied) {
		this.implied = implied;

		setReadOnly(); /* this doesn't really mean anything */
	}

	/**
	 * Assign the administrator defined permissions.
	 *
	 * @param assigned The permissions assigned by the administrator.
	 * @param isDefault If true, the assigned permissions are the default permissions.
	 */
	void setAssignedPermissions(BundlePermissionCollection assigned, boolean isDefault) {
		this.assigned = assigned;
		this.isDefault = isDefault;
	}

	/**
	 * Assign the conditional permissions
	 * 
	 * @param conditional The conditional permissions assigned by the administrator
	 */
	void setConditionalPermissions(ConditionalPermissions conditional) {
		this.conditional = conditional;
	}

	void checkConditionalPermissionInfo(ConditionalPermissionInfoImpl cpi) {
		if (conditional != null) {
			conditional.checkConditionalPermissionInfo(cpi);
		}
	}

	/**
	 * The Permission collection will unresolve the permissions in these packages.
	 *
	 * @param refreshedBundles A list of the bundles which have been refreshed
	 * as a result of a packageRefresh
	 */
	void unresolvePermissions(AbstractBundle[] refreshedBundles) {
		if (assigned != null) {
			assigned.unresolvePermissions(refreshedBundles);
		}

		if (implied != null) {
			implied.unresolvePermissions(refreshedBundles);
		}

		if (conditional != null) {
			conditional.unresolvePermissions(refreshedBundles);
		}

		if (restrictedPermissions != null) {
			restrictedPermissions.unresolvePermissions(refreshedBundles);
		}
	}

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

	/**
	 * Answers an enumeration of the permissions
	 * in the receiver.
	 *
	 * @return		Enumeration
	 *					the permissions in the receiver.
	 */
	public Enumeration elements() {
		return new Enumeration() {
			private int i = 0;
			private Enumeration[] enums;

			{
				enums = new Enumeration[] {(assigned == null) ? null : assigned.elements(), (implied == null) ? null : implied.elements()};
			}

			/**
			 * Answers if this Enumeration has more elements.
			 *
			 * @return		true if there are more elements, false otherwise
			 *
			 * @see			#nextElement
			 */
			public boolean hasMoreElements() {
				while (i < enums.length) {
					Enumeration perms = enums[i];
					if ((perms != null) && perms.hasMoreElements()) {
						return true;
					}

					i++;
				}

				return false;
			}

			/**
			 * Answers the next element in this Enumeration.
			 *
			 * @return		the next element in this Enumeration
			 *
			 * @exception	NoSuchElementException when there are no more elements
			 *
			 * @see			#hasMoreElements
			 */
			public Object nextElement() {
				while (i < enums.length) {
					try {
						Enumeration perms = enums[i];
						if (perms != null) {
							return perms.nextElement();
						}
					} catch (NoSuchElementException e) {
					}
					i++;
				}

				throw new NoSuchElementException();
			}
		};
	}

	/**
	 * 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		permission java.security.Permission
	 *					the permission to check
	 */
	public boolean implies(Permission permission) {
		if ((implied != null) && implied.implies(permission))
			return true;

		/* We must be allowed by the restricted permissions to have any hope of
		 * passing the check */
		if ((restrictedPermissions != null) && !restrictedPermissions.implies(permission)) {
			return false;
		}

		/* If we aren't using the default permissions, then the assigned
		 * permission are the exact permissions the bundle has. */
		if (!isDefault && (assigned != null) && assigned.implies(permission))
			return true;
		if (conditional != null) {
			boolean conditionalImplies = conditional.implies(permission);
			if (!conditional.isEmpty()) {
				return conditionalImplies;
			}
		}
		/* If there aren't any conditional permissions that apply, we use
		 * the default. */
		return assigned.implies(permission);
	}

	/**
	 * Sets the restricted Permissions of the Bundle. This set of Permissions limit the
	 * Permissions available to the Bundle.
	 * 
	 * @param restrictedPermissions the maximum set of permissions allowed to the Bundle 
	 * irrespective of the actual permissions assigned to it.
	 */
	public void setRestrictedPermissions(ConditionalPermissionSet restrictedPermissions) {
		this.restrictedPermissions = restrictedPermissions;
	}
}

Back to the top