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.parser;
017:
018: import org.griphyn.cPlanner.classes.PoolConfig;
019: import org.griphyn.cPlanner.classes.GlobusVersion;
020: import org.griphyn.cPlanner.classes.GridFTPServer;
021: import org.griphyn.cPlanner.classes.GridFTPBandwidth;
022: import org.griphyn.cPlanner.classes.SiteInfo;
023: import org.griphyn.cPlanner.classes.JobManager;
024: import org.griphyn.cPlanner.classes.LRC;
025: import org.griphyn.cPlanner.classes.Profile;
026: import org.griphyn.cPlanner.classes.WorkDir;
027:
028: import org.griphyn.cPlanner.common.LogManager;
029: import org.griphyn.cPlanner.common.PegasusProperties;
030:
031: import java.io.File;
032: import java.io.IOException;
033:
034: import org.xml.sax.Attributes;
035: import org.xml.sax.SAXException;
036: import org.griphyn.cPlanner.namespace.Namespace;
037:
038: /**
039: * This is the parsing class, used to parse the pool config file in xml format.
040: *
041: *
042: * @author Gaurang Mehta gmehta@isi.edu
043: * @author Karan Vahi vahi@isi.edu
044: * @version $Revision: 130 $
045: */
046: public class ConfigXmlParser extends Parser {
047:
048: /**
049: * The "not-so-official" location URL of the VDLx schema definition.
050: */
051: public static final String SCHEMA_LOCATION = "http://pegasus.isi.edu/schema/sc-2.0.xsd";
052:
053: /**
054: * uri namespace
055: */
056: public static final String SCHEMA_NAMESPACE = "http://pegasus.isi.edu/schema/sitecatalog";
057:
058: public PoolConfig m_pconfig = null;
059:
060: private SiteInfo m_pool_info = null;
061:
062: private String m_namespace = null;
063:
064: private String m_key = null;
065:
066: private GridFTPServer gftp = null;
067:
068: /**
069: * Default Class Constructor.
070: *
071: * @param properties the <code>PegasusProperties</code> to be used.
072: */
073: public ConfigXmlParser(PegasusProperties properties) {
074: super (properties);
075: }
076:
077: /**
078: * Class Constructor intializes the parser and turns on validation.
079: *
080: * @param configFileName The file which you want to parse
081: * @param properties the <code>PegasusProperties</code> to be used.
082: */
083: public ConfigXmlParser(String configFileName,
084: PegasusProperties properties) {
085: super (properties);
086:
087: mLogger.log("Parsing the site catalog "/* +
088: configFileName*/,
089: LogManager.INFO_MESSAGE_LEVEL);
090:
091: //setting the schema Locations
092: String schemaLoc = getSchemaLocation();
093: mLogger.log("Picking schema for site catalog" + schemaLoc,
094: LogManager.CONFIG_MESSAGE_LEVEL);
095: String list = ConfigXmlParser.SCHEMA_NAMESPACE + " "
096: + schemaLoc;
097: setSchemaLocations(list);
098: startParser(configFileName);
099: mLogger.logCompletion("Parsing the site catalog",
100: LogManager.INFO_MESSAGE_LEVEL);
101:
102: }
103:
104: public void startParser(String configxml) {
105: try {
106: this .testForFile(configxml);
107: mParser.parse(configxml);
108: } catch (IOException ioe) {
109: mLogger.log("IO Error :" + ioe.getMessage(),
110: LogManager.ERROR_MESSAGE_LEVEL);
111: } catch (SAXException se) {
112:
113: if (mLocator != null) {
114: mLogger.log("Error in " + mLocator.getSystemId()
115: + " at line " + mLocator.getLineNumber()
116: + "at column " + mLocator.getColumnNumber()
117: + " :" + se.getMessage(),
118: LogManager.ERROR_MESSAGE_LEVEL);
119: }
120: }
121: }
122:
123: public void endDocument() {
124:
125: }
126:
127: public void endElement(String uri, String localName, String qName) {
128: if (localName.trim().equalsIgnoreCase("sitecatalog")) {
129: handleConfigTagEnd();
130: } else if (localName.trim().equalsIgnoreCase("site")) {
131: handlePoolTagEnd();
132: } else if (localName.trim().equalsIgnoreCase("lrc")) {
133: handleLRCTagEnd();
134: } else if (localName.trim().equalsIgnoreCase("jobmanager")) {
135: handleJobManagerTagEnd();
136: } else if (localName.trim().equalsIgnoreCase("profile")) {
137: handleProfileTagEnd(m_pool_info);
138: } else if (localName.trim().equalsIgnoreCase("gridftp")) {
139: handleGridFtpTagEnd();
140: } else if (localName.trim().equalsIgnoreCase("workdirectory")) {
141: handleWorkDirectoryTagEnd(m_pool_info);
142: }
143:
144: else if (localName.trim().equalsIgnoreCase("bandwidth")) {
145: handleGridFtpBandwidthTagEnd();
146: } else {
147: mLogger.log("Unkown element end reached :" + uri + ":"
148: + localName + ":" + qName + "-******"
149: + mTextContent + "***********",
150: LogManager.ERROR_MESSAGE_LEVEL);
151: mTextContent.setLength(0);
152: }
153:
154: }
155:
156: public void startElement(String uri, String localName,
157: String qName, Attributes attrs) {
158: try {
159: if (localName.trim().equalsIgnoreCase("sitecatalog")) {
160: handleConfigTagStart();
161: } else if (localName.trim().equalsIgnoreCase("site")) {
162: m_pool_info = handlePoolTagStart(m_pconfig, attrs);
163: } else if (localName.trim().equalsIgnoreCase("lrc")) {
164: handleLRCTagStart(m_pool_info, attrs);
165: } else if (localName.trim().equalsIgnoreCase("jobmanager")) {
166: handleJobManagerTagStart(m_pool_info, attrs);
167: } else if (localName.trim().equalsIgnoreCase("profile")) {
168: handleProfileTagStart(m_pool_info, attrs);
169: } else if (localName.trim().equalsIgnoreCase("gridftp")) {
170: handleGridFtpTagStart(m_pool_info, attrs);
171: } else if (localName.trim().equalsIgnoreCase(
172: "workdirectory")) {
173: handleWorkDirectoryTagStart(m_pool_info, attrs);
174: } else if (localName.trim().equalsIgnoreCase("bandwidth")) {
175: handleGridFtpBandwidthTagStart(m_pool_info, attrs);
176: }
177:
178: else {
179: mLogger.log("Unknown element in xml :" + uri + ":"
180: + localName + ":" + qName,
181: LogManager.ERROR_MESSAGE_LEVEL);
182: }
183: } catch (Exception e) {
184: e.printStackTrace();
185: }
186:
187: }
188:
189: public String getSchemaLocation() {
190: // treat URI as File, yes, I know - I need the basename
191: File uri = new File(ConfigXmlParser.SCHEMA_LOCATION);
192: // create a pointer to the default local position
193: File poolconfig = new File(this .mProps.getSysConfDir(), uri
194: .getName());
195:
196: return this .mProps.getPoolSchemaLocation(poolconfig
197: .getAbsolutePath());
198:
199: }
200:
201: /**
202: *
203: * @return PoolConfig Returns a new <code>PoolConfig<code> object when
204: * it encounters start of XML.
205: *
206: * @see org.griphyn.cPlanner.classes.PoolConfig
207: */
208: private PoolConfig handleConfigTagStart() {
209: m_pconfig = new PoolConfig();
210: return m_pconfig;
211: }
212:
213: /**
214: *
215: * @param pcfg Takes the PoolConfig class.
216: * @param attrs Takes the atrributes returned in XML.
217: *
218: * @return SiteInfo returns the reference to the PooInfo ojject
219: *
220: * @throws Exception
221: * @see org.griphyn.cPlanner.classes.SiteInfo
222: * @see org.griphyn.cPlanner.classes.PoolConfig
223: */
224: private SiteInfo handlePoolTagStart(PoolConfig pcfg,
225: Attributes attrs) throws Exception {
226: m_pool_info = new SiteInfo();
227: String handle = new String(attrs.getValue("", "handle"));
228:
229: //set the id of object
230: m_pool_info.setInfo(SiteInfo.HANDLE, handle);
231: if (attrs.getValue("", "gridlaunch") != null) {
232: String gridlaunch = new String(attrs.getValue("",
233: "gridlaunch"));
234: gridlaunch = (gridlaunch == null
235: || gridlaunch.length() == 0 || gridlaunch
236: .equalsIgnoreCase("null")) ? null : gridlaunch;
237: m_pool_info.setInfo(SiteInfo.GRIDLAUNCH, gridlaunch);
238: }
239: if (attrs.getValue("", "sysinfo") != null) {
240: String sysinfo = new String(attrs.getValue("", "sysinfo"));
241: m_pool_info.setInfo(SiteInfo.SYSINFO, sysinfo);
242: }
243: pcfg.add(handle, m_pool_info);
244:
245: return m_pool_info;
246: }
247:
248: /**
249: *
250: * @param pinfo Poolinfo object that is to be populated
251: * @param attrs Attributes for the element
252: * @throws Exception
253: */
254: private void handleProfileTagStart(SiteInfo pinfo, Attributes attrs)
255: throws Exception {
256: m_namespace = new String(attrs.getValue("", "namespace"));
257: m_key = new String(attrs.getValue("", "key"));
258: }
259:
260: /**
261: *
262: * @param pinfo Poolinfo object that is to be populated
263: * @param attrs Attributes for the element
264: * @throws Exception
265: */
266: private static void handleLRCTagStart(SiteInfo pinfo,
267: Attributes attrs) throws Exception {
268: LRC lrc = new LRC(attrs.getValue("", "url"));
269: pinfo.setInfo(SiteInfo.LRC, lrc);
270: }
271:
272: /**
273: * @param pinfo Poolinfo object that is to be populated
274: * @param attrs Attributes for the element
275: * @throws Exception
276: */
277: private void handleGridFtpTagStart(SiteInfo pinfo, Attributes attrs)
278: throws Exception {
279: gftp = new GridFTPServer();
280:
281: String gftp_url = new String(attrs.getValue("", "url"));
282: gftp.setInfo(GridFTPServer.GRIDFTP_URL, gftp_url);
283: GlobusVersion globusver = new GlobusVersion(new Integer(attrs
284: .getValue("", "major")).intValue(), new Integer(attrs
285: .getValue("", "minor")).intValue(), new Integer(attrs
286: .getValue("", "patch")).intValue());
287: gftp.setInfo(GridFTPServer.GLOBUS_VERSION, globusver
288: .getGlobusVersion());
289:
290: if (attrs.getValue("", "storage") != null) {
291: gftp.setInfo(GridFTPServer.STORAGE_DIR, new String(attrs
292: .getValue("", "storage")));
293:
294: }
295: if (attrs.getValue("", "total-size") != null) {
296: gftp.setInfo(GridFTPServer.TOTAL_SIZE, new String(attrs
297: .getValue("", "total-size")));
298: }
299: if (attrs.getValue("", "free-size") != null) {
300: gftp.setInfo(GridFTPServer.FREE_SIZE, new String(attrs
301: .getValue("", "free-size")));
302: }
303:
304: //following line commented by sk setppolinfo is now called in handleGridFtpTagstop()
305: //pinfo.setPoolInfo(GvdsPoolInfo.GRIDFTP, gftp);
306: }
307:
308: /**
309: * sk added function to handle gridftpbandwidth tag
310: * @param pinfo Poolinfo object that is to be populated
311: * @param attrs Attributes for the element
312: * @throws Exception
313: */
314: private void handleGridFtpBandwidthTagStart(SiteInfo pinfo,
315: Attributes attrs) throws Exception {
316: GridFTPBandwidth gridftp_bandwidth = new GridFTPBandwidth();
317: String dest_id = new String(attrs.getValue("", "dest-subnet"));
318: gridftp_bandwidth.setInfo(GridFTPBandwidth.DEST_ID, dest_id);
319:
320: String avg_bw_range1 = new String(attrs.getValue("",
321: "avg-bandwidth-range1"));
322: if (avg_bw_range1.length() != 0) {
323: gridftp_bandwidth.setInfo(GridFTPBandwidth.AVG_BW_RANGE1,
324: avg_bw_range1);
325: }
326:
327: String avg_bw_range2 = attrs.getValue("",
328: "avg-bandwidth-range2");
329: if (avg_bw_range2 != null) {
330: gridftp_bandwidth.setInfo(GridFTPBandwidth.AVG_BW_RANGE1,
331: avg_bw_range2);
332: }
333:
334: String avg_bw_range3 = attrs.getValue("",
335: "avg-bandwidth-range3");
336: if (avg_bw_range3 != null) {
337: gridftp_bandwidth.setInfo(GridFTPBandwidth.AVG_BW_RANGE1,
338: avg_bw_range3);
339: }
340:
341: String avg_bw_range4 = attrs.getValue("",
342: "avg-bandwidth-range4");
343: if (avg_bw_range4 != null) {
344: gridftp_bandwidth.setInfo(GridFTPBandwidth.AVG_BW_RANGE1,
345: avg_bw_range4);
346: }
347:
348: gridftp_bandwidth.setInfo(GridFTPBandwidth.AVG_BW, new String(
349: attrs.getValue("", "avg-bandwidth")));
350: gridftp_bandwidth.setInfo(GridFTPBandwidth.MAX_BW, new String(
351: attrs.getValue("", "max-bandwidth")));
352: gridftp_bandwidth.setInfo(GridFTPBandwidth.MIN_BW, new String(
353: attrs.getValue("", "min-bandwidth")));
354:
355: gftp.setGridFTPBandwidthInfo(gridftp_bandwidth);
356:
357: }
358:
359: /**
360: * This method handles the start of a jobmanager tag.
361: *
362: * @param pinfo The <code>PoolInfo</code> object which will hold the jobmanager information
363: * @param attrs The attributes about the jobmanager tag returned from the XML.
364: *
365: * @throws Exception
366: * @see org.griphyn.cPlanner.classes.SiteInfo
367: */
368: private static void handleJobManagerTagStart(SiteInfo pinfo,
369: Attributes attrs) throws Exception {
370: JobManager jbinfo = new JobManager();
371:
372: jbinfo.setInfo(JobManager.UNIVERSE, new String(attrs.getValue(
373: "", "universe")));
374: jbinfo.setInfo(JobManager.URL, new String(attrs.getValue("",
375: "url")));
376: GlobusVersion globusver = new GlobusVersion(new Integer(attrs
377: .getValue("", "major")).intValue(), new Integer(attrs
378: .getValue("", "minor")).intValue(), new Integer(attrs
379: .getValue("", "patch")).intValue());
380: jbinfo.setInfo(JobManager.GLOBUS_VERSION, globusver
381: .getGlobusVersion());
382: if (attrs.getValue("", "free-mem") != null) {
383: jbinfo.setInfo(JobManager.FREE_MEM, new String(attrs
384: .getValue("", "free-mem")));
385:
386: }
387: if (attrs.getValue("", "total-mem") != null) {
388: jbinfo.setInfo(JobManager.TOTAL_MEM, new String(attrs
389: .getValue("", "total-mem")));
390:
391: }
392: if (attrs.getValue("", "max-count") != null) {
393: jbinfo.setInfo(JobManager.MAX_COUNT, new String(attrs
394: .getValue("", "max-count")));
395:
396: }
397: if (attrs.getValue("", "max-cpu-time") != null) {
398: jbinfo.setInfo(JobManager.MAX_CPU_TIME, new String(attrs
399: .getValue("", "max-cpu-time")));
400:
401: }
402: if (attrs.getValue("", "running-jobs") != null) {
403: jbinfo.setInfo(JobManager.RUNNING_JOBS, new String(attrs
404: .getValue("", "running-jobs")));
405:
406: }
407: if (attrs.getValue("", "jobs-in-queue") != null) {
408: jbinfo.setInfo(JobManager.JOBS_IN_QUEUE, new String(attrs
409: .getValue("", "jobs-in-queue")));
410:
411: }
412: if (attrs.getValue("", "max-cpu-time") != null) {
413: jbinfo.setInfo(JobManager.MAX_CPU_TIME, new String(attrs
414: .getValue("", "max-cpu-time")));
415: }
416: if (attrs.getValue("", "idle-nodes") != null) {
417: jbinfo.setInfo(JobManager.IDLE_NODES, new String(attrs
418: .getValue("", "idle-nodes")));
419:
420: }
421: if (attrs.getValue("", "total-nodes") != null) {
422: jbinfo.setInfo(JobManager.TOTAL_NODES, new String(attrs
423: .getValue("", "total-nodes")));
424: }
425: if (attrs.getValue("", "os") != null) {
426: jbinfo.setInfo(JobManager.OS_TYPE, new String(attrs
427: .getValue("", "os")));
428: }
429: if (attrs.getValue("", "arch") != null) {
430: jbinfo.setInfo(JobManager.ARCH_TYPE, new String(attrs
431: .getValue("", "arch")));
432: }
433: if (attrs.getValue("", "type") != null) {
434: jbinfo.setInfo(JobManager.JOBMANAGER_TYPE, new String(attrs
435: .getValue("", "type")));
436: }
437: pinfo.setInfo(SiteInfo.JOBMANAGER, jbinfo);
438: }
439:
440: /**
441: * Handles the WorkDirectory Tag Start.
442: * @param pinfo Takes a SiteInfo object for which the work directory is.
443: * @param attrs Takes the attributes returned from the XML by the parser.
444: * @throws Exception
445: * @see org.griphyn.cPlanner.classes.SiteInfo
446: */
447: private static void handleWorkDirectoryTagStart(SiteInfo pinfo,
448: Attributes attrs) throws Exception {
449: WorkDir gwd = new WorkDir();
450: if (attrs.getValue("", "total-size") != null) {
451: gwd.setInfo(WorkDir.TOTAL_SIZE, new String(attrs.getValue(
452: "", "total-size")));
453: }
454: if (attrs.getValue("", "free-size") != null) {
455: gwd.setInfo(WorkDir.FREE_SIZE, new String(attrs.getValue(
456: "", "free-size")));
457: }
458: //pinfo.setInfo( WorkDir.WORKDIR, gwd );
459: pinfo.setInfo(SiteInfo.WORKDIR, gwd);
460: }
461:
462: /**
463: * Handles the end of the Xml files.
464: *
465: */
466: private static void handleConfigTagEnd() {
467: // System.out.println(m_pconfig.toXml());
468: }
469:
470: /**
471: * Handles the end of the pool tag.
472: */
473: private static void handlePoolTagEnd() {
474:
475: }
476:
477: /**
478: * Handles the end of the Profile tag.
479: * @param pinfo <code>PoolInfo</code> object for which the
480: * profiles are collected.
481: *
482: * @throws java.lang.Exception
483: * @see org.griphyn.cPlanner.classes.SiteInfo
484: */
485: private void handleProfileTagEnd(SiteInfo pinfo)
486: throws RuntimeException {
487: if (mTextContent != null && m_namespace != null
488: && m_key != null) {
489:
490: //check if namespace is valid
491: m_namespace = m_namespace.toLowerCase();
492: if (!Namespace.isNamespaceValid(m_namespace)) {
493: mTextContent.setLength(0);
494: mLogger.log(
495: "Namespace specified in Site Catalog not supported. ignoring "
496: + m_namespace,
497: LogManager.WARNING_MESSAGE_LEVEL);
498: return;
499: }
500:
501: Profile profile = new Profile(m_namespace, m_key,
502: mTextContent.toString().trim());
503: pinfo.setInfo(SiteInfo.PROFILE, profile);
504: mTextContent.setLength(0);
505: }
506: }
507:
508: /**
509: * Handles the end of the LRC Tag
510: */
511: private static void handleLRCTagEnd() {
512: }
513:
514: /**
515: * sk made changes to the following function to set GRIDFTPServer instead of
516: * setting it in fn handleGridFtpTagStart()
517: * @throws java.lang.RuntimeException
518: */
519: private void handleGridFtpTagEnd() throws RuntimeException {
520: m_pool_info.setInfo(SiteInfo.GRIDFTP, gftp);
521: }
522:
523: private static void handleGridFtpBandwidthTagEnd() {
524: }
525:
526: /**
527: * Handles the end of the JobManager Tag
528: */
529: private static void handleJobManagerTagEnd() {
530: }
531:
532: /**
533: * This method handles the Workdirectory tg end.
534: * @param pinfo Takes the PoolInfo object.
535: * @throws java.lang.Exception
536: * @see org.griphyn.cPlanner.classes.SiteInfo
537: */
538: private void handleWorkDirectoryTagEnd(SiteInfo pinfo)
539: throws RuntimeException {
540: if (mTextContent != null) {
541: WorkDir gdw = (WorkDir) pinfo.getInfo(SiteInfo.WORKDIR);
542: gdw
543: .setInfo(WorkDir.WORKDIR, mTextContent.toString()
544: .trim());
545: }
546: mTextContent.setLength(0);
547: }
548:
549: /**
550: * This class returns the reference to the <code>PooConfig</code> object
551: * containing information about all the pools.
552: *
553: * @return returns a reference to the <code>PoolConfig</code> object which
554: * contains all the pools.
555: *
556: * @see org.griphyn.cPlanner.classes.PoolConfig
557: */
558: public PoolConfig getPoolConfig() {
559: return m_pconfig;
560: }
561:
562: }
|