001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management;
010:
011: import java.security.AccessController;
012: import java.security.PrivilegedActionException;
013: import java.security.PrivilegedExceptionAction;
014:
015: /**
016: * @version $Revision: 1.11 $
017: */
018: public class AttributeValueExp implements ValueExp {
019: private static final long serialVersionUID = -7768025046539163385L;
020:
021: /**
022: * @serial The name of the attribute
023: */
024: private String attr;
025:
026: private transient MBeanServer server;
027:
028: /**
029: * @deprecated Must not be used
030: */
031: public AttributeValueExp() {
032: }
033:
034: /**
035: * Creates a new AttributeValueExp with the given attribute's name, that will be
036: * used to retrieve the attribute's value used by this ValueExp
037: *
038: * @param attr The name of the MBean attribute
039: */
040: public AttributeValueExp(String attr) {
041: this .attr = attr;
042: }
043:
044: /**
045: * Returns the name of the MBean attribute whose value is used by this ValueExp
046: */
047: public String getAttributeName() {
048: return attr;
049: }
050:
051: public ValueExp apply(ObjectName name)
052: throws BadStringOperationException,
053: BadBinaryOpValueExpException,
054: BadAttributeValueExpException, InvalidApplicationException {
055: try {
056: Object value = getAttribute(name);
057: if (value == null) {
058: // Must understand what to return
059: return createValueExp(name);
060: } else if (value instanceof Number) {
061: return new NumericValueExp((Number) value);
062: } else if (value instanceof Boolean) {
063: return new BooleanValueExp(((Boolean) value)
064: .booleanValue());
065: } else if (value instanceof String) {
066: return new StringValueExp((String) value);
067: } else {
068: throw new BadAttributeValueExpException(value);
069: }
070: } catch (RuntimeOperationsException x) {
071: throw new BadAttributeValueExpException(getAttributeName());
072: }
073: }
074:
075: public void setMBeanServer(MBeanServer server) {
076: this .server = server;
077: }
078:
079: protected Object getAttribute(ObjectName name) {
080: try {
081: return server.getAttribute(name, getAttributeName());
082: } catch (MBeanException x) {
083: } catch (AttributeNotFoundException x) {
084: } catch (InstanceNotFoundException x) {
085: } catch (ReflectionException x) {
086: }
087: throw new RuntimeOperationsException(null);
088: }
089:
090: private ValueExp createValueExp(final ObjectName name)
091: throws BadAttributeValueExpException {
092: try {
093: MBeanInfo info = (MBeanInfo) AccessController
094: .doPrivileged(new PrivilegedExceptionAction() {
095: public Object run() throws Exception {
096: return server.getMBeanInfo(name);
097: }
098: });
099:
100: MBeanAttributeInfo[] attrs = info.getAttributes();
101: for (int i = 0; i < attrs.length; ++i) {
102: MBeanAttributeInfo attribute = attrs[i];
103: if (attribute.getName().equals(getAttributeName())) {
104: String type = attribute.getType();
105: if (type.equals("java.lang.String")) {
106: return new StringValueExp(null);
107: } else {
108: // If an attribute is meant to return a boolean or a number, and returns null,
109: // then the MBean programmer is dumb and we tell him so.
110: throw new BadAttributeValueExpException(null);
111: }
112: }
113: }
114: } catch (PrivilegedActionException x) {
115: }
116: throw new BadAttributeValueExpException(null);
117: }
118: }
|