1 package org.jbpm.scheduler.exe; 2 3 import java.io.PrintWriter ; 4 import java.io.Serializable ; 5 import java.io.StringWriter ; 6 import java.text.DateFormat ; 7 import java.text.SimpleDateFormat ; 8 import java.util.Date ; 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 23 public class Timer implements Serializable { 24 25 private static final long serialVersionUID = 1L; 26 27 long id = 0; 28 String name = null; 29 Date dueDate = null; 30 String repeat = null; 31 String transitionName = null; 32 Action action = null; 33 Token token = null; 34 ProcessInstance processInstance = null; 35 TaskInstance taskInstance = null; 36 GraphElement graphElement = null; 37 String 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 if (graphElement!=null) { 54 graphElement.fireAndPropagateEvent(Event.EVENTTYPE_TIMER, executionContext); 55 } 56 57 if (action!=null) { 59 try { 60 log.debug("executing timer '"+this+"'"); 61 action.execute(executionContext); 62 } catch (Throwable actionException) { 63 log.warn("timer action threw exception", actionException); 64 65 Throwable t = actionException; 67 try { 68 if (graphElement != null) { 70 graphElement.raiseException(actionException, executionContext); 72 log.debug("timer exception got handled by '"+graphElement+"'"); 73 t = null; 74 } 75 } catch (Throwable rethrowOrDelegationException) { 76 t = rethrowOrDelegationException; 78 } 79 80 if (t!=null) { 81 log.error("unhandled timer exception", t); 82 StringWriter sw = new StringWriter (); 84 actionException.printStackTrace(new PrintWriter (sw)); 85 exception = sw.toString(); 86 if (exception.length()>4000) exception = exception.substring(0, 4000); 87 } 88 } 89 } 90 91 if ( (transitionName!=null) 93 && (exception==null) ) { 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 dateFormat = new SimpleDateFormat ("HH:mm:ss,SSS"); 106 public String toString() { 107 StringBuffer buffer = new StringBuffer (); 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 getDueDate() { 125 return dueDate; 126 } 127 public void setDueDate(Date dueDate) { 128 this.dueDate = dueDate; 129 } 130 public String getName() { 131 return name; 132 } 133 public void setName(String name) { 134 this.name = name; 135 } 136 public String getRepeat() { 137 return repeat; 138 } 139 public void setRepeat(String 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 getTransitionName() { 149 return transitionName; 150 } 151 public void setTransitionName(String 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 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 |