001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015:
016: package org.griphyn.cPlanner.visualize;
017:
018: import org.griphyn.cPlanner.common.LogManager;
019:
020: import org.griphyn.vdl.util.ChimeraProperties;
021:
022: import org.griphyn.vdl.directive.ParseKickstart;
023:
024: import org.griphyn.vdl.invocation.InvocationRecord;
025: import org.griphyn.vdl.invocation.StatCall;
026: import org.griphyn.vdl.invocation.Data;
027: import org.griphyn.vdl.invocation.Regular;
028:
029: import org.griphyn.vdl.parser.InvocationParser;
030:
031: import org.griphyn.common.util.Currently;
032:
033: import java.util.List;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036:
037: import java.io.File;
038: import java.io.IOException;
039: import java.io.StringReader;
040: import java.io.StringWriter;
041:
042: /**
043: * A helper class that parses the kickstart records and has calls to callbacks
044: * for working on data sections of standard out, standard error, and standard
045: * input.
046: *
047: * @author Karan Vahi vahi@isi.edu
048: * @version $Revision: 50 $
049: */
050:
051: public class KickstartParser {
052:
053: /**
054: * The parser class that is used to parse a kickstart record and return
055: * the invocation record.
056: */
057: private ParseKickstart mParseKickstart;
058:
059: /**
060: * The handle to the logging object.
061: */
062: private LogManager mLogger;
063:
064: /**
065: * The callback object.
066: */
067: private Callback mCallback;
068:
069: /**
070: * Semi-singleton, dynamically instantiated once for the lifetime.
071: * The properties determine which Xerces parser is being used.
072: */
073: private InvocationParser mInvocationParser;
074:
075: /**
076: * The default constructor.
077: */
078: public KickstartParser() {
079: mLogger = LogManager.getInstance();
080: }
081:
082: /**
083: * Sets the callback to which to callout to while parsing a kickstart
084: * record.
085: *
086: * @param c the Callback to call out to.
087: */
088: public void setCallback(Callback c) {
089: mCallback = c;
090: mLogger = LogManager.getInstance();
091: }
092:
093: /**
094: *
095: */
096: public List parseKickstartFile(String file) throws IOException {
097: List result = new ArrayList();
098:
099: //sanity check
100: if (mCallback == null) {
101: throw new RuntimeException("Callback not initialized");
102: }
103:
104: //initialize the parser if required
105: if (mParseKickstart == null) {
106: mParseKickstart = new ParseKickstart();
107: }
108:
109: // get access to the invocation parser
110: if (mInvocationParser == null) {
111: ChimeraProperties props = ChimeraProperties.instance();
112: String psl = props.getPTCSchemaLocation();
113: mLogger.log("Using XML schema location " + psl,
114: LogManager.DEBUG_MESSAGE_LEVEL);
115: mInvocationParser = new InvocationParser(psl);
116: }
117:
118: //do some sanity checks for the file.
119:
120: //extract to memory
121: File f = new java.io.File(file);
122: List extract = mParseKickstart.extractToMemory(f);
123:
124: org.griphyn.vdl.invocation.File invocationFile = null;
125:
126: // testme: for each record obtained, work on it
127: for (int j = 1; j - 1 < extract.size(); ++j) {
128: String temp = (String) extract.get(j - 1);
129:
130: // test 5: try to parse XML
131: InvocationRecord invocation = mInvocationParser
132: .parse(new StringReader(temp));
133: mCallback.cbInvocationStart(getJobName(f.getName()),
134: invocation.getResource());
135:
136: //get the data about the various jobs
137: List jobs = invocation.getJobList();
138:
139: //callback for the data sections of various streams
140: List stats = invocation.getStatList();
141: StringWriter writer = new StringWriter();
142: for (Iterator it = stats.iterator(); it.hasNext();) {
143: StatCall statC = (StatCall) it.next();
144: String handle = statC.getHandle();
145: invocationFile = statC.getFile();
146:
147: //call out appropriate callback functions with the data
148: char c = handle.charAt(0);
149: Data data = statC.getData();
150: String value = (data == null) ? "" : data.getValue();
151: switch (c) {
152: case 's': //stdout, //stderr,//stdin
153: if (handle.equals("stdout")) {
154: mCallback.cbStdOut(jobs, value);
155: } else if (handle.equals("stdin")) {
156: mCallback.cbStdIN(jobs, value);
157: } else if (handle.equals("stderr")) {
158: mCallback.cbStdERR(jobs, value);
159: }
160: break;
161:
162: case 'i'://initial
163: if (handle.equals("initial")) {
164: if (invocationFile instanceof Regular) {
165: //we are interested in Regular files only
166: mCallback.cbInputFile(
167: ((Regular) invocationFile)
168: .getFilename(), statC
169: .getStatInfo());
170: }
171: }
172: break;
173:
174: case 'f'://final
175: if (handle.equals("final")) {
176: if (invocationFile instanceof Regular) {
177: //we are interested in Regular files only
178: mCallback.cbOutputFile(
179: ((Regular) invocationFile)
180: .getFilename(), statC
181: .getStatInfo());
182: }
183:
184: }
185:
186: default:
187: break;
188: }
189: }
190:
191: //successfully done with an invocation record
192: mCallback.cbInvocationEnd();
193: }
194: return result;
195: }
196:
197: /**
198: * Returns the name of the job from the kickstart output filename.
199: *
200: * @param outName the name of the out file.
201: *
202: * @return the job name.
203: */
204: protected String getJobName(String outName) {
205: return outName.substring(0, outName.indexOf('.'));
206: }
207:
208: }
|