KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > scheduler > exe > Timer


1 package org.jbpm.scheduler.exe;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.io.StringWriter JavaDoc;
6 import java.text.DateFormat JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.jbpm.graph.def.Action;
13 import org.jbpm.graph.def.Event;
14 import org.jbpm.graph.def.GraphElement;
15 import org.jbpm.graph.exe.ExecutionContext;
16 import org.jbpm.graph.exe.ProcessInstance;
17 import org.jbpm.graph.exe.Token;
18 import org.jbpm.taskmgmt.exe.TaskInstance;
19
20 /**
21  * a process timer.
22  */

23 public class Timer implements Serializable JavaDoc {
24
25   private static final long serialVersionUID = 1L;
26   
27   long id = 0;
28   String JavaDoc name = null;
29   Date JavaDoc dueDate = null;
30   String JavaDoc repeat = null;
31   String JavaDoc transitionName = null;
32   Action action = null;
33   Token token = null;
34   ProcessInstance processInstance = null;
35   TaskInstance taskInstance = null;
36   GraphElement graphElement = null;
37   String JavaDoc exception = null;
38
39   Timer(){}
40   
41   public Timer(Token token) {
42     this.token = token;
43     this.processInstance = token.getProcessInstance();
44   }
45
46   public void execute() {
47     ExecutionContext executionContext = new ExecutionContext(token);
48     if (taskInstance!=null) {
49       executionContext.setTaskInstance(taskInstance);
50     }
51
52     // first fire the event if there is a graph element specified
53
if (graphElement!=null) {
54       graphElement.fireAndPropagateEvent(Event.EVENTTYPE_TIMER, executionContext);
55     }
56     
57     // then execute the action if there is one
58
if (action!=null) {
59       try {
60         log.debug("executing timer '"+this+"'");
61         action.execute(executionContext);
62       } catch (Throwable JavaDoc actionException) {
63         log.warn("timer action threw exception", actionException);
64
65         // we put the exception in t
66
Throwable JavaDoc t = actionException;
67         try {
68           // if there is a graphElement connected to this timer...
69
if (graphElement != null) {
70             // we give that graphElement a chance to catch the exception
71
graphElement.raiseException(actionException, executionContext);
72             log.debug("timer exception got handled by '"+graphElement+"'");
73             t = null;
74           }
75         } catch (Throwable JavaDoc rethrowOrDelegationException) {
76           // if the exception handler rethrows or the original exception results in a DelegationException...
77
t = rethrowOrDelegationException;
78         }
79         
80         if (t!=null) {
81           log.error("unhandled timer exception", t);
82           // this means an unhandled exception occurred in this timer
83
StringWriter JavaDoc sw = new StringWriter JavaDoc();
84           actionException.printStackTrace(new PrintWriter JavaDoc(sw));
85           exception = sw.toString();
86           if (exception.length()>4000) exception = exception.substring(0, 4000);
87         }
88       }
89     }
90
91     // then take a transition if one is specified
92
if ( (transitionName!=null)
93          && (exception==null) // and if no unhandled exception occurred during the action
94
) {
95       if (token.getNode().hasLeavingTransition(transitionName)) {
96         token.signal(transitionName);
97       }
98     }
99   }
100
101   public boolean isDue() {
102     return (dueDate.getTime()<=System.currentTimeMillis());
103   }
104   
105   private static DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("HH:mm:ss,SSS");
106   public String JavaDoc toString() {
107     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
108     buffer.append("timer(");
109     buffer.append(name);
110     if ( (action!=null)
111          && (action.getActionDelegation()!=null)
112          && (action.getActionDelegation().getClassName()!=null)) {
113       buffer.append(",");
114       buffer.append(action.getActionDelegation().getClassName());
115     }
116     if (dueDate!=null) {
117       buffer.append(",");
118       buffer.append(dateFormat.format(dueDate));
119     }
120     buffer.append(")");
121     return buffer.toString();
122   }
123   
124   public Date JavaDoc getDueDate() {
125     return dueDate;
126   }
127   public void setDueDate(Date JavaDoc dueDate) {
128     this.dueDate = dueDate;
129   }
130   public String JavaDoc getName() {
131     return name;
132   }
133   public void setName(String JavaDoc name) {
134     this.name = name;
135   }
136   public String JavaDoc getRepeat() {
137     return repeat;
138   }
139   public void setRepeat(String JavaDoc repeatDuration) {
140     this.repeat = repeatDuration;
141   }
142   public Token getToken() {
143     return token;
144   }
145   public void setToken(Token token) {
146     this.token = token;
147   }
148   public String JavaDoc getTransitionName() {
149     return transitionName;
150   }
151   public void setTransitionName(String JavaDoc transitionName) {
152     this.transitionName = transitionName;
153   }
154   public long getId() {
155     return id;
156   }
157   public GraphElement getGraphElement() {
158     return graphElement;
159   }
160   public void setGraphElement(GraphElement graphElement) {
161     this.graphElement = graphElement;
162   }
163   public Action getAction() {
164     return action;
165   }
166   public void setAction(Action action) {
167     this.action = action;
168   }
169   public String JavaDoc getException() {
170     return exception;
171   }
172   public TaskInstance getTaskInstance() {
173     return taskInstance;
174   }
175   public void setTaskInstance(TaskInstance taskInstance) {
176     this.taskInstance = taskInstance;
177   }
178   
179   private static final Log log = LogFactory.getLog(Timer.class);
180 }
181
Popular Tags