Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2010, 2011). 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.framework;
 
 import java.util.List;
 import java.util.Map;

A bundle's authority to adapt an object to a type.

AdaptPermission has one action: adapt.

Version:
$Id: bc4c5d392d2534a7744f6fc00f4665502f82033c $
ThreadSafe:
 
 public class AdaptPermission extends BasicPermission {
 
 	private static final long						serialVersionUID	= 1L;

The action string initiate.
 
 	public final static String						ADAPT"adapt";
 
 	private final static int						ACTION_ADAPT		= 0x00000001;
 	private final static int						ACTION_ALL;
 	final static int								ACTION_NONE			= 0;

The actions mask.
 
 	transient int									action_mask;

The actions in canonical form.

Serial:
 
 	private volatile String							actionsnull;

The bundle used by this AdaptPermission.
 
 	transient final Bundle							bundle;

This holds a Filter matching object used to evaluate the filter in implies.
 
 	transient Filter								filter;

This map holds the properties of the permission, used to match a filter in implies. This is not initialized until necessary, and then cached in this object.
 
 	private transient volatile Map<StringObject>	properties;

Creates a new granted AdaptPermission object. This constructor must only be used to create a permission that is going to be checked.

Examples:

 (adaptClass=com.acme.*)
 (&(signer=\*,o=ACME,c=US)(adaptClass=com.acme.*))
 (signer=\*,o=ACME,c=US)
 

When a signer key is used within the filter expression the signer value must escape the special filter chars ('*', '(', ')').

The name is specified as a filter expression. The filter gives access to the following attributes:

  • signer - A Distinguished Name chain used to sign the exporting bundle. Wildcards in a DN are not matched according to the filter string rules, but according to the rules defined for a DN chain.
  • location - The location of the exporting bundle.
  • id - The bundle ID of the exporting bundle.
  • name - The symbolic name of the exporting bundle.
  • adaptClass - The name of the type to which an object can be adapted.
Filter attribute names are processed in a case sensitive manner.

Parameters:
filter A filter expression. Filter attribute names are processed in a case sensitive manner. A special value of "*" can be used to match all adaptations.
actions adapt.
Throws:
java.lang.IllegalArgumentException If the filter has an invalid syntax.
	public AdaptPermission(String filterString actions) {
		this(parseFilter(filter), parseActions(actions));
	}

Creates a new requested AdaptPermission object to be used by the code that must perform checkPermission. AdaptPermission objects created with this constructor cannot be added to an AdaptPermission permission collection.

Parameters:
adaptClass The name of the type to which an object can be adapted.
adaptableBundle The bundle associated with the object being adapted.
actions adapt.
	public AdaptPermission(String adaptClassBundle adaptableBundle,
			String actions) {
		super(adaptClass);
		setTransients(nullparseActions(actions));
		this. = adaptableBundle;
		if (adaptClass == null) {
			throw new NullPointerException("adaptClass must not be null");
		}
		if (adaptableBundle == null) {
			throw new NullPointerException("adaptableBundle must not be null");
		}
	}

Package private constructor used by AdaptPermissionCollection.

Parameters:
filter name filter
mask action mask
	AdaptPermission(Filter filterint mask) {
		super((filter == null) ? "*" : filter.toString());
		setTransients(filtermask);
		this. = null;
	}

Called by constructors and when deserialized.

Parameters:
filter Permission's filter or null for wildcard.
mask action mask
	private void setTransients(Filter filterint mask) {
		this. = filter;
		if ((mask == ) || ((mask & ) != mask)) {
			throw new IllegalArgumentException("invalid action string");
		}
		this. = mask;
	}

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 >= 4 && (a[i - 4] == 'a' || a[i - 4] == 'A')
					&& (a[i - 3] == 'd' || a[i - 3] == 'D')
					&& (a[i - 2] == 'a' || a[i - 2] == 'A')
					&& (a[i - 1] == 'p' || a[i - 1] == 'P')
					&& (a[i] == 't' || a[i] == 'T')) {
				matchlen = 5;
				mask |= ;
			}
			else {
				// parse error
				throw new IllegalArgumentException("invalid actions: "
actions);
			}
			// make sure we didn't just match the tail of a word
			// like "ackbarfadapt". 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 actions: " + actions);
		}
		return mask;
	}

Parse filter string into a Filter object.

Parameters:
filterString The filter string to parse.
Returns:
a Filter for this bundle.
Throws:
java.lang.IllegalArgumentException If the filter syntax is invalid.
	private static Filter parseFilter(String filterString) {
		filterString = filterString.trim();
		if (filterString.equals("*")) {
			return null;
		}
		try {
			return FrameworkUtil.createFilter(filterString);
		}
					"invalid filter");
			iae.initCause(e);
			throw iae;
		}
	}

Determines if the specified permission is implied by this object.

This method checks that the filter of the target is implied by the adapt class name of this object. The list of AdaptPermission actions must either match or allow for the list of the target object to imply the target AdaptPermission action.

Parameters:
p The requested permission.
Returns:
true if the specified permission is implied by this object; false otherwise.
	public boolean implies(Permission p) {
		if (!(p instanceof AdaptPermission)) {
			return false;
		}
		AdaptPermission requested = (AdaptPermissionp;
		if ( != null) {
			return false;
		}
		// if requested permission has a filter, then it is an invalid argument
		if (requested.filter != null) {
			return false;
		}
		return implies0(requested);
	}

Internal implies method. Used by the implies and the permission collection implies methods.

Parameters:
requested The requested AdaptPermission which has already be validated as a proper argument. The requested AdaptPermission must not have a filter expression.
effective The effective actions with which to start.
Returns:
true if the specified permission is implied by this object; false otherwise.
	boolean implies0(AdaptPermission requestedint effective) {
		/* check actions first - much faster */
		effective |= ;
		final int desired = requested.action_mask;
		if ((effective & desired) != desired) {
			return false;
		}
		/* Get filter */
		Filter f = ;
		if (f == null) {
			// it's "*"
			return true;
		}
		return f.matches(requested.getProperties());
	}

Returns the canonical string representation of the AdaptPermission actions.

Always returns present AdaptPermission actions in the following order: adapt.

Returns:
Canonical string representation of the AdaptPermission actions.
	public String getActions() {
		String result = ;
		if (result == null) {
			 = result = ;
		}
		return result;
	}

Returns a new PermissionCollection object suitable for storing AdaptPermission objects.

Returns:
A new PermissionCollection object.
	}

Determines the equality of two AdaptPermission objects. This method checks that specified permission has the same name and AdaptPermission actions as this AdaptPermission object.

Parameters:
obj The object to test for equality with this AdaptPermission object.
Returns:
true if obj is a AdaptPermission, and has the same name and actions as this AdaptPermission object; false otherwise.
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof AdaptPermission)) {
			return false;
		}
		return ( == cp.action_mask)
				&& (( == cp.bundle) || (( != null) && 
						.equals(cp.bundle)));
	}

Returns the hash code value for this object.

Returns:
A hash code value for this object.
	public int hashCode() {
		int h = 31 * 17 + getName().hashCode();
		h = 31 * h + getActions().hashCode();
		if ( != null) {
			h = 31 * h + .hashCode();
		}
		return h;
	}

WriteObject is called to save the state of this permission object 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 {
		if ( != null) {
			throw new NotSerializableException("cannot serialize");
		}
		// 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 this permission from a stream.
	private synchronized void readObject(java.io.ObjectInputStream s)
		// Read in the action, then initialize the rest
	}

Called by <@link AdaptPermission#implies(Permission)>. This method is only called on a requested permission which cannot have a filter set.

Returns:
a map of properties for this permission.
	private Map<StringObjectgetProperties() {
		Map<StringObjectresult = ;
		if (result != null) {
			return result;
		}
		final Map<StringObjectmap = new HashMap<StringObject>(5);
		map.put("adaptClass"getName());
		if ( != null) {
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				public Object run() {
					map.put("id"new Long(.getBundleId()));
					map.put("location".getLocation());
					if (name != null) {
						map.put("name"name);
					}
					if (signer.isBundleSigned()) {
						map.put("signer"signer);
					}
					return null;
				}
			});
		}
		return  = map;
	}
	static final long						serialVersionUID	= -3350758995234427603L;
Collection of permissions.

Serial:
GuardedBy:
this
Boolean saying if "*" is in the collection.

Serial:
GuardedBy:
this
	private boolean							all_allowed;

Create an empty AdaptPermissions object.
		 = false;
	}

Adds a permission to this permission collection.

Parameters:
permission The AdaptPermission object to add.
Throws:
java.lang.IllegalArgumentException If the specified permission is not a AdaptPermission instance or was constructed with a Bundle object.
java.lang.SecurityException If this AdaptPermissionCollection object has been marked read-only.
	public void add(final Permission permission) {
		if (!(permission instanceof AdaptPermission)) {
			throw new IllegalArgumentException("invalid permission: "
permission);
		}
		if (isReadOnly()) {
			throw new SecurityException("attempt to add a Permission to a "
"readonly PermissionCollection");
		}
		final AdaptPermission ap = (AdaptPermissionpermission;
		if (ap.bundle != null) {
			throw new IllegalArgumentException("cannot add to collection: "
ap);
		}
		final String name = ap.getName();
		synchronized (this) {
			final AdaptPermission existing = pc.get(name);
			if (existing != null) {
				final int oldMask = existing.action_mask;
				final int newMask = ap.action_mask;
				if (oldMask != newMask) {
					pc.put(namenew AdaptPermission(existing.filteroldMask
newMask));
				}
			}
			else {
				pc.put(nameap);
			}
			if (!) {
				if (name.equals("*")) {
					 = true;
				}
			}
		}
	}

Determines if the specified permissions implies the permissions expressed in permission.

Parameters:
permission The Permission object to compare with this AdaptPermission object.
Returns:
true if permission is a proper subset of a permission in the set; false otherwise.
	public boolean implies(final Permission permission) {
		if (!(permission instanceof AdaptPermission)) {
			return false;
		}
		final AdaptPermission requested = (AdaptPermissionpermission;
		/* if requested permission has a filter, then it is an invalid argument */
		if (requested.filter != null) {
			return false;
		}
		int effective = .;
		synchronized (this) {
			/* short circuit if the "*" Permission was added */
			if () {
				AdaptPermission ap = pc.get("*");
				if (ap != null) {
					effective |= ap.action_mask;
					final int desired = requested.action_mask;
					if ((effective & desired) == desired) {
						return true;
					}
				}
			}
			perms = pc.values();
		}
		/* iterate one by one over filteredPermissions */
		for (AdaptPermission perm : perms) {
			if (perm.implies0(requestedeffective)) {
				return true;
			}
		}
		return false;
	}

Returns an enumeration of all AdaptPermission objects in the container.

Returns:
Enumeration of all AdaptPermission objects.
	public synchronized Enumeration<Permissionelements() {
		return Collections.enumeration(all);
	}
	/* serialization logic */
	private static final ObjectStreamField[]	serialPersistentFields	= {
			new ObjectStreamField("permissions"HashMap.class),
			new ObjectStreamField("all_allowed".)			};
	private synchronized void writeObject(ObjectOutputStream out)
			throws IOException {
		pfields.put("permissions");
		pfields.put("all_allowed");
	}
	private synchronized void readObject(java.io.ObjectInputStream in)
				"permissions"null);
		 = gfields.get("all_allowed"false);
	}
New to GrepCode? Check out our FAQ X