jdk/src/share/classes/javax/management/AttributeValueExp.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4156 acaa49a2768a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    29
import com.sun.jmx.mbeanserver.Introspector;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    30
import java.io.IOException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    31
import java.io.ObjectInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    34
 * <p>Represents attributes used as arguments to relational constraints.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    35
 * Instances of this class are usually obtained using {@link Query#attr(String)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    36
 * Query.attr}.</p>
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    37
 *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    38
 * <p>An <CODE>AttributeValueExp</CODE> may be used anywhere a
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    39
 * <CODE>ValueExp</CODE> is required.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class AttributeValueExp implements ValueExp  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final long serialVersionUID = -7768025046539163385L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @serial The name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * An <code>AttributeValueExp</code> with a null attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @deprecated An instance created with this constructor cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * used in a query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public AttributeValueExp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a new <CODE>AttributeValueExp</CODE> representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * specified object attribute, named attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param attr the name of the attribute whose value is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * of this {@link ValueExp}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public AttributeValueExp(String attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.attr = attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns a string representation of the name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the attribute name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getAttributeName()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    84
     * <p>Applies the <CODE>AttributeValueExp</CODE> on an MBean.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    85
     * This method calls {@link #getAttribute getAttribute(name)} and wraps
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    86
     * the result as a {@code ValueExp}.  The value returned by
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    87
     * {@code getAttribute} must be a {@code Number}, {@code String},
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    88
     * or {@code Boolean}; otherwise this method throws a
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    89
     * {@code BadAttributeValueExpException}, which will cause
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    90
     * the containing query to be false for this {@code name}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return  The <CODE>ValueExp</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @exception BadAttributeValueExpException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception InvalidApplicationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception BadStringOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @exception BadBinaryOpValueExpException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   102
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        BadAttributeValueExpException, InvalidApplicationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Object result = getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (result instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return new NumericValueExp((Number)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else if (result instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return new StringValueExp((String)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else if (result instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return new BooleanValueExp((Boolean)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new BadAttributeValueExpException(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns the string representing its value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   121
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public String toString()  {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 715
diff changeset
   123
        return attr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Sets the MBean server on which the query is to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param s The MBean server on which the query is to be performed.
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   131
     *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   132
     * @deprecated This method has no effect.  The MBean Server used to
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   133
     * obtain an attribute value is {@link QueryEval#getMBeanServer()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /* There is no need for this method, because if a query is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
       evaluted an AttributeValueExp can only appear inside a QueryExp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
       and that QueryExp will itself have done setMBeanServer.  */
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   138
    @Deprecated
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   139
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void setMBeanServer(MBeanServer s)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   145
     * <p>Return the value of the given attribute in the named MBean.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * If the attempt to access the attribute generates an exception,
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   147
     * return null.</p>
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   148
     *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   149
     * <p>The MBean Server used is the one returned by {@link
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   150
     * QueryEval#getMBeanServer()}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param name the name of the MBean whose attribute is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return the value of the attribute, or null if it could not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * obtained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected Object getAttribute(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // Get the value from the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            MBeanServer server = QueryEval.getMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return server.getAttribute(name, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (Exception re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}