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: import com.adventnet.jmx.utils.JmxUtilities;
029:
030: /**
031: * The QueryExpSupport class represents relational constraints that can be used
032: * in database query "where clauses." Instances of QueryExpSupport are returned
033: * by the static methods of the Query class.
034: */
035: public class QueryExpSupport implements QueryExp {
036: private transient MBeanServer server = null;
037:
038: public static final int DIV = Query.DIV;
039: public static final int EQ = Query.EQ;
040: public static final int GE = Query.GE;
041: public static final int GT = Query.GT;
042: public static final int LE = Query.LE;
043: public static final int LT = Query.LT;
044: public static final int MINUS = Query.MINUS;
045: public static final int PLUS = Query.PLUS;
046: public static final int TIMES = Query.TIMES;
047:
048: public static final int MATCH = Query.MATCH;
049: public static final int AND = Query.AND;
050: public static final int OR = Query.OR;
051: public static final int BET = Query.BET;
052: public static final int NOT = Query.NOT;
053: public static final int IN = Query.IN;
054: public static final int INITIAL = Query.INITIAL;
055: public static final int ANY = Query.ANY;
056: public static final int FINAL = Query.FINAL;
057:
058: public QueryExpSupport() {
059: }
060:
061: private QueryExp q1 = null;
062: private QueryExp q2 = null;
063: boolean queryFlag = true;
064:
065: private ValueExp v1 = null;
066: private ValueExp v2 = null;
067: private ValueExp v3 = null;
068: private ValueExp[] list = null;
069: boolean valueFlag = false;
070:
071: private int type = Query.AND;
072:
073: QueryExpSupport(QueryExp q1, QueryExp q2, int type) {
074: this .q1 = q1;
075: this .q2 = q2;
076: this .type = type;
077: queryFlag = true;
078: }
079:
080: //used for negation Query.not..
081: QueryExpSupport(QueryExp q1, int type) {
082: this .q1 = q1;
083: this .type = type;
084: queryFlag = true;
085: }
086:
087: QueryExpSupport(ValueExp v1, ValueExp v2, int type) {
088: this .v1 = v1;
089: this .v2 = v2;
090: this .type = type;
091: valueFlag = true;
092: }
093:
094: QueryExpSupport(ValueExp v1, ValueExp v2, ValueExp v3, int type) {
095: this .v1 = v1;
096: this .v2 = v2;
097: this .v3 = v3;
098: this .type = type;
099: valueFlag = true;
100: }
101:
102: //added for Query.in(...)
103: QueryExpSupport(ValueExp v1, ValueExp[] list, int type) {
104: this .v1 = v1;
105: if (list != null && list.length > 0)
106: this .v2 = list[0];
107: this .list = list;
108: this .type = type;
109: valueFlag = true;
110: }
111:
112: /**
113: * Applies the QueryExp on a MBean.
114: *
115: * @param object - The name of the MBean on which the QueryExp will be applied.
116: *
117: * @return True if the query was successfully applied to the MBean, false otherwise.
118: *
119: * @throws BadStringOperationException
120: *
121: * @throws BadBinaryOpValueExpException
122: *
123: * @throws BadAttributeValueExpException
124: *
125: * @throws InvalidApplicationException
126: */
127: public boolean apply(ObjectName object)
128: throws BadStringOperationException,
129: BadBinaryOpValueExpException,
130: BadAttributeValueExpException, InvalidApplicationException {
131: if (valueFlag) {
132: String str1 = null;
133: String str2 = null;
134: String str3 = null;
135:
136: Object obj1 = null;
137: Object obj2 = null;
138: Object obj3 = null;
139:
140: v1.apply(object);
141:
142: str1 = v1.toString();
143:
144: if (v1 instanceof StringValueExp)
145: obj1 = ((StringValueExp) v1).getObject();
146: else if (v1 instanceof AttributeValueExp)
147: obj1 = ((AttributeValueExp) v1).getObject();
148:
149: v2.apply(object);
150:
151: str2 = v2.toString();
152:
153: if (v2 instanceof StringValueExp)
154: obj2 = ((StringValueExp) v2).getObject();
155: else if (v2 instanceof AttributeValueExp)
156: obj2 = ((AttributeValueExp) v2).getObject();
157:
158: if (v3 != null) {
159: v3.apply(object);
160: str3 = v3.toString();
161: if (v3 instanceof StringValueExp)
162: obj3 = ((StringValueExp) v3).getObject();
163: else if (v3 instanceof AttributeValueExp)
164: obj3 = ((AttributeValueExp) v3).getObject();
165: }
166:
167: switch (type) {
168: case Query.GT:
169: int ret;
170:
171: if ((obj1 instanceof Integer) && (obj2 instanceof Long)) {
172: if (((Integer) obj1).intValue() > ((Long) obj2)
173: .intValue())
174: return true;
175: else
176: return false;
177: }
178:
179: if ((obj1 instanceof Long) && (obj2 instanceof Integer)) {
180: if (((Long) obj1).intValue() > ((Integer) obj2)
181: .intValue())
182: return true;
183: else
184: return false;
185: }
186:
187: ret = ((Comparable) obj1).compareTo(obj2);
188:
189: if (ret > 0)
190: return true;
191: else
192: return false;
193:
194: case Query.GE:
195:
196: if ((obj1 instanceof Integer) && (obj2 instanceof Long)) {
197: if (((Integer) obj1).intValue() >= ((Long) obj2)
198: .intValue())
199: return true;
200: else
201: return false;
202: }
203:
204: if ((obj1 instanceof Long) && (obj2 instanceof Integer)) {
205: if (((Long) obj1).intValue() >= ((Integer) obj2)
206: .intValue())
207: return true;
208: else
209: return false;
210: }
211:
212: ret = ((Comparable) obj1).compareTo(obj2);
213:
214: if (ret >= 0)
215: return true;
216: else
217: return false;
218:
219: case Query.LE:
220:
221: if ((obj1 instanceof Integer) && (obj2 instanceof Long)) {
222: if (((Integer) obj1).intValue() <= ((Long) obj2)
223: .intValue())
224: return true;
225: else
226: return false;
227: }
228:
229: if ((obj1 instanceof Long) && (obj2 instanceof Integer)) {
230: if (((Long) obj1).intValue() <= ((Integer) obj2)
231: .intValue())
232: return true;
233: else
234: return false;
235: }
236:
237: ret = ((Comparable) obj1).compareTo(obj2);
238:
239: if (ret <= 0)
240: return true;
241: else
242: return false;
243:
244: case Query.LT:
245: if ((obj1 instanceof Integer) && (obj2 instanceof Long)) {
246: if (((Integer) obj1).intValue() < ((Long) obj2)
247: .intValue())
248: return true;
249: else
250: return false;
251: }
252:
253: if ((obj1 instanceof Long) && (obj2 instanceof Integer)) {
254: if (((Long) obj1).intValue() < ((Integer) obj2)
255: .intValue())
256: return true;
257: else
258: return false;
259: }
260:
261: ret = ((Comparable) obj1).compareTo(obj2);
262:
263: if (ret < 0)
264: return true;
265: else
266: return false;
267:
268: case Query.EQ:
269:
270: if ((obj1 instanceof Integer) && (obj2 instanceof Long)) {
271: if (((Integer) obj1).intValue() == ((Long) obj2)
272: .intValue())
273: return true;
274: else
275: return false;
276: }
277:
278: if ((obj1 instanceof Long) && (obj2 instanceof Integer)) {
279: if (((Long) obj1).intValue() == ((Integer) obj2)
280: .intValue())
281: return true;
282: else
283: return false;
284: }
285:
286: ret = ((Comparable) obj1).compareTo(obj2);
287:
288: if (ret == 0)
289: return true;
290: else
291: return false;
292:
293: case Query.BET:
294: ret = ((Comparable) obj1).compareTo(obj2);
295:
296: int ret2 = ((Comparable) obj1).compareTo(obj3);
297:
298: if (ret >= 0 && ret2 <= 0)
299: return true;
300: else
301: return false;
302:
303: case Query.MATCH:
304:
305: int length = str2.length();
306: StringBuffer buff = new StringBuffer();
307: for (int i = 0; i < length; i++) {
308: char ch = str2.charAt(i);
309:
310: buff.append(ch);
311: }
312:
313: str2 = buff.toString();
314:
315: return JmxUtilities.checkPattern(str1, str2, true);
316:
317: case Query.IN:
318: for (int i = 0; i < list.length; i++) {
319: v2 = list[i];
320: v2.apply(object);
321: str2 = v2.toString();
322: if (v2 instanceof StringValueExp)
323: obj2 = ((StringValueExp) v2).getObject();
324: else if (v2 instanceof AttributeValueExp)
325: obj2 = ((AttributeValueExp) v2).getObject();
326:
327: ret = ((Comparable) obj1).compareTo(obj2);
328:
329: if (ret == 0)
330: return true;
331: }
332: return false;
333:
334: case Query.INITIAL:
335: return str1.startsWith(str2);
336:
337: case Query.ANY:
338:
339: if (str1.indexOf(str2) != -1)
340: return true;
341: return false;
342:
343: case Query.FINAL:
344:
345: return str1.endsWith(str2);
346:
347: default:
348:
349: }
350: } else if (queryFlag) {
351: boolean f1 = false;
352: boolean f2 = false;
353:
354: switch (type) {
355: case Query.AND:
356:
357: f1 = q1.apply(object);
358: f2 = q2.apply(object);
359:
360: return f1 && f2;
361: //return (q1.apply(object) && q2.apply(object));
362: case Query.OR:
363: f1 = q1.apply(object);
364: f2 = q2.apply(object);
365:
366: return f1 || f2;
367:
368: case Query.NOT:
369: f1 = q1.apply(object);
370:
371: return !f1;
372:
373: default:
374:
375: }
376: }
377:
378: return false;
379: }
380:
381: /**
382: * Sets the MBeanServer on which the query is to be accessed.
383: *
384: * @param server - The MBeanServer on which the query is to be accessed.
385: */
386: public void setMBeanServer(MBeanServer server) {
387: this .server = server;
388: if (v1 != null)
389: v1.setMBeanServer(server);
390: if (v2 != null)
391: v2.setMBeanServer(server);
392: if (v3 != null)
393: v3.setMBeanServer(server);
394:
395: if (q1 != null)
396: q1.setMBeanServer(server);
397: if (q2 != null)
398: q2.setMBeanServer(server);
399: }
400:
401: public boolean isQueryFlag() {
402: return queryFlag;
403: }
404:
405: public boolean isValueFlag() {
406: return valueFlag;
407: }
408:
409: public int getType() {
410: return type;
411: }
412:
413: public QueryExp getQueryExp1() {
414: return q1;
415: }
416:
417: public QueryExp getQueryExp2() {
418: return q2;
419: }
420:
421: public ValueExp getValueExp1() {
422: return v1;
423: }
424:
425: public ValueExp getValueExp2() {
426: return v2;
427: }
428:
429: public ValueExp getValueExp3() {
430: return v3;
431: }
432:
433: public ValueExp[] getValueExpArray() {
434: return list;
435: }
436: }//End of class QueryExp
|