001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.logging.exe;
023:
024: import java.util.*;
025:
026: import org.apache.commons.logging.*;
027: import org.jbpm.graph.log.*;
028: import org.jbpm.logging.log.*;
029: import org.jbpm.module.exe.*;
030: import org.jbpm.util.Clock;
031:
032: /**
033: * non persisted class that collects {@link org.jbpm.logging.log.ProcessLog}s
034: * during process execution. When the process instance gets saved, the
035: * process logs will be saved by the {@link org.jbpm.db.LoggingSession}.
036: */
037: public class LoggingInstance extends ModuleInstance {
038:
039: private static final long serialVersionUID = 1L;
040:
041: List logs = new ArrayList();
042: transient LinkedList compositeLogStack = new LinkedList();
043:
044: public LoggingInstance() {
045: }
046:
047: public void startCompositeLog(CompositeLog compositeLog) {
048: addLog(compositeLog);
049: compositeLogStack.addFirst(compositeLog);
050: }
051:
052: public void endCompositeLog() {
053: compositeLogStack.removeFirst();
054: }
055:
056: public void addLog(ProcessLog processLog) {
057: if (!compositeLogStack.isEmpty()) {
058: CompositeLog currentCompositeLog = (CompositeLog) compositeLogStack
059: .getFirst();
060: processLog.setParent(currentCompositeLog);
061: currentCompositeLog.addChild(processLog);
062: }
063: processLog.setDate(Clock.getCurrentTime());
064:
065: logs.add(processLog);
066: }
067:
068: public List getLogs() {
069: return logs;
070: }
071:
072: /**
073: * get logs, filetered by log type.
074: */
075: public List getLogs(Class filterClass) {
076: return getLogs(logs, filterClass);
077: }
078:
079: public static List getLogs(Collection logs, Class filterClass) {
080: List filteredLogs = new ArrayList();
081: if (logs != null) {
082: Iterator iter = logs.iterator();
083: while (iter.hasNext()) {
084: Object log = iter.next();
085: if (filterClass.isAssignableFrom(log.getClass())) {
086: filteredLogs.add(log);
087: }
088: }
089: }
090: return filteredLogs;
091: }
092:
093: LinkedList getCompositeLogStack() {
094: return compositeLogStack;
095: }
096:
097: List getCurrentOperationReversedActionLogs() {
098: List actionLogs = new ArrayList();
099: ProcessLog operationLog = (ProcessLog) compositeLogStack
100: .getFirst();
101: ListIterator listIterator = logs.listIterator(logs.size());
102: ProcessLog processLog = (ProcessLog) listIterator.previous();
103: while ((listIterator.hasNext()) && (processLog != operationLog)) {
104: if (processLog instanceof ActionLog) {
105: actionLogs.add(0, processLog);
106: }
107: }
108: return actionLogs;
109: }
110:
111: public void logLogs() {
112: Iterator iter = logs.iterator();
113: while (iter.hasNext()) {
114: ProcessLog processLog = (ProcessLog) iter.next();
115: if (processLog.getParent() == null) {
116: logLog("+-", processLog);
117: }
118: }
119: }
120:
121: void logLog(String indentation, ProcessLog processLog) {
122: log.debug(processLog.getToken() + "[" + processLog.getIndex()
123: + "] " + processLog + " on " + processLog.getToken());
124: if (processLog instanceof CompositeLog) {
125: CompositeLog compositeLog = (CompositeLog) processLog;
126: if (compositeLog.getChildren() != null) {
127: Iterator iter = compositeLog.getChildren().iterator();
128: while (iter.hasNext()) {
129: logLog("| " + indentation, (ProcessLog) iter.next());
130: }
131: }
132: }
133: }
134:
135: Object readResolve() {
136: compositeLogStack = new LinkedList();
137: return this ;
138: }
139:
140: private static final Log log = LogFactory
141: .getLog(LoggingInstance.class);
142: }
|