AttributeValueExp.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* AttributeValueExp.java -- Represents attributes to be passed to queries.
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management;
  32. /**
  33. * Represents an attribute value being used as an argument
  34. * to a relational constraint.
  35. *
  36. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  37. * @since 1.5
  38. */
  39. public class AttributeValueExp
  40. implements ValueExp
  41. {
  42. /**
  43. * Compatible with JDK 1.5
  44. */
  45. private static final long serialVersionUID = -7768025046539163385L;
  46. /**
  47. * The name of the attribute.
  48. */
  49. private String attr;
  50. /**
  51. * Constructs a new {@link AttributeValueExp}.
  52. *
  53. * @deprecated An instance created with a <code>null</code>
  54. * attribute name can not be used in a query.
  55. */
  56. @Deprecated public AttributeValueExp()
  57. {
  58. }
  59. /**
  60. * Constructs a new {@link AttributeValueExp} using the
  61. * specified attribute.
  62. *
  63. * @param attr the name of the attribute whose value
  64. * will be used for this expression.
  65. */
  66. public AttributeValueExp(String attr)
  67. {
  68. this.attr = attr;
  69. }
  70. /**
  71. * Applies the {@link AttributeValueExp} to the specified
  72. * management bean by obtaining the attribute value from
  73. * the {@link MBeanServer} and using it to create a
  74. * {@link StringValueExp}.
  75. *
  76. * @param name the {@link ObjectName} of the bean to obtain
  77. * the value from.
  78. * @return a {@link StringValueExp} containing the result.
  79. * @throws BadStringOperationException if an invalid string
  80. * operation is used by
  81. * the value expression.
  82. * @throws BadBinaryOpValueExpException if an invalid expression
  83. * is used by the value expression.
  84. * @throws BadAttributeValueExpException if an invalid attribute
  85. * is used by the value expression.
  86. * @throws InvalidApplicationException if the value expression is applied
  87. * to the wrong type of bean.
  88. */
  89. public ValueExp apply(ObjectName name)
  90. throws BadStringOperationException, BadBinaryOpValueExpException,
  91. BadAttributeValueExpException, InvalidApplicationException
  92. {
  93. Object val = getAttribute(name);
  94. if (val == null || !(val instanceof String))
  95. throw new BadAttributeValueExpException(val);
  96. return new StringValueExp((String) val);
  97. }
  98. /**
  99. * Returns the value of the attribute by calling the
  100. * {@link MBeanServer#getAttribute(ObjectName)} method of
  101. * the server returned by {@link QueryEval#getMBeanServer()}.
  102. * If an exception occurs, <code>null</code> is returned.
  103. *
  104. * @param name the {@link ObjectName} of the bean to obtain
  105. * the value from.
  106. * @return a {@link StringValueExp} containing the result.
  107. */
  108. protected Object getAttribute(ObjectName name)
  109. {
  110. try
  111. {
  112. return QueryEval.getMBeanServer().getAttribute(name, attr);
  113. }
  114. catch (NullPointerException e)
  115. {
  116. return null;
  117. }
  118. catch (MBeanException e)
  119. {
  120. return null;
  121. }
  122. catch (AttributeNotFoundException e)
  123. {
  124. return null;
  125. }
  126. catch (InstanceNotFoundException e)
  127. {
  128. return null;
  129. }
  130. catch (ReflectionException e)
  131. {
  132. return null;
  133. }
  134. }
  135. /**
  136. * Returns the attribute name.
  137. *
  138. * @return the attribute name.
  139. */
  140. public String getAttributeName()
  141. {
  142. return attr;
  143. }
  144. /**
  145. * Sets the {@link MBeanServer} on which the query
  146. * will be performed.
  147. *
  148. * @param server the new server.
  149. */
  150. public void setMBeanServer(MBeanServer server)
  151. {
  152. /* This seems to do nothing any more */
  153. }
  154. /**
  155. * Returns the attribute name, quoted.
  156. *
  157. * @return the quoted attribute name.
  158. */
  159. public String toString()
  160. {
  161. return "'" + attr + "'";
  162. }
  163. }