001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx.queries;
009:
010: import java.io.ObjectStreamField;
011: import java.io.ObjectInputStream;
012: import java.io.IOException;
013: import java.io.ObjectOutputStream;
014: import javax.management.QueryEval;
015: import javax.management.ValueExp;
016: import javax.management.ObjectName;
017: import javax.management.BadStringOperationException;
018: import javax.management.BadBinaryOpValueExpException;
019: import javax.management.BadAttributeValueExpException;
020: import javax.management.InvalidApplicationException;
021:
022: /**
023: *
024: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
025: */
026:
027: public class NumericValueExp extends QueryEval implements ValueExp {
028:
029: private static final long oldSerialVersionUID = 0xa99223860c6d2a40L;
030: private static final long newSerialVersionUID = 0xbf0e39bb8d1abdc0L;
031: private static final ObjectStreamField oldSerialPersistentFields[];
032: private static final ObjectStreamField newSerialPersistentFields[];
033: private static final long serialVersionUID;
034: private static final ObjectStreamField serialPersistentFields[];
035: private static boolean compat;
036: private Number val;
037: static Class class$java$lang$Number; /* synthetic field */
038:
039: static {
040: oldSerialPersistentFields = (new ObjectStreamField[] {
041: new ObjectStreamField("longVal", Long.TYPE),
042: new ObjectStreamField("doubleVal", Double.TYPE),
043: new ObjectStreamField("valIsLong", Boolean.TYPE) });
044: newSerialPersistentFields = (new ObjectStreamField[] { new ObjectStreamField(
045: "val",
046: class$java$lang$Number != null ? class$java$lang$Number
047: : (class$java$lang$Number = InternalClass1("java.lang.Number"))) });
048: compat = false;
049: String s = System.getProperty("jmx.serial.form");
050: if (s != null && s.equals("1.0"))
051: compat = true;
052: if (compat) {
053: serialPersistentFields = oldSerialPersistentFields;
054: serialVersionUID = 0xa99223860c6d2a40L;
055: } else {
056: serialPersistentFields = newSerialPersistentFields;
057: serialVersionUID = 0xbf0e39bb8d1abdc0L;
058: }
059: }
060:
061: public NumericValueExp(Number number) {
062: val = new Double(0.0D);
063: val = number;
064: }
065:
066: public double doubleValue() {
067: if ((val instanceof Long) || (val instanceof Integer))
068: return (double) val.longValue();
069: else
070: return val.doubleValue();
071: }
072:
073: public long longValue() {
074: if ((val instanceof Long) || (val instanceof Integer))
075: return val.longValue();
076: else
077: return (long) val.doubleValue();
078: }
079:
080: public boolean isLong() {
081: return (val instanceof Long) || (val instanceof Integer);
082: }
083:
084: public String toString() {
085: if ((val instanceof Long) || (val instanceof Integer))
086: return String.valueOf(val.longValue());
087: else
088: return String.valueOf(val.doubleValue());
089: }
090:
091: public ValueExp apply(ObjectName objectname)
092: throws BadStringOperationException,
093: BadBinaryOpValueExpException,
094: BadAttributeValueExpException, InvalidApplicationException {
095: return this ;
096: }
097:
098: private void readObject(ObjectInputStream objectinputstream)
099: throws IOException, ClassNotFoundException {
100: if (compat) {
101: java.io.ObjectInputStream.GetField getfield = objectinputstream
102: .readFields();
103: double d = getfield.get("doubleVal", 0.0D);
104: if (getfield.defaulted("doubleVal"))
105: throw new NullPointerException("doubleVal");
106: long l = getfield.get("longVal", 0L);
107: if (getfield.defaulted("longVal"))
108: throw new NullPointerException("longVal");
109: boolean flag = getfield.get("valIsLong", false);
110: if (getfield.defaulted("valIsLong"))
111: throw new NullPointerException("valIsLong");
112: if (flag)
113: val = new Long(l);
114: else
115: val = new Double(d);
116: } else {
117: objectinputstream.defaultReadObject();
118: }
119: }
120:
121: private void writeObject(ObjectOutputStream objectoutputstream)
122: throws IOException {
123: if (compat) {
124: java.io.ObjectOutputStream.PutField putfield = objectoutputstream
125: .putFields();
126: putfield.put("doubleVal", doubleValue());
127: putfield.put("longVal", longValue());
128: putfield.put("valIsLong", isLong());
129: objectoutputstream.writeFields();
130: } else {
131: objectoutputstream.defaultWriteObject();
132: }
133: }
134:
135: static Class InternalClass1(String s) {
136: try {
137: return Class.forName(s);
138: } catch (ClassNotFoundException classnotfoundexception) {
139: throw new NoClassDefFoundError(classnotfoundexception
140: .getMessage());
141: }
142: }
143:
144: }
|