Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

A bundle's authority to import or export a package.

A package is a dot-separated string that defines a fully qualified Java package.

For example:

 org.osgi.service.http
 

PackagePermission has three actions: exportonly, import and export. The export action, which is deprecated, implies the import action.

Version:
$Id: a286af94405e583f8bedc2ff5d7c818198f8caaf $
ThreadSafe:
 
 
 public final class PackagePermission extends BasicPermission {
 	static final long						serialVersionUID	= -5107705877071099135L;

The action string export. The export action implies the import action.

Deprecated:
Since 1.5. Use exportonly instead.
 
 	public final static String				EXPORT"export";

The action string exportonly. The exportonly action does not imply the import action.

Since:
1.5
 
 	public final static String				EXPORTONLY"exportonly";

The action string import.
 
 	public final static String				IMPORT"import";
 
 	private final static int				ACTION_EXPORT		= 0x00000001;
 	private final static int				ACTION_IMPORT		= 0x00000002;
 	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 PackagePermission.
	transient final Bundle					bundle;

If this PackagePermission was constructed with a filter, 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 PackagePermission object.

The name is specified as a normal Java package name: a dot-separated string. Wildcards may be used.

 name ::= <package name> | <package name ending in ".*"> | *
 
Examples:
 org.osgi.service.http
 javax.servlet.*
 *
 
For the import action, the name can also be 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.
  • package.name - The name of the requested package.
Filter attribute names are processed in a case sensitive manner.

Package Permissions are granted over all possible versions of a package. A bundle that needs to export a package must have the appropriate PackagePermission for that package; similarly, a bundle that needs to import a package must have the appropriate PackagePermssion for that package.

Permission is granted for both classes and resources.

Parameters:
name Package name or filter expression. A filter expression can only be specified if the specified action is import.
actions exportonly,import (canonical order).
Throws:
java.lang.IllegalArgumentException If the specified name is a filter expression and either the specified action is not import or the filter has an invalid syntax.
	public PackagePermission(String nameString actions) {
		this(nameparseActions(actions));
		if (( != null)
					"invalid action string for filter expression");
		}
	}

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

Parameters:
name The name of the requested package to import.
exportingBundle The bundle exporting the requested package.
actions The action import.
Throws:
java.lang.IllegalArgumentException If the specified action is not import or the name is a filter expression.
Since:
1.5
	public PackagePermission(String nameBundle exportingBundleString actions) {
		super(name);
		setTransients(nameparseActions(actions));
		this. = exportingBundle;
		if (exportingBundle == null) {
			throw new IllegalArgumentException("bundle must not be null");
		}
		if ( != null) {
			throw new IllegalArgumentException("invalid name");
		}
			throw new IllegalArgumentException("invalid action string");
		}
	}

Package private constructor used by PackagePermissionCollection.

Parameters:
name package name
mask action mask
	PackagePermission(String nameint mask) {
		super(name);
		setTransients(namemask);
		this. = null;
	}

Called by constructors and when deserialized.

Parameters:
mask action mask
	private void setTransients(String nameint mask) {
		if ((mask == ) || ((mask & ) != mask)) {
			throw new IllegalArgumentException("invalid action string");
		}
		 = 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 >= 5 && (a[i - 5] == 'i' || a[i - 5] == 'I')
					&& (a[i - 4] == 'm' || a[i - 4] == 'M')
					&& (a[i - 3] == 'p' || a[i - 3] == 'P')
					&& (a[i - 2] == 'o' || a[i - 2] == 'O')
					&& (a[i - 1] == 'r' || a[i - 1] == 'R')
					&& (a[i] == 't' || a[i] == 'T')) {
				matchlen = 6;
				mask |= ;
			}
			else
				if (i >= 5 && (a[i - 5] == 'e' || a[i - 5] == 'E')
						&& (a[i - 4] == 'x' || a[i - 4] == 'X')
						&& (a[i - 3] == 'p' || a[i - 3] == 'P')
						&& (a[i - 2] == 'o' || a[i - 2] == 'O')
						&& (a[i - 1] == 'r' || a[i - 1] == 'R')
						&& (a[i] == 't' || a[i] == 'T')) {
					matchlen = 6;
				}
				else {
					if (i >= 9 && (a[i - 9] == 'e' || a[i - 9] == 'E')
							&& (a[i - 8] == 'x' || a[i - 8] == 'X')
							&& (a[i - 7] == 'p' || a[i - 7] == 'P')
							&& (a[i - 6] == 'o' || a[i - 6] == 'O')
							&& (a[i - 5] == 'r' || a[i - 5] == 'R')
							&& (a[i - 4] == 't' || a[i - 4] == 'T')
							&& (a[i - 3] == 'o' || a[i - 3] == 'O')
							&& (a[i - 2] == 'n' || a[i - 2] == 'N')
							&& (a[i - 1] == 'l' || a[i - 1] == 'L')
							&& (a[i] == 'y' || a[i] == 'Y')) {
						matchlen = 10;
						mask |= ;
					}
					else {
						// parse error
								"invalid permission: " + actions);
					}
				}
			// make sure we didn't just match the tail of a word
			// like "ackbarfimport". 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;
	}

Parse filter string into a Filter object.

Parameters:
filterString The filter string to parse.
Returns:
a Filter for this bundle. If the specified filterString is not a filter expression, then null is returned.
Throws:
java.lang.IllegalArgumentException If the filter syntax is invalid.
	private static Filter parseFilter(String filterString) {
		filterString = filterString.trim();
		if (filterString.charAt(0) != '(') {
			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 package name of the target is implied by the package name of this object. The list of PackagePermission actions must either match or allow for the list of the target object to imply the target PackagePermission action.

The permission to export a package implies the permission to import the named package.

 x.y.*,"export" -> x.y.z,"export" is true
 *,"import" -> x.y, "import"      is true
 *,"export" -> x.y, "import"      is true
 x.y,"export" -> x.y.z, "export"  is false
 

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 PackagePermission)) {
			return false;
		}
		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 PackagePermission which has already be validated as a proper argument. The requested PackagePermission 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(PackagePermission requestedint effective) {
		/* check actions first - much faster */
		effective |= ;
		final int desired = requested.action_mask;
		if ((effective & desired) != desired) {
			return false;
		}
		/* Get filter if any */
		Filter f = ;
		if (f == null) {
			return super.implies(requested);
		}
		return f.matches(requested.getProperties());
	}

Returns the canonical string representation of the PackagePermission actions.

Always returns present PackagePermission actions in the following order: EXPORTONLY,IMPORT.

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

Returns a new PermissionCollection object suitable for storing PackagePermission objects.

Returns:
A new PermissionCollection object.
	}

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

Parameters:
obj The object to test for equality with this PackagePermission object.
Returns:
true if obj is a PackagePermission, and has the same package name and actions as this PackagePermission object; false otherwise.
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof PackagePermission)) {
			return false;
		}
		return ( == pp.action_mask)
				&& (( == pp.bundle) || (( != null) && 
						.equals(pp.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 PackagePermission#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("package.name"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;
	}
Stores a set of PackagePermission permissions.

	static final long		serialVersionUID	= -3350758995234427603L;
Table of permissions with names.

GuardedBy:
this
	private transient Map<StringPackagePermission>	permissions;

Boolean saying if "*" is in the collection.

Serial:
GuardedBy:
this
	private boolean			all_allowed;

Table of permissions with filter expressions.

Serial:
GuardedBy:
this
Create an empty PackagePermissions object.
		 = false;
	}

Adds a permission to this permission collection.

Parameters:
permission The PackagePermission object to add.
Throws:
java.lang.IllegalArgumentException If the specified permission is not a PackagePermission instance or was constructed with a Bundle object.
java.lang.SecurityException If this PackagePermissionCollection object has been marked read-only.
	public void add(final Permission permission) {
		if (!(permission instanceof PackagePermission)) {
			throw new IllegalArgumentException("invalid permission: "
permission);
		}
		if (isReadOnly()) {
			throw new SecurityException("attempt to add a Permission to a "
"readonly PermissionCollection");
		}
		final PackagePermission pp = (PackagePermissionpermission;
		if (pp.bundle != null) {
			throw new IllegalArgumentException("cannot add to collection: "
pp);
		}
		final String name = pp.getName();
		final Filter f = pp.filter;
		synchronized (this) {
			/* select the bucket for the permission */
			if (f != null) {
				if (pc == null) {
				}
			}
			else {
			}
			final PackagePermission existing = pc.get(name);
			if (existing != null) {
				final int oldMask = existing.action_mask;
				final int newMask = pp.action_mask;
				if (oldMask != newMask) {
					pc
							.put(namenew PackagePermission(nameoldMask
newMask));
				}
			}
			else {
				pc.put(namepp);
			}
			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 PackagePermission 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 PackagePermission)) {
			return false;
		}
		final PackagePermission requested = (PackagePermissionpermission;
		/* if requested permission has a filter, then it is an invalid argument */
		if (requested.filter != null) {
			return false;
		}
		String requestedName = requested.getName();
		final int desired = requested.action_mask;
		int effective = .;
		synchronized (this) {
			/* short circuit if the "*" Permission was added */
			if () {
				pp = pc.get("*");
				if (pp != null) {
					effective |= pp.action_mask;
					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.*
			 */
			pp = pc.get(requestedName);
			if (pp != null) {
				/* we have a direct hit! */
				effective |= pp.action_mask;
				if ((effective & desired) == desired) {
					return true;
				}
			}
			/* work our way up the tree... */
			int last;
			int offset = requestedName.length() - 1;
			while ((last = requestedName.lastIndexOf("."offset)) != -1) {
				requestedName = requestedName.substring(0, last + 1) + "*";
				pp = pc.get(requestedName);
				if (pp != null) {
					effective |= pp.action_mask;
					if ((effective & desired) == desired) {
						return true;
					}
				}
				offset = last - 1;
			}
			/*
			 * we don't have to check for "*" as it was already checked before
			 * we were called.
			 */
			if (pc == null) {
				return false;
			}
			perms = pc.values();
		}
		/* iterate one by one over filteredPermissions */
		for (PackagePermission perm : perms) {
			if (perm.implies0(requestedeffective)) {
				return true;
			}
		}
		return false;
	}

Returns an enumeration of all PackagePermission objects in the container.

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