001: /**
002: * InstantJ
003: *
004: * Copyright (C) 2002 Nils Meier
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */package instantj.tst;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import instantj.compile.CompilationFailedException;
027: import instantj.expression.EvaluationFailedException;
028: import instantj.expression.Expression;
029: import instantj.reflect.PropertyException;
030: import instantj.reflect.ReflectAccess;
031:
032: /**
033: * SerializationTest - shows that you can (de)serialize an
034: * Expression and evaluate it. How about sending expressions
035: * to a remote client next time (already pre-compiled on the
036: * server)
037: */
038: public class SerializationTest {
039:
040: /**
041: * Testing Expression Evaluation -
042: */
043: public static void main(String[] args) {
044:
045: // Disclaimer
046: System.out.println("InstantJ - "
047: + ReflectAccess.getInstance().calcClassNameOf(
048: SerializationTest.class));
049:
050: // Do the test
051: try {
052: test();
053: } catch (Throwable t) {
054: t.printStackTrace();
055: }
056:
057: // Done
058: }
059:
060: /**
061: * test()
062: */
063: private static void test() throws CompilationFailedException,
064: IOException, ClassNotFoundException,
065: EvaluationFailedException, PropertyException {
066:
067: String body = "a + b";
068: String a = "Hello ", b = "World!";
069:
070: // Create the expression
071: System.out.println(" Creating expression: " + body);
072: Map props = new HashMap();
073: props.put("a", String.class);
074: props.put("b", String.class);
075: Expression e = new Expression(body, props);
076:
077: // Serialize!
078: System.out.println(" Serializing ...");
079: ByteArrayOutputStream bout = new ByteArrayOutputStream();
080: ObjectOutputStream oout = new ObjectOutputStream(bout);
081: oout.writeObject(e);
082: oout.close();
083:
084: // De-Serialize!
085: System.out.println(" Deserializing ...");
086: ByteArrayInputStream bin = new ByteArrayInputStream(bout
087: .toByteArray());
088: ObjectInputStream in = new ObjectInputStream(bin);
089: e = (Expression) in.readObject();
090:
091: // Evaluate it
092: System.out.println(" Evaluating (a=" + a + ",b=" + b + ") ...");
093: Object result = e.getInstance().set("a", a).set("b", b)
094: .evaluate();
095: System.out.println(" Done (result=" + result + ")");
096:
097: // Done
098:
099: }
100: }
|