/* * 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.service.remoteserviceadmin; import static org.osgi.service.remoteserviceadmin.RemoteConstants.*; import java.io.IOException; import java.io.NotSerializableException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.ObjectStreamField; import java.security.Permission; import java.security.PermissionCollection; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Dictionary; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; /** * A bundle's authority to export, import or read an Endpoint. *
export
action allows a bundle to export a service as an
* Endpoint.import
action allows a bundle to import a service from
* an Endpoint.read
action allows a bundle to read references to an
* Endpoint.EndpointPermission
to read the specific service.
*
* @ThreadSafe
* @version $Revision$
*/
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 = ACTION_EXPORT
| ACTION_IMPORT
| ACTION_READ;
final static int ACTION_NONE = 0;
/**
* The actions mask.
*/
transient int action_mask;
/**
* The actions in canonical form.
*
* @serial
*/
private volatile String actions = null;
/**
* 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* 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.
*
* @param filterString The filter string or "*" to match all
* endpoints.
* @param actions The actions read
, import
, or
* export
.
* @throws IllegalArgumentException If the filter has an invalid syntax or
* the actions are not valid.
*/
public EndpointPermission(String filterString, String actions) {
this(filterString, parseActions(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.
*
* @param endpoint The requested endpoint.
* @param localFrameworkUUID The UUID of the local framework. This is used
* to support matching the
* {@link RemoteConstants#ENDPOINT_FRAMEWORK_UUID
* endpoint.framework.uuid} endpoint property to the
* <<LOCAL>>
value in the filter expression.
* @param actions The actions read
, import
, or
* export
.
* @throws IllegalArgumentException If the endpoint is null
or
* the actions are not valid.
*/
public EndpointPermission(EndpointDescription endpoint,
String localFrameworkUUID, String actions) {
super(createName(endpoint));
setTransients(null, parseActions(actions));
MapEndpointPermission
object "implies" the
* specified permission.
*
* @param p The target permission to check.
* @return true
if the specified permission is implied by this
* object; false
otherwise.
*/
public boolean implies(Permission p) {
if (!(p instanceof EndpointPermission)) {
return false;
}
EndpointPermission requested = (EndpointPermission) p;
if (endpoint != null) {
return false;
}
// if requested permission has a filter, then it is an invalid argument
if (requested.filter != null) {
return false;
}
return implies0(requested, ACTION_NONE);
}
/**
* Internal implies method. Used by the implies and the permission
* collection implies methods.
*
* @param requested The requested EndpointPermission which has already be
* validated as a proper argument. The requested EndpointPermission
* must not have a filter expression.
* @param effective The effective actions with which to start.
* @return true
if the specified permission is implied by this
* object; false
otherwise.
*/
boolean implies0(EndpointPermission requested, int effective) {
/* check actions first - much faster */
effective |= action_mask;
final int desired = requested.action_mask;
if ((effective & desired) != desired) {
return false;
}
/* if we have no filter */
Filter f = filter;
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
.
*
* @return The canonical string representation of the actions.
*/
public String getActions() {
String result = actions;
if (result == null) {
StringBuffer sb = new StringBuffer();
boolean comma = false;
int mask = action_mask;
if ((mask & ACTION_READ) == ACTION_READ) {
sb.append(READ);
comma = true;
}
if ((mask & ACTION_IMPORT) == ACTION_IMPORT) {
if (comma)
sb.append(',');
sb.append(IMPORT);
}
if ((mask & ACTION_EXPORT) == ACTION_EXPORT) {
if (comma)
sb.append(',');
sb.append(EXPORT);
}
actions = result = sb.toString();
}
return result;
}
/**
* Returns a new PermissionCollection
object for storing
* EndpointPermission
objects.
*
* @return A new
PermissionCollection
object suitable for
* storing EndpointPermission
objects.
*/
public PermissionCollection newPermissionCollection() {
return new EndpointPermissionCollection();
}
/**
* Determines the equality of two EndpointPermission objects.
*
* Checks that specified object has the same name, actions and endpoint as
* this EndpointPermission
.
*
* @param obj The object to test for equality.
* @return 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;
}
EndpointPermission ep = (EndpointPermission) obj;
return (action_mask == ep.action_mask)
&& getName().equals(ep.getName())
&& ((endpoint == ep.endpoint) || ((endpoint != null)
&& (ep.endpoint != null) && endpoint
.equals(ep.endpoint)));
}
/**
* Returns the hash code value for this object.
*
* @return Hash code value for this object.
*/
public int hashCode() {
int h = 31 * 17 + getName().hashCode();
h = 31 * h + getActions().hashCode();
if (endpoint != null) {
h = 31 * h + endpoint.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 s)
throws IOException {
if (endpoint != 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 (actions == null) {
getActions();
}
s.defaultWriteObject();
}
/**
* readObject is called to restore the state of this permission from a
* stream.
*/
private synchronized void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException {
// Read in the action, then initialize the rest
s.defaultReadObject();
setTransients(parseFilter(getName()), parseActions(actions));
}
/**
* Called by <@link EndpointPermission#implies(Permission)>
.
*
* @return a dictionary of properties for this permission.
*/
private DictionaryEndpointPermissionCollection
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 = (EndpointPermission) permission;
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 */
Mappermission
.
*
* @param permission The Permission object to compare.
* @return 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 = (EndpointPermission) permission;
/* if requested permission has a filter, then it is an invalid argument */
if (requested.filter != null) {
return false;
}
int effective = EndpointPermission.ACTION_NONE;
CollectionEndpointPermission
objects
* in the container.
*
* @return Enumeration of all the EndpointPermission objects.
*/
public synchronized Enumeration