01: package dalma.impl;
02:
03: import java.io.Serializable;
04: import java.util.UUID;
05:
06: /**
07: * @author Kohsuke Kawaguchi
08: */
09: public abstract class GeneratorImpl implements Serializable {
10:
11: /**
12: * {@link GeneratorImpl} has an unique ID so that it can always deserialize back
13: * to the same instance.
14: */
15: /*package*/final UUID id = UUID.randomUUID();
16:
17: /**
18: * Conversation to which this generator belongs to.
19: */
20: private ConversationImpl conv;
21:
22: /**
23: * Called after this generator is restored from disk.
24: *
25: * Typically used to requeue this object.
26: * This happens while the conversation is being restored from the disk.
27: */
28: protected abstract void onLoad();
29:
30: /**
31: * Called when the conversation completes. Used to dequeue this object.
32: */
33: protected abstract void dispose();
34:
35: /*package*/final void setConversation(ConversationImpl conv) {
36: assert this .conv == null;
37: this .conv = conv;
38: }
39:
40: protected Object writeReplace() {
41: if (SerializationContext.get().mode == SerializationContext.Mode.CONVERSATION)
42: return this ;
43: else
44: return new Moniker(conv, id);
45: }
46:
47: private static final class Moniker implements Serializable {
48: private final ConversationImpl conv;
49: private final UUID id;
50:
51: public Moniker(ConversationImpl conv, UUID id) {
52: this .conv = conv;
53: this .id = id;
54: }
55:
56: private Object readResolve() {
57: // TODO: what if the id is already removed from engine?
58: // we can fix this by allowing Conversation object itself to be persisted
59: // (and then readResolve may replace if it's still running),
60: // but how do we do about the classLoader field?
61: return conv.getGenerator(id);
62: }
63:
64: private static final long serialVersionUID = 1L;
65: }
66:
67: private static final long serialVersionUID = 1L;
68: }
|