Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2004, 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.service.cm;
 
 import java.util.List;
 import java.util.Map;
 
Indicates a bundle's authority to configure bundles or be updated by Configuration Admin.

Version:
$Id: fd5efb5f4db6eb95e02296e4afe86a9698936edd $
Since:
1.2
ThreadSafe:
 
 
 public final class ConfigurationPermission extends BasicPermission {
 	static final long				serialVersionUID	= 5716868734811965383L;
Provides permission to create new configurations for other bundles as well as manipulate them. The action string CONFIGURE.
 
 	public final static String		CONFIGURE"configure";

The permission to be updated, that is, act as a Managed Service or Managed Service Factory. The action string TARGET.

Since:
1.4
 
 	public final static String		TARGET"target";
 
 	private final static int		ACTION_CONFIGURE	= 0x00000001;
 	private final static int		ACTION_TARGET		= 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;

Parsed name if it includes wildcards: "*"
 
 	private transient List<String>	substrings;

Create a new ConfigurationPermission.

Parameters:
name Name of the permission. Wildcards ('*') are allowed in the name. During implies(java.security.Permission), the name is matched to the requested permission using the substring matching rules used by org.osgi.framework.Filters.
actions Comma separated list of CONFIGURE, TARGET (case insensitive).
 
 
 	public ConfigurationPermission(String nameString actions) {
 		this(nameparseActions(actions));
 	}

Package private constructor used by ConfigurationPermissionCollection.

Parameters:
name location string
mask action mask
	ConfigurationPermission(String nameint mask) {
		super(name);
	}

Called by constructors and when deserialized.

Parameters:
mask action mask
	private void setTransients(int 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] == 't' || a[i - 5] == 'T')
					&& (a[i - 4] == 'a' || a[i - 4] == 'A')
					&& (a[i - 3] == 'r' || a[i - 3] == 'R')
					&& (a[i - 2] == 'g' || a[i - 2] == 'G')
					&& (a[i - 1] == 'e' || a[i - 1] == 'E')
					&& (a[i] == 't' || a[i] == 'T')) {
				matchlen = 6;
				mask |= ;
			}
			else
				if (i >= 8 && (a[i - 8] == 'c' || a[i - 8] == 'C')
						&& (a[i - 7] == 'o' || a[i - 7] == 'O')
						&& (a[i - 6] == 'n' || a[i - 6] == 'N')
						&& (a[i - 5] == 'f' || a[i - 5] == 'F')
						&& (a[i - 4] == 'i' || a[i - 4] == 'I')
						&& (a[i - 3] == 'g' || a[i - 3] == 'G')
						&& (a[i - 2] == 'u' || a[i - 2] == 'U')
						&& (a[i - 1] == 'r' || a[i - 1] == 'R')
						&& (a[i] == 'e' || a[i] == 'E')) {
					matchlen = 9;
				}
				else {
					// parse error
					throw new IllegalArgumentException("invalid actions: "
actions);
				}
			// make sure we didn't just match the tail of a word
			// like "ackbarftarget". 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 the name for wildcard processing.

Parameters:
name The name of the permission.
Returns:
null is the name has no wildcards or a List<String> where element is a substring to match or null for '*'.
	private static List<StringparseSubstring(String name) {
		if (name.indexOf('*') < 0) {
			return null;
		}
		char[] chars = name.toCharArray();
		StringBuffer sb = new StringBuffer(chars.length);
		List<Stringsub = new ArrayList<String>(10);
		for (int pos = 0; pos < chars.lengthpos++) {
			char c = chars[pos];
			switch (c) {
				case '*' : {
					if (sb.length() > 0) {
						sub.add(sb.toString());
					}
					sb.setLength(0);
					sub.add(null);
					break;
				}
				case '\\' : {
					pos++;
					if (pos < chars.length) {
						c = chars[pos];
					}
					/* fall through into default */
				}
				default : {
					sb.append(c);
					break;
				}
			}
		}
		if (sb.length() > 0) {
			sub.add(sb.toString());
		}
		int size = sub.size();
		if (size == 0) {
			return null;
		}
		if (size == 1) {
			if (sub.get(0) != null) {
				return null;
			}
		}
		return sub;
	}

Determines if a ConfigurationPermission 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 ConfigurationPermission)) {
			return false;
		}
		return implies0(requested);
	}

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

Parameters:
requested The requested ConfigurationPermission which has already be validated as a proper argument.
effective The effective actions with which to start.
Returns:
true if the specified permission is implied by this object; false otherwise.
	boolean implies0(ConfigurationPermission requestedint effective) {
		/* check actions first - much faster */
		effective |= ;
		final int desired = requested.action_mask;
		if ((effective & desired) != desired) {
			return false;
		}
		String requestedName = requested.getName();
		if ( == null) {
			return getName().equals(requestedName);
		}
		for (int i = 0, pos = 0, size = .size(); i < sizei++) {
			String substr = .get(i);
			if (i + 1 < size/* if this is not that last substr */{
				if (substr == null/* * */{
					String substr2 = .get(i + 1);
					if (substr2 == null/* ** */
						continue/* ignore first star */
					/* xxx */
					int index = requestedName.indexOf(substr2pos);
					if (index == -1) {
						return false;
					}
					pos = index + substr2.length();
					if (i + 2 < size// if there are more
						// substrings, increment
						// over the string we just
						// matched; otherwise need
						// to do the last substr
						// check
						i++;
				}
				else /* xxx */{
					int len = substr.length();
					if (requestedName.regionMatches(possubstr, 0, len)) {
						pos += len;
					}
					else {
						return false;
					}
				}
			}
			else /* last substr */{
				if (substr == null/* * */{
					return true;
				}
				/* xxx */
				return requestedName.endsWith(substr);
			}
		}
		return false;
	}

Determines the equality of two ConfigurationPermission objects.

Two ConfigurationPermission objects are equal.

Parameters:
obj The object being compared for equality with this object.
Returns:
true if obj is equivalent to this ConfigurationPermission; false otherwise.
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof ConfigurationPermission)) {
			return false;
		}
		return ( == cp.action_mask)
				&& getName().equals(cp.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 the canonical string representation of the ConfigurationPermission actions.

Always returns present ConfigurationPermission actions in the following order: CONFIGURE, TARGET

Returns:
Canonical string representation of the ConfigurationPermission 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 ConfigurationPermissions.

Returns:
A new PermissionCollection object.
	}

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 {
		// 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 data, then initialize the transients
	}
Stores a set of ConfigurationPermission permissions.

	static final long								serialVersionUID	= -6917638867081695839L;
Collection of permissions.

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

Serial:
GuardedBy:
this
	private boolean									all_allowed;

Creates an empty ConfigurationPermissionCollection object.
		 = false;
	}

Adds the specified permission to the ConfigurationPermissionCollection. The key for the hash is the interface name of the service.

Parameters:
permission The Permission object to add.
Throws:
java.lang.IllegalArgumentException If the permission is not an ConfigurationPermission.
java.lang.SecurityException If this ConfigurationPermissionCollection object has been marked read-only.
	public void add(Permission permission) {
		if (!(permission instanceof ConfigurationPermission)) {
			throw new IllegalArgumentException("invalid permission: "
permission);
		}
		if (isReadOnly())
			throw new SecurityException("attempt to add a Permission to a "
"readonly PermissionCollection");
		final String name = cp.getName();
		synchronized (this) {
			final ConfigurationPermission existing = pc.get(name);
			if (existing != null) {
				final int oldMask = existing.action_mask;
				final int newMask = cp.action_mask;
				if (oldMask != newMask) {
					pc.put(namenew ConfigurationPermission(nameoldMask
newMask));
				}
			}
			else {
				pc.put(namecp);
			}
			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 ConfigurationPermission object.
Returns:
true if permission is a proper subset of a permission in the set; false otherwise.
	public boolean implies(Permission permission) {
		if (!(permission instanceof ConfigurationPermission)) {
			return false;
		}
		final ConfigurationPermission requested = (ConfigurationPermissionpermission;
		synchronized (this) {
			/* short circuit if the "*" Permission was added */
			if () {
				if (cp != null) {
					effective |= cp.action_mask;
					final int desired = requested.action_mask;
					if ((effective & desired) == desired) {
						return true;
					}
				}
			}
			perms = pc.values();
		}
		/* iterate one by one over permissions */
		for (ConfigurationPermission perm : perms) {
			if (perm.implies0(requestedeffective)) {
				return true;
			}
		}
		return false;
	}

Returns an enumeration of all ConfigurationPermission objects in the container.

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