1 /*
   2  * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.management;
  27 
  28 
  29 import com.sun.jmx.mbeanserver.Introspector;
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;
  32 
  33 /**
  34  * <p>Represents attributes used as arguments to relational constraints.
  35  * Instances of this class are usually obtained using {@link Query#attr(String)
  36  * Query.attr}.</p>
  37  *
  38  * <p>An <CODE>AttributeValueExp</CODE> may be used anywhere a
  39  * <CODE>ValueExp</CODE> is required.
  40  *
  41  * @since 1.5
  42  */
  43 public class AttributeValueExp implements ValueExp  {
  44 
  45 
  46     /* Serial version */
  47     private static final long serialVersionUID = -7768025046539163385L;
  48 
  49     /**
  50      * @serial The name of the attribute
  51      */
  52     private String attr;
  53 
  54     /**
  55      * An <code>AttributeValueExp</code> with a null attribute.
  56      * @deprecated An instance created with this constructor cannot be
  57      * used in a query.
  58      */
  59     @Deprecated
  60     public AttributeValueExp() {
  61     }
  62 
  63     /**
  64      * Creates a new <CODE>AttributeValueExp</CODE> representing the
  65      * specified object attribute, named attr.
  66      *
  67      * @param attr the name of the attribute whose value is the value
  68      * of this {@link ValueExp}.
  69      */
  70     public AttributeValueExp(String attr) {
  71         this.attr = attr;
  72     }
  73 
  74     /**
  75      * Returns a string representation of the name of the attribute.
  76      *
  77      * @return the attribute name.
  78      */
  79     public String getAttributeName()  {
  80         return attr;
  81     }
  82 
  83     /**
  84      * <p>Applies the <CODE>AttributeValueExp</CODE> on an MBean.
  85      * This method calls {@link #getAttribute getAttribute(name)} and wraps
  86      * the result as a {@code ValueExp}.  The value returned by
  87      * {@code getAttribute} must be a {@code Number}, {@code String},
  88      * or {@code Boolean}; otherwise this method throws a
  89      * {@code BadAttributeValueExpException}, which will cause
  90      * the containing query to be false for this {@code name}.</p>
  91      *
  92      * @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.
  93      *
  94      * @return  The <CODE>ValueExp</CODE>.
  95      *
  96      * @exception BadAttributeValueExpException
  97      * @exception InvalidApplicationException
  98      * @exception BadStringOperationException
  99      * @exception BadBinaryOpValueExpException
 100      *
 101      */
 102     @Override
 103     public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
 104         BadAttributeValueExpException, InvalidApplicationException {
 105         Object result = getAttribute(name);
 106 
 107         if (result instanceof Number) {
 108             return new NumericValueExp((Number)result);
 109         } else if (result instanceof String) {
 110             return new StringValueExp((String)result);
 111         } else if (result instanceof Boolean) {
 112             return new BooleanValueExp((Boolean)result);
 113         } else {
 114             throw new BadAttributeValueExpException(result);
 115         }
 116     }
 117 
 118     /**
 119      * Returns the string representing its value.
 120      */
 121     @Override
 122     public String toString()  {
 123         return attr;
 124     }
 125 
 126 
 127     /**
 128      * Sets the MBean server on which the query is to be performed.
 129      *
 130      * @param s The MBean server on which the query is to be performed.
 131      *
 132      * @deprecated This method has no effect.  The MBean Server used to
 133      * obtain an attribute value is {@link QueryEval#getMBeanServer()}.
 134      */
 135     /* There is no need for this method, because if a query is being
 136        evaluted an AttributeValueExp can only appear inside a QueryExp,
 137        and that QueryExp will itself have done setMBeanServer.  */
 138     @Deprecated
 139     @Override
 140     public void setMBeanServer(MBeanServer s)  {
 141     }
 142 
 143 
 144     /**
 145      * <p>Return the value of the given attribute in the named MBean.
 146      * If the attempt to access the attribute generates an exception,
 147      * return null.</p>
 148      *
 149      * <p>The MBean Server used is the one returned by {@link
 150      * QueryEval#getMBeanServer()}.</p>
 151      *
 152      * @param name the name of the MBean whose attribute is to be returned.
 153      *
 154      * @return the value of the attribute, or null if it could not be
 155      * obtained.
 156      */
 157     protected Object getAttribute(ObjectName name) {
 158         try {
 159             // Get the value from the MBeanServer
 160 
 161             MBeanServer server = QueryEval.getMBeanServer();
 162 
 163             return server.getAttribute(name, attr);
 164         } catch (Exception re) {
 165             return null;
 166         }
 167     }
 168 }