001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management;
027:
028: /**
029: * This class represents attributes used as arguments to relational constraints.
030: * An <CODE>AttributeValueExp</CODE> may be used anywhere a
031: * <CODE>ValueExp</CODE> is required.
032: */
033: public class AttributeValueExp implements ValueExp {
034: /** The name of the attribute */
035: private String attr = null;
036:
037: private String type = null;
038:
039: private Object retObj = null;
040:
041: private String className = null;
042:
043: private Object nobject = null;
044:
045: private transient MBeanServer server = null;
046:
047: private int qtype = -1;
048:
049: private AttributeValueExp v1 = null;
050:
051: private AttributeValueExp v2 = null;
052:
053: ObjectName mbeanName = null;
054:
055: //used in Query.classattr() method..
056: boolean classattr = false;
057:
058: /**
059: * Basic Constructor.
060: */
061: public AttributeValueExp() {
062: classattr = true;
063: }
064:
065: /**
066: * Creates a new <CODE>AttributeValueExp</CODE> representing the specified
067: * object attribute, named attr.
068: *
069: * @param attr creates AttributeValueExp with the specified attribute.
070: */
071: public AttributeValueExp(String attr) {
072: this .attr = attr;
073: }
074:
075: //this constructor may be replaced with NumericValueExp ,
076: //called from Query.value(..)
077: //confirm whether NumericValueExp is available...
078: AttributeValueExp(String attr, Object nobject) {
079: this .nobject = nobject;
080: }
081:
082: //added for Query.attr(className, attr);
083: AttributeValueExp(String className, String attr) {
084: this .attr = attr;
085: this .className = className;
086: }
087:
088: //added for Query.plus(v1, v2)..
089: AttributeValueExp(AttributeValueExp v1, AttributeValueExp v2,
090: int qtype) {
091: this .qtype = qtype;
092:
093: this .v1 = v1;
094: this .v2 = v2;
095: }
096:
097: /**
098: * Applies the <CODE>AttributeValueExp</CODE> on an MBean.
099: *
100: * @param object The MBean objectname on which the
101: * <CODE>AttributeValueExp</CODE> will be applied.
102: *
103: * @return The <CODE>ValueExp</CODE>.
104: *
105: * @exception BadAttributeValueExpException This ExpException is thrown
106: * when an invalid MBean attribute is passed to a query
107: * constructing method.
108: *
109: * @exception InvalidApplicationException This exception is thrown when
110: * an attempt is made to apply either of the following:
111: * A subquery expression to an MBean
112: * A qualified attribute expression to an MBean of the wrong class
113: *
114: * @exception BadStringOperationException This exception is thrown when
115: * an invalid string operation is passed to a method for
116: * constructing a query.
117: *
118: * @exception BadBinaryOpValueExpException This exception is thrown when
119: * an invalid expression is passed to a method for
120: * constructing a query.
121: *
122: * @overrides apply in class ValueExp
123: */
124: public ValueExp apply(ObjectName object)
125: throws BadAttributeValueExpException,
126: InvalidApplicationException, BadStringOperationException,
127: BadBinaryOpValueExpException {
128: this .mbeanName = object;
129:
130: if (v1 != null)
131: v1.apply(object);
132:
133: if (v2 != null)
134: v2.apply(object);
135:
136: retObj = null;
137:
138: type = null;
139:
140: //this condition(fails) useful when the Query.classattr case occurs...
141: if (attr == null && nobject != null)
142: return null;
143:
144: try {
145: server.getObjectInstance(object);
146:
147: } catch (InstanceNotFoundException infe) {
148: throw new InvalidApplicationException("MBean :: " + object
149: + " :: not registered");
150: }
151:
152: MBeanInfo info = null;
153:
154: try {
155: info = server.getMBeanInfo(object);
156: } catch (Exception e1) {
157: return null;
158: }
159:
160: if (className != null) {
161: if (!className.equals(info.getClassName()))
162: return null;
163: }
164:
165: //probably then it would be Query.classattr case..
166: if (attr == null && nobject == null && classattr) {
167: retObj = info.getClassName();
168:
169: type = "java.lang.String";
170:
171: //attr = "";
172:
173: return null;
174: }
175:
176: MBeanAttributeInfo[] attInfo = info.getAttributes();
177:
178: for (int i = 0; i < attInfo.length; i++) {
179: MBeanAttributeInfo att = attInfo[i];
180:
181: if (att.getName().equals(attr)) {
182: if (!att.isReadable())
183: continue;
184: type = att.getType();
185:
186: break;
187: }
188: }
189:
190: if (type == null) {
191: return null;
192: }
193:
194: try {
195: retObj = server.getAttribute(object, attr);
196: } catch (Exception e1) {
197: return null;
198: }
199:
200: if (retObj == null)
201: return null;
202:
203: return getValueExp();
204: }
205:
206: /**
207: * Returns a string representation of the name of the attribute.
208: *
209: * @return A string representation of the name of the attribute.
210: */
211: public String getAttributeName() {
212: return attr;
213: }
214:
215: /**
216: * Sets the MBeanServer on which the query is to be accessed.
217: *
218: * @param s - The MBeanServer on which the query is to be accessed.
219: */
220: public void setMBeanServer(MBeanServer server) {
221: this .server = server;
222:
223: if (v1 != null)
224: v1.setMBeanServer(server);
225: if (v2 != null)
226: v2.setMBeanServer(server);
227: }
228:
229: /**
230: * Returns the string representing its value
231: *
232: * @overrides toString in class java.lang.Object
233: */
234: public String toString() {
235: if (attr != null || classattr)
236: return retObj.toString();
237: else if (nobject != null)
238: return nobject.toString();
239:
240: if (qtype == Query.PLUS || qtype == Query.MINUS
241: || qtype == Query.TIMES || qtype == Query.DIV)
242: return v1.toString() + v2.toString();
243:
244: return null;
245: }
246:
247: //-------------------------- Protected methods --------------------------//
248:
249: protected Object getAttribute(Object mo) {
250: return attr;
251: }
252:
253: //---------------------- Package access methods -------------------------//
254:
255: Object getObject() {
256: if (attr != null || classattr)
257: return retObj;
258: else if (nobject != null)
259: return nobject;
260:
261: if (qtype == Query.PLUS || qtype == Query.MINUS
262: || qtype == Query.TIMES || qtype == Query.DIV) {
263: return getCombinedObject(v1.getObject(), v2.getObject());
264: }
265:
266: return null;
267: }
268:
269: //--------------------------- Private methods ---------------------------//
270:
271: private ValueExp getValueExp() {
272: ValueExp toRet = null;
273:
274: if (type.indexOf("String") != -1)
275: toRet = Query.value((String) retObj);
276: else if (type.indexOf("Integer") != -1
277: || type.indexOf("int") != -1)
278: toRet = Query.value(((Integer) retObj).intValue());
279: else if (type.indexOf("Long") != -1
280: || type.indexOf("long") != -1)
281: toRet = Query.value(((Long) retObj).longValue());
282: else if (type.indexOf("Boolean") != -1
283: || type.indexOf("boolean") != -1)
284: toRet = Query.value(((Boolean) retObj).booleanValue());
285: else if (type.indexOf("Float") != -1
286: || type.indexOf("float") != -1)
287: toRet = Query.value(((Float) retObj).floatValue());
288: else if (type.indexOf("Double") != -1
289: || type.indexOf("double") != -1)
290: toRet = Query.value(((Double) retObj).doubleValue());
291: else if (type.indexOf("Byte") != -1
292: || type.indexOf("byte") != -1)
293: toRet = Query.value(((Byte) retObj).intValue());
294: else if (type.indexOf("Character") != -1
295: || type.indexOf("char") != -1)
296: toRet = Query.value(Character
297: .getNumericValue(((Character) retObj).charValue()));
298: else if (type.indexOf("Short") != -1
299: || type.indexOf("short") != -1)
300: toRet = Query.value(((Short) retObj).longValue());
301:
302: return toRet;
303: }
304:
305: private Object getCombinedObject(Object o1, Object o2) {
306: Object toRet = null;
307:
308: if ((o1 instanceof Integer) && (o2 instanceof Long))
309: o2 = new Integer(((Long) o2).intValue());
310:
311: if ((o1 instanceof Long) && (o2 instanceof Integer))
312: o1 = new Integer(((Long) o1).intValue());
313:
314: String o1Type = o1.getClass().getName();
315: String o2Type = o2.getClass().getName();
316:
317: if (o1Type.indexOf("Integer") != -1
318: && o2Type.indexOf("Integer") != -1) {
319: if (qtype == Query.PLUS) {
320: toRet = new Integer(((Integer) o1).intValue()
321: + ((Integer) o2).intValue());
322: } else if (qtype == Query.MINUS) {
323: toRet = new Integer(((Integer) o1).intValue()
324: - ((Integer) o2).intValue());
325: } else if (qtype == Query.TIMES) {
326: toRet = new Integer(((Integer) o1).intValue()
327: * ((Integer) o2).intValue());
328: } else if (qtype == Query.DIV) {
329: toRet = new Integer(((Integer) o1).intValue()
330: / ((Integer) o2).intValue());
331: }
332: } else if (o1Type.indexOf("Long") != -1
333: && o2Type.indexOf("Long") != -1) {
334: if (qtype == Query.PLUS) {
335: toRet = new Long(((Long) o1).longValue()
336: + ((Long) o2).longValue());
337: } else if (qtype == Query.MINUS) {
338: toRet = new Long(((Long) o1).longValue()
339: - ((Long) o2).longValue());
340: } else if (qtype == Query.TIMES) {
341: toRet = new Long(((Long) o1).longValue()
342: * ((Long) o2).longValue());
343: } else if (qtype == Query.DIV) {
344: toRet = new Long(((Long) o1).longValue()
345: / ((Long) o2).longValue());
346: }
347: } else if (o1Type.indexOf("Float") != -1
348: && o2Type.indexOf("Float") != -1) {
349: if (qtype == Query.PLUS) {
350: toRet = new Float(((Float) o1).floatValue()
351: + ((Float) o2).floatValue());
352: } else if (qtype == Query.MINUS) {
353: toRet = new Float(((Float) o1).floatValue()
354: - ((Float) o2).floatValue());
355: } else if (qtype == Query.TIMES) {
356: toRet = new Float(((Float) o1).floatValue()
357: * ((Float) o2).floatValue());
358: } else if (qtype == Query.DIV) {
359: toRet = new Float(((Float) o1).floatValue()
360: / ((Float) o2).floatValue());
361: }
362: } else if (o1Type.indexOf("Double") != -1
363: && o2Type.indexOf("Double") != -1) {
364: if (qtype == Query.PLUS) {
365: toRet = new Double(((Double) o1).doubleValue()
366: + ((Double) o2).doubleValue());
367: } else if (qtype == Query.MINUS) {
368: toRet = new Double(((Double) o1).doubleValue()
369: - ((Double) o2).doubleValue());
370: } else if (qtype == Query.TIMES) {
371: toRet = new Double(((Double) o1).doubleValue()
372: * ((Double) o2).doubleValue());
373: } else if (qtype == Query.DIV) {
374: toRet = new Double(((Double) o1).doubleValue()
375: / ((Double) o2).doubleValue());
376: }
377: } else if (o1Type.indexOf("Byte") != -1
378: && o2Type.indexOf("Byte") != -1) {
379: if (qtype == Query.PLUS) {
380: toRet = new Byte(
381: (byte) (((Byte) o1).byteValue() + ((Byte) o2)
382: .byteValue()));
383: } else if (qtype == Query.MINUS) {
384: toRet = new Byte(
385: (byte) (((Byte) o1).byteValue() - ((Byte) o2)
386: .byteValue()));
387: } else if (qtype == Query.TIMES) {
388: toRet = new Byte(
389: (byte) (((Byte) o1).byteValue() * ((Byte) o2)
390: .byteValue()));
391: } else if (qtype == Query.DIV) {
392: toRet = new Byte(
393: (byte) (((Byte) o1).byteValue() / ((Byte) o2)
394: .byteValue()));
395: }
396: } else if (o1Type.indexOf("Short") != -1
397: && o2Type.indexOf("Short") != -1) {
398: if (qtype == Query.PLUS) {
399: toRet = new Short(
400: (short) (((Short) o1).shortValue() + ((Short) o2)
401: .shortValue()));
402: } else if (qtype == Query.MINUS) {
403: toRet = new Short(
404: (short) (((Short) o1).shortValue() - ((Short) o2)
405: .shortValue()));
406: } else if (qtype == Query.TIMES) {
407: toRet = new Short(
408: (short) (((Short) o1).shortValue() * ((Short) o2)
409: .shortValue()));
410: } else if (qtype == Query.DIV) {
411: toRet = new Short(
412: (short) (((Short) o1).shortValue() / ((Short) o2)
413: .shortValue()));
414: }
415: }
416:
417: return toRet;
418: }
419: }
|