[e-cvs] cvs commit: e/src/jsrc/org/erights/e/elib/serial PersistenceReplacer.java PersistenceResolver.java Persistent.java Serializer.java Unserializer.java
markm@eros.cs.jhu.edu
markm@eros.cs.jhu.edu
Sun, 19 Aug 2001 18:26:10 -0400
markm 01/08/19 18:26:10
Modified: src/jsrc/org/erights/e/elib/serial Persistent.java
Serializer.java Unserializer.java
Added: src/jsrc/org/erights/e/elib/serial PersistenceReplacer.java
PersistenceResolver.java
Log:
a bit of refactoring of persistence
Revision Changes Path
1.3 +3 -0 e/src/jsrc/org/erights/e/elib/serial/Persistent.java
Index: Persistent.java
===================================================================
RCS file: /cvs/e/src/jsrc/org/erights/e/elib/serial/Persistent.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Persistent.java 2001/08/19 06:35:56 1.2
+++ Persistent.java 2001/08/19 22:26:10 1.3
@@ -25,6 +25,9 @@
/**
* Marker interface that enables object to be saved and restored using the
* {@link Serializer} and {@link Unserializer}.
+ * <p>
+ * This interface helps to determine whether an object {@link
+ * org.erights.e.elib.ref.Ref#isPersistent(Object)}.
*
* @author Mark S. Miller
*/
1.8 +35 -12 e/src/jsrc/org/erights/e/elib/serial/Serializer.java
Index: Serializer.java
===================================================================
RCS file: /cvs/e/src/jsrc/org/erights/e/elib/serial/Serializer.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Serializer.java 2001/08/19 06:35:56 1.7
+++ Serializer.java 2001/08/19 22:26:10 1.8
@@ -20,12 +20,13 @@
*/
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import org.erights.e.elib.ref.Ref;
-import org.erights.e.elib.util.IdentityFunc;
import org.erights.e.elib.util.OneArgFunc;
+import org.erights.e.meta.java.io.FileSugar;
/**
* Made usable from E by parameterization rather than subclassing.
@@ -65,15 +66,16 @@
* replacer defaults to identity function
*/
public Serializer(OutputStream out) throws IOException {
- this(out, IdentityFunc.TheOne);
+ this(out, PersistenceReplacer.TheOne);
}
/**
* Using a Serializer parameterized by this replacer, record the
- * seriaized form of the specimen and return the resulting byte array.
+ * seriaized form of the specimen and return the resulting byte array.
+ * <p>
* Such recordings can be play()ed by Unserializer.
*/
- static public byte[] record(Object specimen, OneArgFunc replacer)
+ static public byte[] record(Object specimen, OneArgFunc replacer)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Serializer ser = new Serializer(out, replacer);
@@ -83,16 +85,37 @@
}
/**
+ * replacer defaults to the identity function
+ */
+ static public byte[] record(Object specimen)
+ throws IOException {
+ return record(specimen, PersistenceReplacer.TheOne);
+ }
+
+ /**
+ *
+ */
+ static public void recordFile(File file,
+ Object specimen,
+ OneArgFunc replacer)
+ throws IOException {
+ byte[] recording = record(specimen, replacer);
+ FileSugar.setBytes(file, recording);
+ }
+
+ /**
+ * replacer defaults to the identity function
+ */
+ static public void recordFile(File file, Object specimen)
+ throws IOException {
+ recordFile(file, specimen, PersistenceReplacer.TheOne);
+ }
+
+ /**
* Just applies the replacer I was constructed with, and insists that the
* resulting object {@link Ref#isPersistent(Object)}.
*/
- protected Object replaceObject(Object obj) throws IOException {
- obj = Ref.resolution(obj);
- obj = myReplacer.run(obj);
- obj = Ref.resolution(obj);
- if (! Ref.isPersistent(obj)) {
- throw new RuntimeException("not persistent: " + obj);
- }
- return obj;
+ protected Object replaceObject(Object ref) {
+ return Ref.resolution(myReplacer.run(ref));
}
}
1.7 +57 -29 e/src/jsrc/org/erights/e/elib/serial/Unserializer.java
Index: Unserializer.java
===================================================================
RCS file: /cvs/e/src/jsrc/org/erights/e/elib/serial/Unserializer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Unserializer.java 2001/08/19 06:35:56 1.6
+++ Unserializer.java 2001/08/19 22:26:10 1.7
@@ -20,23 +20,31 @@
*/
import java.io.ByteArrayInputStream;
+import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.InputStream;
import org.erights.e.elib.ref.Ref;
-import org.erights.e.elib.util.IdentityFunc;
import org.erights.e.elib.util.OneArgFunc;
+import org.erights.e.meta.java.io.FileSugar;
/**
* Made usable from E by parameterization rather than subclassing.
- * This stream should be used at least for persistence and pluribus. It will
- * probably need more parameters, but we should add them as we find we need
- * them, rather than create another subclass. <p>
+ * <p>
+ * This stream should be used at least for persistence and CapTP. It is
+ * specialized by composition rather than subclassing -- by providing a
+ * resolver(..) function rather than overriding the .resolver(..) method, so
+ * E programs (which can't subclass Java classes) can still specialize
+ * unserialization behavior.
+ * <p>
+ * Unserializer will probably need more parameters, but we should add them as
+ * we find we need them, rather than create another subclass.
+ * <p>
+ * Likely additional parameters: <ul>
+ * <li>version string checker
+ * <li>optional ClassLoader
+ * </ul>
*
- * Likely additional parameters:
- * version string checker
- * optional ClassLoader
- *
* @author <a href="mailto:markm@erights.org">Mark S. Miller</a>
*/
public final class Unserializer extends ObjectInputStream {
@@ -44,14 +52,17 @@
private final OneArgFunc myResolver;
/**
- * Makes an ObjectInputStream on inp with the following differences:
- * 1) The stream header is read first, checking that the format
- * in which the stream is encoded in one we can read.
- * 2) When you do unserializer.readObject(), rather than the encoded
- * object, let's say foo, being returned, resolver(foo) is used, and
- * likewise for every object reachable from foo. resolver is used as the
- * overriding of resolveObject(). See the Java Serialization spec for
- * the detailed implications of this.
+ * Makes an Unserializer specialized by 'resolver'.
+ * <p>
+ * Makes an ObjectInputStream on inp with the following differences: <ul>
+ * <li>The stream header is read first, checking that the format
+ * in which the stream is encoded in one we can read.
+ * <li>When you do unserializer.readObject(), rather than the encoded
+ * object, let's say foo, being returned, resolver(foo) is used, and
+ * likewise for every object reachable from foo.
+ * </ul>
+ * resolver(..) is used as the overriding of .resolveObject(..). See the
+ * Java Serialization spec for the detailed implications of this.
*/
public Unserializer(InputStream inp, OneArgFunc resolver)
throws IOException {
@@ -62,10 +73,10 @@
}
/**
- * resolver defaults to identity function
+ * resolver(..) defaults to {@link PersistenceResolver#TheOne}
*/
public Unserializer(InputStream inp) throws IOException {
- this(inp, IdentityFunc.TheOne);
+ this(inp, PersistenceResolver.TheOne);
}
/**
@@ -79,19 +90,36 @@
uns.close();
return result;
}
-
/**
- * Just insists that obj {@link Ref#isPersistent(Object)}, and applies
- * the resolver I was constructed with.
+ * resolver(..) defaults to {@link PersistenceResolver#TheOne}
*/
- protected Object resolveObject(Object obj) throws IOException {
- obj = Ref.resolution(obj);
- if (! Ref.isPersistent(obj)) {
- throw new RuntimeException("not persistent: " + obj);
- }
- obj = myResolver.run(obj);
- obj = Ref.resolution(obj);
- return obj;
+ static public Object play(byte[] recording)
+ throws IOException, ClassNotFoundException {
+ return play(recording, PersistenceResolver.TheOne);
+ }
+
+ /**
+ *
+ */
+ static public Object playFile(File file, OneArgFunc resolver)
+ throws IOException, ClassNotFoundException {
+ byte[] recording = FileSugar.getBytes(file);
+ return play(recording, resolver);
+ }
+
+ /**
+ * resolver(..) defaults to {@link PersistenceResolver#TheOne}
+ */
+ static public Object playFile(File file)
+ throws IOException, ClassNotFoundException {
+ return playFile(file, PersistenceResolver.TheOne);
+ }
+
+ /**
+ * returns the {@link Ref#resolution(Object)} of resolver(ref)
+ */
+ protected Object resolveObject(Object ref) {
+ return Ref.resolution(myResolver.run(ref));
}
}
1.1 e/src/jsrc/org/erights/e/elib/serial/PersistenceReplacer.java
Index: PersistenceReplacer.java
===================================================================
package org.erights.e.elib.serial;
/*
The contents of this file are subject to the Improvements to the
Distributed E Language Implementation License Version 1.0 (the
"License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.erights.org/mmlicense.html
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
The Original Code is the Improvements to the Distributed E Language
Implementation, released May 27, 1999.
The Initial Developer of the Original Code is Mark S. Miller.
Copyright (C) 1999 Mark S. Miller. All Rights Reserved.
Contributor(s): ______________________________________.
*/
import org.erights.e.elib.ref.Ref;
import org.erights.e.elib.util.OneArgFunc;
/**
*
*
* @author <a href="mailto:markm@erights.org">Mark S. Miller</a>
*/
public class PersistenceReplacer implements OneArgFunc {
/** The canonical instance */
static public final PersistenceReplacer TheOne = new PersistenceReplacer();
/**
*
*/
private PersistenceReplacer() {}
/**
* Just insists that ref {@link Ref#isPersistent(Object)}.
*/
public Object run(Object ref) {
ref = Ref.resolution(ref);
if (Ref.isPersistent(ref)) {
return ref;
}
if (Ref.isEventual(ref)) {
if (Ref.isResolved(ref)) {
throw new RuntimeException
("XXX far ref serialization not yet implemented");
} else {
return Ref.broken("Was a promise");
}
}
return Ref.broken("not persistent: " + ref);
}
}
1.1 e/src/jsrc/org/erights/e/elib/serial/PersistenceResolver.java
Index: PersistenceResolver.java
===================================================================
package org.erights.e.elib.serial;
/*
The contents of this file are subject to the Improvements to the
Distributed E Language Implementation License Version 1.0 (the
"License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.erights.org/mmlicense.html
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
The Original Code is the Improvements to the Distributed E Language
Implementation, released May 27, 1999.
The Initial Developer of the Original Code is Mark S. Miller.
Copyright (C) 1999 Mark S. Miller. All Rights Reserved.
Contributor(s): ______________________________________.
*/
import org.erights.e.elib.ref.Ref;
import org.erights.e.elib.util.OneArgFunc;
/**
*
*
* @author <a href="mailto:markm@erights.org">Mark S. Miller</a>
*/
public class PersistenceResolver implements OneArgFunc {
/** The canonical instance */
static public final PersistenceResolver TheOne = new PersistenceResolver();
/**
*
*/
private PersistenceResolver() {}
/**
* Just insists that ref {@link Ref#isPersistent(Object)}.
*/
public Object run(Object ref) {
if (! Ref.isPersistent(ref)) {
throw new RuntimeException("not persistent: " + ref);
}
return ref;
}
}