Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2000, 2013). 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.remoteserviceadmin;
 
 import static org.osgi.service.remoteserviceadmin.RemoteConstants.*;
 import java.util.List;
 import java.util.Map;
A bundle's authority to export, import or read an Endpoint.
  • The export action allows a bundle to export a service as an Endpoint.
  • The import action allows a bundle to import a service from an Endpoint.
  • The read action allows a bundle to read references to an Endpoint.
Permission to read an Endpoint is required in order to detect events regarding an Endpoint. Untrusted bundles should not be able to detect the presence of certain Endpoints unless they have the appropriate EndpointPermission to read the specific service.

Author(s):
$Id: 60ecb3f4ed224d9dc02c95ee9ce4e4786b4ff126 $
ThreadSafe:
 
 
 public final class EndpointPermission extends Permission {
 	static final long									serialVersionUID	= -7662148639076511574L;
The action string read.
 
 	public final static String							READ"read";
The action string import. The import action implies the read action.
 
 	public final static String							IMPORT"import";
The action string export. The export action implies the read action.
 
 	public final static String							EXPORT"export";
 
 	private final static int							ACTION_READ			= 0x00000001;
 	private final static int							ACTION_IMPORT		= 0x00000002;
 	private final static int							ACTION_EXPORT		= 0x00000004;
 	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 endpoint used by this EndpointPermission. Must be null if not constructed with a endpoint.
 
 	transient final EndpointDescription					endpoint;

This dictionary holds the properties of the permission, used to match a filter in implies.
	private transient final Dictionary<StringObject>	properties;

If this EndpointPermission was not constructed with an EndpointDescription, this holds a Filter matching object used to evaluate the filter in implies or null for wildcard.
	transient Filter									filter;

Create a new EndpointPermission with the specified filter.

The filter will be evaluated against the endpoint properties of a requested EndpointPermission.

There are three possible actions: read, import and export. The read action allows the owner of this permission to see the presence of distributed services. The import action allows the owner of this permission to import an endpoint. The export action allows the owner of this permission to export a service.

Parameters:
filterString The filter string or "*" to match all endpoints.
actions The actions read, import, or export.
Throws:
java.lang.IllegalArgumentException If the filter has an invalid syntax or the actions are not valid.
	public EndpointPermission(String filterStringString actions) {
		this(filterStringparseActions(actions));
	}

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

Parameters:
endpoint The requested endpoint.
localFrameworkUUID The UUID of the local framework. This is used to support matching the endpoint.framework.uuid endpoint property to the <<LOCAL>> value in the filter expression.
actions The actions read, import, or export.
Throws:
java.lang.IllegalArgumentException If the endpoint is null or the actions are not valid.
	public EndpointPermission(EndpointDescription endpointString localFrameworkUUIDString actions) {
		super(createName(endpoint));
		setTransients(nullparseActions(actions));
		Map<StringObjectprops;
		if ((localFrameworkUUID != null) && localFrameworkUUID.equals(endpoint.getFrameworkUUID())) {
			props.putAll(endpoint.getProperties());
			props.put(new String[] {endpoint.getFrameworkUUID(), "<<LOCAL>>"});
else {
			props = endpoint.getProperties();
		}
		this. = endpoint;
	}

Create a permission name from a EndpointDescription.

Parameters:
endpoint EndpointDescription to use to create permission name.
Returns:
permission name.
	private static String createName(EndpointDescription endpoint) {
		if (endpoint == null) {
			throw new IllegalArgumentException("invalid endpoint: null");
		}
		StringBuffer sb = new StringBuffer("(" +  + "=");
		sb.append(endpoint.getId());
		sb.append(")");
		return sb.toString();
	}

Package private constructor used by EndpointPermissionCollection.

Parameters:
name class name
mask action mask
	EndpointPermission(String nameint mask) {
		super(name);
		this. = null;
		this. = null;
	}

Called by constructors and when deserialized.

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

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

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) {
		if (filterString == null) {
			throw new IllegalArgumentException("invalid filter: null");
		}
		filterString = filterString.trim();
		if (filterString.equals("*")) {
			return null// wildcard
		}
		try {
			return FrameworkUtil.createFilter(filterString);
catch (InvalidSyntaxException e) {
			IllegalArgumentException iae = new IllegalArgumentException("invalid filter");
			iae.initCause(e);
			throw iae;
		}
	}

Determines if a EndpointPermission object "implies" the specified permission.

Parameters:
p The target permission to check.
Returns:
true if the specified permission is implied by this object; false otherwise.
	public boolean implies(Permission p) {
		if (!(p instanceof EndpointPermission)) {
			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 EndpointPermission which has already be validated as a proper argument. The requested EndpointPermission 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(EndpointPermission requestedint effective) {
		/* check actions first - much faster */
		effective |= ;
		final int desired = requested.action_mask;
		if ((effective & desired) != desired) {
			return false;
		}
		/* if we have no filter */
		Filter f = ;
		if (f == null) {
			// it's "*"
			return true;
		}
		return f.matchCase(requested.getProperties());
	}

Returns the canonical string representation of the actions. Always returns present actions in the following canonical order: read, import, export.

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 = ;
			if ((mask & ) == ) {
				comma = true;
			}
			if ((mask & ) == ) {
				if (comma)
					sb.append(',');
			}
			if ((mask & ) == ) {
				if (comma)
					sb.append(',');
			}
			 = result = sb.toString();
		}
		return result;
	}

Returns a new PermissionCollection object for storing EndpointPermission objects.

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

Determines the equality of two EndpointPermission objects. Checks that specified object has the same name, actions and endpoint as this EndpointPermission.

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

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();
		if ( != null) {
			h = 31 * h + .hashCode();
		}
		return h;
	}

WriteObject is called to save the state of this permission to a stream. The actions are serialized, and the superclass takes care of the name.
	private synchronized void writeObject(java.io.ObjectOutputStream sthrows 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 sthrows IOExceptionClassNotFoundException {
		// Read in the action, then initialize the rest
	}

Called by implies(java.security.Permission).

Returns:
a dictionary of properties for this permission.
		return ;
	}
Stores a set of EndpointPermission permissions.

	static final long						serialVersionUID	= 662615640374640621L;
Table of permissions.

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

Serial:
GuardedBy:
this
	private boolean							all_allowed;

Creates an empty EndpointPermissions object.
		 = false;
	}

Adds a permission to this permission collection.

Parameters:
permission The Permission object to add.
Throws:
java.lang.IllegalArgumentException If the specified permission is not a EndpointPermission object.
java.lang.SecurityException If this EndpointPermissionCollection object has been marked read-only.
	public void add(final Permission permission) {
		if (!(permission instanceof EndpointPermission)) {
			throw new IllegalArgumentException("invalid permission: " + permission);
		}
		if (isReadOnly()) {
			throw new SecurityException("attempt to add a Permission to a " + "readonly PermissionCollection");
		}
		final EndpointPermission ep = (EndpointPermissionpermission;
		if (ep.endpoint != null) {
			throw new IllegalArgumentException("cannot add to collection: " + ep);
		}
		final String name = ep.getName();
		synchronized (this) {
			/* select the bucket for the permission */
			final EndpointPermission existing = pc.get(name);
			if (existing != null) {
				final int oldMask = existing.action_mask;
				final int newMask = ep.action_mask;
				if (oldMask != newMask) {
					pc.put(namenew EndpointPermission(nameoldMask | newMask));
				}
else {
				pc.put(nameep);
			}
			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(final Permission permission) {
		if (!(permission instanceof EndpointPermission)) {
			return false;
		}
		final EndpointPermission requested = (EndpointPermissionpermission;
		/* if requested permission has a filter, then it is an invalid argument */
		if (requested.filter != null) {
			return false;
		}
		int effective = .;
		synchronized (this) {
			final int desired = requested.action_mask;
			/* short circuit if the "*" Permission was added */
			if () {
				if (ep != null) {
					effective |= ep.action_mask;
					if ((effective & desired) == desired) {
						return true;
					}
				}
			}
			perms = .values();
		}
		/* iterate one by one over permissions */
		for (Iterator<EndpointPermissioniter = perms.iterator(); iter.hasNext();) {
			if (iter.next().implies0(requestedeffective)) {
				return true;
			}
		}
		return false;
	}

Returns an enumeration of all the EndpointPermission objects in the container.

Returns:
Enumeration of all the EndpointPermission 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 outthrows IOException {
		pfields.put("permissions");
		pfields.put("all_allowed");
	}
	private synchronized void readObject(java.io.ObjectInputStream inthrows IOExceptionClassNotFoundException {
		 = (HashMap<StringEndpointPermission>) gfields.get("permissions"new HashMap<StringEndpointPermission>());
		 = gfields.get("all_allowed"false);
	}
New to GrepCode? Check out our FAQ X