1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package javax.management.openmbean;
8
9 import java.io.Serializable;
10
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import javax.management.MBeanAttributeInfo;
17
18 /**
19 * OpenMBeanAttributeInfo implementation
20 *
21 * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
22 *
23 * @version $Revision: 1.1.2.1 $
24 *
25 */
26 public class OpenMBeanAttributeInfoSupport
27 extends MBeanAttributeInfo
28 implements OpenMBeanAttributeInfo, Serializable
29 {
30 // Constants -----------------------------------------------------
31
32 private static final long serialVersionUID = -4867215622149721849L;
33
34 // Attributes ----------------------------------------------------
35
36 /**
37 * The OpenType of this attribute
38 */
39 private OpenType openType;
40
41 /**
42 * The default value of this attribute
43 */
44 private Object defaultValue;
45
46 /**
47 * The legal values of this attribute
48 */
49 private Set legalValues;
50
51 /**
52 * The minimum value of this attribute
53 */
54 private Comparable minValue;
55
56 /**
57 * The maximum value of this attribute
58 */
59 private Comparable maxValue;
60
61 private transient int cachedHashCode;
62
63 private transient String cachedToString;
64
65 // Static --------------------------------------------------------
66
67 // Constructors --------------------------------------------------
68
69 /**
70 * Contruct an OpenMBeanAttributeInfoSupport<p>
71 *
72 * @param name cannot be null or empty
73 * @param description cannot be null or empty
74 * @param openType cannot be null
75 * @param isReadable true when the getter is exposed for management
76 * @param isWritable true when the setter is exposed for management
77 * @param isIs true when the getter is of the form isXXX
78 * @exception IllegalArgumentException when one of the above
79 * constraints is not satisfied
80 */
81 public OpenMBeanAttributeInfoSupport(String name, String description,
82 OpenType openType, boolean isReadable,
83 boolean isWritable, boolean isIs)
84 {
85 super(name, openType == null ? null : openType.getClassName(),
86 description, isReadable, isWritable, isIs);
87 try
88 {
89 init(name, description, openType, isReadable, isWritable, isIs, null,
90 null, null, null);
91 }
92 catch (OpenDataException notRelevent)
93 {
94 }
95 }
96
97 /**
98 * Contruct an OpenMBeanAttributeInfoSupport<p>
99 *
100 * @param name cannot be null or empty
101 * @param description cannot be null or empty
102 * @param openType cannot be null
103 * @param isReadable true when the getter is exposed for management
104 * @param isWritable true when the setter is exposed for management
105 * @param isIs true when the getter is of the form isXXX
106 * @param defaultValue the default value
107 * @exception IllegalArgumentException when one of the above
108 * constraints is not satisfied
109 * @exception OpenDataException when default value is not correct for
110 * the open type or cannot specify a default value for
111 * ArrayType and TabularType
112 */
113 public OpenMBeanAttributeInfoSupport(String name, String description,
114 OpenType openType, boolean isReadable,
115 boolean isWritable, boolean isIs,
116 Object defaultValue)
117 throws OpenDataException
118 {
119 super(name, openType == null ? null : openType.getClassName(),
120 description, isReadable, isWritable, isIs);
121 init(name, description, openType, isReadable, isWritable, isIs, defaultValue,
122 null, null, null);
123 }
124
125 /**
126 * Contruct an OpenMBeanAttributeInfoSupport<p>
127 *
128 * @param name cannot be null or empty
129 * @param description cannot be null or empty
130 * @param openType cannot be null
131 * @param isReadable true when the getter is exposed for management
132 * @param isWritable true when the setter is exposed for management
133 * @param isIs true when the getter is of the form isXXX
134 * @param defaultValue the default value
135 * @param legalValues an array of legal values
136 * @exception IllegalArgumentException when one of the above
137 * constraints is not satisfied
138 */
139 public OpenMBeanAttributeInfoSupport(String name, String description,
140 OpenType openType, boolean isReadable,
141 boolean isWritable, boolean isIs,
142 Object defaultValue, Object[] legalValues)
143 throws OpenDataException
144 {
145 super(name, openType == null ? null : openType.getClassName(),
146 description, isReadable, isWritable, isIs);
147 init(name, description, openType, isReadable, isWritable, isIs, defaultValue,
148 legalValues, null, null);
149 }
150
151 /**
152 * Contruct an OpenMBeanAttributeInfoSupport<p>
153 *
154 * @param name cannot be null or empty
155 * @param description cannot be null or empty
156 * @param openType cannot be null
157 * @param isReadable true when the getter is exposed for management
158 * @param isWritable true when the setter is exposed for management
159 * @param isIs true when the getter is of the form isXXX
160 * @param defaultValue the default value
161 * @param minValue the minimum value
162 * @param maxValue the maximum value
163 * @exception IllegalArgumentException when one of the above
164 * constraints is not satisfied
165 */
166 public OpenMBeanAttributeInfoSupport(String name, String description,
167 OpenType openType, boolean isReadable,
168 boolean isWritable, boolean isIs,
169 Object defaultValue, Comparable minValue,
170 Comparable maxValue)
171 throws OpenDataException
172 {
173 super(name, openType == null ? null : openType.getClassName(),
174 description, isReadable, isWritable, isIs);
175 init(name, description, openType, isReadable, isWritable, isIs, defaultValue,
176 null, minValue, maxValue);
177 }
178
179 // Public --------------------------------------------------------
180
181 // OpenMBeanAttributeInfo Implementation -------------------------
182
183 public Object getDefaultValue()
184 {
185 return defaultValue;
186 }
187
188 public Set getLegalValues()
189 {
190 return legalValues;
191 }
192
193 public Comparable getMinValue()
194 {
195 return minValue;
196 }
197
198 public Comparable getMaxValue()
199 {
200 return maxValue;
201 }
202
203 public OpenType getOpenType()
204 {
205 return openType;
206 }
207
208 public boolean hasDefaultValue()
209 {
210 return (defaultValue != null);
211 }
212
213 public boolean hasLegalValues()
214 {
215 return (legalValues != null);
216 }
217
218 public boolean hasMinValue()
219 {
220 return (minValue != null);
221 }
222
223 public boolean hasMaxValue()
224 {
225 return (maxValue != null);
226 }
227
228 public boolean isValue(Object obj)
229 {
230 if (openType.isValue(obj) == false)
231 return false;
232 if (minValue != null && minValue.compareTo(obj) > 0)
233 return false;
234 if (maxValue != null && maxValue.compareTo(obj) < 0)
235 return false;
236 if (legalValues != null && legalValues.contains(obj) == false)
237 return false;
238 return true;
239 }
240
241 // Object Overrides ----------------------------------------------
242
243 public boolean equals(Object obj)
244 {
245 if (this == obj)
246 return true;
247 if (obj == null || !(obj instanceof OpenMBeanAttributeInfo))
248 return false;
249 OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj;
250
251 if (this.getName().equals(other.getName()) == false)
252 return false;
253
254 if (this.getOpenType().equals(other.getOpenType()) == false)
255 return false;
256
257 if (isReadable() != other.isReadable())
258 return false;
259
260 if (isWritable() != other.isWritable())
261 return false;
262
263 if (isIs() != other.isIs())
264 return false;
265
266 if (hasDefaultValue() == false && other.hasDefaultValue() == true)
267 return false;
268 if (hasDefaultValue() == true && this.getDefaultValue().equals(other.getDefaultValue()) == false)
269 return false;
270
271 if (hasMinValue() == false && other.hasMinValue() == true)
272 return false;
273 if (hasMinValue() == true && this.getMinValue().equals(other.getMinValue()) == false )
274 return false;
275
276 if (hasMaxValue() == false && other.hasMaxValue() == true)
277 return false;
278 if (hasMaxValue() == true && this.getMaxValue().equals(other.getMaxValue()) == false)
279 return false;
280
281 if (hasLegalValues() == false && other.hasLegalValues() == true)
282 return false;
283 if (hasLegalValues() == true)
284 {
285 Set otherLegal = other.getLegalValues();
286 if (otherLegal == null)
287 return false;
288 Set thisLegal = this.getLegalValues();
289 if (thisLegal.size() != otherLegal.size())
290 return false;
291 if (thisLegal.containsAll(otherLegal) == false)
292 return false;
293 }
294 return true;
295 }
296
297 public int hashCode()
298 {
299 if (cachedHashCode != 0)
300 return cachedHashCode;
301 cachedHashCode = getName().hashCode();
302 cachedHashCode += getOpenType().hashCode();
303 if (defaultValue != null)
304 cachedHashCode += getDefaultValue().hashCode();
305 if (minValue != null)
306 cachedHashCode += getMinValue().hashCode();
307 if (maxValue != null)
308 cachedHashCode += getMaxValue().hashCode();
309 if (legalValues != null)
310 cachedHashCode += getLegalValues().hashCode();
311 return cachedHashCode;
312 }
313
314 public String toString()
315 {
316 if (cachedToString != null)
317 return cachedToString;
318 StringBuffer buffer = new StringBuffer(getClass().getName());
319 buffer.append(": name=");
320 buffer.append(getName());
321 buffer.append(", openType=");
322 buffer.append(getOpenType());
323 buffer.append(", isWritable=");
324 buffer.append(isWritable());
325 buffer.append(", isReadable=");
326 buffer.append(isReadable());
327 buffer.append(", isIs=");
328 buffer.append(isIs());
329 buffer.append(", defaultValue=");
330 buffer.append(getDefaultValue());
331 buffer.append(", minValue=");
332 buffer.append(getMinValue());
333 buffer.append(", maxValue=");
334 buffer.append(getMaxValue());
335 buffer.append(", legalValues=");
336 buffer.append(getLegalValues());
337 cachedToString = buffer.toString();
338 return cachedToString;
339 }
340
341 // Protected -----------------------------------------------------
342
343 // Private -------------------------------------------------------
344
345 /**
346 * Initialise an OpenMBeanAttributeInfoSupport<p>
347 *
348 * WARNING: For the MBeanAttributeInfo only validation is performed
349 *
350 * @param name cannot be null or empty
351 * @param description cannot be null or empty
352 * @param openType cannot be null
353 * @param isReadable true when the getter is exposed for management
354 * @param isWritable true when the setter is exposed for management
355 * @param isIs true when the getter is of the form isXXX
356 * @param defaultValue the default value
357 * @param minValue the minimum value
358 * @param maxValue the maximum value
359 * @exception IllegalArgumentException when one of the above
360 * constraints is not satisfied
361 */
362 private void init(String name, String Description,
363 OpenType openType, boolean isReadable,
364 boolean isWritable, boolean isIs,
365 Object defaultValue, Object[] legalValues,
366 Comparable minValue, Comparable maxValue)
367 throws OpenDataException
368 {
369 if (name == null || name.trim().length() == 0)
370 throw new IllegalArgumentException("null or empty name");
371
372 if (description == null || description.trim().length() == 0)
373 throw new IllegalArgumentException("null or empty description");
374
375 if (openType == null)
376 throw new IllegalArgumentException("null open type");
377 this.openType = openType;
378
379 if (defaultValue != null &&
380 (openType instanceof ArrayType || openType instanceof TabularType))
381 throw new OpenDataException("default value is not supported for "
382 + openType.getClass().getName());
383 if (defaultValue != null && openType.isValue(defaultValue) == false)
384 throw new OpenDataException("default value is not valid for "
385 + openType.getClass().getName());
386
387 if (legalValues != null && legalValues.length != 0)
388 {
389 if (openType instanceof ArrayType || openType instanceof TabularType)
390 throw new OpenDataException("legal values are not supported for "
391 + openType.getClass().getName());
392 HashSet legals = new HashSet(legalValues.length);
393 for (int i = 0; i < legalValues.length; i++)
394 {
395 if (openType.isValue(legalValues[i]) == false)
396 throw new OpenDataException("legal value " + legalValues[i] + " at index " + i
397 + " is not valid for " + openType.getClass().getName());
398 legals.add(legalValues[i]);
399 }
400 if (defaultValue != null && legals.contains(defaultValue) == false)
401 throw new OpenDataException("default value is not a legal value");
402 this.legalValues = Collections.unmodifiableSet(legals);
403 }
404
405 if (minValue != null && openType.isValue(minValue) == false)
406 throw new OpenDataException("minimum value is not valid for "
407 + openType.getClass().getName());
408 if (defaultValue != null && minValue != null && minValue.compareTo(defaultValue) > 0)
409 throw new OpenDataException("the default value is less than the minimum value ");
410
411 if (maxValue != null && openType.isValue(maxValue) == false)
412 throw new OpenDataException("maximum value is not valid for "
413 + openType.getClass().getName());
414 if (defaultValue != null && maxValue != null && maxValue.compareTo(defaultValue) < 0)
415 throw new OpenDataException("the default value is greater than the maximum value ");
416
417 if (minValue != null && maxValue != null && minValue.compareTo(maxValue) > 0)
418 throw new OpenDataException("the minimum value is greater than the maximum value ");
419
420 this.defaultValue = defaultValue;
421 this.minValue = minValue;
422 this.maxValue = maxValue;
423 }
424
425 // Inner Classes -------------------------------------------------
426 }