01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: */package org.objectweb.speedo.mim.lib;
04:
05: import org.objectweb.speedo.mim.api.StateItf;
06: import org.objectweb.speedo.mim.api.PersistentObjectItf;
07: import org.objectweb.speedo.pm.api.POManagerItf;
08:
09: import java.io.IOException;
10:
11: /**
12: * Is an helper for the the serialization of the persistent objects.
13: *
14: * @author S.Chassande-Barrioz
15: */
16: public class SpeedoPOSerializer {
17:
18: /**
19: * Serializes a persistent class (PersistentObjectItf) into an ObjectOutputStream.
20: *
21: * @param sp is the speedo po to serialize
22: * @param fieldIds indicates which have to be serialized at least.
23: *
24: * @throws IOException
25: */
26: public static void writeObject(java.io.ObjectOutputStream out,
27: PersistentObjectItf sp, long[] fieldIds) throws IOException {
28: StateItf state;
29: boolean pmAllocated = false;
30: POManagerItf pm = null;
31: if (sp.speedoIsActive()) {
32: // Fetch the PersistenceManager
33: pm = sp.speedoGetHome().getPOManagerFactory().lookup();
34: if (pm == null) {
35: // Allocate a new PersistenceManager to close at the end
36: pm = (POManagerItf) sp.speedoGetHome()
37: .getPOManagerFactory().getPOManager();
38: pmAllocated = true;
39: }
40: try {
41: state = sp.speedoGetHome().readIntention(sp, fieldIds);
42: } catch (Exception e) {
43: // Close the PersistenceManager if it has been allocated localy
44: if (pmAllocated) {
45: pm.closePOManager();
46: }
47: throw new IOException(e.getMessage());
48: }
49: } else {
50: state = sp.speedoGetReferenceState();
51: }
52: try {
53: out.writeObject(state);
54: out.defaultWriteObject();
55: } finally {
56: // Close the PersistenceManager if it has been allocated localy
57: if (pmAllocated) {
58: pm.closePOManager();
59: }
60: }
61: }
62:
63: /**
64: * Deserializes a persistent class (PersistentObjectItf) from an ObjectInputStream.
65: *
66: * @param sp is the speedo po where attaches the content
67: *
68: * @throws IOException
69: */
70: public static void readObject(java.io.ObjectInputStream in,
71: PersistentObjectItf sp) throws IOException,
72: ClassNotFoundException {
73: sp.speedoIsActive(false);
74: sp.speedoSetReferenceState((StateItf) in.readObject());
75: in.defaultReadObject();
76: }
77:
78: }
|