J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2003 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 represents indexed attributes used as arguments to relational |
| 31 | * constraints. An QualifiedAttributeValueExp may be used anywhere a |
| 32 | * ValueExp is required. |
| 33 | * @serial include |
| 34 | * |
| 35 | * @since 1.5 |
| 36 | */ |
| 37 | class QualifiedAttributeValueExp extends AttributeValueExp { |
| 38 | |
| 39 | |
| 40 | /* Serial version */ |
| 41 | private static final long serialVersionUID = 8832517277410933254L; |
| 42 | |
| 43 | /** |
| 44 | * @serial The attribute class name |
| 45 | */ |
| 46 | private String className; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Basic Constructor. |
| 51 | */ |
| 52 | public QualifiedAttributeValueExp() { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a new QualifiedAttributeValueExp representing the specified object |
| 57 | * attribute, named attr with class name className. |
| 58 | */ |
| 59 | public QualifiedAttributeValueExp(String className, String attr) { |
| 60 | super(attr); |
| 61 | this.className = className; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Returns a string representation of the class name of the attribute. |
| 67 | */ |
| 68 | public String getAttrClassName() { |
| 69 | return className; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Applies the QualifiedAttributeValueExp to an MBean. |
| 74 | * |
| 75 | * @param name The name of the MBean on which the QualifiedAttributeValueExp will be applied. |
| 76 | * |
| 77 | * @return The ValueExp. |
| 78 | * |
| 79 | * @exception BadStringOperationException |
| 80 | * @exception BadBinaryOpValueExpException |
| 81 | * @exception BadAttributeValueExpException |
| 82 | * @exception InvalidApplicationException |
| 83 | */ |
| 84 | public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException, |
| 85 | BadAttributeValueExpException, InvalidApplicationException { |
| 86 | try { |
| 87 | MBeanServer server = QueryEval.getMBeanServer(); |
| 88 | String v = server.getObjectInstance(name).getClassName(); |
| 89 | |
| 90 | if (v.equals(className)) { |
| 91 | return super.apply(name); |
| 92 | } |
| 93 | throw new InvalidApplicationException("Class name is " + v + |
| 94 | ", should be " + className); |
| 95 | |
| 96 | } catch (Exception e) { |
| 97 | throw new InvalidApplicationException("Qualified attribute: " + e); |
| 98 | /* Can happen if MBean disappears between the time we |
| 99 | construct the list of MBeans to query and the time we |
| 100 | evaluate the query on this MBean, or if |
| 101 | getObjectInstance throws SecurityException. */ |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the string representing its value |
| 107 | */ |
| 108 | public String toString() { |
| 109 | if (className != null) { |
| 110 | return className + "." + super.toString(); |
| 111 | } else { |
| 112 | return super.toString(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |