KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > logging > exe > LoggingInstance


1 package org.jbpm.logging.exe;
2
3 import java.util.*;
4
5 import org.apache.commons.logging.*;
6 import org.jbpm.graph.log.*;
7 import org.jbpm.logging.log.*;
8 import org.jbpm.module.exe.*;
9
10 /**
11  * non persisted class that collects {@link org.jbpm.logging.log.ProcessLog}s
12  * during process execution. When the process instance gets saved, the
13  * process logs will be saved by the {@link org.jbpm.db.LoggingSession}.
14  */

15 public class LoggingInstance extends ModuleInstance {
16
17   private static final long serialVersionUID = 1L;
18   
19   List logs = new ArrayList();
20   transient LinkedList compositeLogStack = new LinkedList();
21   
22   public LoggingInstance() {
23   }
24   
25   public void startCompositeLog(CompositeLog compositeLog) {
26     addLog(compositeLog);
27     compositeLogStack.addFirst(compositeLog);
28   }
29   
30   public void endCompositeLog() {
31     compositeLogStack.removeFirst();
32   }
33
34   public void addLog(ProcessLog processLog) {
35     if (!compositeLogStack.isEmpty()) {
36       CompositeLog currentCompositeLog = (CompositeLog) compositeLogStack.getFirst();
37       processLog.setParent(currentCompositeLog);
38       currentCompositeLog.addChild(processLog);
39     }
40     processLog.setDate( new Date() );
41     
42     logs.add(processLog);
43   }
44   
45   public List getLogs() {
46     return logs;
47   }
48
49   /**
50    * get logs, filetered by log type.
51    */

52   public List getLogs(Class JavaDoc filterClass) {
53     return getLogs(logs, filterClass);
54   }
55   
56   public static List getLogs(Collection logs, Class JavaDoc filterClass) {
57     List filteredLogs = new ArrayList();
58     if (logs!=null) {
59       Iterator iter = logs.iterator();
60       while (iter.hasNext()) {
61         Object JavaDoc log = iter.next();
62         if (filterClass.isAssignableFrom(log.getClass())) {
63           filteredLogs.add(log);
64         }
65       }
66     }
67     return filteredLogs;
68   }
69
70   LinkedList getCompositeLogStack() {
71     return compositeLogStack;
72   }
73
74   List getCurrentOperationReversedActionLogs() {
75     List actionLogs = new ArrayList();
76     ProcessLog operationLog = (ProcessLog) compositeLogStack.getFirst();
77     ListIterator listIterator = logs.listIterator(logs.size());
78     ProcessLog processLog = (ProcessLog) listIterator.previous();
79     while ( (listIterator.hasNext())
80             && (processLog!=operationLog) ) {
81       if (processLog instanceof ActionLog) {
82         actionLogs.add(0, processLog);
83       }
84     }
85     return actionLogs;
86   }
87   
88   public void logLogs() {
89     Iterator iter = logs.iterator();
90     while (iter.hasNext()) {
91       ProcessLog processLog = (ProcessLog) iter.next();
92       if (processLog.getParent()==null) {
93         logLog("+-", processLog);
94       }
95     }
96   }
97
98   private void logLog(String JavaDoc indentation, ProcessLog processLog) {
99     log.debug(processLog.getToken()+"["+processLog.getIndex()+"] "+processLog+" on "+processLog.getToken());
100     if (processLog instanceof CompositeLog) {
101       CompositeLog compositeLog = (CompositeLog) processLog;
102       if (compositeLog.getChildren()!=null) {
103         Iterator iter = compositeLog.getChildren().iterator();
104         while (iter.hasNext()) {
105           logLog("| "+indentation, (ProcessLog) iter.next());
106         }
107       }
108     }
109   }
110   
111   private static final Log log = LogFactory.getLog(LoggingInstance.class);
112
113 }
114
Popular Tags