Save This Page
Home » openjdk-7 » javax » management » modelmbean » [javadoc | source]
    1   /*
    2    * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   /*
   26    * @author    IBM Corp.
   27    *
   28    * Copyright IBM Corp. 1999-2000.  All rights reserved.
   29    */
   30   
   31   package javax.management.modelmbean;
   32   
   33   import static com.sun.jmx.defaults.JmxProperties.MODELMBEAN_LOGGER;
   34   import com.sun.jmx.mbeanserver.GetPropertyAction;
   35   
   36   import java.io.IOException;
   37   import java.io.ObjectInputStream;
   38   import java.io.ObjectOutputStream;
   39   import java.io.ObjectStreamField;
   40   import java.lang.reflect.Method;
   41   import java.security.AccessController;
   42   import java.util.logging.Level;
   43   
   44   import javax.management.Descriptor;
   45   import javax.management.DescriptorAccess;
   46   import javax.management.DescriptorKey;
   47   import javax.management.MBeanOperationInfo;
   48   import javax.management.MBeanParameterInfo;
   49   import javax.management.RuntimeOperationsException;
   50   
   51   /**
   52    * The ModelMBeanOperationInfo object describes a management operation of the ModelMBean.
   53    * It is a subclass of MBeanOperationInfo with the addition of an associated Descriptor
   54    * and an implementation of the DescriptorAccess interface.
   55    * <P>
   56    * <PRE>
   57    * The fields in the descriptor are defined, but not limited to, the following:
   58    * name           : operation name
   59    * descriptorType : must be "operation"
   60    * class          : class where method is defined (fully qualified)
   61    * role           : must be "operation", "getter", or "setter
   62    * targetObject   : object on which to execute this method
   63    * targetType     : type of object reference for targetObject. Can be:
   64    *                  ObjectReference | Handle | EJBHandle | IOR | RMIReference.
   65    * value          : cached value for operation
   66    * currencyTimeLimit : how long cached value is valid
   67    * lastUpdatedTimeStamp : when cached value was set
   68    * visibility            : 1-4 where 1: always visible 4: rarely visible
   69    * presentationString :  xml formatted string to describe how to present operation
   70    * </PRE>
   71    * The default descriptor will have name, descriptorType, displayName and role fields set.
   72    * The default value of the name and displayName fields is the operation name.
   73    *
   74    * <p><b>Note:</b> because of inconsistencies in previous versions of
   75    * this specification, it is recommended not to use negative or zero
   76    * values for <code>currencyTimeLimit</code>.  To indicate that a
   77    * cached value is never valid, omit the
   78    * <code>currencyTimeLimit</code> field.  To indicate that it is
   79    * always valid, use a very large number for this field.</p>
   80    *
   81    * <p>The <b>serialVersionUID</b> of this class is <code>6532732096650090465L</code>.
   82    *
   83    * @since 1.5
   84    */
   85   
   86   @SuppressWarnings("serial")  // serialVersionUID is not constant
   87   public class ModelMBeanOperationInfo extends MBeanOperationInfo
   88            implements DescriptorAccess
   89   {
   90   
   91       // Serialization compatibility stuff:
   92       // Two serial forms are supported in this class. The selected form depends
   93       // on system property "jmx.serial.form":
   94       //  - "1.0" for JMX 1.0
   95       //  - any other value for JMX 1.1 and higher
   96       //
   97       // Serial version for old serial form
   98       private static final long oldSerialVersionUID = 9087646304346171239L;
   99       //
  100       // Serial version for new serial form
  101       private static final long newSerialVersionUID = 6532732096650090465L;
  102       //
  103       // Serializable fields in old serial form
  104       private static final ObjectStreamField[] oldSerialPersistentFields =
  105       {
  106         new ObjectStreamField("operationDescriptor", Descriptor.class),
  107         new ObjectStreamField("currClass", String.class)
  108       };
  109       //
  110       // Serializable fields in new serial form
  111       private static final ObjectStreamField[] newSerialPersistentFields =
  112       {
  113         new ObjectStreamField("operationDescriptor", Descriptor.class)
  114       };
  115       //
  116       // Actual serial version and serial form
  117       private static final long serialVersionUID;
  118       /**
  119        * @serialField operationDescriptor Descriptor The descriptor
  120        * containing the appropriate metadata for this instance
  121        */
  122       private static final ObjectStreamField[] serialPersistentFields;
  123       private static boolean compat = false;
  124       static {
  125           try {
  126               GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
  127               String form = AccessController.doPrivileged(act);
  128               compat = (form != null && form.equals("1.0"));
  129           } catch (Exception e) {
  130               // OK: No compat with 1.0
  131           }
  132           if (compat) {
  133               serialPersistentFields = oldSerialPersistentFields;
  134               serialVersionUID = oldSerialVersionUID;
  135           } else {
  136               serialPersistentFields = newSerialPersistentFields;
  137               serialVersionUID = newSerialVersionUID;
  138           }
  139       }
  140       //
  141       // END Serialization compatibility stuff
  142   
  143           /**
  144            * @serial The descriptor containing the appropriate metadata for this instance
  145            */
  146           private Descriptor operationDescriptor = validDescriptor(null);
  147   
  148           private static final String currClass = "ModelMBeanOperationInfo";
  149   
  150           /**
  151            * Constructs a ModelMBeanOperationInfo object with a default
  152            * descriptor. The {@link Descriptor} of the constructed
  153            * object will include fields contributed by any annotations
  154            * on the {@code Method} object that contain the {@link
  155            * DescriptorKey} meta-annotation.
  156            *
  157            * @param operationMethod The java.lang.reflect.Method object
  158            * describing the MBean operation.
  159            * @param description A human readable description of the operation.
  160            */
  161   
  162           public ModelMBeanOperationInfo(String description,
  163                                          Method operationMethod)
  164           {
  165                   super(description, operationMethod);
  166                   // create default descriptor
  167                   if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  168                       MODELMBEAN_LOGGER.logp(Level.FINER,
  169                               ModelMBeanOperationInfo.class.getName(),
  170                               "ModelMBeanOperationInfo(String,Method)",
  171                               "Entry");
  172                   }
  173                   operationDescriptor = validDescriptor(null);
  174           }
  175   
  176           /**
  177            * Constructs a ModelMBeanOperationInfo object. The {@link
  178            * Descriptor} of the constructed object will include fields
  179            * contributed by any annotations on the {@code Method} object
  180            * that contain the {@link DescriptorKey} meta-annotation.
  181            *
  182            * @param operationMethod The java.lang.reflect.Method object
  183            * describing the MBean operation.
  184            * @param description A human readable description of the
  185            * operation.
  186            * @param descriptor An instance of Descriptor containing the
  187            * appropriate metadata for this instance of the
  188            * ModelMBeanOperationInfo.  If it is null a default
  189            * descriptor will be created. If the descriptor does not
  190            * contain all the fields "name", "descriptorType",
  191            * "displayName", and "role", the missing ones are added with
  192            * their default values.
  193            *
  194            * @exception RuntimeOperationsException Wraps an
  195            * IllegalArgumentException. The descriptor is invalid; or
  196            * descriptor field "name" is present but not equal to
  197            * operation name; or descriptor field "DescriptorType" is
  198            * present but not equal to "operation"; or descriptor
  199            * optional field "role" is not equal to "operation",
  200            * "getter", or "setter".
  201            *
  202            */
  203   
  204           public ModelMBeanOperationInfo(String description,
  205                                          Method operationMethod,
  206                                          Descriptor descriptor)
  207           {
  208   
  209                   super(description, operationMethod);
  210                   if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  211                       MODELMBEAN_LOGGER.logp(Level.FINER,
  212                               ModelMBeanOperationInfo.class.getName(),
  213                               "ModelMBeanOperationInfo(String,Method,Descriptor)",
  214                               "Entry");
  215                   }
  216                   operationDescriptor = validDescriptor(descriptor);
  217           }
  218   
  219           /**
  220           * Constructs a ModelMBeanOperationInfo object with a default descriptor.
  221           *
  222           * @param name The name of the method.
  223           * @param description A human readable description of the operation.
  224           * @param signature MBeanParameterInfo objects describing the
  225           * parameters(arguments) of the method.
  226           * @param type The type of the method's return value.
  227           * @param impact The impact of the method, one of INFO, ACTION,
  228           * ACTION_INFO, UNKNOWN.
  229           */
  230   
  231           public ModelMBeanOperationInfo(String name,
  232                                          String description,
  233                                          MBeanParameterInfo[] signature,
  234                                          String type,
  235                                          int impact)
  236           {
  237   
  238                   super(name, description, signature, type, impact);
  239                   // create default descriptor
  240                   if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  241                       MODELMBEAN_LOGGER.logp(Level.FINER,
  242                               ModelMBeanOperationInfo.class.getName(),
  243                               "ModelMBeanOperationInfo(" +
  244                               "String,String,MBeanParameterInfo[],String,int)",
  245                               "Entry");
  246                   }
  247                   operationDescriptor = validDescriptor(null);
  248           }
  249   
  250           /**
  251           * Constructs a ModelMBeanOperationInfo object.
  252           *
  253           * @param name The name of the method.
  254           * @param description A human readable description of the operation.
  255           * @param signature MBeanParameterInfo objects describing the
  256           * parameters(arguments) of the method.
  257           * @param type The type of the method's return value.
  258           * @param impact The impact of the method, one of INFO, ACTION,
  259           * ACTION_INFO, UNKNOWN.
  260           * @param descriptor An instance of Descriptor containing the
  261           * appropriate metadata for this instance of the
  262           * MBeanOperationInfo. If it is null then a default descriptor
  263           * will be created.  If the descriptor does not contain all the
  264           * fields "name", "descriptorType", "displayName", and "role",
  265           * the missing ones are added with their default values.
  266           *
  267           * @exception RuntimeOperationsException Wraps an
  268           * IllegalArgumentException. The descriptor is invalid; or
  269           * descriptor field "name" is present but not equal to
  270           * operation name; or descriptor field "DescriptorType" is
  271           * present but not equal to "operation"; or descriptor optional
  272           * field "role" is not equal to "operation", "getter", or
  273           * "setter".
  274           */
  275   
  276           public ModelMBeanOperationInfo(String name,
  277                                          String description,
  278                                          MBeanParameterInfo[] signature,
  279                                          String type,
  280                                          int impact,
  281                                          Descriptor descriptor)
  282           {
  283                   super(name, description, signature, type, impact);
  284                   if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  285                       MODELMBEAN_LOGGER.logp(Level.FINER,
  286                               ModelMBeanOperationInfo.class.getName(),
  287                               "ModelMBeanOperationInfo(String,String," +
  288                               "MBeanParameterInfo[],String,int,Descriptor)",
  289                               "Entry");
  290                   }
  291                   operationDescriptor = validDescriptor(descriptor);
  292           }
  293   
  294           /**
  295            * Constructs a new ModelMBeanOperationInfo object from this
  296            * ModelMBeanOperation Object.
  297            *
  298            * @param inInfo the ModelMBeanOperationInfo to be duplicated
  299            *
  300            */
  301   
  302           public ModelMBeanOperationInfo(ModelMBeanOperationInfo inInfo)
  303           {
  304                   super(inInfo.getName(),
  305                             inInfo.getDescription(),
  306                             inInfo.getSignature(),
  307                             inInfo.getReturnType(),
  308                             inInfo.getImpact());
  309                   if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  310                       MODELMBEAN_LOGGER.logp(Level.FINER,
  311                               ModelMBeanOperationInfo.class.getName(),
  312                               "ModelMBeanOperationInfo(ModelMBeanOperationInfo)",
  313                               "Entry");
  314                   }
  315                   Descriptor newDesc = inInfo.getDescriptor();
  316                   operationDescriptor = validDescriptor(newDesc);
  317           }
  318   
  319           /**
  320           * Creates and returns a new ModelMBeanOperationInfo which is a
  321           * duplicate of this ModelMBeanOperationInfo.
  322           *
  323           */
  324   
  325           public Object clone ()
  326           {
  327               if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  328                   MODELMBEAN_LOGGER.logp(Level.FINER,
  329                           ModelMBeanOperationInfo.class.getName(),
  330                           "clone()", "Entry");
  331               }
  332                   return(new ModelMBeanOperationInfo(this)) ;
  333           }
  334   
  335           /**
  336            * Returns a copy of the associated Descriptor of the
  337            * ModelMBeanOperationInfo.
  338            *
  339            * @return Descriptor associated with the
  340            * ModelMBeanOperationInfo object.
  341            *
  342            * @see #setDescriptor
  343            */
  344   
  345           public Descriptor getDescriptor()
  346           {
  347               if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  348                   MODELMBEAN_LOGGER.logp(Level.FINER,
  349                           ModelMBeanOperationInfo.class.getName(),
  350                           "getDescriptor()", "Entry");
  351               }
  352               if (operationDescriptor == null) {
  353                   operationDescriptor = validDescriptor(null);
  354               }
  355   
  356               return((Descriptor) operationDescriptor.clone());
  357           }
  358   
  359           /**
  360            * Sets associated Descriptor (full replace) for the
  361            * ModelMBeanOperationInfo If the new Descriptor is null, then
  362            * the associated Descriptor reverts to a default descriptor.
  363            * The Descriptor is validated before it is assigned.  If the
  364            * new Descriptor is invalid, then a
  365            * RuntimeOperationsException wrapping an
  366            * IllegalArgumentException is thrown.
  367            *
  368            * @param inDescriptor replaces the Descriptor associated with the
  369            * ModelMBeanOperation.
  370            *
  371            * @exception RuntimeOperationsException Wraps an
  372            * IllegalArgumentException for invalid Descriptor.
  373            *
  374            * @see #getDescriptor
  375            */
  376           public void setDescriptor(Descriptor inDescriptor)
  377           {
  378               if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  379                   MODELMBEAN_LOGGER.logp(Level.FINER,
  380                           ModelMBeanOperationInfo.class.getName(),
  381                           "setDescriptor(Descriptor)", "Entry");
  382               }
  383               operationDescriptor = validDescriptor(inDescriptor);
  384           }
  385   
  386           /**
  387           * Returns a string containing the entire contents of the
  388           * ModelMBeanOperationInfo in human readable form.
  389           */
  390           public String toString()
  391           {
  392               if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
  393                   MODELMBEAN_LOGGER.logp(Level.FINER,
  394                           ModelMBeanOperationInfo.class.getName(),
  395                           "toString()", "Entry");
  396               }
  397                   String retStr =
  398                       "ModelMBeanOperationInfo: " + this.getName() +
  399                       " ; Description: " + this.getDescription() +
  400                       " ; Descriptor: " + this.getDescriptor() +
  401                       " ; ReturnType: " + this.getReturnType() +
  402                       " ; Signature: ";
  403                   MBeanParameterInfo[] pTypes = this.getSignature();
  404                   for (int i=0; i < pTypes.length; i++)
  405                   {
  406                           retStr = retStr.concat((pTypes[i]).getType() + ", ");
  407                   }
  408                   return retStr;
  409           }
  410   
  411           /**
  412            * Clones the passed in Descriptor, sets default values, and checks for validity.
  413            * If the Descriptor is invalid (for instance by having the wrong "name"),
  414            * this indicates programming error and a RuntimeOperationsException will be thrown.
  415            *
  416            * The following fields will be defaulted if they are not already set:
  417            * displayName=this.getName(),name=this.getName(),
  418            * descriptorType="operation", role="operation"
  419            *
  420            *
  421            * @param in Descriptor to be checked, or null which is equivalent to
  422            * an empty Descriptor.
  423            * @exception RuntimeOperationsException if Descriptor is invalid
  424            */
  425           private Descriptor validDescriptor(final Descriptor in)
  426           throws RuntimeOperationsException {
  427               Descriptor clone;
  428               if (in == null) {
  429                   clone = new DescriptorSupport();
  430                   MODELMBEAN_LOGGER.finer("Null Descriptor, creating new.");
  431               } else {
  432                   clone = (Descriptor) in.clone();
  433               }
  434   
  435               //Setting defaults.
  436               if (clone.getFieldValue("name")==null) {
  437                   clone.setField("name", this.getName());
  438                   MODELMBEAN_LOGGER.finer("Defaulting Descriptor name to " + this.getName());
  439               }
  440               if (clone.getFieldValue("descriptorType")==null) {
  441                   clone.setField("descriptorType", "operation");
  442                   MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"operation\"");
  443               }
  444               if (clone.getFieldValue("displayName") == null) {
  445                   clone.setField("displayName",this.getName());
  446                   MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getName());
  447               }
  448               if (clone.getFieldValue("role") == null) {
  449                   clone.setField("role","operation");
  450                   MODELMBEAN_LOGGER.finer("Defaulting Descriptor role field to \"operation\"");
  451               }
  452   
  453               //Checking validity
  454               if (!clone.isValid()) {
  455                    throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
  456                       "The isValid() method of the Descriptor object itself returned false,"+
  457                       "one or more required fields are invalid. Descriptor:" + clone.toString());
  458               }
  459               if (! ((String)clone.getFieldValue("name")).equalsIgnoreCase(this.getName())) {
  460                       throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
  461                       "The Descriptor \"name\" field does not match the object described. " +
  462                        " Expected: "+ this.getName() + " , was: " + clone.getFieldValue("name"));
  463               }
  464               if (! ((String)clone.getFieldValue("descriptorType")).equalsIgnoreCase("operation")) {
  465                        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
  466                       "The Descriptor \"descriptorType\" field does not match the object described. " +
  467                        " Expected: \"attribute\" ," + " was: " + clone.getFieldValue("descriptorType"));
  468               }
  469               final String role = (String)clone.getFieldValue("role");
  470               if (!(role.equalsIgnoreCase("operation") ||
  471                     role.equalsIgnoreCase("setter") ||
  472                     role.equalsIgnoreCase("getter"))) {
  473                        throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
  474                       "The Descriptor \"role\" field does not match the object described. " +
  475                        " Expected: \"operation\", \"setter\", or \"getter\" ," + " was: " + clone.getFieldValue("role"));
  476               }
  477               final Object targetValue = clone.getFieldValue("targetType");
  478               if (targetValue != null) {
  479                   if (!(targetValue instanceof java.lang.String)) {
  480                       throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
  481                       "The Descriptor field \"targetValue\" is invalid class. " +
  482                        " Expected: java.lang.String, " + " was: " + targetValue.getClass().getName());
  483                   }
  484               }
  485               return clone;
  486           }
  487   
  488       /**
  489        * Deserializes a {@link ModelMBeanOperationInfo} from an {@link ObjectInputStream}.
  490        */
  491       private void readObject(ObjectInputStream in)
  492               throws IOException, ClassNotFoundException {
  493         // New serial form ignores extra field "currClass"
  494         in.defaultReadObject();
  495       }
  496   
  497   
  498       /**
  499        * Serializes a {@link ModelMBeanOperationInfo} to an {@link ObjectOutputStream}.
  500        */
  501       private void writeObject(ObjectOutputStream out)
  502               throws IOException {
  503         if (compat)
  504         {
  505           // Serializes this instance in the old serial form
  506           //
  507           ObjectOutputStream.PutField fields = out.putFields();
  508           fields.put("operationDescriptor", operationDescriptor);
  509           fields.put("currClass", currClass);
  510           out.writeFields();
  511         }
  512         else
  513         {
  514           // Serializes this instance in the new serial form
  515           //
  516           out.defaultWriteObject();
  517         }
  518       }
  519   
  520   }

Save This Page
Home » openjdk-7 » javax » management » modelmbean » [javadoc | source]