001 /* OpenType.java -- Superclass of all open types.
002 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package javax.management.openmbean;
039
040 import java.io.Serializable;
041
042 import java.util.Arrays;
043 import java.util.List;
044
045 /**
046 * The superclass of all open types, which describe the
047 * applicable data values for open MBeans. An open type
048 * is defined by its name and description, and the name
049 * of the Java class it maps to.
050 *
051 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
052 * @since 1.5
053 */
054 public abstract class OpenType<T>
055 implements Serializable
056 {
057
058 /**
059 * Compatible with JDK 1.5
060 */
061 private static final long serialVersionUID = -9195195325186646468L;
062
063 /**
064 * The name of the Java class this type represents.
065 */
066 private String className;
067
068 /**
069 * The name of this type.
070 */
071 private String typeName;
072
073 /**
074 * A description of this type.
075 */
076 private String description;
077
078 /**
079 * An array which defines the set of Java types which can be
080 * used as open types. Note that each type is also available
081 * in array form, possibly with multiple dimensions.
082 *
083 * @deprecated Use {@link ALLOWED_CLASSNAMES_LIST} instead.
084 */
085 @Deprecated
086 public static final String[] ALLOWED_CLASSNAMES = {
087 "java.lang.Void",
088 "java.lang.Boolean",
089 "java.lang.Character",
090 "java.lang.Byte",
091 "java.lang.Short",
092 "java.lang.Integer",
093 "java.lang.Long",
094 "java.lang.Float",
095 "java.lang.Double",
096 "java.lang.String",
097 "java.math.BigDecimal",
098 "java.math.BigInteger",
099 "java.util.Date",
100 "javax.management.ObjectName",
101 CompositeData.class.getName(),
102 TabularData.class.getName()
103 };
104
105 /**
106 * A list which defines the set of Java types that may be
107 * used as open types. Note that each type is also available
108 * in array form, possibly with multiple dimensions.
109 */
110 public static final List<String> ALLOWED_CLASSNAMES_LIST =
111 Arrays.asList(ALLOWED_CLASSNAMES);
112
113 /**
114 * Constructs a new {@link OpenType} for the specified class
115 * with the given name and description. The name of the class
116 * must be taken from the list of {@link ALLOWED_CLASSNAMES}.
117 * Arrays are implictly included in this, and follow the usual
118 * syntax of {@link java.lang.Class#getName()} with the name
119 * preceded by n instances of '[' (where n is the number of
120 * dimensions) and an L. The name and description can not be
121 * <code>null</code> or the empty string.
122 *
123 * @param className the name of the Java class this type
124 * represents.
125 * @param name the name of the type.
126 * @param desc the description of the type.
127 * @throws IllegalArgumentException if either of <code>name</code>
128 * or <code>desc</code> are
129 * <code>null</code> or the empty
130 * string.
131 * @throws OpenDataException if the class name does not reference
132 * a listed class (from @{link ALLOWED_CLASSNAMES})
133 */
134 protected OpenType(String className, String name, String desc)
135 throws OpenDataException
136 {
137 if (name == null || name.equals(""))
138 throw new IllegalArgumentException("The name can not be null " +
139 "or the empty string.");
140 if (desc == null || desc.equals(""))
141 throw new IllegalArgumentException("The description can not " +
142 "be null or the empty string.");
143 Class<?> type;
144 try
145 {
146 type = Class.forName(className);
147 }
148 catch (ClassNotFoundException e)
149 {
150 throw (OpenDataException) new OpenDataException("The class name, " + className +
151 ", is unavailable.").initCause(e);
152 }
153 while (type.isArray())
154 type = type.getComponentType();
155 if (!(type.isPrimitive() || ALLOWED_CLASSNAMES_LIST.contains(type.getName())))
156 throw new OpenDataException("The class name, " + className +
157 ", does not specify a valid open type.");
158 this.className = className;
159 typeName = name;
160 description = desc;
161 }
162
163 /**
164 * Performs an equality test on this object and the one specified.
165 *
166 * @param obj the object to test against this one.
167 * @return true if the two objects are equivalent.
168 * @see java.lang.Object#hashCode()
169 */
170 public abstract boolean equals(Object obj);
171
172 /**
173 * Returns the name of the Java class this type represents. This must
174 * be one of the {@link ALLOWED_CLASSNAMES} or an array of one of them.
175 * The specification of arrays follows the standard set by
176 * {@link java.lang.Class#getName()} i.e. the name is the class name
177 * preceded by n instances of '[' and an 'L', where n is number of
178 * dimensions used by the array.
179 *
180 * @return the class name.
181 */
182 public String getClassName()
183 {
184 return className;
185 }
186
187 /**
188 * Returns a description of this open type.
189 *
190 * @return the description.
191 */
192 public String getDescription()
193 {
194 return description;
195 }
196
197 /**
198 * Returns the name of this open type.
199 *
200 * @return the type name.
201 */
202 public String getTypeName()
203 {
204 return typeName;
205 }
206
207 /**
208 * Returns a hash code for this open type. The hash code
209 * should be consistent with the {@link equals()} method.
210 * Thus, it should continue to return the same value while
211 * the values used by the {@link equals()} method remain
212 * the same, and should return different hash codes for
213 * objects which are judged to be different using the
214 * {@link equals()} method.
215 *
216 * @return the hash code of this instance.
217 */
218 public abstract int hashCode();
219
220 /**
221 * Returns true if this open type represents an array type.
222 *
223 * @return true if this open type represents an array type.
224 */
225 public boolean isArray()
226 {
227 return className.startsWith("[");
228 }
229
230 /**
231 * Returns true if the specified object is a member of this
232 * type.
233 *
234 * @param obj the object to test for membership.
235 * @return true if the object is a member of this type.
236 */
237 public abstract boolean isValue(Object obj);
238
239 /**
240 * Returns a textual representation of this type.
241 *
242 * @return a {@link java.lang.String} representation of this
243 * type.
244 */
245 public abstract String toString();
246
247 }