blob: c101064b4487d4df0f434aa9d831793b6cd8f168 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25/*
26 * @author IBM Corp.
27 *
28 * Copyright IBM Corp. 1999-2000. All rights reserved.
29 */
30
31
32package javax.management.modelmbean;
33
34import com.sun.jmx.mbeanserver.GetPropertyAction;
35
36import java.io.IOException;
37import java.io.ObjectInputStream;
38import java.io.ObjectOutputStream;
39import java.io.ObjectStreamField;
40import java.security.AccessController;
41
42/**
43* This exception is thrown when an XML formatted string is being parsed into ModelMBean objects
44* or when XML formatted strings are being created from ModelMBean objects.
45*
46* It is also used to wrapper exceptions from XML parsers that may be used.
47*
48* <p>The <b>serialVersionUID</b> of this class is <code>3176664577895105181L</code>.
49*
50* @since 1.5
51*/
52@SuppressWarnings("serial") // serialVersionUID not constant
53public class XMLParseException
54extends Exception
55{
56 // Serialization compatibility stuff:
57 // Two serial forms are supported in this class. The selected form depends
58 // on system property "jmx.serial.form":
59 // - "1.0" for JMX 1.0
60 // - any other value for JMX 1.1 and higher
61 //
62 // Serial version for old serial form
63 private static final long oldSerialVersionUID = -7780049316655891976L;
64 //
65 // Serial version for new serial form
66 private static final long newSerialVersionUID = 3176664577895105181L;
67 //
68 // Serializable fields in old serial form
69 private static final ObjectStreamField[] oldSerialPersistentFields =
70 {
71 new ObjectStreamField("msgStr", String.class)
72 };
73 //
74 // Serializable fields in new serial form
75 private static final ObjectStreamField[] newSerialPersistentFields = { };
76 //
77 // Actual serial version and serial form
78 private static final long serialVersionUID;
79 private static final ObjectStreamField[] serialPersistentFields;
80 private static boolean compat = false;
81 static {
82 try {
83 GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
84 String form = AccessController.doPrivileged(act);
85 compat = (form != null && form.equals("1.0"));
86 } catch (Exception e) {
87 // OK: No compat with 1.0
88 }
89 if (compat) {
90 serialPersistentFields = oldSerialPersistentFields;
91 serialVersionUID = oldSerialVersionUID;
92 } else {
93 serialPersistentFields = newSerialPersistentFields;
94 serialVersionUID = newSerialVersionUID;
95 }
96 }
97 //
98 // END Serialization compatibility stuff
99
100 /**
101 * Default constructor .
102 */
103 public XMLParseException ()
104 {
105 super("XML Parse Exception.");
106 }
107
108 /**
109 * Constructor taking a string.
110 *
111 * @param s the detail message.
112 */
113 public XMLParseException (String s)
114 {
115 super("XML Parse Exception: " + s);
116 }
117 /**
118 * Constructor taking a string and an exception.
119 *
120 * @param e the nested exception.
121 * @param s the detail message.
122 */
123 public XMLParseException (Exception e, String s)
124 {
125 super("XML Parse Exception: " + s + ":" + e.toString());
126 }
127
128 /**
129 * Deserializes an {@link XMLParseException} from an {@link ObjectInputStream}.
130 */
131 private void readObject(ObjectInputStream in)
132 throws IOException, ClassNotFoundException {
133 // New serial form ignores extra field "msgStr"
134 in.defaultReadObject();
135 }
136
137
138 /**
139 * Serializes an {@link XMLParseException} to an {@link ObjectOutputStream}.
140 */
141 private void writeObject(ObjectOutputStream out)
142 throws IOException {
143 if (compat)
144 {
145 // Serializes this instance in the old serial form
146 //
147 ObjectOutputStream.PutField fields = out.putFields();
148 fields.put("msgStr", getMessage());
149 out.writeFields();
150 }
151 else
152 {
153 // Serializes this instance in the new serial form
154 //
155 out.defaultWriteObject();
156 }
157 }
158}