Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ecf1b5fa83469af0568192d53250f6c491ca5bc8 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (c) OSGi Alliance (2002, 2015). All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.osgi.service.wireadmin;

import java.io.IOException;
import java.security.BasicPermission;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Permission for the scope of a {@code Wire} object. When a {@code Envelope}
 * object is used for communication with the {@code poll} or {@code update}
 * method, and the scope is set, then the {@code Wire} object must verify that
 * the Consumer service has {@code WirePermission[name,CONSUME]} and the
 * Producer service has {@code WirePermission[name,PRODUCE]} for all names in
 * the scope.
 * <p>
 * The names are compared with the normal rules for permission names. This means
 * that they may end with a "*" to indicate wildcards. E.g. Door.* indicates all
 * scope names starting with the string "Door". The last period is required due
 * to the implementations of the {@code BasicPermission} class.
 * 
 * @ThreadSafe
 * @author $Id$
 */
final public class WirePermission extends BasicPermission {
	static final long			serialVersionUID	= -5583709391516569321L;
	/**
	 * The action string for the {@code produce} action.
	 */
	public static final String	PRODUCE				= "produce";
	/**
	 * The action string for the {@code consume} action.
	 */
	public static final String	CONSUME				= "consume";
	private final static int	ACTION_PRODUCE		= 0x00000001;
	private final static int	ACTION_CONSUME		= 0x00000002;
	private final static int	ACTION_ALL			= ACTION_PRODUCE | ACTION_CONSUME;
	private final static int	ACTION_NONE			= 0;
	/**
	 * The actions mask.
	 */
	private transient int		action_mask;
	/**
	 * The actions in canonical form.
	 * 
	 * @serial
	 */
	private volatile String		actions				= null;

	/**
	 * Create a new WirePermission with the given name (may be wildcard) and
	 * actions.
	 * 
	 * @param name Wire name.
	 * @param actions {@code produce}, {@code consume} (canonical order).
	 */
	public WirePermission(String name, String actions) {
		this(name, parseActions(actions));
	}

	/**
	 * Package private constructor used by WirePermissionCollection.
	 * 
	 * @param name class name
	 * @param mask action mask
	 */
	WirePermission(String name, int mask) {
		super(name);
		setTransients(mask);
	}

	/**
	 * Called by constructors and when deserialized.
	 * 
	 * @param mask action mask
	 */
	private synchronized void setTransients(int mask) {
		if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) {
			throw new IllegalArgumentException("invalid action string");
		}
		action_mask = mask;
	}

	/**
	 * Returns the current action mask. Used by the WirePermissionCollection
	 * object.
	 * 
	 * @return The actions mask.
	 */
	synchronized int getActionsMask() {
		return action_mask;
	}

	/**
	 * Parse action string into action mask.
	 * 
	 * @param actions Action string.
	 * @return action mask.
	 */
	private static int parseActions(String actions) {
		boolean seencomma = false;
		int mask = ACTION_NONE;
		if (actions == null) {
			return mask;
		}
		char[] a = actions.toCharArray();
		int i = a.length - 1;
		if (i < 0)
			return mask;
		while (i != -1) {
			char c;
			// skip whitespace
			while ((i != -1) && ((c = a[i]) == ' ' || c == '\r' || c == '\n' || c == '\f' || c == '\t'))
				i--;
			// check for the known strings
			int matchlen;
			if (i >= 6 && (a[i - 6] == 'p' || a[i - 6] == 'P') && (a[i - 5] == 'r' || a[i - 5] == 'R') && (a[i - 4] == 'o' || a[i - 4] == 'O') && (a[i - 3] == 'd' || a[i - 3] == 'D')
					&& (a[i - 2] == 'u' || a[i - 2] == 'U') && (a[i - 1] == 'c' || a[i - 1] == 'C') && (a[i] == 'e' || a[i] == 'E')) {
				matchlen = 7;
				mask |= ACTION_PRODUCE;
			} else
				if (i >= 6 && (a[i - 6] == 'c' || a[i - 6] == 'C') && (a[i - 5] == 'o' || a[i - 5] == 'O') && (a[i - 4] == 'n' || a[i - 4] == 'N') && (a[i - 3] == 's' || a[i - 3] == 'S')
						&& (a[i - 2] == 'u' || a[i - 2] == 'U') && (a[i - 1] == 'm' || a[i - 1] == 'M') && (a[i] == 'e' || a[i] == 'E')) {
					matchlen = 7;
					mask |= ACTION_CONSUME;
				} else {
					// parse error
					throw new IllegalArgumentException("invalid permission: " + actions);
				}
			// make sure we didn't just match the tail of a word
			// like "ackbarfregister". Also, skip to the comma.
			seencomma = false;
			while (i >= matchlen && !seencomma) {
				switch (a[i - matchlen]) {
					case ',' :
						seencomma = true;
						/* FALLTHROUGH */
					case ' ' :
					case '\r' :
					case '\n' :
					case '\f' :
					case '\t' :
						break;
					default :
						throw new IllegalArgumentException("invalid permission: " + actions);
				}
				i--;
			}
			// point i at the location of the comma minus one (or -1).
			i -= matchlen;
		}
		if (seencomma) {
			throw new IllegalArgumentException("invalid permission: " + actions);
		}
		return mask;
	}

	/**
	 * Checks if this {@code WirePermission} object {@code implies} the
	 * specified permission.
	 * <P>
	 * More specifically, this method returns {@code true} if:
	 * <p>
	 * <ul>
	 * <li><i>p</i> is an instanceof the {@code WirePermission} class,</li>
	 * <li><i>p</i>'s actions are a proper subset of this object's actions, and</li>
	 * <li><i>p</i>'s name is implied by this object's name. For example,
	 * {@code java.*} implies {@code java.home}.</li>
	 * </ul>
	 * 
	 * @param p The permission to check against.
	 * 
	 * @return {@code true} if the specified permission is implied by this
	 *         object; {@code false} otherwise.
	 */
	public boolean implies(Permission p) {
		if (p instanceof WirePermission) {
			WirePermission requested = (WirePermission) p;
			int requestedMask = requested.getActionsMask();
			return ((getActionsMask() & requestedMask) == requestedMask) && super.implies(p);
		}
		return false;
	}

	/**
	 * Returns the canonical string representation of the actions. Always
	 * returns present actions in the following order: {@code produce},
	 * {@code consume}.
	 * 
	 * @return The canonical string representation of the actions.
	 */
	public String getActions() {
		String result = actions;
		if (result == null) {
			StringBuffer sb = new StringBuffer();
			boolean comma = false;
			int mask = getActionsMask();
			if ((mask & ACTION_PRODUCE) == ACTION_PRODUCE) {
				sb.append(PRODUCE);
				comma = true;
			}
			if ((mask & ACTION_CONSUME) == ACTION_CONSUME) {
				if (comma)
					sb.append(',');
				sb.append(CONSUME);
			}
			actions = result = sb.toString();
		}
		return result;
	}

	/**
	 * Returns a new {@code PermissionCollection} object for storing
	 * {@code WirePermission} objects.
	 * 
	 * @return A new {@code PermissionCollection} object suitable for storing
	 *         {@code WirePermission} objects.
	 */
	public PermissionCollection newPermissionCollection() {
		return new WirePermissionCollection();
	}

	/**
	 * Determines the equality of two {@code WirePermission} objects.
	 * 
	 * Checks that specified object has the same name and actions as this
	 * {@code WirePermission} object.
	 * 
	 * @param obj The object to test for equality.
	 * @return true if {@code obj} is a {@code WirePermission}, and has the same
	 *         name and actions as this {@code WirePermission} object;
	 *         {@code false} otherwise.
	 */
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof WirePermission)) {
			return false;
		}
		WirePermission wp = (WirePermission) obj;
		return (getActionsMask() == wp.getActionsMask()) && getName().equals(wp.getName());
	}

	/**
	 * Returns the hash code value for this object.
	 * 
	 * @return Hash code value for this object.
	 */
	public int hashCode() {
		int h = 31 * 17 + getName().hashCode();
		h = 31 * h + getActions().hashCode();
		return h;
	}

	/**
	 * Returns a string describing this {@code WirePermission}. The convention
	 * is to specify the class name, the permission name, and the actions in the
	 * following format: '(org.osgi.service.wireadmin.WirePermission
	 * &quot;name&quot; &quot;actions&quot;)'.
	 * 
	 * @return information about this {@code Permission} object.
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append('(');
		sb.append(getClass().getName());
		sb.append(" \"");
		sb.append(getName());
		String a = getActions();
		if (a.length() > 0) {
			sb.append("\" \"");
			sb.append(a);
		}
		sb.append("\")");
		return sb.toString();
	}

	/**
	 * WriteObject is called to save the state of the ServicePermission to a
	 * stream. The actions are serialized, and the superclass takes care of the
	 * name.
	 */
	private synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException {
		// Write out the actions. The superclass takes care of the name
		// call getActions to make sure actions field is initialized
		if (actions == null)
			getActions();
		s.defaultWriteObject();
	}

	/**
	 * readObject is called to restore the state of the ServicePermission from a
	 * stream.
	 */
	private synchronized void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
		// Read in the action, then initialize the rest
		s.defaultReadObject();
		setTransients(parseActions(actions));
	}
}

/**
 * A {@code WirePermissionCollection} stores a set of {@code WirePermission}
 * permissions.
 */

final class WirePermissionCollection extends PermissionCollection {
	static final long		serialVersionUID	= 2617521094909826016L;
	/**
	 * Table of permissions.
	 * 
	 * @GuardedBy this
	 * @serial
	 */
	private final Hashtable	permissions;
	/**
	 * Boolean saying if "*" is in the collection.
	 * 
	 * @GuardedBy this
	 * @serial
	 */
	private boolean			all_allowed;

	/**
	 * Creates an empty WirePermissionCollection object.
	 * 
	 */
	public WirePermissionCollection() {
		permissions = new Hashtable();
		all_allowed = false;
	}

	/**
	 * Adds a permission to this PermissionCollection.
	 * 
	 * @param permission The Permission object to add.
	 * 
	 * @throws IllegalArgumentException If the permission is not a
	 *         WirePermission object.
	 * 
	 * @throws SecurityException If this PermissionCollection has been marked
	 *         read-only.
	 */
	public void add(Permission permission) {
		if (!(permission instanceof WirePermission)) {
			throw new IllegalArgumentException("invalid permission: " + permission);
		}
		if (isReadOnly()) {
			throw new SecurityException("attempt to add a Permission to a " + "readonly PermissionCollection");
		}
		WirePermission wp = (WirePermission) permission;
		String name = wp.getName();
		synchronized (this) {
			WirePermission existing = (WirePermission) permissions.get(name);
			if (existing != null) {
				int oldMask = existing.getActionsMask();
				int newMask = wp.getActionsMask();
				if (oldMask != newMask) {
					permissions.put(name, new WirePermission(name, oldMask | newMask));
				}
			} else {
				permissions.put(name, wp);
			}
			if (!all_allowed) {
				if (name.equals("*")) {
					all_allowed = true;
				}
			}
		}
	}

	/**
	 * Determines if a set of permissions implies the permissions expressed in
	 * {@code permission}.
	 * 
	 * @param permission The Permission object to compare.
	 * 
	 * @return {@code true} if {@code permission} is a proper subset of a
	 *         permission in the set; {@code false} otherwise.
	 */
	public boolean implies(Permission permission) {
		if (!(permission instanceof WirePermission)) {
			return false;
		}
		WirePermission requested = (WirePermission) permission;
		WirePermission x;
		int desired = requested.getActionsMask();
		int effective = 0;
		String name = requested.getName();
		synchronized (this) {
			// short circuit if the "*" Permission was added
			if (all_allowed) {
				x = (WirePermission) permissions.get("*");
				if (x != null) {
					effective |= x.getActionsMask();
					if ((effective & desired) == desired)
						return true;
				}
			}
			// strategy:
			// Check for full match first. Then work our way up the
			// name looking for matches on a.b.*
			x = (WirePermission) permissions.get(name);
		}
		if (x != null) {
			// we have a direct hit!
			effective |= x.getActionsMask();
			if ((effective & desired) == desired) {
				return true;
			}
		}
		// work our way up the tree...
		int last;
		int offset = name.length() - 1;
		while ((last = name.lastIndexOf(".", offset)) != -1) {
			name = name.substring(0, last + 1) + "*";
			synchronized (this) {
				x = (WirePermission) permissions.get(name);
			}
			if (x != null) {
				effective |= x.getActionsMask();
				if ((effective & desired) == desired) {
					return (true);
				}
			}
			offset = last - 1;
		}
		// we don't have to check for "*" as it was already checked
		// at the top (all_allowed), so we just return false
		return false;
	}

	/**
	 * Returns an enumeration of all the Permission objects in the container.
	 * 
	 * @return Enumeration of all the Permission objects.
	 */
	public Enumeration elements() {
		return permissions.elements();
	}
}

Back to the top