001 /*
002 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management;
027
028 /**
029 * This class is used by the query building mechanism for isInstanceOf expressions.
030 * @serial include
031 *
032 * @since 1.6
033 */
034 class InstanceOfQueryExp extends QueryEval implements QueryExp {
035
036 /* Serial version */
037 private static final long serialVersionUID = -1081892073854801359L;
038
039 /**
040 * @serial The {@link StringValueExp} returning the name of the class
041 * of which selected MBeans should be instances.
042 */
043 private StringValueExp classNameValue;
044
045 /**
046 * Creates a new InstanceOfExp with a specific class name.
047 * @param classNameValue The {@link StringValueExp} returning the name of
048 * the class of which selected MBeans should be instances.
049 */
050 // We are using StringValueExp here to be consistent with other queries,
051 // although we should actually either use a simple string (the classname)
052 // or a ValueExp - which would allow more complex queries - like for
053 // instance evaluating the class name from an AttributeValueExp.
054 // As it stands - using StringValueExp instead of a simple constant string
055 // doesn't serve any useful purpose besides offering a consistent
056 // look & feel.
057 public InstanceOfQueryExp(StringValueExp classNameValue) {
058 if (classNameValue == null) {
059 throw new IllegalArgumentException("Null class name.");
060 }
061
062 this .classNameValue = classNameValue;
063 }
064
065 /**
066 * Returns the class name.
067 * @returns The {@link StringValueExp} returning the name of
068 * the class of which selected MBeans should be instances.
069 */
070 public StringValueExp getClassNameValue() {
071 return classNameValue;
072 }
073
074 /**
075 * Applies the InstanceOf on a MBean.
076 *
077 * @param name The name of the MBean on which the InstanceOf will be applied.
078 *
079 * @return True if the MBean specified by the name is instance of the class.
080 * @exception BadAttributeValueExpException
081 * @exception InvalidApplicationException
082 * @exception BadStringOperationException
083 * @exception BadBinaryOpValueExpException
084 */
085 public boolean apply(ObjectName name)
086 throws BadStringOperationException,
087 BadBinaryOpValueExpException,
088 BadAttributeValueExpException, InvalidApplicationException {
089
090 // Get the class name value
091 final StringValueExp val;
092 try {
093 val = (StringValueExp) classNameValue.apply(name);
094 } catch (ClassCastException x) {
095 // Should not happen - unless someone wrongly implemented
096 // StringValueExp.apply().
097 final BadStringOperationException y = new BadStringOperationException(
098 x.toString());
099 y.initCause(x);
100 throw y;
101 }
102
103 // Test whether the MBean is an instance of that class.
104 try {
105 return getMBeanServer().isInstanceOf(name, val.getValue());
106 } catch (InstanceNotFoundException infe) {
107 return false;
108 }
109 }
110
111 /**
112 * Returns a string representation of this InstanceOfQueryExp.
113 * @return a string representation of this InstanceOfQueryExp.
114 */
115 public String toString() {
116 return "InstanceOf " + classNameValue.toString();
117 }
118 }
|