1. /*
  2. * @(#)file InvalidTargetObjectTypeException.java
  3. * @(#)author IBM Corp.
  4. * @(#)version 1.26
  5. * @(#)lastedit 03/12/19
  6. */
  7. /*
  8. * Copyright IBM Corp. 1999-2000. All rights reserved.
  9. *
  10. * The program is provided "as is" without any warranty express or implied,
  11. * including the warranty of non-infringement and the implied warranties of
  12. * merchantibility and fitness for a particular purpose. IBM will not be
  13. * liable for any damages suffered by you or any third party claim against
  14. * you regarding the Program.
  15. *
  16. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  17. * This software is the proprietary information of Sun Microsystems, Inc.
  18. * Use is subject to license terms.
  19. *
  20. * Copyright 2004 Sun Microsystems, Inc. Tous droits reserves.
  21. * Ce logiciel est propriete de Sun Microsystems, Inc.
  22. * Distribue par des licences qui en restreignent l'utilisation.
  23. *
  24. */
  25. package javax.management.modelmbean;
  26. import java.io.IOException;
  27. import java.io.ObjectInputStream;
  28. import java.io.ObjectOutputStream;
  29. import java.io.ObjectStreamField;
  30. import java.security.AccessController;
  31. import java.security.PrivilegedAction;
  32. import com.sun.jmx.mbeanserver.GetPropertyAction;
  33. /**
  34. * Exception thrown when an invalid target object type is specified.
  35. *
  36. *
  37. * @since 1.5
  38. */
  39. public class InvalidTargetObjectTypeException extends Exception
  40. {
  41. // Serialization compatibility stuff:
  42. // Two serial forms are supported in this class. The selected form depends
  43. // on system property "jmx.serial.form":
  44. // - "1.0" for JMX 1.0
  45. // - any other value for JMX 1.1 and higher
  46. //
  47. // Serial version for old serial form
  48. private static final long oldSerialVersionUID = 3711724570458346634L;
  49. //
  50. // Serial version for new serial form
  51. private static final long newSerialVersionUID = 1190536278266811217L;
  52. //
  53. // Serializable fields in old serial form
  54. private static final ObjectStreamField[] oldSerialPersistentFields =
  55. {
  56. new ObjectStreamField("msgStr", String.class),
  57. new ObjectStreamField("relatedExcept", Exception.class)
  58. };
  59. //
  60. // Serializable fields in new serial form
  61. private static final ObjectStreamField[] newSerialPersistentFields =
  62. {
  63. new ObjectStreamField("exception", Exception.class)
  64. };
  65. //
  66. // Actual serial version and serial form
  67. private static final long serialVersionUID;
  68. /**
  69. * @serialField exception Exception Encapsulated {@link Exception}
  70. */
  71. private static final ObjectStreamField[] serialPersistentFields;
  72. private static boolean compat = false;
  73. static {
  74. try {
  75. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  76. String form = (String) AccessController.doPrivileged(act);
  77. compat = (form != null && form.equals("1.0"));
  78. } catch (Exception e) {
  79. // OK: No compat with 1.0
  80. }
  81. if (compat) {
  82. serialPersistentFields = oldSerialPersistentFields;
  83. serialVersionUID = oldSerialVersionUID;
  84. } else {
  85. serialPersistentFields = newSerialPersistentFields;
  86. serialVersionUID = newSerialVersionUID;
  87. }
  88. }
  89. //
  90. // END Serialization compatibility stuff
  91. /**
  92. * @serial Encapsulated {@link Exception}
  93. */
  94. Exception exception;
  95. /**
  96. * Default constructor.
  97. */
  98. public InvalidTargetObjectTypeException ()
  99. {
  100. super("InvalidTargetObjectTypeException: ");
  101. exception = null;
  102. }
  103. /**
  104. * Constructor from a string.
  105. *
  106. * @param s String value that will be incorporated in the message for
  107. * this exception.
  108. */
  109. public InvalidTargetObjectTypeException (String s)
  110. {
  111. super("InvalidTargetObjectTypeException: " + s);
  112. exception = null;
  113. }
  114. /**
  115. * Constructor taking an exception and a string.
  116. *
  117. * @param e Exception that we may have caught to reissue as an
  118. * InvalidTargetObjectTypeException. The message will be used, and we may want to
  119. * consider overriding the printStackTrace() methods to get data
  120. * pointing back to original throw stack.
  121. * @param s String value that will be incorporated in message for
  122. * this exception.
  123. */
  124. public InvalidTargetObjectTypeException (Exception e, String s)
  125. {
  126. super("InvalidTargetObjectTypeException: " +
  127. s +
  128. ((e != null)?("\n\t triggered by:" + e.toString()):""));
  129. exception = e;
  130. }
  131. /**
  132. * Deserializes an {@link InvalidTargetObjectTypeException} from an {@link ObjectInputStream}.
  133. */
  134. private void readObject(ObjectInputStream in)
  135. throws IOException, ClassNotFoundException {
  136. if (compat)
  137. {
  138. // Read an object serialized in the old serial form
  139. //
  140. ObjectInputStream.GetField fields = in.readFields();
  141. exception = (Exception) fields.get("relatedExcept", null);
  142. if (fields.defaulted("relatedExcept"))
  143. {
  144. throw new NullPointerException("relatedExcept");
  145. }
  146. }
  147. else
  148. {
  149. // Read an object serialized in the new serial form
  150. //
  151. in.defaultReadObject();
  152. }
  153. }
  154. /**
  155. * Serializes an {@link InvalidTargetObjectTypeException} to an {@link ObjectOutputStream}.
  156. */
  157. private void writeObject(ObjectOutputStream out)
  158. throws IOException {
  159. if (compat)
  160. {
  161. // Serializes this instance in the old serial form
  162. //
  163. ObjectOutputStream.PutField fields = out.putFields();
  164. fields.put("relatedExcept", exception);
  165. fields.put("msgStr", ((exception != null)?exception.getMessage():""));
  166. out.writeFields();
  167. }
  168. else
  169. {
  170. // Serializes this instance in the new serial form
  171. //
  172. out.defaultWriteObject();
  173. }
  174. }
  175. }