001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.openmbean;
023:
024: import java.io.Serializable;
025: import java.util.Collections;
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: import javax.management.MBeanAttributeInfo;
030:
031: /**
032: * OpenMBeanAttributeInfo implementation
033: *
034: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
035: *
036: * @version $Revision: 57200 $
037: *
038: */
039: public class OpenMBeanAttributeInfoSupport extends MBeanAttributeInfo
040: implements OpenMBeanAttributeInfo, Serializable {
041: // Constants -----------------------------------------------------
042:
043: private static final long serialVersionUID = -4867215622149721849L;
044:
045: // Attributes ----------------------------------------------------
046:
047: /**
048: * The OpenType of this attribute
049: */
050: private OpenType openType;
051:
052: /**
053: * The default value of this attribute
054: */
055: private Object defaultValue;
056:
057: /**
058: * The legal values of this attribute
059: */
060: private Set legalValues;
061:
062: /**
063: * The minimum value of this attribute
064: */
065: private Comparable minValue;
066:
067: /**
068: * The maximum value of this attribute
069: */
070: private Comparable maxValue;
071:
072: private transient int cachedHashCode;
073:
074: private transient String cachedToString;
075:
076: // Static --------------------------------------------------------
077:
078: // Constructors --------------------------------------------------
079:
080: /**
081: * Contruct an OpenMBeanAttributeInfoSupport<p>
082: *
083: * @param name cannot be null or empty
084: * @param description cannot be null or empty
085: * @param openType cannot be null
086: * @param isReadable true when the getter is exposed for management
087: * @param isWritable true when the setter is exposed for management
088: * @param isIs true when the getter is of the form isXXX
089: * @exception IllegalArgumentException when one of the above
090: * constraints is not satisfied
091: */
092: public OpenMBeanAttributeInfoSupport(String name,
093: String description, OpenType openType, boolean isReadable,
094: boolean isWritable, boolean isIs) {
095: super (name, openType == null ? null : openType.getClassName(),
096: description, isReadable, isWritable, isIs);
097: try {
098: init(name, description, openType, isReadable, isWritable,
099: isIs, null, null, null, null);
100: } catch (OpenDataException notRelevent) {
101: }
102: }
103:
104: /**
105: * Contruct an OpenMBeanAttributeInfoSupport<p>
106: *
107: * @param name cannot be null or empty
108: * @param description cannot be null or empty
109: * @param openType cannot be null
110: * @param isReadable true when the getter is exposed for management
111: * @param isWritable true when the setter is exposed for management
112: * @param isIs true when the getter is of the form isXXX
113: * @param defaultValue the default value
114: * @exception IllegalArgumentException when one of the above
115: * constraints is not satisfied
116: * @exception OpenDataException when default value is not correct for
117: * the open type or cannot specify a default value for
118: * ArrayType and TabularType
119: */
120: public OpenMBeanAttributeInfoSupport(String name,
121: String description, OpenType openType, boolean isReadable,
122: boolean isWritable, boolean isIs, Object defaultValue)
123: throws OpenDataException {
124: super (name, openType == null ? null : openType.getClassName(),
125: description, isReadable, isWritable, isIs);
126: init(name, description, openType, isReadable, isWritable, isIs,
127: defaultValue, null, null, null);
128: }
129:
130: /**
131: * Contruct an OpenMBeanAttributeInfoSupport<p>
132: *
133: * @param name cannot be null or empty
134: * @param description cannot be null or empty
135: * @param openType cannot be null
136: * @param isReadable true when the getter is exposed for management
137: * @param isWritable true when the setter is exposed for management
138: * @param isIs true when the getter is of the form isXXX
139: * @param defaultValue the default value
140: * @param legalValues an array of legal values
141: * @exception IllegalArgumentException when one of the above
142: * constraints is not satisfied
143: */
144: public OpenMBeanAttributeInfoSupport(String name,
145: String description, OpenType openType, boolean isReadable,
146: boolean isWritable, boolean isIs, Object defaultValue,
147: Object[] legalValues) throws OpenDataException {
148: super (name, openType == null ? null : openType.getClassName(),
149: description, isReadable, isWritable, isIs);
150: init(name, description, openType, isReadable, isWritable, isIs,
151: defaultValue, legalValues, null, null);
152: }
153:
154: /**
155: * Contruct an OpenMBeanAttributeInfoSupport<p>
156: *
157: * @param name cannot be null or empty
158: * @param description cannot be null or empty
159: * @param openType cannot be null
160: * @param isReadable true when the getter is exposed for management
161: * @param isWritable true when the setter is exposed for management
162: * @param isIs true when the getter is of the form isXXX
163: * @param defaultValue the default value
164: * @param minValue the minimum value
165: * @param maxValue the maximum value
166: * @exception IllegalArgumentException when one of the above
167: * constraints is not satisfied
168: */
169: public OpenMBeanAttributeInfoSupport(String name,
170: String description, OpenType openType, boolean isReadable,
171: boolean isWritable, boolean isIs, Object defaultValue,
172: Comparable minValue, Comparable maxValue)
173: throws OpenDataException {
174: super (name, openType == null ? null : openType.getClassName(),
175: description, isReadable, isWritable, isIs);
176: init(name, description, openType, isReadable, isWritable, isIs,
177: defaultValue, null, minValue, maxValue);
178: }
179:
180: // Public --------------------------------------------------------
181:
182: // OpenMBeanAttributeInfo Implementation -------------------------
183:
184: public Object getDefaultValue() {
185: return defaultValue;
186: }
187:
188: public Set getLegalValues() {
189: return legalValues;
190: }
191:
192: public Comparable getMinValue() {
193: return minValue;
194: }
195:
196: public Comparable getMaxValue() {
197: return maxValue;
198: }
199:
200: public OpenType getOpenType() {
201: return openType;
202: }
203:
204: public boolean hasDefaultValue() {
205: return (defaultValue != null);
206: }
207:
208: public boolean hasLegalValues() {
209: return (legalValues != null);
210: }
211:
212: public boolean hasMinValue() {
213: return (minValue != null);
214: }
215:
216: public boolean hasMaxValue() {
217: return (maxValue != null);
218: }
219:
220: public boolean isValue(Object obj) {
221: if (openType.isValue(obj) == false)
222: return false;
223: if (minValue != null && minValue.compareTo(obj) > 0)
224: return false;
225: if (maxValue != null && maxValue.compareTo(obj) < 0)
226: return false;
227: if (legalValues != null && legalValues.contains(obj) == false)
228: return false;
229: return true;
230: }
231:
232: // Object Overrides ----------------------------------------------
233:
234: public boolean equals(Object obj) {
235: if (this == obj)
236: return true;
237: if (obj == null || !(obj instanceof OpenMBeanAttributeInfo))
238: return false;
239: OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj;
240:
241: if (this .getName().equals(other.getName()) == false)
242: return false;
243:
244: if (this .getOpenType().equals(other.getOpenType()) == false)
245: return false;
246:
247: if (isReadable() != other.isReadable())
248: return false;
249:
250: if (isWritable() != other.isWritable())
251: return false;
252:
253: if (isIs() != other.isIs())
254: return false;
255:
256: if (hasDefaultValue() == false
257: && other.hasDefaultValue() == true)
258: return false;
259: if (hasDefaultValue() == true
260: && this .getDefaultValue().equals(
261: other.getDefaultValue()) == false)
262: return false;
263:
264: if (hasMinValue() == false && other.hasMinValue() == true)
265: return false;
266: if (hasMinValue() == true
267: && this .getMinValue().equals(other.getMinValue()) == false)
268: return false;
269:
270: if (hasMaxValue() == false && other.hasMaxValue() == true)
271: return false;
272: if (hasMaxValue() == true
273: && this .getMaxValue().equals(other.getMaxValue()) == false)
274: return false;
275:
276: if (hasLegalValues() == false && other.hasLegalValues() == true)
277: return false;
278: if (hasLegalValues() == true) {
279: Set otherLegal = other.getLegalValues();
280: if (otherLegal == null)
281: return false;
282: Set this Legal = this .getLegalValues();
283: if (this Legal.size() != otherLegal.size())
284: return false;
285: if (this Legal.containsAll(otherLegal) == false)
286: return false;
287: }
288: return true;
289: }
290:
291: public int hashCode() {
292: if (cachedHashCode != 0)
293: return cachedHashCode;
294: cachedHashCode = getName().hashCode();
295: cachedHashCode += getOpenType().hashCode();
296: if (defaultValue != null)
297: cachedHashCode += getDefaultValue().hashCode();
298: if (minValue != null)
299: cachedHashCode += getMinValue().hashCode();
300: if (maxValue != null)
301: cachedHashCode += getMaxValue().hashCode();
302: if (legalValues != null)
303: cachedHashCode += getLegalValues().hashCode();
304: return cachedHashCode;
305: }
306:
307: public String toString() {
308: if (cachedToString != null)
309: return cachedToString;
310: StringBuffer buffer = new StringBuffer(getClass().getName());
311: buffer.append(": name=");
312: buffer.append(getName());
313: buffer.append(", openType=");
314: buffer.append(getOpenType());
315: buffer.append(", isWritable=");
316: buffer.append(isWritable());
317: buffer.append(", isReadable=");
318: buffer.append(isReadable());
319: buffer.append(", isIs=");
320: buffer.append(isIs());
321: buffer.append(", defaultValue=");
322: buffer.append(getDefaultValue());
323: buffer.append(", minValue=");
324: buffer.append(getMinValue());
325: buffer.append(", maxValue=");
326: buffer.append(getMaxValue());
327: buffer.append(", legalValues=");
328: buffer.append(getLegalValues());
329: cachedToString = buffer.toString();
330: return cachedToString;
331: }
332:
333: // Protected -----------------------------------------------------
334:
335: // Private -------------------------------------------------------
336:
337: /**
338: * Initialise an OpenMBeanAttributeInfoSupport<p>
339: *
340: * WARNING: For the MBeanAttributeInfo only validation is performed
341: *
342: * @param name cannot be null or empty
343: * @param description cannot be null or empty
344: * @param openType cannot be null
345: * @param isReadable true when the getter is exposed for management
346: * @param isWritable true when the setter is exposed for management
347: * @param isIs true when the getter is of the form isXXX
348: * @param defaultValue the default value
349: * @param minValue the minimum value
350: * @param maxValue the maximum value
351: * @exception IllegalArgumentException when one of the above
352: * constraints is not satisfied
353: */
354: private void init(String name, String Description,
355: OpenType openType, boolean isReadable, boolean isWritable,
356: boolean isIs, Object defaultValue, Object[] legalValues,
357: Comparable minValue, Comparable maxValue)
358: throws OpenDataException {
359: if (name == null || name.trim().length() == 0)
360: throw new IllegalArgumentException("null or empty name");
361:
362: if (description == null || description.trim().length() == 0)
363: throw new IllegalArgumentException(
364: "null or empty description");
365:
366: if (openType == null)
367: throw new IllegalArgumentException("null open type");
368: this .openType = openType;
369:
370: if (defaultValue != null
371: && (openType instanceof ArrayType || openType instanceof TabularType))
372: throw new OpenDataException(
373: "default value is not supported for "
374: + openType.getClass().getName());
375: if (defaultValue != null
376: && openType.isValue(defaultValue) == false)
377: throw new OpenDataException(
378: "default value is not valid for "
379: + openType.getClass().getName());
380:
381: if (legalValues != null && legalValues.length != 0) {
382: if (openType instanceof ArrayType
383: || openType instanceof TabularType)
384: throw new OpenDataException(
385: "legal values are not supported for "
386: + openType.getClass().getName());
387: HashSet legals = new HashSet(legalValues.length);
388: for (int i = 0; i < legalValues.length; i++) {
389: if (openType.isValue(legalValues[i]) == false)
390: throw new OpenDataException("legal value "
391: + legalValues[i] + " at index " + i
392: + " is not valid for "
393: + openType.getClass().getName());
394: legals.add(legalValues[i]);
395: }
396: if (defaultValue != null
397: && legals.contains(defaultValue) == false)
398: throw new OpenDataException(
399: "default value is not a legal value");
400: this .legalValues = Collections.unmodifiableSet(legals);
401: }
402:
403: if (minValue != null && openType.isValue(minValue) == false)
404: throw new OpenDataException(
405: "minimum value is not valid for "
406: + openType.getClass().getName());
407: if (defaultValue != null && minValue != null
408: && minValue.compareTo(defaultValue) > 0)
409: throw new OpenDataException(
410: "the default value is less than the minimum value ");
411:
412: if (maxValue != null && openType.isValue(maxValue) == false)
413: throw new OpenDataException(
414: "maximum value is not valid for "
415: + openType.getClass().getName());
416: if (defaultValue != null && maxValue != null
417: && maxValue.compareTo(defaultValue) < 0)
418: throw new OpenDataException(
419: "the default value is greater than the maximum value ");
420:
421: if (minValue != null && maxValue != null
422: && minValue.compareTo(maxValue) > 0)
423: throw new OpenDataException(
424: "the minimum value is greater than the maximum value ");
425:
426: this .defaultValue = defaultValue;
427: this .minValue = minValue;
428: this .maxValue = maxValue;
429: }
430:
431: // Inner Classes -------------------------------------------------
432: }
|