001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management;
023:
024: import org.jboss.mx.util.QueryExpSupport;
025:
026: /**
027: * A String that is an arguement to a query.<p>
028: *
029: * <p><b>Revisions:</b>
030: * <p><b>20020317 Adrian Brock:</b>
031: * <ul>
032: * <li> Make queries thread safe
033: * </ul>
034: * <p><b>20020711 Adrian Brock:</b>
035: * <ul>
036: * <li> Serialization
037: * </ul>
038: *
039: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
040: * @version $Revision: 57200 $
041: */
042: public class AttributeValueExp implements ValueExp {
043: // Constants ---------------------------------------------------
044:
045: private static final long serialVersionUID = -7768025046539163385L;
046:
047: // Attributes --------------------------------------------------
048:
049: /**
050: * The attribute name
051: */
052: private String attr;
053:
054: // Static -----------------------------------------------------
055:
056: // Constructors ------------------------------------------------
057:
058: /**
059: * Construct an attribute value expression for the null attribute name
060: */
061: public AttributeValueExp() {
062: }
063:
064: /**
065: * Construct an attribute value expression for the passed attribute name
066: *
067: * @param attr the attribute name
068: */
069: public AttributeValueExp(String attr) {
070: this .attr = attr;
071: }
072:
073: // Public ------------------------------------------------------
074:
075: /**
076: * Get the attribute name.
077: *
078: * @return the attribute name
079: */
080: public String getAttributeName() {
081: return attr;
082: }
083:
084: // ValueExp Implementation -------------------------------------
085:
086: public ValueExp apply(ObjectName name)
087: throws BadStringOperationException,
088: BadBinaryOpValueExpException,
089: BadAttributeValueExpException, InvalidApplicationException {
090: Object object = getAttribute(name);
091: if (object != null && object instanceof String)
092: return new StringValueExp((String) object);
093: if (object != null && object instanceof Boolean)
094: return new BooleanValueExp((Boolean) object);
095: if (object != null && object instanceof Number)
096: return new NumericValueExp((Number) object);
097: throw new BadAttributeValueExpException(object);
098: }
099:
100: public void setMBeanServer(MBeanServer server) {
101: QueryExpSupport.server.set(server);
102: }
103:
104: // Object overrides --------------------------------------------
105:
106: public String toString() {
107: return attr;
108: }
109:
110: // Protected ---------------------------------------------------
111:
112: /**
113: * Get the value of the attribute for a given object name
114: *
115: * @param name - the object name
116: * @return the value of the attribute
117: */
118: protected Object getAttribute(ObjectName name) {
119: try {
120: return QueryEval.getMBeanServer().getAttribute(name, attr);
121: } catch (Exception e) {
122: return null;
123: }
124: }
125:
126: }
|