1. /*
  2. * @(#)AttributeValueExp.java 4.24 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management;
  8. // RI import
  9. import javax.management.MBeanServer;
  10. /**
  11. * Represents attributes used as arguments to relational constraints.
  12. * An <CODE>AttributeValueExp</CODE> may be used anywhere a <CODE>ValueExp</CODE> is required.
  13. *
  14. * @since 1.5
  15. */
  16. public class AttributeValueExp implements ValueExp {
  17. /* Serial version */
  18. private static final long serialVersionUID = -7768025046539163385L;
  19. /**
  20. * @serial The name of the attribute
  21. */
  22. private String attr;
  23. /**
  24. * An <code>AttributeValueExp</code> with a null attribute.
  25. * @deprecated An instance created with this constructor cannot be
  26. * used in a query.
  27. */
  28. @Deprecated
  29. public AttributeValueExp() {
  30. }
  31. /**
  32. * Creates a new <CODE>AttributeValueExp</CODE> representing the
  33. * specified object attribute, named attr.
  34. *
  35. * @param attr the name of the attribute whose value is the value
  36. * of this {@link ValueExp}.
  37. */
  38. public AttributeValueExp(String attr) {
  39. this.attr = attr;
  40. }
  41. /**
  42. * Returns a string representation of the name of the attribute.
  43. *
  44. * @return the attribute name.
  45. */
  46. public String getAttributeName() {
  47. return attr;
  48. }
  49. /**
  50. * Applies the <CODE>AttributeValueExp</CODE> on an MBean.
  51. *
  52. * @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.
  53. *
  54. * @return The <CODE>ValueExp</CODE>.
  55. *
  56. * @exception BadAttributeValueExpException
  57. * @exception InvalidApplicationException
  58. * @exception BadStringOperationException
  59. * @exception BadBinaryOpValueExpException
  60. *
  61. */
  62. public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
  63. BadAttributeValueExpException, InvalidApplicationException {
  64. Object result = getAttribute(name);
  65. if (result instanceof Number) {
  66. return new NumericValueExp((Number)result);
  67. } else if (result instanceof String) {
  68. return new StringValueExp((String)result);
  69. } else if (result instanceof Boolean) {
  70. return new BooleanValueExp((Boolean)result);
  71. } else {
  72. throw new BadAttributeValueExpException(result);
  73. }
  74. }
  75. /**
  76. * Returns the string representing its value.
  77. */
  78. public String toString() {
  79. return attr;
  80. }
  81. /**
  82. * Sets the MBean server on which the query is to be performed.
  83. *
  84. * @param s The MBean server on which the query is to be performed.
  85. */
  86. /* There is no need for this method, because if a query is being
  87. evaluted an AttributeValueExp can only appear inside a QueryExp,
  88. and that QueryExp will itself have done setMBeanServer. */
  89. public void setMBeanServer(MBeanServer s) {
  90. }
  91. /**
  92. * Return the value of the given attribute in the named MBean.
  93. * If the attempt to access the attribute generates an exception,
  94. * return null.
  95. *
  96. * @param name the name of the MBean whose attribute is to be returned.
  97. *
  98. * @return the value of the attribute, or null if it could not be
  99. * obtained.
  100. */
  101. protected Object getAttribute(ObjectName name) {
  102. try {
  103. // Get the value from the MBeanServer
  104. MBeanServer server = QueryEval.getMBeanServer();
  105. return server.getAttribute(name, attr);
  106. } catch (Exception re) {
  107. return null;
  108. }
  109. }
  110. }