KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.enhydra.shark.api.RootException;
8 import org.enhydra.shark.api.SharkTransaction;
9 import org.enhydra.shark.api.client.wfbase.BaseException;
10 import org.enhydra.shark.api.client.wfservice.DeadlineAdministration;
11 import org.enhydra.shark.api.common.DeadlineInfo;
12 import org.enhydra.shark.api.common.SharkConstants;
13 import org.enhydra.shark.api.internal.instancepersistence.ProcessPersistenceInterface;
14 import org.enhydra.shark.api.internal.security.SecurityManager;
15 import org.enhydra.shark.api.internal.working.WfActivityInternal;
16 import org.enhydra.shark.api.internal.working.WfProcessInternal;
17 import org.enhydra.shark.api.internal.working.WfRequesterInternal;
18
19 /**
20  * The implementation of client interface through which client can check deadlines.
21  * @author Sasa Bojanic
22  */

23 public class DeadlineAdmin implements DeadlineAdministration {
24
25    private String JavaDoc userId="Unknown";
26
27    protected DeadlineAdmin () {
28    }
29
30    public void connect (String JavaDoc userId) {
31       this.userId=userId;
32    }
33
34    public void checkDeadlines () throws BaseException {
35       checkSecurity();
36       SharkTransaction t = null;
37       List JavaDoc pos;
38
39       try {
40          t = SharkUtilities.createTransaction();
41          pos=SharkEngineManager.getInstance().getInstancePersistenceManager().getAllRunningProcesses(t);
42          //SharkUtilities.commitTransaction(t);
43
} catch (RootException e) {
44          //SharkUtilities.rollbackTransaction(t);
45
SharkUtilities.emptyCaches(t);
46          if (e instanceof BaseException)
47             throw (BaseException)e;
48          else
49             throw new BaseException(e);
50       } finally {
51          SharkUtilities.releaseTransaction(t);
52       }
53       for (int i=0; i<pos.size(); i++) {
54          try {
55             t = SharkUtilities.createTransaction();
56             ProcessPersistenceInterface po=(ProcessPersistenceInterface)pos.get(i);
57
58             WfProcessInternal proc=SharkUtilities.getProcess(t,po.getId());
59             if (proc.state(t).equals(SharkConstants.STATE_OPEN_RUNNING)) {
60                proc.checkDeadlines(t);
61                SharkUtilities.commitTransaction(t);
62             }
63          } catch (Exception JavaDoc e) {
64             BaseException be=null;
65             if (e instanceof BaseException)
66                be=(BaseException)e;
67             else
68                be=new BaseException(e);
69
70             SharkUtilities.rollbackTransaction(t,be);
71             throw be;
72          } finally {
73             SharkUtilities.releaseTransaction(t);
74          }
75       }
76    }
77
78    public String JavaDoc[] checkDeadlines(final int instancesPerTransaction, int failuresToIgnore) throws BaseException {
79       checkSecurity();
80       
81       List JavaDoc instancesFailed2check = new ArrayList JavaDoc();
82        String JavaDoc reevalStr=SharkEngineManager
83           .getInstance()
84           .getCallbackUtilities()
85           .getProperty("Deadlines.reevaluateDeadlines","true");
86        boolean dreeval=Boolean.valueOf(reevalStr).booleanValue();
87
88        List JavaDoc instancesToCheck = null;
89        if (dreeval) {
90           instancesToCheck=getAllRunningProcesses();
91        } else {
92           instancesToCheck=getAllDeadlineInvalidProcessIds();
93        }
94
95        int sizeToCheck=instancesToCheck.size();
96        Iterator JavaDoc iterProcesses = instancesToCheck.iterator();
97        List JavaDoc currentBatch = null;
98        do {
99            SharkTransaction t = null;
100            String JavaDoc iid = null;
101            currentBatch = new ArrayList JavaDoc();
102            try {
103                t = SharkUtilities.createTransaction();
104                for (int n = 0; n < instancesPerTransaction; ++n) {
105                    if (!iterProcesses.hasNext()) {
106                        break;
107                    }
108                    if (dreeval) {
109                       iid = ((ProcessPersistenceInterface)iterProcesses.next()).getId();
110                    } else {
111                       iid = (String JavaDoc)iterProcesses.next();
112                    }
113                    iterProcesses.remove();
114                    currentBatch.add(iid);
115                    checkDeadlines(t, iid);
116                }
117                SharkUtilities.commitTransaction(t);
118            } catch (RootException _) {
119                SharkUtilities.rollbackTransaction(t,_);
120                instancesFailed2check.addAll(currentBatch);
121                // may log something
122
} finally {
123                SharkUtilities.releaseTransaction(t);
124            }
125            //System.err.println("\ttransaction finished: batch size:"+currentBatch.size());
126
} while (instancesFailed2check.size() <= failuresToIgnore && iterProcesses.hasNext());
127        String JavaDoc[] ret = new String JavaDoc[instancesFailed2check.size()];
128        instancesFailed2check.toArray(ret);
129        System.out.println(" deadline check finished: checked:"+sizeToCheck+", failed:"+ret.length);
130        return ret;
131    }
132
133    public String JavaDoc[] checkDeadlinesWithTermination() throws BaseException {
134       checkSecurity();
135       List JavaDoc instancesFailed2check = new ArrayList JavaDoc();
136       String JavaDoc reevalStr=SharkEngineManager
137          .getInstance()
138          .getCallbackUtilities()
139          .getProperty("Deadlines.reevaluateDeadlines","true");
140       boolean dreeval=Boolean.valueOf(reevalStr).booleanValue();
141
142       List JavaDoc instancesToCheck = null;
143       if (dreeval) {
144          instancesToCheck=getAllRunningProcesses();
145       } else {
146          instancesToCheck=getAllDeadlineInvalidProcessIds();
147       }
148
149       Iterator JavaDoc iterProcesses = instancesToCheck.iterator();
150       String JavaDoc iid=null;
151       while (iterProcesses.hasNext()) {
152          iid = (String JavaDoc)iterProcesses.next();
153          SharkTransaction st = null;
154          try {
155             st = SharkUtilities.createTransaction();
156             WfProcessInternal proc=SharkUtilities.getProcess(st,iid);
157             if (proc.state(st).equals(SharkConstants.STATE_OPEN_RUNNING)) {
158                WfRequesterInternal req=proc.requester(st);
159                if (req instanceof WfActivityInternal) {
160                   WfActivityInternal act=((WfActivityInternal)req);
161                   act.terminate(st);
162                } else {
163                   proc.terminate(st);
164                }
165                SharkUtilities.commitTransaction(st);
166                String JavaDoc msg="Proc "+iid+" terminated ";
167                if (req instanceof WfActivityInternal) {
168                   msg+=" through his parent subflow activity "+req;
169                }
170                msg+=" !";
171                System.out.println(msg);
172             }
173           } catch (RootException _) {
174             SharkUtilities.rollbackTransaction(st,_);
175             instancesFailed2check.add(iid);
176             System.out.println("Failed to terminate process "+iid);
177             // may log something
178
} finally {
179               SharkUtilities.releaseTransaction(st);
180           }
181           //System.err.println("\ttransaction finished: batch size:"+currentBatch.size());
182
}
183       String JavaDoc[] ret = new String JavaDoc[instancesFailed2check.size()];
184       instancesFailed2check.toArray(ret);
185       System.out.println(" deadline check finished: checked:"+instancesToCheck.size()+", failed:"+ret.length);
186       return ret;
187   }
188
189    public void checkDeadlines (SharkTransaction t) throws BaseException {
190       throw new BaseException("Not implemented - please use the method without SharkTransaction parameter");
191    }
192
193    public void checkDeadlines (String JavaDoc[] procIds) throws BaseException {
194       checkSecurity();
195       if (procIds==null) throw new BaseException("Invalid null value for parameter procIds");
196       SharkTransaction t = null;
197       for (int i=0; i<procIds.length; i++) {
198          try {
199             t = SharkUtilities.createTransaction();
200             WfProcessInternal proc=SharkUtilities.getProcess(t,procIds[i]);
201             if (proc.state(t).equals(SharkConstants.STATE_OPEN_RUNNING)) {
202                proc.checkDeadlines(t);
203                SharkUtilities.commitTransaction(t);
204             }
205          } catch (Exception JavaDoc e) {
206             BaseException be=null;
207             if (e instanceof BaseException)
208                be=(BaseException)e;
209             else
210                be=new BaseException(e);
211
212             SharkUtilities.rollbackTransaction(t,be);
213             throw be;
214          } finally {
215             SharkUtilities.releaseTransaction(t);
216          }
217       }
218    }
219
220    public void checkDeadlines (SharkTransaction t,String JavaDoc[] procIds) throws BaseException {
221       throw new BaseException("Not implemented - please use the method without SharkTransaction parameter");
222    }
223
224    public void checkDeadlines (String JavaDoc procId) throws BaseException {
225       SharkTransaction t = null;
226       try {
227          t = SharkUtilities.createTransaction();
228          checkDeadlines(t,procId);
229          SharkUtilities.commitTransaction(t);
230       } catch (RootException e) {
231          SharkUtilities.rollbackTransaction(t,e);
232          if (e instanceof BaseException)
233             throw (BaseException)e;
234          else
235             throw new BaseException(e);
236       } finally {
237          SharkUtilities.releaseTransaction(t);
238       }
239    }
240
241    public void checkDeadlines (SharkTransaction t,String JavaDoc procId) throws BaseException {
242       checkSecurity();
243
244       WfProcessInternal proc=SharkUtilities.getProcess(t,procId);
245       if (proc==null) throw new BaseException("Deadline checking failed - can't find process with Id="+procId);
246       if (proc.state(t).equals(SharkConstants.STATE_OPEN_RUNNING)) {
247          proc.checkDeadlines(t);
248       }
249    }
250
251    public void checkDeadline (String JavaDoc procId,String JavaDoc actId) throws BaseException {
252       SharkTransaction t = null;
253       try {
254          t = SharkUtilities.createTransaction();
255          checkDeadline(t,procId,actId);
256          SharkUtilities.commitTransaction(t);
257       } catch (RootException e) {
258          SharkUtilities.rollbackTransaction(t,e);
259          if (e instanceof BaseException)
260             throw (BaseException)e;
261          else
262             throw new BaseException(e);
263       } finally {
264          SharkUtilities.releaseTransaction(t);
265       }
266    }
267
268    public void checkDeadline (SharkTransaction t,String JavaDoc procId,String JavaDoc actId) throws BaseException {
269       checkSecurity();
270
271       WfProcessInternal proc=SharkUtilities.getProcess(t,procId);
272       if (proc==null) throw new BaseException("Deadline checking failed - can't find process with Id="+procId);
273       if (proc.state(t).equals(SharkConstants.STATE_OPEN_RUNNING)) {
274          proc.checkDeadline(t,actId);
275       }
276    }
277
278    public DeadlineInfo[] getDeadlineInfo(String JavaDoc procId) throws BaseException {
279       DeadlineInfo[] ret;
280       SharkTransaction t = null;
281       try {
282          t = SharkUtilities.createTransaction();
283          ret = getDeadlineInfo(t, procId);
284          //SharkUtilities.commitTransaction(t);
285
} catch (RootException e) {
286          //SharkUtilities.rollbackTransaction(t,e);
287
SharkUtilities.emptyCaches(t);
288          if (e instanceof BaseException)
289             throw (BaseException)e;
290          else
291             throw new BaseException(e);
292       } finally {
293          SharkUtilities.releaseTransaction(t);
294       }
295       return ret;
296    }
297
298    public DeadlineInfo[] getDeadlineInfo(SharkTransaction t,String JavaDoc procId) throws BaseException {
299       checkSecurity();
300       WfProcessInternal proc=SharkUtilities.getProcess(t, procId);
301       if (proc==null) {
302          throw new BaseException ("There is no process with id "+procId+" !");
303       }
304       List JavaDoc acts=proc.getActiveActivities(t);
305       List JavaDoc dinfo=new ArrayList JavaDoc();
306       Iterator JavaDoc it=acts.iterator();
307       while (it.hasNext()) {
308          WfActivityInternal act=(WfActivityInternal)it.next();
309          dinfo.addAll(act.getDeadlineInfo(t));
310       }
311       DeadlineInfo[] ret=new DeadlineInfo[dinfo.size()];
312       dinfo.toArray(ret);
313       return ret;
314    }
315
316    public DeadlineInfo[] getDeadlineInfo(String JavaDoc procId, String JavaDoc actId) throws BaseException {
317       DeadlineInfo[] ret;
318       SharkTransaction t = null;
319       try {
320          t = SharkUtilities.createTransaction();
321          ret = getDeadlineInfo(t, procId, actId);
322          //SharkUtilities.commitTransaction(t);
323
} catch (RootException e) {
324          //SharkUtilities.rollbackTransaction(t,e);
325
SharkUtilities.emptyCaches(t);
326          if (e instanceof BaseException)
327             throw (BaseException)e;
328          else
329             throw new BaseException(e);
330       } finally {
331          SharkUtilities.releaseTransaction(t);
332       }
333       return ret;
334    }
335
336    public DeadlineInfo[] getDeadlineInfo(SharkTransaction t,String JavaDoc procId, String JavaDoc actId) throws BaseException {
337       checkSecurity();
338       WfActivityInternal act=SharkUtilities.getActivity(t, procId, actId);
339       if (act==null) {
340          throw new BaseException ("There is no activity with id "+actId+" in process "+procId+" !");
341       }
342       List JavaDoc dinfo=act.getDeadlineInfo(t);
343       DeadlineInfo[] ret=new DeadlineInfo[dinfo.size()];
344       dinfo.toArray(ret);
345       return ret;
346    }
347
348    protected List JavaDoc getAllRunningProcesses() throws BaseException {
349       SharkTransaction t=null;
350       try {
351           t = SharkUtilities.createTransaction();
352           return SharkEngineManager.getInstance().getInstancePersistenceManager().getAllRunningProcesses(t);
353           //SharkUtilities.commitTransaction(t);
354
} catch (RootException e) {
355           //SharkUtilities.rollbackTransaction(t);
356
SharkUtilities.emptyCaches(t);
357           if (e instanceof BaseException)
358              throw (BaseException)e;
359           else
360              throw new BaseException(e);
361        } finally {
362           SharkUtilities.releaseTransaction(t);
363        }
364   }
365
366   protected List JavaDoc getAllDeadlineInvalidProcessIds() throws BaseException {
367      SharkTransaction t = null;
368      try {
369          t = SharkUtilities.createTransaction();
370          return SharkEngineManager.getInstance().getInstancePersistenceManager().getAllIdsForProcessesWithExpiriedDeadlines(System.currentTimeMillis(), t);
371          //SharkUtilities.commitTransaction(t);
372
} catch (RootException e) {
373          //SharkUtilities.rollbackTransaction(t);
374
SharkUtilities.emptyCaches(t);
375          if (e instanceof BaseException)
376             throw (BaseException)e;
377          else
378             throw new BaseException(e);
379       } finally {
380          SharkUtilities.releaseTransaction(t);
381       }
382   }
383
384    protected void checkSecurity () throws BaseException {
385       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
386       if (sm!=null) {
387          SharkTransaction t=null;
388          try {
389             t = SharkUtilities.createTransaction();
390             sm.check_deadlines (t,userId);
391             SharkUtilities.commitTransaction(t);
392          } catch (RootException e) {
393             SharkUtilities.rollbackTransaction(t,e);
394             if (e instanceof BaseException)
395                throw (BaseException)e;
396             else
397                throw new BaseException(e);
398          } finally {
399             SharkUtilities.releaseTransaction(t);
400          }
401       }
402    }
403
404 }
405
Popular Tags