Source for javax.management.MBeanOperationInfo

   1: /* MBeanOperationInfo.java -- Information about a bean's operations.
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.management;
  39: 
  40: import java.lang.reflect.Method;
  41: 
  42: import java.util.Arrays;
  43: 
  44: /**
  45:  * Describes the operations of a management bean.
  46:  * The information in this class is immutable as standard.
  47:  * Of course, subclasses may change this, but this
  48:  * behaviour is not recommended.
  49:  *
  50:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  51:  * @since 1.5
  52:  */
  53: public class MBeanOperationInfo
  54:   extends MBeanFeatureInfo
  55:   implements Cloneable
  56: {
  57: 
  58:   /**
  59:    * Compatible with JDK 1.5
  60:    */
  61:   private static final long serialVersionUID = -6178860474881375330L;
  62: 
  63:   /**
  64:    * Used to signify that the operation merely provides information
  65:    * (akin to an accessor).
  66:    */
  67:   public static final int INFO = 0;
  68: 
  69:   /**
  70:    * Used to signify that the operation makes some change to the
  71:    * state of the bean (akin to a mutator).
  72:    */
  73:   public static final int ACTION = 1;
  74: 
  75:   /**
  76:    * Used to signify that the operation makes some state change
  77:    * to the bean and also returns information.
  78:    */
  79:   public static final int ACTION_INFO = 2;
  80: 
  81:   /**
  82:    * Used to signify that the behaviour of the operation is
  83:    * unknown.
  84:    */
  85:   public static final int UNKNOWN = 3;
  86: 
  87:   /**
  88:    * The return type of the method, in the form of its class name.
  89:    */
  90:   private String type;
  91: 
  92:   /**
  93:    * The signature of the constructor i.e. the argument types.
  94:    */
  95:   private MBeanParameterInfo[] signature;
  96: 
  97:   /**
  98:    * The impact of the method, as one of {@link #INFO}, {@link #ACTION},
  99:    * {@link #ACTION_INFO} and {@link #UNKNOWN}.
 100:    */
 101:   private int impact;
 102: 
 103:   /**
 104:    * Constructs a @link{MBeanOperationInfo} with the specified
 105:    * description using the given method.  Each parameter is
 106:    * described merely by its type; the name and description are
 107:    * <code>null</code>.  The return type and impact of the
 108:    * method are determined from the {@link Method} instance.
 109:    *
 110:    * @param desc a description of the attribute.
 111:    * @param method the method.
 112:    */
 113:   public MBeanOperationInfo(String desc, Method method)
 114:   {
 115:     super(method.getName(), desc);
 116:     Class[] paramTypes = method.getParameterTypes();
 117:     signature = new MBeanParameterInfo[paramTypes.length];
 118:     for (int a = 0; a < paramTypes.length; ++a)
 119:       signature[a] = new MBeanParameterInfo(null,
 120:                         paramTypes[a].getName(),
 121:                         null);
 122:     type = method.getReturnType().getName();
 123:     if (method.getReturnType() == Void.TYPE)
 124:       {
 125:     if (paramTypes.length == 0)
 126:       impact = UNKNOWN;
 127:     else
 128:       impact = ACTION;
 129:       }
 130:     else
 131:       {
 132:     if (paramTypes.length == 0)
 133:       impact = INFO;
 134:     else
 135:       impact = ACTION_INFO;
 136:       }
 137:   }
 138: 
 139:   /**
 140:    * Constructs a @link{MBeanOperationInfo} with the specified name,
 141:    * description, parameter information, return type and impact. A
 142:    * <code>null</code> value for the parameter information is the same
 143:    * as passing in an empty array.
 144:    *
 145:    * @param name the name of the constructor.
 146:    * @param desc a description of the attribute.
 147:    * @param sig the signature of the method, as a series
 148:    *            of {@link MBeanParameterInfo} objects, one for
 149:    *            each parameter.
 150:    * @param type the return type of the method, as the class name.
 151:    * @param impact the impact of performing the operation.
 152:    */
 153:   public MBeanOperationInfo(String name, String desc,
 154:                 MBeanParameterInfo[] sig, String type,
 155:                 int impact)
 156:   {
 157:     super(name, desc);
 158:     if (sig == null)
 159:       signature = new MBeanParameterInfo[0];
 160:     else
 161:       signature = sig;
 162:     this.type = type;
 163:     this.impact = impact;
 164:   }
 165: 
 166:   /**
 167:    * Returns a clone of this instance.  The clone is created
 168:    * using just the method provided by {@link java.lang.Object}.
 169:    * Thus, the clone is just a shallow clone as returned by
 170:    * that method, and does not contain any deeper cloning based
 171:    * on the subject of this class.
 172:    *
 173:    * @return a clone of this instance.
 174:    * @see java.lang.Cloneable
 175:    */
 176:   public Object clone()
 177:   {
 178:     try
 179:       {
 180:     return super.clone();
 181:       }
 182:     catch (CloneNotSupportedException e)
 183:       {
 184:     /* This shouldn't happen; we implement Cloneable */
 185:     throw new IllegalStateException("clone() called on " +
 186:                     "non-cloneable object.");
 187:       }
 188:   }
 189: 
 190:   /**
 191:    * Compares this feature with the supplied object.  This returns
 192:    * true iff the object is an instance of {@link
 193:    * MBeanConstructorInfo}, {@link Object#equals()} returns true for a
 194:    * comparison of both the name and description of this notification
 195:    * with that of the specified object (performed by the superclass),
 196:    * the return type and impact are equal and the two signature arrays
 197:    * contain the same elements in the same order (but one may be
 198:    * longer than the other).
 199:    *
 200:    * @param obj the object to compare.
 201:    * @return true if the object is a {@link MBeanOperationInfo}
 202:    *         instance, 
 203:    *         <code>name.equals(object.getName())</code>,
 204:    *         <code>description.equals(object.getDescription())</code>,
 205:    *         <code>type.equals(object.getReturnType())</code>,
 206:    *         <code>impact == object.getImpact()</code>,
 207:    *         and the corresponding elements of the signature arrays are
 208:    *         equal.
 209:    */
 210:   public boolean equals(Object obj)
 211:   {
 212:     if (!(obj instanceof MBeanOperationInfo))
 213:       return false;
 214:     if (!(super.equals(obj)))
 215:       return false;
 216:     MBeanOperationInfo o = (MBeanOperationInfo) obj;
 217:     MBeanParameterInfo[] sig = o.getSignature();
 218:     for (int a = 0; a < signature.length; ++a)
 219:       {
 220:     if (a == sig.length)
 221:       return true;
 222:     if (!(signature[a].equals(sig[a])))
 223:       return false;
 224:       }
 225:     return (type.equals(o.getReturnType()) &&
 226:         impact == o.getImpact());
 227:   }
 228:   
 229:   /**
 230:    * <p>
 231:    * Returns the impact of performing this operation.
 232:    * The value is equal to one of the following:
 233:    * </p>
 234:    * <ol>
 235:    * <li>{@link #INFO} &mdash; the method just returns
 236:    * information (akin to an accessor).</li>
 237:    * <li>{@link #ACTION} &mdash; the method just alters
 238:    * the state of the bean, without returning a value
 239:    * (akin to a mutator).</li>
 240:    * <li>{@link #ACTION_INFO} &mdash; the method both makes
 241:    * state changes and returns a value.</li>
 242:    * <li>{@link #UNKNOWN} &mdash; the behaviour of the operation
 243:    * is unknown.</li>
 244:    * </ol>
 245:    *
 246:    * @return the impact of performing the operation.
 247:    */
 248:   public int getImpact()
 249:   {
 250:     return impact;
 251:   }
 252: 
 253:   /**
 254:    * Returns the return type of the operation, as the class
 255:    * name.
 256:    *
 257:    * @return the return type.
 258:    */
 259:   public String getReturnType()
 260:   {
 261:     return type;
 262:   }
 263: 
 264:   /**
 265:    * Returns the operation's signature, in the form of
 266:    * information on each parameter.  Each parameter is
 267:    * described by an instance of {@link MBeanParameterInfo}.
 268:    * The returned array is a shallow copy of the array used
 269:    * by this instance, so changing which elements are stored
 270:    * in the array won't affect the array used by this, but
 271:    * changing the actual elements will affect the ones used
 272:    * here.
 273:    *
 274:    * @return an array of {@link MBeanParameterInfo} objects,
 275:    *         describing the operation parameters.
 276:    */
 277:   public MBeanParameterInfo[] getSignature()
 278:   {
 279:     return (MBeanParameterInfo[]) signature.clone();
 280:   }
 281: 
 282:   /**
 283:    * Returns the hashcode of the operation information as the sum of
 284:    * the hashcode of the superclass, the parameter array, the return
 285:    * type and the impact factor.
 286:    *
 287:    * @return the hashcode of the operation information.
 288:    */
 289:   public int hashCode()
 290:   {
 291:     return super.hashCode() + Arrays.hashCode(signature)
 292:       + type.hashCode() + Integer.valueOf(impact).hashCode();
 293:   }
 294: 
 295:   /**
 296:    * <p>
 297:    * Returns a textual representation of this instance.  This
 298:    * is constructed using the class name
 299:    * (<code>javax.management.MBeanOperationInfo</code>),
 300:    * the name, description, return type and impact of the
 301:    * operation and the contents of the array of parameters.
 302:    * </p>
 303:    * <p>
 304:    * As instances of this class are immutable, the return value
 305:    * is computed just once for each instance and reused
 306:    * throughout its life.
 307:    * </p>
 308:    *
 309:    * @return a @link{java.lang.String} instance representing
 310:    *         the instance in textual form.
 311:    */
 312:   public String toString()
 313:   {
 314:     if (string == null)
 315:       {
 316:     String impactString;
 317:     switch (impact)
 318:       {
 319:       case INFO:
 320:         impactString = "INFO";
 321:         break;
 322:       case ACTION:
 323:         impactString = "ACTION";
 324:         break;
 325:       case ACTION_INFO:
 326:         impactString = "ACTION_INFO";
 327:         break;
 328:       case UNKNOWN:
 329:         impactString = "UNKNOWN";
 330:         break;
 331:       default:
 332:         impactString = "ERRONEOUS VALUE";
 333:       }
 334:     super.toString();
 335:     string = string.substring(0, string.length() - 1) 
 336:       + ",returnType=" + type
 337:       + ",impact=" + impactString 
 338:       + ",signature=" + Arrays.toString(signature)
 339:       + "]";
 340:       }
 341:     return string;
 342:   }
 343: 
 344: }