1. /*
  2. * @(#)ClassAttributeValueExp.java 4.21 03/12/19
  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. import java.security.AccessController;
  9. import java.security.PrivilegedAction;
  10. import com.sun.jmx.mbeanserver.GetPropertyAction;
  11. /**
  12. * This class represents the name of the Java implementation class of
  13. * the MBean. It is used for performing queries based on the class of
  14. * the MBean.
  15. * @serial include
  16. *
  17. * @since 1.5
  18. */
  19. class ClassAttributeValueExp extends AttributeValueExp {
  20. // Serialization compatibility stuff:
  21. // Two serial forms are supported in this class. The selected form depends
  22. // on system property "jmx.serial.form":
  23. // - "1.0" for JMX 1.0
  24. // - any other value for JMX 1.1 and higher
  25. //
  26. // Serial version for old serial form
  27. private static final long oldSerialVersionUID = -2212731951078526753L;
  28. //
  29. // Serial version for new serial form
  30. private static final long newSerialVersionUID = -1081892073854801359L;
  31. private static final long serialVersionUID;
  32. static {
  33. boolean compat = false;
  34. try {
  35. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  36. String form = (String) AccessController.doPrivileged(act);
  37. compat = (form != null && form.equals("1.0"));
  38. } catch (Exception e) {
  39. // OK: exception means no compat with 1.0, too bad
  40. }
  41. if (compat)
  42. serialVersionUID = oldSerialVersionUID;
  43. else
  44. serialVersionUID = newSerialVersionUID;
  45. }
  46. /**
  47. * @serial The name of the attribute
  48. */
  49. private String attr;
  50. /**
  51. * Basic Constructor.
  52. */
  53. public ClassAttributeValueExp() {
  54. attr = "Class";
  55. }
  56. /**
  57. * Applies the ClassAttributeValueExp on an MBean. Returns the name of
  58. * the Java implementation class of the MBean.
  59. *
  60. * @param name The name of the MBean on which the ClassAttributeValueExp will be applied.
  61. *
  62. * @return The ValueExp.
  63. *
  64. * @exception BadAttributeValueExpException
  65. * @exception InvalidApplicationException
  66. */
  67. public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
  68. BadAttributeValueExpException, InvalidApplicationException {
  69. getAttribute(name);
  70. Object result = getValue(name);
  71. if (result instanceof String) {
  72. return new StringValueExp((String)result);
  73. } else {
  74. throw new BadAttributeValueExpException(result);
  75. }
  76. }
  77. /**
  78. * Returns the string "Class" representing its value
  79. */
  80. public String toString() {
  81. return attr;
  82. }
  83. protected Object getValue(ObjectName name) {
  84. try {
  85. // Get the class of the object
  86. MBeanServer server = QueryEval.getMBeanServer();
  87. return server.getObjectInstance(name).getClassName();
  88. } catch (Exception re) {
  89. return null;
  90. /* In principle the MBean does exist because otherwise we
  91. wouldn't be evaluating the query on it. But it could
  92. potentially have disappeared in between the time we
  93. discovered it and the time the query is evaluated.
  94. Also, the exception could be a SecurityException.
  95. Returning null from here will cause
  96. BadAttributeValueExpException, which will in turn cause
  97. this MBean to be omitted from the query result. */
  98. }
  99. }
  100. }