KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > openmbean > OpenType


1 /*
2  * @(#)OpenType.java 3.28 06/07/27
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package javax.management.openmbean;
10
11
12 // java import
13
//
14
import java.io.IOException JavaDoc;
15 import java.io.ObjectInputStream JavaDoc;
16 import java.io.Serializable JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 // jmx import
23
//
24

25
26 /**
27  * The <code>OpenType</code> class is the parent abstract class of all classes which describe the actual <i>open type</i>
28  * of open data values.
29  * <p>
30  * An <i>open type</i> is defined by:
31  * <ul>
32  * <li>the fully qualified Java class name of the open data values this type describes;
33  * note that only a limited set of Java classes is allowed for open data values
34  * (see {@link #ALLOWED_CLASSNAMES ALLOWED_CLASSNAMES}),</li>
35  * <li>its name,</li>
36  * <li>its description.</li>
37  * </ul>
38  *
39  * @version 3.28 06/07/27
40  * @author Sun Microsystems, Inc.
41  *
42  * @since 1.5
43  * @since.unbundled JMX 1.1
44  */

45 public abstract class OpenType implements Serializable JavaDoc {
46
47     /* Serial version */
48     static final long serialVersionUID = -9195195325186646468L;
49
50
51     /**
52      * The ALLOWED_CLASSNAMES array is kept for compatibility reasons, but:
53      * our implementation should use only ALLOWED_CLASSNAMES_LIST which is
54      * unmodifiable, and not use the ALLOWED_CLASSNAMES array which is
55      * modifiable by external code.
56      */

57     static final List JavaDoc<String JavaDoc> ALLOWED_CLASSNAMES_LIST =
58       Collections.unmodifiableList(
59         Arrays.asList(
60       "java.lang.Void",
61       "java.lang.Boolean",
62       "java.lang.Character",
63       "java.lang.Byte",
64       "java.lang.Short",
65       "java.lang.Integer",
66       "java.lang.Long",
67       "java.lang.Float",
68       "java.lang.Double",
69       "java.lang.String",
70       "java.math.BigDecimal",
71       "java.math.BigInteger",
72       "java.util.Date",
73       "javax.management.ObjectName",
74       CompositeData JavaDoc.class.getName(), // better refer to these two class names like this, rather than hardcoding a string,
75
TabularData JavaDoc.class.getName()) ); // in case the package of these classes should change (who knows...)
76

77     /**
78      * List of the fully qualified names of the Java classes allowed for open data values.
79      * A multidimensional array of any one of these classes is also an allowed for open data values.
80      *
81        <pre>ALLOWED_CLASSNAMES = {
82     "java.lang.Void",
83     "java.lang.Boolean",
84     "java.lang.Character",
85     "java.lang.Byte",
86     "java.lang.Short",
87     "java.lang.Integer",
88     "java.lang.Long",
89     "java.lang.Float",
90     "java.lang.Double",
91     "java.lang.String",
92     "java.math.BigDecimal",
93     "java.math.BigInteger",
94     "java.util.Date",
95     "javax.management.ObjectName",
96     CompositeData.class.getName(),
97     TabularData.class.getName() } ;
98        </pre>
99      *
100      */

101     public static final String JavaDoc[] ALLOWED_CLASSNAMES =
102     ALLOWED_CLASSNAMES_LIST.toArray(new String JavaDoc[0]);
103
104     /**
105      * @serial The fully qualified Java class name of open data values this type describes.
106      */

107     private String JavaDoc className;
108  
109     /**
110      * @serial The type description (should not be null or empty).
111      */

112     private String JavaDoc description;
113
114     /**
115      * @serial The name given to this type (should not be null or empty).
116      */

117     private String JavaDoc typeName;
118
119     /**
120      * @serial Tells if this type describes an array (checked in constructor).
121      */

122     private transient boolean isArray = false;
123  
124
125     /* *** Constructor *** */
126
127     /**
128      * Constructs an <code>OpenType</code> instance (actually a subclass instance as <code>OpenType</code> is abstract),
129      * checking for the validity of the given parameters.
130      * The validity constraints are described below for each parameter.
131      * <br>&nbsp;
132      * @param className The fully qualified Java class name of the open data values this open type describes.
133      * The valid Java class names allowed for open data values are listed in
134      * {@link #ALLOWED_CLASSNAMES ALLOWED_CLASSNAMES}.
135      * A multidimensional array of any one of these classes is also an allowed class,
136      * in which case the class name follows the rules defined by the method
137      * {@link Class#getName() getName()} of <code>java.lang.Class</code>.
138      * For example, a 3-dimensional array of Strings has for class name
139      * &quot;<code>[[[Ljava.lang.String;</code>&quot; (without the quotes).
140      * <br>&nbsp;
141      * @param typeName The name given to the open type this instance represents; cannot be a null or empty string.
142      * <br>&nbsp;
143      * @param description The human readable description of the open type this instance represents;
144      * cannot be a null or empty string.
145      * <br>&nbsp;
146      * @throws IllegalArgumentException if <var>className</var>, <var>typeName</var> or <var>description</var>
147      * is a null or empty string
148      * <br>&nbsp;
149      * @throws OpenDataException if <var>className</var> is not one of the allowed Java class names for open data
150      */

151     protected OpenType(String JavaDoc className,
152                String JavaDoc typeName,
153                String JavaDoc description) throws OpenDataException JavaDoc {
154     
155     // Check parameters that cannot be null or empty
156
//
157
if ( (className == null) || (className.trim().equals("")) ) {
158         throw new IllegalArgumentException JavaDoc("Argument className cannot be null or empty.");
159     }
160     if ( (typeName == null) || (typeName.trim().equals("")) ) {
161         throw new IllegalArgumentException JavaDoc("Argument typeName cannot be null or empty.");
162     }
163     if ( (description == null) || (description.trim().equals("")) ) {
164         throw new IllegalArgumentException JavaDoc("Argument description cannot be null or empty.");
165     }
166
167     // remove leading and trailing white spaces, if any
168
//
169
className = className.trim();
170     typeName = typeName.trim();
171     description = description.trim();
172
173     // Check if className describes an array class, and determines its elements' class name.
174
// (eg: a 3-dimensional array of Strings has for class name: "[[[Ljava.lang.String;")
175
//
176
int n = 0;
177     while (className.startsWith("[", n)) {
178         n++;
179     }
180     String JavaDoc eltClassName; // class name of array elements
181
boolean isArray = false;
182     if (n > 0) {
183         // removes the n leading '[' + the 'L' characters and the last ';' character
184
eltClassName = className.substring(n+1, className.length()-1); // see javadoc of String.substring(begin,end)
185
isArray = true;
186     } else {
187         // not an array
188
eltClassName = className;
189     }
190
191     // Check that eltClassName's value is one of the allowed basic data types for open data
192
//
193
if ( ! ALLOWED_CLASSNAMES_LIST.contains(eltClassName) ) {
194         throw new OpenDataException JavaDoc("Argument className=\""+ className +
195                     "\" is not one of the allowed Java class names for open data.");
196     }
197
198     // Now initializes this OpenType instance
199
//
200
this.className = className;
201     this.typeName = typeName;
202     this.description = description;
203     this.isArray = isArray;
204     }
205  
206
207     /* *** Open type information methods *** */
208
209     /**
210      * Returns the fully qualified Java class name of the open data values this open type describes.
211      * The only possible Java class names for open data values are listed in
212      * {@link #ALLOWED_CLASSNAMES ALLOWED_CLASSNAMES}.
213      * A multidimensional array of any one of these classes is also an allowed class,
214      * in which case the class name follows the rules defined by the method
215      * {@link Class#getName() getName()} of <code>java.lang.Class</code>.
216      * For example, a 3-dimensional array of Strings has for class name
217      * &quot;<code>[[[Ljava.lang.String;</code>&quot; (without the quotes).
218      *
219      * @return the class name.
220      */

221     public String JavaDoc getClassName() {
222
223     return className;
224     }
225
226     /**
227      * Returns the name of this <code>OpenType</code> instance.
228      *
229      * @return the type name.
230      */

231     public String JavaDoc getTypeName() {
232
233     return typeName;
234     }
235
236     /**
237      * Returns the text description of this <code>OpenType</code> instance.
238      *
239      * @return the description.
240      */

241     public String JavaDoc getDescription() {
242
243     return description;
244     }
245
246     /**
247      * Returns <code>true</code> if the open data values this open
248      * type describes are arrays, <code>false</code> otherwise.
249      *
250      * @return true if this is an array type.
251      */

252     public boolean isArray() {
253
254     return isArray;
255     }
256
257     /**
258      * Tests whether <var>obj</var> is a value for this open type.
259      *
260      * @param obj the object to be tested for validity.
261      *
262      * @return <code>true</code> if <var>obj</var> is a value for this
263      * open type, <code>false</code> otherwise.
264      */

265     public abstract boolean isValue(Object JavaDoc obj) ;
266
267
268     /* *** Methods overriden from class Object *** */
269
270     /**
271      * Compares the specified <code>obj</code> parameter with this
272      * open type instance for equality.
273      *
274      * @param obj the object to compare to.
275      *
276      * @return true if this object and <code>obj</code> are equal.
277      */

278     public abstract boolean equals(Object JavaDoc obj) ;
279
280     public abstract int hashCode() ;
281
282     /**
283      * Returns a string representation of this open type instance.
284      *
285      * @return the string representation.
286      */

287     public abstract String JavaDoc toString() ;
288
289     /**
290      * Deserializes an {@link OpenType} from an {@link ObjectInputStream}.
291      */

292     private void readObject(ObjectInputStream JavaDoc in)
293         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
294       in.defaultReadObject();
295       isArray = (className.startsWith("["));
296     }
297
298 }
299
300
Popular Tags