Add serilaize()/deserialize() support to ScriptingEngine to allow engines
to take care of serialization of scriptable objects.
This commit is contained in:
parent
301d589fae
commit
e51725aba0
2 changed files with 66 additions and 3 deletions
|
@ -19,7 +19,10 @@ package helma.scripting;
|
|||
import helma.framework.IPathElement;
|
||||
import helma.framework.core.Application;
|
||||
import helma.framework.core.RequestEvaluator;
|
||||
import java.util.*;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* This is the interface that must be implemented to make a scripting environment
|
||||
|
@ -116,4 +119,26 @@ public interface ScriptingEngine {
|
|||
* class should be compatible with helma.doc.DocApplication.
|
||||
*/
|
||||
public IPathElement getIntrospector();
|
||||
|
||||
/**
|
||||
* Provide object serialization for this engine's scripted objects. If no special
|
||||
* provisions are required, this method should just wrap the stream with an
|
||||
* ObjectOutputStream and write the object.
|
||||
*
|
||||
* @param obj the object to serialize
|
||||
* @param out the stream to write to
|
||||
* @throws IOException
|
||||
*/
|
||||
public void serialize(Object obj, OutputStream out) throws IOException;
|
||||
|
||||
/**
|
||||
* Provide object deserialization for this engine's scripted objects. If no special
|
||||
* provisions are required, this method should just wrap the stream with an
|
||||
* ObjectIntputStream and read the object.
|
||||
*
|
||||
* @param in the stream to read from
|
||||
* @return the deserialized object
|
||||
* @throws IOException
|
||||
*/
|
||||
public Object deserialize(InputStream in) throws IOException, ClassNotFoundException;
|
||||
}
|
||||
|
|
|
@ -28,10 +28,11 @@ import helma.objectmodel.db.Relation;
|
|||
import helma.scripting.*;
|
||||
import helma.scripting.rhino.debug.Tracer;
|
||||
import org.mozilla.javascript.*;
|
||||
import org.mozilla.javascript.serialize.ScriptableOutputStream;
|
||||
import org.mozilla.javascript.serialize.ScriptableInputStream;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
|
@ -450,6 +451,43 @@ public class RhinoEngine implements ScriptingEngine {
|
|||
return doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide object serialization for this engine's scripted objects. If no special
|
||||
* provisions are required, this method should just wrap the stream with an
|
||||
* ObjectOutputStream and write the object.
|
||||
*
|
||||
* @param obj the object to serialize
|
||||
* @param out the stream to write to
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public void serialize(Object obj, OutputStream out) throws IOException {
|
||||
// use a special ScriptableOutputStream that unwraps Wrappers
|
||||
ScriptableOutputStream sout = new ScriptableOutputStream(out, core.global) {
|
||||
protected Object replaceObject(Object obj) throws IOException {
|
||||
if (obj instanceof Wrapper)
|
||||
obj = ((Wrapper) obj).unwrap();
|
||||
return super.replaceObject(obj);
|
||||
}
|
||||
};
|
||||
sout.writeObject(obj);
|
||||
sout.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide object deserialization for this engine's scripted objects. If no special
|
||||
* provisions are required, this method should just wrap the stream with an
|
||||
* ObjectIntputStream and read the object.
|
||||
*
|
||||
* @param in the stream to read from
|
||||
* @return the deserialized object
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public Object deserialize(InputStream in) throws IOException, ClassNotFoundException {
|
||||
ObjectInputStream sin = new ScriptableInputStream(in, core.global);
|
||||
Object deserialized = sin.readObject();
|
||||
return Context.toObject(deserialized, core.global);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the application we're running in
|
||||
*/
|
||||
|
|
Loading…
Add table
Reference in a new issue