/**
* Copyright (C) 2001-2005 France Telecom R&D
*/
package org.objectweb.speedo.mim.lib;
import org.objectweb.speedo.mim.api.StateItf;
import org.objectweb.speedo.mim.api.PersistentObjectItf;
import org.objectweb.speedo.pm.api.POManagerItf;
import java.io.IOException;
/**
* Is an helper for the the serialization of the persistent objects.
*
* @author S.Chassande-Barrioz
*/
public class SpeedoPOSerializer {
/**
* Serializes a persistent class (PersistentObjectItf) into an ObjectOutputStream.
*
* @param sp is the speedo po to serialize
* @param fieldIds indicates which have to be serialized at least.
*
* @throws IOException
*/
public static void writeObject(java.io.ObjectOutputStream out,
PersistentObjectItf sp, long[] fieldIds) throws IOException {
StateItf state;
boolean pmAllocated = false;
POManagerItf pm = null;
if (sp.speedoIsActive()) {
// Fetch the PersistenceManager
pm = sp.speedoGetHome().getPOManagerFactory().lookup();
if (pm == null) {
// Allocate a new PersistenceManager to close at the end
pm = (POManagerItf) sp.speedoGetHome().getPOManagerFactory()
.getPOManager();
pmAllocated = true;
}
try {
state = sp.speedoGetHome().readIntention(sp, fieldIds);
} catch (Exception e) {
// Close the PersistenceManager if it has been allocated localy
if (pmAllocated) {
pm.closePOManager();
}
throw new IOException(e.getMessage());
}
} else {
state = sp.speedoGetReferenceState();
}
try {
out.writeObject(state);
out.defaultWriteObject();
} finally {
// Close the PersistenceManager if it has been allocated localy
if (pmAllocated) {
pm.closePOManager();
}
}
}
/**
* Deserializes a persistent class (PersistentObjectItf) from an ObjectInputStream.
*
* @param sp is the speedo po where attaches the content
*
* @throws IOException
*/
public static void readObject(java.io.ObjectInputStream in, PersistentObjectItf sp) throws IOException,
ClassNotFoundException {
sp.speedoIsActive(false);
sp.speedoSetReferenceState((StateItf) in.readObject());
in.defaultReadObject();
}
}