Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2002, 2010). 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;
 
Permission for the scope of a Wire object. When a Envelope object is used for communication with the poll or update method, and the scope is set, then the Wire object must verify that the Consumer service has WirePermission[name,CONSUME] and the Producer service has WirePermission[name,PRODUCE] for all names in the scope.

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 BasicPermission class.

Version:
$Id: 2910866cb953b6311421e6d2a33ae1f597be4b95 $
ThreadSafe:
 
 final public class WirePermission extends BasicPermission {
 	static final long			serialVersionUID	= -5583709391516569321L;
The action string for the produce action.
 
 	public static final String	PRODUCE"produce";
The action string for the 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
 	private final static int	ACTION_NONE			= 0;
The actions mask.
 
 	private transient int		action_mask;
The actions in canonical form.

Serial:
 
 	private volatile String		actionsnull;

Create a new WirePermission with the given name (may be wildcard) and actions.

Parameters:
name Wire name.
actions produce, consume (canonical order).
 
 	public WirePermission(String nameString actions) {
 		this(nameparseActions(actions));
 	}

Package private constructor used by WirePermissionCollection.

Parameters:
name class name
mask action mask
 
 	WirePermission(String nameint mask) {
 		super(name);
 		setTransients(mask);
 	}

Called by constructors and when deserialized.

Parameters:
mask action mask
 
 	private synchronized void setTransients(int mask) {
 		if ((mask == ) || ((mask & ) != mask)) {
 			throw new IllegalArgumentException("invalid action string");
 		}
 		 = mask;
	}

Returns the current action mask. Used by the WirePermissionCollection object.

Returns:
The actions mask.
	synchronized int getActionsMask() {
		return ;
	}

Parse action string into action mask.

Parameters:
actions Action string.
Returns:
action mask.
	private static int parseActions(String actions) {
		boolean seencomma = false;
		int mask = ;
		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 |= ;
			}
			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 |= ;
				}
				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 :
								"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 WirePermission object implies the specified permission.

More specifically, this method returns true if:

  • p is an instanceof the WirePermission class,
  • p 's actions are a proper subset of this object's actions, and
  • p 's name is implied by this object's name. For example, java.* implies java.home.

Parameters:
p The permission to check against.
Returns:
true if the specified permission is implied by this object; false otherwise.
	public boolean implies(Permission p) {
		if (p instanceof WirePermission) {
			WirePermission requested = (WirePermissionp;
			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: produce, consume.

Returns:
The canonical string representation of the actions.
	public String getActions() {
		String result = ;
		if (result == null) {
			StringBuffer sb = new StringBuffer();
			boolean comma = false;
			int mask = getActionsMask();
			if ((mask & ) == ) {
				comma = true;
			}
			if ((mask & ) == ) {
				if (comma)
					sb.append(',');
			}
			 = result = sb.toString();
		}
		return result;
	}

Returns a new PermissionCollection object for storing WirePermission objects.

Returns:
A new PermissionCollection object suitable for storing WirePermission objects.
	}

Determines the equalty of two WirePermission objects. Checks that specified object has the same name and actions as this WirePermission object.

Parameters:
obj The object to test for equality.
Returns:
true if obj is a WirePermission, and has the same name and actions as this WirePermission object; false otherwise.
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof WirePermission)) {
			return false;
		}
		return (getActionsMask() == wp.getActionsMask())
				&& getName().equals(wp.getName());
	}

Returns the hash code value for this object.

Returns:
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 WirePermission. The convention is to specify the class name, the permission name, and the actions in the following format: '(org.osgi.service.wireadmin.WirePermission "name" "actions")'.

Returns:
information about this Permission object.
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append('(');
		sb.append(" \"");
		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 ( == null)
	}

readObject is called to restore the state of the ServicePermission from a stream.
	private synchronized void readObject(java.io.ObjectInputStream s)
		// Read in the action, then initialize the rest
	}
A WirePermissionCollection stores a set of WirePermission permissions.
	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.
		 = false;
	}

Adds a permission to this PermissionCollection.

Parameters:
permission The Permission object to add.
Throws:
java.lang.IllegalArgumentException If the permission is not a WirePermission object.
java.lang.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 = (WirePermissionpermission;
		String name = wp.getName();
		synchronized (this) {
			if (existing != null) {
				int oldMask = existing.getActionsMask();
				int newMask = wp.getActionsMask();
				if (oldMask != newMask) {
					.put(namenew WirePermission(nameoldMask
newMask));
				}
			}
			else {
				.put(namewp);
			}
			if (!) {
				if (name.equals("*")) {
					 = true;
				}
			}
		}
	}

Determines if a set of permissions implies the permissions expressed in permission.

Parameters:
permission The Permission object to compare.
Returns:
true if permission is a proper subset of a permission in the set; false otherwise.
	public boolean implies(Permission permission) {
		if (!(permission instanceof WirePermission)) {
			return false;
		}
		WirePermission requested = (WirePermissionpermission;
		int desired = requested.getActionsMask();
		int effective = 0;
		String name = requested.getName();
		synchronized (this) {
			// short circuit if the "*" Permission was added
			if () {
				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.*
		}
		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) {
			}
			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.

Returns:
Enumeration of all the Permission objects.
	public Enumeration elements() {
	}
New to GrepCode? Check out our FAQ X