Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2005, 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.monitor;
 
Indicates the callers authority to publish, read or reset StatusVariables, to switch event sending on or off or to start monitoring jobs. The target of the permission is the identifier of the StatusVariable, the action can be read, publish, reset, startjob, switchevents, or the combination of these separated by commas. Action names are interpreted case-insensitively, but the canonical action string returned by getActions() uses the forms defined by the action constants.

If the wildcard * appears in the actions field, all legal monitoring commands are allowed on the designated target(s) by the owner of the permission.

Version:
$Id: 5080b409276c750ce22c00aa1749f97cf9fb1fa7 $
 
 public class MonitorPermission extends Permission {

    
 
 	private static final long	serialVersionUID	= -9084425194463274314L;

Holders of MonitorPermission with the read action present are allowed to read the value of the StatusVariables specified in the permission's target field.
 
     public static final String READ = "read";

    
Holders of MonitorPermission with the reset action present are allowed to reset the value of the StatusVariables specified in the permission's target field.
 
     public static final String RESET = "reset";

    
Holders of MonitorPermission with the publish action present are Monitorable services that are allowed to publish the StatusVariables specified in the permission's target field. Note, that this permission cannot be enforced when a Monitorable registers to the framework, because the Service Registry does not know about this permission. Instead, any StatusVariables published by a Monitorable without the corresponding publish permission are silently ignored by MonitorAdmin, and are therefore invisible to the users of the monitoring service.
 
     public static final String PUBLISH = "publish";

    
Holders of MonitorPermission with the startjob action present are allowed to initiate monitoring jobs involving the StatusVariables specified in the permission's target field.

A minimal sampling interval can be optionally defined in the following form: startjob:n. This allows the holder of the permission to initiate time based jobs with a measurement interval of at least n seconds. If n is not specified or 0 then the holder of this permission is allowed to start monitoring jobs specifying any frequency.

 
     public static final String STARTJOB = "startjob";

    
Holders of MonitorPermission with the switchevents action present are allowed to switch event sending on or off for the value of the StatusVariables specified in the permission's target field.
 
     public static final String SWITCHEVENTS = "switchevents";
 
     private static final int READ_FLAG         = 0x1;
     private static final int RESET_FLAG        = 0x2;
     private static final int PUBLISH_FLAG      = 0x4;
     private static final int STARTJOB_FLAG     = 0x8;
    private static final int SWITCHEVENTS_FLAG = 0x10;
    
    private static final int ALL_FLAGS =  |  | 
    private String monId;
    private String varId;
    private boolean prefixMonId;
    private boolean prefixVarId;
    private int mask;
    private int minJobInterval;

    
Create a MonitorPermission object, specifying the target and actions.

The statusVariable parameter is the target of the permission, defining one or more status variable names to which the specified actions apply. Multiple status variable names can be selected by using the wildcard * in the target string. The wildcard is allowed in both fragments, but only at the end of the fragments.

For example, the following targets are valid: com.mycomp.myapp/queue_length, com.mycomp.myapp/*, com.mycomp.*/*, */*, */queue_length, */queue*.

The following targets are invalid: *.myapp/queue_length, com.*.myapp/*, *.

The actions parameter specifies the allowed action(s): read, publish, startjob, reset, switchevents, or the combination of these separated by commas. String constants are defined in this class for each valid action. Passing "*" as the action string is equivalent to listing all actions.

Parameters:
statusVariable the identifier of the StatusVariable in [Monitorable_id]/[StatusVariable_id] format
actions the list of allowed actions separated by commas, or * for all actions
Throws:
java.lang.IllegalArgumentException if either parameter is null, or invalid with regard to the constraints defined above and in the documentation of the used actions
    public MonitorPermission(String statusVariableString actions
            throws IllegalArgumentException {
        super(statusVariable);
        if(statusVariable == null)
            throw new IllegalArgumentException(
                    "Invalid StatusVariable path 'null'.");
        
        if(actions == null)
            throw new IllegalArgumentException(
                    "Invalid actions string 'null'.");
        
        int sep = statusVariable.indexOf('/');
        int len = statusVariable.length();
        if (sep == -1)
            throw new IllegalArgumentException(
                    "Invalid StatusVariable path: should contain '/' separator.");
        if (sep == 0 || sep == statusVariable.length() - 1)
            throw new IllegalArgumentException(
                    "Invalid StatusVariable path: empty monitorable ID or StatusVariable name.");
         = statusVariable.charAt(sep - 1) == '*';
         = statusVariable.charAt(len - 1) == '*';
        
         = statusVariable.substring(0,  ? sep - 1 : sep);
         = statusVariable.substring(sep + 1,  ? len - 1 : len);
        checkId("Monitorable ID part of the target");
        checkId("Status Variable ID part of the target");
         = 0;
        if(actions.equals("*"))
             = ;
        else {
             = 0;
            StringTokenizer st = new StringTokenizer(actions",");
            while (st.hasMoreTokens()) {
                String action = st.nextToken().trim();
                if (action.equalsIgnoreCase()) {
                    addToMask();
                } else if (action.equalsIgnoreCase()) {
                    addToMask();
                } else if (action.equalsIgnoreCase()) {
                    addToMask();
                } else if (action.equalsIgnoreCase()) {
                    addToMask();
                } else if (action.toLowerCase().startsWith()) {
                     = 0;
    
                    int slen = .length();
                    if (action.length() != slen) {
                        if (action.charAt(slen) != ':')
                            throw new IllegalArgumentException(
                                    "Invalid action '" + action + "'.");
    
                        try {
                             = Integer.parseInt(action
                                    .substring(slen + 1));
                        } catch (NumberFormatException e) {
                            throw new IllegalArgumentException(
                                    "Invalid parameter in startjob action '"
                                            + action + "'.");
                        }
                    }
                    addToMask();
                } else
                    throw new IllegalArgumentException("Invalid action '" + 
                            action + "'");
            }
        }
    }
    
    private void addToMask(int actionString actionString) {
        if(( & action) != 0)
            throw new IllegalArgumentException("Invalid action string: " + 
                    actionString + " appears multiple times.");
        
         |= action;
    }
    private void checkId(String idString idName)
            throws IllegalArgumentException {
        
        byte[] nameBytes;
        try {
            nameBytes = id.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // never happens, "UTF-8" must always be supported
            throw new IllegalStateException(e.getMessage());
        }
        if(nameBytes.length > .)
            throw new IllegalArgumentException(idName + " is too long (over " +
                    . + " bytes in UTF-8 encoding).");
        
        if (id.equals(".") || id.equals(".."))
            throw new IllegalArgumentException(idName + " is invalid.");
        
        char[] chars = id.toCharArray();
        for (int i = 0; i < chars.lengthi++)
            if (..indexOf(chars[i]) == -1)
                throw new IllegalArgumentException(idName +
                        " contains invalid characters.");
    }
    
    
Create an integer hash of the object. The hash codes of MonitorPermissions p1 and p2 are the same if p1.equals(p2).

Returns:
the hash of the object
    public int hashCode() {
        return new Integer().hashCode()
                ^ new Integer().hashCode() ^ .hashCode()
                ^ new Boolean().hashCode()
                ^ .hashCode()
                ^ new Boolean().hashCode();
    }

    
Determines the equality of two MonitorPermission objects. Two MonitorPermission objects are equal if their target strings are equal and the same set of actions are listed in their action strings.

Parameters:
o the object being compared for equality with this object
Returns:
true if the two permissions are equal
    public boolean equals(Object o) {
        if (!(o instanceof MonitorPermission))
            return false;
        MonitorPermission other = (MonitorPermissiono;
        return  == other.mask &&  == other.minJobInterval
                && .equals(other.monId)
                &&  == other.prefixMonId
                && .equals(other.varId)
                &&  == other.prefixVarId;
    }

    
Get the action string associated with this permission. The actions are returned in the following order: read, reset, publish, startjob, switchevents.

Returns:
the allowed actions separated by commas, cannot be null
    public String getActions() {
        StringBuffer sb = new StringBuffer();
        appendAction(sb,         );
        appendAction(sb,        );
        appendAction(sb,      );
        appendAction(sb,     );
        return sb.toString();
    }
    private void appendAction(StringBuffer sbint flagString actionName) {
        if (( & flag) != 0) {
            if(sb.length() != 0)
                sb.append(',');
            sb.append(actionName);
            
            if(flag ==  &&  != 0)
                sb.append(':').append();
        }
    }

    
Determines if the specified permission is implied by this permission.

This method returns false if and only if at least one of the following conditions are fulfilled for the specified permission:

  • it is not a MonitorPermission
  • it has a broader set of actions allowed than this one
  • it allows initiating time based monitoring jobs with a lower minimal sampling interval
  • the target set of Monitorables is not the same nor a subset of the target set of Monitorables of this permission
  • the target set of StatusVariables is not the same nor a subset of the target set of StatusVariables of this permission

Parameters:
p the permission to be checked
Returns:
true if the given permission is implied by this permission
    public boolean implies(Permission p) {
        if (!(p instanceof MonitorPermission))
            return false;
        MonitorPermission other = (MonitorPermissionp;
        if (( & other.mask) != other.mask)
            return false;
        if ((other.mask & ) != 0
                &&  > other.minJobInterval)
            return false;
        return implies(other.monIdother.prefixMonId)
                && implies(other.varIdother.prefixVarId);
    }
    private boolean implies(String idboolean prefixString oid,
            boolean oprefix) {
        return prefix ? oid.startsWith(id) : !oprefix && id.equals(oid);
    }
New to GrepCode? Check out our FAQ X