Path: blob/master/src/java.management/share/classes/javax/management/InstanceOfQueryExp.java
41155 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;262728/**29* This class is used by the query building mechanism for isInstanceOf expressions.30* @serial include31*32* @since 1.633*/34class InstanceOfQueryExp extends QueryEval implements QueryExp {3536/* Serial version */37private static final long serialVersionUID = -1081892073854801359L;3839/**40* @serial The {@link StringValueExp} returning the name of the class41* of which selected MBeans should be instances.42*/43private StringValueExp classNameValue;4445/**46* Creates a new InstanceOfExp with a specific class name.47* @param classNameValue The {@link StringValueExp} returning the name of48* the class of which selected MBeans should be instances.49*/50// We are using StringValueExp here to be consistent with other queries,51// although we should actually either use a simple string (the classname)52// or a ValueExp - which would allow more complex queries - like for53// instance evaluating the class name from an AttributeValueExp.54// As it stands - using StringValueExp instead of a simple constant string55// doesn't serve any useful purpose besides offering a consistent56// look & feel.57public InstanceOfQueryExp(StringValueExp classNameValue) {58if (classNameValue == null) {59throw new IllegalArgumentException("Null class name.");60}6162this.classNameValue = classNameValue;63}6465/**66* Returns the class name.67* @return The {@link StringValueExp} returning the name of68* the class of which selected MBeans should be instances.69*/70public StringValueExp getClassNameValue() {71return classNameValue;72}7374/**75* Applies the InstanceOf on a MBean.76*77* @param name The name of the MBean on which the InstanceOf will be applied.78*79* @return True if the MBean specified by the name is instance of the class.80* @exception BadAttributeValueExpException81* @exception InvalidApplicationException82* @exception BadStringOperationException83* @exception BadBinaryOpValueExpException84*/85public boolean apply(ObjectName name)86throws BadStringOperationException,87BadBinaryOpValueExpException,88BadAttributeValueExpException,89InvalidApplicationException {9091// Get the class name value92final StringValueExp val;93try {94val = (StringValueExp) classNameValue.apply(name);95} catch (ClassCastException x) {96// Should not happen - unless someone wrongly implemented97// StringValueExp.apply().98final BadStringOperationException y =99new BadStringOperationException(x.toString());100y.initCause(x);101throw y;102}103104// Test whether the MBean is an instance of that class.105try {106return getMBeanServer().isInstanceOf(name, val.getValue());107} catch (InstanceNotFoundException infe) {108return false;109}110}111112/**113* Returns a string representation of this InstanceOfQueryExp.114* @return a string representation of this InstanceOfQueryExp.115*/116public String toString() {117return "InstanceOf " + classNameValue.toString();118}119}120121122