J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 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 | package javax.management; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * This class is used by the query building mechanism for isInstanceOf expressions. |
| 31 | * @serial include |
| 32 | * |
| 33 | * @since 1.6 |
| 34 | */ |
| 35 | class InstanceOfQueryExp extends QueryEval implements QueryExp { |
| 36 | |
| 37 | /* Serial version */ |
| 38 | private static final long serialVersionUID = -1081892073854801359L; |
| 39 | |
| 40 | /** |
| 41 | * @serial The {@link StringValueExp} returning the name of the class |
| 42 | * of which selected MBeans should be instances. |
| 43 | */ |
| 44 | private StringValueExp classNameValue; |
| 45 | |
| 46 | /** |
| 47 | * Creates a new InstanceOfExp with a specific class name. |
| 48 | * @param classNameValue The {@link StringValueExp} returning the name of |
| 49 | * the class of which selected MBeans should be instances. |
| 50 | */ |
| 51 | // We are using StringValueExp here to be consistent with other queries, |
| 52 | // although we should actually either use a simple string (the classname) |
| 53 | // or a ValueExp - which would allow more complex queries - like for |
| 54 | // instance evaluating the class name from an AttributeValueExp. |
| 55 | // As it stands - using StringValueExp instead of a simple constant string |
| 56 | // doesn't serve any useful purpose besides offering a consistent |
| 57 | // look & feel. |
| 58 | public InstanceOfQueryExp(StringValueExp classNameValue) { |
| 59 | if (classNameValue == null) { |
| 60 | throw new IllegalArgumentException("Null class name."); |
| 61 | } |
| 62 | |
| 63 | this.classNameValue = classNameValue; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the class name. |
| 68 | * @returns The {@link StringValueExp} returning the name of |
| 69 | * the class of which selected MBeans should be instances. |
| 70 | */ |
| 71 | public StringValueExp getClassNameValue() { |
| 72 | return classNameValue; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Applies the InstanceOf on a MBean. |
| 77 | * |
| 78 | * @param name The name of the MBean on which the InstanceOf will be applied. |
| 79 | * |
| 80 | * @return True if the MBean specified by the name is instance of the class. |
| 81 | * @exception BadAttributeValueExpException |
| 82 | * @exception InvalidApplicationException |
| 83 | * @exception BadStringOperationException |
| 84 | * @exception BadBinaryOpValueExpException |
| 85 | */ |
| 86 | public boolean apply(ObjectName name) |
| 87 | throws BadStringOperationException, |
| 88 | BadBinaryOpValueExpException, |
| 89 | BadAttributeValueExpException, |
| 90 | InvalidApplicationException { |
| 91 | |
| 92 | // Get the class name value |
| 93 | final StringValueExp val; |
| 94 | try { |
| 95 | val = (StringValueExp) classNameValue.apply(name); |
| 96 | } catch (ClassCastException x) { |
| 97 | // Should not happen - unless someone wrongly implemented |
| 98 | // StringValueExp.apply(). |
| 99 | final BadStringOperationException y = |
| 100 | new BadStringOperationException(x.toString()); |
| 101 | y.initCause(x); |
| 102 | throw y; |
| 103 | } |
| 104 | |
| 105 | // Test whether the MBean is an instance of that class. |
| 106 | try { |
| 107 | return getMBeanServer().isInstanceOf(name, val.getValue()); |
| 108 | } catch (InstanceNotFoundException infe) { |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns a string representation of this InstanceOfQueryExp. |
| 115 | * @return a string representation of this InstanceOfQueryExp. |
| 116 | */ |
| 117 | public String toString() { |
| 118 | return "InstanceOf " + classNameValue.toString(); |
| 119 | } |
| 120 | } |