001: /**
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found at $PEGASUS_HOME/GTPL or
004: * http://www.globus.org/toolkit/download/license.html.
005: * This notice must appear in redistributions of this file
006: * with or without modification.
007: *
008: * Redistributions of this Software, with or without modification, must reproduce
009: * the GTPL in:
010: * (1) the Software, or
011: * (2) the Documentation or
012: * some other similar material which is provided with the Software (if any).
013: *
014: * Copyright 1999-2004
015: * University of Chicago and The University of Southern California.
016: * All rights reserved.
017: */package org.griphyn.cPlanner.classes;
018:
019: import org.griphyn.cPlanner.common.LogManager;
020:
021: import java.io.IOException;
022: import java.io.Reader;
023: import java.util.StringTokenizer;
024:
025: /**
026: * Parses the input stream and generates site configuration map as
027: * output.
028: *
029: * @author Jens Vöckler
030: * @author Gaurang Mehta
031: * @author Karan Vahi
032: * @version $Revision: 178 $
033: *
034: * @see org.griphyn.cPlanner.classes.PoolConfigScanner
035: * @see org.griphyn.cPlanner.classes.PoolConfigToken
036: */
037: public class PoolConfigParser2 {
038:
039: /**
040: * The access to the lexical scanner is stored here.
041: */
042: private PoolConfigScanner m_scanner = null;
043:
044: /**
045: * Stores the look-ahead symbol.
046: */
047: private PoolConfigToken m_lookAhead = null;
048:
049: /**
050: * The handle to the logger used to log messages.
051: */
052: private LogManager m_logger;
053:
054: /**
055: * Initializes the parser with an input stream to read from.
056: * @param r is the stream opened for reading.
057: *
058: * @throws IOException
059: * @throws PoolConfigException
060: */
061: public PoolConfigParser2(Reader r) throws IOException,
062: PoolConfigException {
063: m_logger = LogManager.getInstance();
064: m_scanner = new PoolConfigScanner(r);
065: m_lookAhead = m_scanner.nextToken();
066: }
067:
068: /**
069: * Parses the complete input stream, into the PoolConfig data object that
070: * holds the contents of all the sites referred to in the stream.
071: *
072: * @return a map indexed by the site handle strings.
073: * @throws IOException
074: * @throws PoolConfigException
075: * @throws Exception
076: * @see org.griphyn.cPlanner.classes.PoolConfig
077: */
078: public PoolConfig parse() throws IOException, PoolConfigException,
079: Exception {
080: //to check more
081: PoolConfig sites = new PoolConfig();
082: String handle = null;
083: do {
084: if (m_lookAhead != null) {
085: //get the site handle/id, that is parsed differently
086: //compared to the rest of the attributes of the site.
087: handle = getSiteHandle();
088:
089: SiteInfo site = new SiteInfo();
090: site.setInfo(SiteInfo.HANDLE, handle);
091: while (!(m_lookAhead instanceof PoolConfigCloseBrace)) {
092: //populate all the rest of the attributes
093: //associated with the site
094: populate(site);
095: }
096:
097: if (!(m_lookAhead instanceof PoolConfigCloseBrace)) {
098: throw new PoolConfigException(m_scanner
099: .getLineNumber(),
100: "expecting a closing brace");
101: }
102: //we have information about one complete site!
103: m_logger.log("Site parsed is - " + site.toMultiLine(),
104: LogManager.DEBUG_MESSAGE_LEVEL);
105:
106: m_lookAhead = m_scanner.nextToken();
107:
108: // enter the site information.
109: if (sites.contains(handle)) {
110: //Karan October 13, 2005
111: //NEEDS CLARIFICATION FROM GAURANG
112: //PROBABLY IS A MDS ARTIFACT. ALSO NEEDS
113: //TO BE MOVED TO PoolConfig.add(PoolConfig,boolean)
114: java.util.Date date = new java.util.Date();
115: sites.add(handle + "-" + date.getTime(), site);
116: } else {
117: sites.add(handle, site);
118: }
119:
120: }
121: } while (m_scanner.hasMoreTokens());
122:
123: return sites;
124: }
125:
126: /**
127: * Remove potential leading and trainling quotes from a string.
128: *
129: * @param input is a string which may have leading and trailing quotes
130: * @return a string that is either identical to the input, or a
131: * substring thereof.
132: */
133: public String niceString(String input) {
134: // sanity
135: if (input == null) {
136: return input;
137: }
138: int l = input.length();
139: if (l < 2) {
140: return input;
141: }
142:
143: // check for leading/trailing quotes
144: if (input.charAt(0) == '"' && input.charAt(l - 1) == '"') {
145: return input.substring(1, l - 1);
146: } else {
147: return input;
148: }
149: }
150:
151: /**
152: * Populates all the attributes except the handle, associated with the site
153: * in the <code>SiteInfo</code> object.
154: *
155: * @param site the <code>SiteInfo<code> object that is to be populated.
156: * @throws even more mystery
157: */
158: private void populate(SiteInfo site) throws IOException,
159: PoolConfigException, Exception {
160:
161: if (!(m_lookAhead instanceof PoolConfigReservedWord)) {
162: throw new PoolConfigException(m_scanner.getLineNumber(),
163: "expecting a reserved word describing a site attribute instead of "
164: + m_lookAhead);
165: }
166: int word = ((PoolConfigReservedWord) m_lookAhead).getValue();
167: m_lookAhead = m_scanner.nextToken();
168:
169: switch (word) {
170: case PoolConfigReservedWord.UNIVERSE:
171: if (!(m_lookAhead instanceof PoolConfigIdentifier)) {
172: throw new PoolConfigException(
173: m_scanner.getLineNumber(),
174: "the \"universe\" requires an identifier as first argument");
175: }
176: JobManager jbminfo = new JobManager();
177: String universe = ((PoolConfigIdentifier) m_lookAhead)
178: .getValue();
179: m_lookAhead = m_scanner.nextToken();
180: jbminfo.setInfo(JobManager.UNIVERSE, universe);
181:
182: // System.out.println("universe="+universe );
183: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
184: throw new PoolConfigException(
185: m_scanner.getLineNumber(),
186: "the \"universe\" requires a quoted string as second argument");
187: }
188:
189: // System.out.println("url="+((PoolConfigQuotedString) m_lookAhead).getValue() );
190: jbminfo.setInfo(JobManager.URL,
191: niceString(((PoolConfigQuotedString) m_lookAhead)
192: .getValue()));
193: m_lookAhead = m_scanner.nextToken();
194:
195: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
196: throw new PoolConfigException(
197: m_scanner.getLineNumber(),
198: "the \"universe\" requires a quoted string for version as third argument");
199: }
200: jbminfo.setInfo(JobManager.GLOBUS_VERSION,
201: niceString(((PoolConfigQuotedString) m_lookAhead)
202: .getValue()));
203: m_lookAhead = m_scanner.nextToken();
204: site.setInfo(SiteInfo.JOBMANAGER, jbminfo);
205: break;
206:
207: case PoolConfigReservedWord.LRC:
208: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
209: throw new PoolConfigException(
210: m_scanner.getLineNumber(),
211: "the \"lrc\" requires a quoted string argument");
212: }
213: LRC lrc = new LRC(
214: niceString(((PoolConfigQuotedString) m_lookAhead)
215: .getValue()));
216: site.setInfo(SiteInfo.LRC, lrc);
217: m_lookAhead = m_scanner.nextToken();
218: break;
219:
220: case PoolConfigReservedWord.GRIDLAUNCH:
221: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
222: throw new PoolConfigException(
223: m_scanner.getLineNumber(),
224: "the \"gridlaunch\" requires a quoted string argument");
225: }
226: site.setInfo(SiteInfo.GRIDLAUNCH,
227: niceString(((PoolConfigQuotedString) m_lookAhead)
228: .getValue()));
229: m_lookAhead = m_scanner.nextToken();
230: break;
231:
232: case PoolConfigReservedWord.WORKDIR:
233: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
234: throw new PoolConfigException(
235: m_scanner.getLineNumber(),
236: "the \"workdir\" requires a quoted string argument");
237: }
238: WorkDir gdw = new WorkDir();
239: gdw.setInfo(WorkDir.WORKDIR,
240: niceString(((PoolConfigQuotedString) m_lookAhead)
241: .getValue()));
242: site.setInfo(SiteInfo.WORKDIR, gdw);
243:
244: //System.out.println("workdir ="+((PoolConfigQuotedString) m_lookAhead).getValue() );
245: m_lookAhead = m_scanner.nextToken();
246: break;
247:
248: case PoolConfigReservedWord.GRIDFTP:
249: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
250: throw new PoolConfigException(
251: m_scanner.getLineNumber(),
252: "the \"gridftp\" requires a quoted string argument for url");
253: }
254: GridFTPServer gftp = new GridFTPServer();
255: String gftp_url = new String(
256: niceString(((PoolConfigQuotedString) m_lookAhead)
257: .getValue()));
258: StringTokenizer stt = new StringTokenizer(gftp_url, "/");
259: String gridftpurl = stt.nextToken() + "//"
260: + stt.nextToken();
261: String storagedir = "";
262: while (stt.hasMoreTokens()) {
263: storagedir += "/" + stt.nextToken();
264: }
265: gftp.setInfo(GridFTPServer.GRIDFTP_URL, gridftpurl);
266: gftp.setInfo(GridFTPServer.STORAGE_DIR, storagedir);
267:
268: // System.out.println(" gridftp url="+((PoolConfigQuotedString) m_lookAhead).getValue() );
269: m_lookAhead = m_scanner.nextToken();
270: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
271: throw new PoolConfigException(
272: m_scanner.getLineNumber(),
273: "the \"gridftp\" requires a quoted string argument for globus version");
274: }
275: gftp.setInfo(GridFTPServer.GLOBUS_VERSION,
276: niceString(((PoolConfigQuotedString) m_lookAhead)
277: .getValue()));
278:
279: // System.out.println("version="+((PoolConfigQuotedString) m_lookAhead).getValue() );
280: site.setInfo(SiteInfo.GRIDFTP, gftp);
281: m_lookAhead = m_scanner.nextToken();
282: break;
283:
284: case PoolConfigReservedWord.PROFILE:
285: if (!(m_lookAhead instanceof PoolConfigIdentifier)) {
286: throw new PoolConfigException(
287: m_scanner.getLineNumber(),
288: "the \"profile\" requires a namespace identifier as first argument");
289: }
290: String namespace = ((PoolConfigIdentifier) m_lookAhead)
291: .getValue();
292: m_lookAhead = m_scanner.nextToken();
293:
294: // System.out.println("profile namespace="+namespace );
295: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
296: throw new PoolConfigException(
297: m_scanner.getLineNumber(),
298: "the \"profile\" requires a quoted string argument");
299: }
300: String key = ((PoolConfigQuotedString) m_lookAhead)
301: .getValue();
302:
303: // System.out.println("key="+((PoolConfigQuotedString) m_lookAhead).getValue() );
304: m_lookAhead = m_scanner.nextToken();
305: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
306: throw new PoolConfigException(
307: m_scanner.getLineNumber(),
308: "the \"profile\" requires a quoted string argument");
309: }
310: String value = ((PoolConfigQuotedString) m_lookAhead)
311: .getValue();
312:
313: // System.out.println("value="+((PoolConfigQuotedString) m_lookAhead).getValue() );
314: m_lookAhead = m_scanner.nextToken();
315: Profile profile = new Profile(namespace, niceString(key),
316: niceString(value));
317: site.setInfo(SiteInfo.PROFILE, profile);
318: break;
319:
320: case PoolConfigReservedWord.SYSINFO:
321: if (!(m_lookAhead instanceof PoolConfigQuotedString)) {
322: throw new PoolConfigException(
323: m_scanner.getLineNumber(),
324: "the \"sysinfo\" requires a quoted string argument");
325: }
326: String sysinfo = ((PoolConfigQuotedString) m_lookAhead)
327: .getValue();
328:
329: // System.out.println("key="+((PoolConfigQuotedString) m_lookAhead).getValue() );
330: m_lookAhead = m_scanner.nextToken();
331: site.setInfo(SiteInfo.SYSINFO, niceString(sysinfo));
332: break;
333:
334: default:
335: throw new PoolConfigException(m_scanner.getLineNumber(),
336: "invalid reserved word used to configure a site entry");
337: }
338: }
339:
340: /**
341: * Returns the site handle for a site, and moves the scanner to hold the next
342: * <code>PoolConfigReservedWord</code>.
343: *
344: * @return the site handle for a site, usually the name of the site.
345: *
346: * @throws plenty
347: */
348: private String getSiteHandle() throws IOException,
349: PoolConfigException {
350: String handle = null;
351: if (!(m_lookAhead instanceof PoolConfigReservedWord)
352: || ((PoolConfigReservedWord) m_lookAhead).getValue() != PoolConfigReservedWord.SITE) {
353: throw new PoolConfigException(m_scanner.getLineNumber(),
354: "expecting reserved word \"site\"");
355: }
356: m_lookAhead = m_scanner.nextToken();
357:
358: // proceed with next token
359: if (!(m_lookAhead instanceof PoolConfigIdentifier)) {
360: throw new PoolConfigException(m_scanner.getLineNumber(),
361: "expecting the pool handle identifier");
362: }
363:
364: handle = ((PoolConfigIdentifier) m_lookAhead).getValue();
365: m_lookAhead = m_scanner.nextToken();
366:
367: // proceed with next token
368: if (!(m_lookAhead instanceof PoolConfigOpenBrace)) {
369: throw new PoolConfigException(m_scanner.getLineNumber(),
370: "expecting an opening brace");
371: }
372: m_lookAhead = m_scanner.nextToken();
373: return handle;
374: }
375:
376: }
|