001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.engine.node;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
023:
024: /**
025: * Logs {@link RouteNodeInstance} graphs in a format which is indented and easy to read.
026: *
027: * @author ewestfal
028: * @author rkirkend
029: */
030: public class NodeJotter {
031:
032: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
033: .getLogger(NodeJotter.class);
034: private static final String INDENT = " ";
035:
036: public static void jotNodeInstance(
037: DocumentRouteHeaderValue document,
038: RouteNodeInstance nodeInstance) {
039: try {
040: if (LOG.isDebugEnabled()) {
041: List initialNodeInstances = document
042: .getInitialRouteNodeInstances();
043: for (Iterator iterator = initialNodeInstances
044: .iterator(); iterator.hasNext();) {
045: RouteNodeInstance initialNodeInstance = (RouteNodeInstance) iterator
046: .next();
047: NodeType nodeType = NodeType
048: .fromNodeInstance(initialNodeInstance);
049: LOG.debug(orchestrateOutput(initialNodeInstance,
050: nodeType, null, 0));
051: }
052: } else if (LOG.isInfoEnabled()) {
053: NodeType nodeType = NodeType
054: .fromNodeInstance(nodeInstance);
055: LOG.info(outputNodeInstanceToLog(nodeInstance,
056: nodeType, 0));
057: }
058: } catch (Throwable t) {
059: LOG.warn("Caught error attempting to jot node instance"
060: + nodeInstance);
061: }
062: }
063:
064: private static String orchestrateOutput(
065: RouteNodeInstance nodeInstance, NodeType nodeType,
066: SplitJoinContext sjCtx, int indentDepth) throws Exception {
067: String output = "";
068: boolean isSplit = NodeType.SPLIT.equals(nodeType);
069: boolean isJoin = NodeType.JOIN.equals(nodeType);
070: if (isJoin && sjCtx != null) {
071: sjCtx.joinNodeInstance = nodeInstance;
072: return output;
073: }
074: SplitJoinContext newSplitJoinContext = null;
075: if (isSplit) {
076: newSplitJoinContext = new SplitJoinContext(nodeInstance);
077: }
078: output += outputNodeInstanceToLog(nodeInstance, nodeType,
079: indentDepth);
080: for (Iterator iterator = nodeInstance.getNextNodeInstances()
081: .iterator(); iterator.hasNext();) {
082: RouteNodeInstance nextNodeInstance = (RouteNodeInstance) iterator
083: .next();
084: nodeType = NodeType.fromNodeInstance(nextNodeInstance);
085: if (newSplitJoinContext != null) {
086: output += orchestrateOutput(nextNodeInstance, nodeType,
087: newSplitJoinContext, indentDepth + 1);
088: } else {
089: output += orchestrateOutput(nextNodeInstance, nodeType,
090: sjCtx, indentDepth + 1);
091: }
092: }
093: if (isSplit) {
094: if (newSplitJoinContext != null
095: && newSplitJoinContext.joinNodeInstance != null) {
096: nodeType = NodeType
097: .fromNodeInstance(newSplitJoinContext.joinNodeInstance);
098: output += orchestrateOutput(
099: newSplitJoinContext.joinNodeInstance, nodeType,
100: sjCtx, indentDepth);
101: }
102: }
103: return output;
104: }
105:
106: private static String outputNodeInstanceToLog(
107: RouteNodeInstance nodeInstance, NodeType nodeType,
108: int indentDepth) throws Exception {
109: String memAddress = nodeInstance.toString().split("@")[1];
110: String dataIndent = getIndent(indentDepth + 1);
111: String output = getIndent(indentDepth) + "[NODE type="
112: + nodeType.getName() + "]\n" + dataIndent + "Name: "
113: + nodeInstance.getName() + "(" + memAddress + ")\n";
114: output += dataIndent + "State: ";
115: for (Iterator iterator = nodeInstance.getState().iterator(); iterator
116: .hasNext();) {
117: NodeState nodeState = (NodeState) iterator.next();
118: output += nodeState.getKey() + "=" + nodeState.getValue();
119: if (iterator.hasNext()) {
120: output += ",";
121: }
122: }
123: output += "\n" + dataIndent + "Status Flags: initial="
124: + nodeInstance.isInitial() + ", active="
125: + nodeInstance.isActive() + ", complete="
126: + nodeInstance.isComplete();
127: output += (nodeInstance.getProcess() == null ? "" : "\n"
128: + dataIndent + "Process Name: "
129: + nodeInstance.getProcess().getName());
130: output += "\n";
131: return output;
132: }
133:
134: private static String getIndent(int indentDepth) {
135: StringBuffer buffer = new StringBuffer();
136: for (int depth = 0; depth < indentDepth; depth++) {
137: buffer.append(INDENT);
138: }
139: return buffer.toString();
140: }
141:
142: private static class SplitJoinContext {
143: public RouteNodeInstance splitNodeInstance;
144: public RouteNodeInstance joinNodeInstance;
145:
146: public SplitJoinContext(RouteNodeInstance splitNodeInstance) {
147: this.splitNodeInstance = splitNodeInstance;
148: }
149: }
150:
151: }
|