01: package dalma;
02:
03: import dalma.endpoints.timer.TimerEndPoint;
04: import dalma.impl.ConversationImpl;
05:
06: import java.io.Serializable;
07: import java.util.logging.Logger;
08:
09: /**
10: * Base class of a workflow program.
11: *
12: * <p>
13: * In Dalma, a workflow consists of two instances.
14: * A {@link Conversation}, which is always retained in memory
15: * to act as the outer shell of a workflow, and
16: * a {@link Workflow}, which contains application state and
17: * persisted to the disk. This class represents the inner shell part.
18: *
19: * <p>
20: * This class also defines a bunch of convenience methods for
21: * workflow programs to access various parts of dalma.
22: *
23: * @author Kohsuke Kawaguchi
24: */
25: public abstract class Workflow implements Runnable, Serializable {
26:
27: private ConversationImpl owner;
28:
29: /**
30: * This method shall be overrided to implement the actual workflow code
31: * (much like {@link Thread#run()}.
32: */
33: public abstract void run();
34:
35: /**
36: * Gets the {@link Conversation} coupled with this workflow.
37: *
38: * @return
39: * always non-null. Same object.
40: */
41: public Conversation getOwner() {
42: return owner;
43: }
44:
45: /**
46: * Invoked by the dalma implementation to set the owner.
47: *
48: * Shall never be used by anyone else.
49: */
50: public void setOwner(ConversationImpl owner) {
51: this .owner = owner;
52: }
53:
54: /**
55: * Sets the title of this workflow.
56: *
57: * The title set from here will be made accessible
58: * to {@link Conversation#getTitle()}
59: *
60: * @see Conversation#getTitle()
61: */
62: public void setTitle(String title) {
63: owner.setTitle(title);
64: }
65:
66: /**
67: * Gets the {@link Logger} for this workflow.
68: *
69: * <p>
70: * Logs recorded to this logger are persisted
71: * as a part of {@link Conversation}, and allows the monitoring
72: * application (and users) to see what's going on.
73: */
74: protected Logger getLogger() {
75: return owner.getLogger();
76: }
77:
78: /**
79: * Sleeps the workflow for the given amount of time.
80: *
81: * <p>
82: * This method differs from {@link Thread#sleep(long)} in that
83: * this method doesn't block the thread that's running workflow.
84: *
85: * <p>
86: * For this reason,
87: * this method is recommended over {@link Thread#sleep(long)}.
88: */
89: protected static void sleep(long delay, TimeUnit unit) {
90: TimerEndPoint.waitFor(delay, unit);
91: }
92:
93: private static final long serialVersionUID = 1L;
94: }
|