001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.openmbean;
008:
009: import java.io.IOException;
010: import java.io.ObjectInputStream;
011: import java.io.Serializable;
012: import java.io.StreamCorruptedException;
013:
014: import mx4j.util.Utils;
015:
016: /**
017: * @version $Revision: 1.12 $
018: */
019: public abstract class OpenType implements Serializable {
020: private static final long serialVersionUID = -9195195325186646468L;
021:
022: public static final String[] ALLOWED_CLASSNAMES = {
023: "java.lang.Void", "java.lang.Boolean", "java.lang.Byte",
024: "java.lang.Character", "java.lang.Short",
025: "java.lang.Integer", "java.lang.Long", "java.lang.Float",
026: "java.lang.Double", "java.lang.String",
027: "java.math.BigDecimal", "java.math.BigInteger",
028: "java.util.Date", "javax.management.ObjectName",
029: CompositeData.class.getName(), TabularData.class.getName() };
030:
031: private String className = null;
032: private String typeName = null;
033: private String description = null;
034:
035: protected OpenType(String className, String typeName,
036: String description) throws OpenDataException {
037: initialize(className, typeName, description);
038: }
039:
040: /**
041: * private convenience method for the readObject method to call so that code is not being duplicated in the constructor and the readObject methods
042: */
043: private void initialize(String className, String typeName,
044: String description) throws OpenDataException {
045: if (className == null)
046: throw new IllegalArgumentException(
047: "null className is invalid");
048: if (typeName == null)
049: throw new IllegalArgumentException(
050: "null typeName is invalid");
051: if (description == null)
052: throw new IllegalArgumentException(
053: "null description is invalid");
054: if (!(validateClass(className)))
055: throw new OpenDataException(
056: "Class does not represent one of the allowed className types");
057: this .className = className;
058: this .typeName = typeName;
059: this .description = description;
060: }
061:
062: /**
063: * private validate method just to do some type checking with regards to that the entered className matches the array of allowed types
064: */
065: private boolean validateClass(String className) {
066: /** test we do not have an array if it is it is allowed but it is not listed in the allowed types (be too long) so remove the array
067: * indicator [[[[[L etc and the ; at the end to test if the array type is of the correct class listed above.
068: */
069: if (className.startsWith("["))
070: className = className.substring(className.indexOf("L") + 1,
071: (className.length() - 1));
072:
073: for (int i = 0; i < ALLOWED_CLASSNAMES.length; i++) {
074: if (className.equals(ALLOWED_CLASSNAMES[i]))
075: return true;
076: }
077: return false;
078: }
079:
080: /**
081: * Retrieve the className
082: *
083: * @return the name of the class
084: */
085: public String getClassName() {
086: return className;
087: }
088:
089: /**
090: * Retrieve the description
091: *
092: * @return description
093: */
094: public String getDescription() {
095: return description;
096: }
097:
098: /**
099: * Retrieve the typeName
100: *
101: * @return typeName
102: */
103: public String getTypeName() {
104: return typeName;
105: }
106:
107: /**
108: * check if this instance represents an array or not
109: *
110: * @return true if the class represents an array, false otherwise
111: */
112: public boolean isArray() {
113: Class c = null;
114: try {
115: c = Utils.loadClass(Thread.currentThread()
116: .getContextClassLoader(), className);
117: } catch (ClassNotFoundException e) {
118: return false;
119: }
120: return c.isArray();
121: }
122:
123: /**
124: * read out object, validate it using the same methods the constructor uses to validate its' invariants
125: */
126: private void readObject(ObjectInputStream inputStream)
127: throws IOException, ClassNotFoundException {
128: inputStream.defaultReadObject();
129: try {
130: initialize(className, typeName, description);
131: } catch (OpenDataException e) {
132: throw new StreamCorruptedException(
133: "The object read from the ObjectInputStream during deserialization is not valid");
134: }
135: }
136:
137: public abstract boolean isValue(Object object);
138:
139: public abstract boolean equals(Object object);
140:
141: public abstract int hashCode();
142:
143: public abstract String toString();
144: }
|