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.partitioner;
017:
018: import org.griphyn.cPlanner.common.PegasusProperties;
019: import org.griphyn.cPlanner.common.LogManager;
020:
021: import java.util.List;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import java.io.File;
027: import java.io.IOException;
028:
029: /**
030: * This callback writes out a <code>DAX</code> file for each of the partitions,
031: * and also writes out a <code>PDAX</code> file that captures the relations
032: * between the partitions.
033: *
034: * @author not attributable
035: * @version $Revision: 464 $
036: */
037:
038: public class WriterCallback implements Callback {
039:
040: /**
041: * The handle to the partition graph writer.
042: */
043: protected PDAXWriter mPDAXWriter;
044:
045: /**
046: * The handle to the dax writer that writes out the dax corresponding to the
047: * partition identified. The base name of the partition is gotten from it.
048: */
049: protected DAXWriter mDAXWriter;
050:
051: /**
052: * The path to the PDAX file written out.
053: */
054: protected String mPDAX;
055:
056: /**
057: * Handle to the properties available.
058: */
059: protected PegasusProperties mProps;
060:
061: /**
062: * The handle to the logger object.
063: */
064: protected LogManager mLogger;
065:
066: /**
067: * A boolean indicating that the partitioning has started. This is set,
068: * by the first call to the cbPartition( Partition ) callback.
069: */
070: protected boolean mPartitioningStarted;
071:
072: /**
073: * The default constructor.
074: *
075: */
076: public WriterCallback() {
077: mLogger = LogManager.getInstance();
078: }
079:
080: /**
081: * Initializes the Writer Callback.
082: *
083: * @param properties the properties passed to the planner.
084: * @param daxFile the path to the DAX file that is being partitioned.
085: * @param daxName the namelabel of the DAX as set in the root element of the DAX.
086: * @param directory the directory where the partitioned daxes have to reside.
087: */
088: public void initialize(PegasusProperties properties,
089: String daxFile, String daxName, String directory) {
090:
091: mProps = properties;
092:
093: //load the writer for the partitioned daxes
094: mDAXWriter = DAXWriter.loadInstance(properties, daxFile,
095: directory);
096: mDAXWriter.setPartitionName(daxName);
097:
098: //name of pdax file is same as the dax file
099: //meaning the name attribute in root element are same.
100: mPDAXWriter = getHandletoPDAXWriter(daxFile, daxName, directory);
101:
102: //write out the XML header for the PDAX file
103: mPDAXWriter.writeHeader();
104: }
105:
106: /**
107: * Callback for when a partitioner determines that partition has been
108: * constructed. A DAX file is written out for the partition.
109: *
110: * @param p the constructed partition.
111: *
112: * @throws RuntimeException in case of any error while writing out the DAX or
113: * the PDAX files.
114: */
115: public void cbPartition(Partition p) {
116: mPartitioningStarted = true;
117:
118: //not sure if we still need it
119: p.setName(mDAXWriter.getPartitionName());
120:
121: //for time being do a localize catch
122: //till i change the interface
123: try {
124: //write out the partition information to the PDAX file
125: mLogger.log("Writing to the pdax file for partition "
126: + p.getID(), LogManager.DEBUG_MESSAGE_LEVEL);
127: mPDAXWriter.write(p);
128: mLogger
129: .logCompletion(
130: "Writing to the pdax file for partition "
131: + p.getID(),
132: LogManager.DEBUG_MESSAGE_LEVEL);
133: //write out the DAX file
134: mDAXWriter.writePartitionDax(p);
135:
136: } catch (IOException ioe) {
137: //wrap and throw in Runtime Exception
138: throw new RuntimeException("Writer Callback for partition "
139: + p.getID(), ioe);
140: }
141:
142: }
143:
144: /**
145: * Callback for when a partitioner determines the relations between partitions
146: * that it has previously constructed.
147: *
148: * @param child the id of a partition.
149: * @param parents the list of <code>String</code> objects that contain
150: * the id's of the parents of the partition.
151: *
152: *
153: * @throws RuntimeException in case of any error while writing out the DAX or
154: * the PDAX files.
155: */
156: public void cbParents(String child, List parents) {
157: mPDAXWriter.write(partitionRelation2XML(child, parents));
158: }
159:
160: /**
161: * Callback for the partitioner to signal that it is done with the processing.
162: * This internally closes all the handles to the DAX and PDAX writers.
163: *
164: */
165: public void cbDone() {
166: //change internal state to signal
167: //that we are done with partitioning.
168: mPartitioningStarted = false;
169:
170: mPDAXWriter.close();
171: mDAXWriter.close();
172: }
173:
174: /**
175: * Returns the name of the pdax file written out.
176: * Will be null if the partitioning has not completed.
177: *
178: * @return path to the pdax file.
179: */
180: public String getPDAX() {
181: return this .mPDAX;
182: }
183:
184: /**
185: * Returns the name of the partition, that needs to be set while creating
186: * the Partition object corresponding to each partition.
187: *
188: * @return the name of the partition.
189: */
190: protected String getPartitionName() {
191: return mDAXWriter.getPartitionName();
192: }
193:
194: /**
195: * It returns the handle to the writer for writing out the pdax file
196: * that contains the relations amongst the partitions and the jobs making
197: * up the partitions.
198: *
199: * @param daxFile the path to the DAX file that is being partitioned.
200: * @param name the name/label that is to be assigned to the pdax file.
201: * @param directory the directory where the partitioned daxes have to reside.
202: *
203: * @return handle to the writer of pdax file.
204: */
205: protected PDAXWriter getHandletoPDAXWriter(String daxFile,
206: String name, String directory) {
207: String pdaxPath;
208: //get the name of dax file sans the path
209: String daxName = new java.io.File(daxFile).getName();
210: //construct the basename of the pdax file
211: pdaxPath = (daxName == null) ? "partition" : ((daxName
212: .indexOf('.') > 0) ? daxName.substring(0, daxName
213: .indexOf('.')) : daxName);
214: //now the complete path
215: pdaxPath = directory + File.separator + pdaxPath + ".pdax";
216: //System.out.println("Name is " + nameOfPDAX);
217: mPDAX = pdaxPath;
218:
219: return new PDAXWriter(name, pdaxPath);
220:
221: }
222:
223: /**
224: * Returns the xml description of a relation between 2 partitions.
225: *
226: * @param childID the ID of the child.
227: * @param parentID the ID of the parent.
228: *
229: * @return the XML description of child parent relation.
230: */
231: protected String partitionRelation2XML(String childID,
232: String parentID) {
233: StringBuffer sb = new StringBuffer();
234: sb.append("\n\t<child ref=\"").append(childID).append("\">");
235: sb.append("\n\t\t<parent ref=\"").append(parentID).append(
236: "\"/>");
237: sb.append("\n\t</child>");
238: return sb.toString();
239: }
240:
241: /**
242: * Returns the xml description of a relation between 2 partitions.
243: *
244: * @param childID the ID of the child
245: * @param parentIDs <code>List</code> of parent IDs.
246: *
247: * @return the XML description of child parent relations.
248: */
249: protected String partitionRelation2XML(String childID,
250: List parentIDs) {
251: StringBuffer sb = new StringBuffer();
252: sb.append("\n\t<child ref=\"").append(childID).append("\">");
253: for (Iterator it = parentIDs.iterator(); it.hasNext();) {
254: sb.append("\n\t\t<parent ref=\"").append(it.next()).append(
255: "\"/>");
256: }
257: sb.append("\n\t</child>");
258: return sb.toString();
259: }
260:
261: /**
262: * Returns the xml description of a relation between 2 partitions.
263: *
264: * @param childID the ID of the child
265: * @param parentIDs <code>Set</code> of parent IDs.
266: *
267: * @return the XML description of child parent relations.
268: */
269: protected String partitionRelation2XML(String childID, Set parentIDs) {
270: StringBuffer sb = new StringBuffer();
271: sb.append("\n\t<child ref=\"").append(childID).append("\">");
272: for (Iterator it = parentIDs.iterator(); it.hasNext();) {
273: sb.append("\n\t\t<parent ref=\"").append(it.next()).append(
274: "\"/>");
275: }
276: sb.append("\n\t</child>");
277: return sb.toString();
278: }
279:
280: }
|