KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > WfExecutionObjectImpl


1 package org.enhydra.shark;
2
3
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.enhydra.shark.api.SharkTransaction;
11 import org.enhydra.shark.api.client.timebase.UtcT;
12 import org.enhydra.shark.api.client.wfbase.BaseException;
13 import org.enhydra.shark.api.client.wfmodel.AlreadySuspended;
14 import org.enhydra.shark.api.client.wfmodel.CannotResume;
15 import org.enhydra.shark.api.client.wfmodel.CannotStop;
16 import org.enhydra.shark.api.client.wfmodel.CannotSuspend;
17 import org.enhydra.shark.api.client.wfmodel.InvalidData;
18 import org.enhydra.shark.api.client.wfmodel.NotRunning;
19 import org.enhydra.shark.api.client.wfmodel.NotSuspended;
20 import org.enhydra.shark.api.client.wfmodel.UpdateNotAllowed;
21 import org.enhydra.shark.api.common.SharkConstants;
22 import org.enhydra.shark.api.internal.limitagent.LimitAgentManager;
23 import org.enhydra.shark.api.internal.working.WfExecutionObjectInternal;
24 import org.enhydra.shark.api.internal.working.WfStateEventAuditInternal;
25 import org.enhydra.shark.utilities.MiscUtilities;
26 import org.enhydra.shark.xpdl.XMLCollectionElement;
27 import org.enhydra.shark.xpdl.XMLUtil;
28 import org.enhydra.shark.xpdl.elements.Activity;
29 import org.enhydra.shark.xpdl.elements.ProcessHeader;
30 import org.enhydra.shark.xpdl.elements.WorkflowProcess;
31
32 /**
33  * WfExecutionObjectImpl - Workflow Execution Object implementation
34  * The WfProcessImpl and WfActivityImpl classes are extended from this class.
35  * @author Sasa Bojanic
36  * @author Vladimir Puskas
37  */

38 public abstract class WfExecutionObjectImpl implements WfExecutionObjectInternal {
39
40    protected String JavaDoc state=SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED;
41    protected String JavaDoc name;
42    protected String JavaDoc key;
43    protected String JavaDoc description;
44    protected short priority;
45    protected long lastStateTime;
46    protected long limitTime=Long.MAX_VALUE/2;
47
48    protected WfStateEventAuditInternal lastStateEventAudit;
49
50    public String JavaDoc state (SharkTransaction t) throws BaseException {
51       return state;
52    }
53
54    public String JavaDoc name (SharkTransaction t) throws BaseException {
55       return name;
56    }
57
58    public void set_name (SharkTransaction t,String JavaDoc new_value) throws BaseException {
59       try {
60          name=new_value;
61          if (name.length()>254) {
62             name=name.substring(0,254);
63          }
64          persist(t);
65       } catch (Exception JavaDoc tme) {
66          throw new BaseException(tme);
67       }
68    }
69
70    public String JavaDoc key (SharkTransaction t) throws BaseException {
71       return key;
72    }
73
74    public String JavaDoc description (SharkTransaction t) throws BaseException {
75       return description;
76    }
77
78    public void set_description (SharkTransaction t,String JavaDoc new_value) throws BaseException {
79       try {
80          description=new_value;
81          if (description.length()>254) {
82             description=description.substring(0,254);
83          }
84          persist(t);
85       } catch (Exception JavaDoc tme) {
86          throw new BaseException(tme);
87       }
88    }
89
90    public Map JavaDoc process_context (SharkTransaction t) throws BaseException {
91       Map JavaDoc m=getContext(t);
92       Map JavaDoc ret=new HashMap JavaDoc();
93       Iterator JavaDoc it=m.entrySet().iterator();
94       while (it.hasNext()) {
95          Map.Entry JavaDoc me=(Map.Entry JavaDoc)it.next();
96          Object JavaDoc id=me.getKey();
97          Object JavaDoc val=me.getValue();
98          try {
99             ret.put(id,MiscUtilities.cloneWRD(val));
100          } catch (Throwable JavaDoc thr) {
101             throw new BaseException(thr);
102          }
103       }
104       return ret;
105    }
106
107    public abstract void set_process_context (SharkTransaction t,Map JavaDoc new_value) throws BaseException, InvalidData, UpdateNotAllowed;
108
109    public short priority (SharkTransaction t) throws BaseException {
110       return priority;
111    }
112
113    public void set_priority (SharkTransaction t,short new_value) throws BaseException {
114       if (new_value<1 || new_value>5) throw new BaseException("Priority is out of range!");
115       if (state(t).startsWith(SharkConstants.STATEPREFIX_CLOSED)) throw new BaseException("Priority cannot be updated.!");
116       priority=new_value;
117       try {
118          persist(t);
119       } catch (Exception JavaDoc ex) {
120          throw new BaseException(ex);
121       }
122    }
123
124    public void activateLimitAgent(SharkTransaction trans) throws BaseException {
125       LimitAgentManager mgr = SharkEngineManager.getInstance().getLimitAgentManager();
126
127       // check the current state
128
if (mgr==null || this.state(trans).startsWith(SharkConstants.STATEPREFIX_CLOSED)) {
129          // we only set this if we are running
130
return;
131       }
132
133       String JavaDoc durationStr;
134       String JavaDoc limitStr;
135       long startTime=-1;
136
137       // get the duration unit string
138
XMLCollectionElement xpdlObj=getXPDLObject(trans);
139       WorkflowProcess wp=XMLUtil.getWorkflowProcess(xpdlObj);
140       ProcessHeader ph = wp.getProcessHeader();
141       durationStr = ph.getDurationUnit();
142
143       if (xpdlObj instanceof Activity) {
144          limitStr = ((Activity)xpdlObj).getLimit();
145          startTime=getCreationTime(trans);
146       } else {
147          limitStr = ph.getLimit();
148          startTime=getStartTime(trans);
149       }
150
151       // duration unit
152
int durationUnit = 0;
153
154       // convert to calendar value
155
if (durationStr == null || durationStr.trim().length() == 0) {
156          SharkEngineManager.getInstance().getCallbackUtilities().debug("DurationUnit is not set, not setting limit agent");
157          return;
158       } else {
159          // get the Calendar value
160
if ("Y".equals(durationStr)) {
161             durationUnit = Calendar.YEAR;
162          } else if ("M".equals(durationStr)) {
163             durationUnit = Calendar.MONTH;
164          } else if ("D".equals(durationStr)) {
165             durationUnit = Calendar.DAY_OF_MONTH;
166          } else if ("h".equals(durationStr)) {
167             durationUnit = Calendar.HOUR;
168          } else if ("m".equals(durationStr)) {
169             durationUnit = Calendar.MINUTE;
170          } else if ("s".equals(durationStr)) {
171             durationUnit = Calendar.SECOND;
172          }
173       }
174
175       // limit value - convert to integer
176
int limit = 0;
177       if (limitStr == null || limitStr.trim().length() == 0) {
178          SharkEngineManager.getInstance().getCallbackUtilities().debug("Limit value is not set, not setting limit agent");
179          return;
180       } else {
181          try {
182             limit = Integer.parseInt(limitStr);
183          } catch (NumberFormatException JavaDoc e) {
184             SharkEngineManager.getInstance().getCallbackUtilities().error("Defined Limit is not a valid integer, not setting limit : " + limitStr);
185             return;
186          }
187       }
188
189       // determine the limit runtime
190
long runtime = 0;
191       if (durationUnit > 0 && limit > 0) {
192          Calendar JavaDoc cal = Calendar.getInstance();
193          // MUST support JDK 1.3.x, so we cannot use
194
// cal.setTimeInMillis & cal.getTimeInMillis
195
cal.setTime(new Date JavaDoc(startTime));
196          cal.add(durationUnit, limit);
197          runtime = cal.getTime().getTime();
198       }
199
200       // get the process context for this execution object
201
Map JavaDoc context = this.process_context(trans);
202
203       // register with the limit agent manager
204
if (runtime > 0) {
205          notifyStart(trans,context,runtime);
206          limitTime=runtime;
207       }
208    }
209
210    public abstract long getCreationTime (SharkTransaction trans) throws BaseException;
211
212    public abstract long getStartTime (SharkTransaction trans) throws BaseException;
213
214    public UtcT last_state_time(SharkTransaction t) throws BaseException {
215       return new UtcT(lastStateTime, 0, (short)0, (short)0);
216    }
217
218    public abstract void resume(SharkTransaction t) throws BaseException, CannotResume, NotSuspended;
219
220    public abstract void suspend(SharkTransaction t) throws BaseException, CannotSuspend, NotRunning, AlreadySuspended;
221
222    public abstract void terminate(SharkTransaction t) throws BaseException, CannotStop, NotRunning;
223
224    public abstract void abort(SharkTransaction t) throws BaseException, CannotStop, NotRunning;
225
226    protected abstract XMLCollectionElement getXPDLObject(SharkTransaction t) throws BaseException;
227
228    protected abstract void notifyStart (SharkTransaction t,Map JavaDoc context,long runtime) throws BaseException;
229 }
230
231
232
Popular Tags