001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management.loading;
027:
028: import java.io.BufferedReader;
029: import java.io.Reader;
030: import java.io.InputStreamReader;
031: import java.io.FileInputStream;
032: import java.io.StreamTokenizer;
033: import java.util.ArrayList;
034: import java.util.Vector;
035: import java.util.StringTokenizer;
036: import java.text.ParseException;
037:
038: /**
039: * This class does the parsing of management applet(MLET) downloaded from
040: * remote host. Makes a mLetVector which contains SimpleMLet objects, which
041: * describes the information derived from the MLET file for each remote mbean.
042: */
043: class MLetParser {
044: StreamTokenizer strTok = null;
045: String fileString = "";
046: Vector mLetVector = null;
047:
048: MLetParser(String fileName) throws ParseException {
049: try {
050: Reader r = new BufferedReader(new InputStreamReader(
051: new FileInputStream(fileName)));
052: strTok = new StreamTokenizer(r);
053:
054: initTokens();
055:
056: mLetVector = new Vector();
057:
058: parseMLet();
059: } catch (Exception e) {
060: throw (ParseException) e;
061: }
062: }
063:
064: MLetParser(Reader r) throws ParseException {
065: try {
066: strTok = new StreamTokenizer(r);
067:
068: initTokens();
069:
070: mLetVector = new Vector();
071:
072: parseMLet();
073: } catch (Exception e) {
074: throw (ParseException) e;
075: }
076: }
077:
078: Vector getMLetVector() {
079: return mLetVector;
080: }
081:
082: void initTokens() {
083: strTok.wordChars('{', '}');
084: strTok.wordChars('(', ')');
085: strTok.wordChars('0', '9');
086: strTok.wordChars(',', ',');
087:
088: //strTok.wordChars(' ',' ');
089: //strTok.wordChars('\t','\t');
090:
091: strTok.wordChars('<', '<');
092: strTok.wordChars('>', '>');
093: //strTok.wordChars('>','>');
094: //strTok.ordinaryChar('<');
095: //strTok.ordinaryChar('>');
096: //strTok.ordinaryChar('=');
097: strTok.wordChars('/', '/');
098:
099: strTok.parseNumbers();
100: strTok.eolIsSignificant(false);
101: //tok.ordinaryChars('\0','\0');
102:
103: //strTok.wordChars('#','z');
104: strTok.wordChars('"', '"');
105: strTok.wordChars('_', '_');
106: strTok.wordChars(':', ':');
107: strTok.ordinaryChar(';');
108: strTok.ordinaryChar('_');
109: strTok.wordChars('@', '@');
110: strTok.ordinaryChar('(');
111: strTok.ordinaryChar(')');
112: //strTok.ordinaryChar('.');
113: //strTok.ordinaryChar(',');
114: }
115:
116: private boolean mletFlag = false;
117: private boolean tagClosed = false;
118: private SimpleMLet mLet = null;
119:
120: private void parseMLet() throws Exception {
121: while (strTok.ttype != strTok.TT_EOF) {
122: if (strTok.sval != null) {
123: String tokVal = strTok.sval;
124:
125: fileString += tokVal + "\n";
126:
127: if (tokVal.equals("<MLET")) {
128: mLet = new SimpleMLet();
129:
130: if (mletFlag)
131: throw new ParseException("Parse Error", strTok
132: .lineno());
133: mletFlag = true;
134: tagClosed = false;
135:
136: strTok.sval = "";
137: strTok.nextToken();
138: try {
139:
140: while (strTok.ttype != strTok.TT_EOF
141: && (strTok.sval != null && !strTok.sval
142: .equals("<MLET"))) {
143: //if(strTok.sval == null)
144: //{
145: // strTok.nextToken();
146: // continue;
147: //}
148: tokVal = strTok.sval.trim();
149:
150: fileString += tokVal + "\n";
151:
152: if (tokVal.equals(">")) {
153: tagClosed = true;
154: } else if (tokVal.equals("CODE")) {
155: strTok.nextToken();
156: int type = strTok.ttype;
157: if (type != 61)
158: throw new ParseException(
159: "Parse Error", strTok
160: .lineno());
161:
162: strTok.nextToken();
163: tokVal = strTok.sval.trim();
164: fileString += tokVal + "\n";
165:
166: if (tokVal.startsWith("\"")) {
167: tokVal = tokVal.substring(1, tokVal
168: .length() - 1);
169: }
170:
171: if (tokVal.endsWith("class")) {
172: tokVal = tokVal.substring(0, tokVal
173: .length() - 6);
174: }
175: tokVal = tokVal.replace('@', '_');
176:
177: mLet.setCode(tokVal);
178: } else if (tokVal.equals("OBJECT")) {
179: strTok.nextToken();
180: int type = strTok.ttype;
181: if (type != 61)
182: throw new ParseException(
183: "Parse Error", strTok
184: .lineno());
185:
186: strTok.nextToken();
187: tokVal = strTok.sval.trim();
188: fileString += tokVal + "\n";
189:
190: if (tokVal.startsWith("\"")) {
191: tokVal = tokVal.substring(1, tokVal
192: .length() - 1);
193: }
194: tokVal = tokVal.replace('@', '_');
195:
196: mLet.setObject(tokVal);
197: } else if (tokVal.equals("ARCHIVE")) {
198: strTok.nextToken();
199: int type = strTok.ttype;
200: if (type != 61) {
201: throw new ParseException(
202: "Parse Error", strTok
203: .lineno());
204: }
205:
206: strTok.nextToken();
207: tokVal = strTok.sval.trim();
208:
209: fileString += tokVal + "\n";
210: if (tokVal.startsWith("\"")) {
211: tokVal = tokVal.substring(1, tokVal
212: .length() - 1);
213: } else {
214: throw new ParseException(
215: "Invalid Archive Format",
216: strTok.lineno());
217: }
218:
219: Vector vec = parseArchiveTok(tokVal);
220: mLet.setArchive(vec);
221: } else if (tokVal.equals("CODEBASE")) {
222: strTok.nextToken();
223: int type = strTok.ttype;
224: if (type != 61)
225: throw new ParseException(
226: "Parse Error", strTok
227: .lineno());
228:
229: strTok.nextToken();
230: tokVal = strTok.sval.trim();
231: fileString += tokVal + "\n";
232:
233: if (tokVal.startsWith("\"")) {
234: tokVal = tokVal.substring(1, tokVal
235: .length() - 1);
236: }
237: tokVal = tokVal.replace('@', '_');
238:
239: mLet.setCodebase(tokVal);
240: } else if (tokVal.equals("NAME")) {
241: strTok.nextToken();
242:
243: int type = strTok.ttype;
244: if (type != 61)
245: throw new ParseException(
246: "Parse Error", strTok
247: .lineno());
248:
249: strTok.wordChars('=', '=');
250:
251: strTok.nextToken();
252: tokVal = strTok.sval.trim();
253: fileString += tokVal + "\n";
254: if (tokVal.startsWith("\"")) {
255: tokVal = tokVal.substring(1, tokVal
256: .length() - 1);
257: }
258: tokVal = tokVal.replace('@', '_');
259:
260: mLet.setName(tokVal);
261: strTok.ordinaryChar('=');
262: } else if (tokVal.equals("VERSION")) {
263: strTok.nextToken();
264: int type = strTok.ttype;
265: if (type != 61)
266: throw new ParseException(
267: "Parse Error", strTok
268: .lineno());
269:
270: strTok.nextToken();
271: tokVal = strTok.sval.trim();
272: fileString += tokVal + "\n";
273:
274: if (tokVal.indexOf("-") != -1)
275: throw new ParseException(
276: "Version number cannot be negative",
277: strTok.lineno());
278: if (tokVal.startsWith("\"")) {
279: tokVal = tokVal.substring(1, tokVal
280: .length() - 1);
281: }
282: tokVal = tokVal.replace('@', '_');
283:
284: mLet.setVersion(tokVal);
285: } else if (tokVal.equals("<ARG")) {
286: strTok.nextToken();
287: parseArglist(mLet, strTok);
288: //mLet.setArglist(hashTab);
289: } else if (strTok.sval.equals("</MLET>")) {
290: if (!tagClosed)
291: throw new ParseException(
292: "MLetTag not closed",
293: strTok.lineno());
294: mletFlag = false;
295: }
296: strTok.nextToken();
297: }
298: } catch (Exception e) {
299: throw new ParseException(e.getMessage(), strTok
300: .lineno());
301: }
302: strTok.pushBack();
303:
304: if (mLet.getCode() == null
305: && mLet.getObject() == null)
306: throw new ParseException(
307: "No Code/Object defined", strTok
308: .lineno());
309:
310: if (mLet.getArchive() == null)
311: throw new ParseException("Parse Error", strTok
312: .lineno());
313:
314: mLetVector.addElement(mLet);
315: } else {
316: strTok.nextToken();
317: }
318: } else {
319: strTok.nextToken();
320: }
321: }
322: }
323:
324: private Vector parseArchiveTok(String token) throws Exception {
325: if (token == null) {
326: throw new Exception("Invalid ArchiveList ");
327: }
328:
329: Vector toRet = new Vector();
330:
331: token = token.replace('@', '_');
332:
333: if (token.indexOf(",") != -1) {
334: //More than One jar file is present.so following checks have to be performed.
335: //1.Each file must be separated by a comma
336: //2.The whole list must be enclosed in "".
337:
338: StringTokenizer st = new StringTokenizer(token, ",");
339: while (st.hasMoreTokens()) {
340: toRet.addElement(st.nextToken());
341: }
342: } else if (token.indexOf(" ") != -1)
343: throw new Exception("Invalid Archive Tag");
344: else
345: toRet.addElement(token);
346:
347: return toRet;
348: }
349:
350: private void parseArglist(SimpleMLet entry, StreamTokenizer strTok)
351: throws Exception {
352: //boolean argTagClosed = false;
353: //Hashtable argTable = entry.getArglist();
354: ArrayList argTypes = entry.getArgTypes();
355: ArrayList argValues = entry.getArgValues();
356:
357: strTok.ordinaryChar('>');
358:
359: //int type = strTok.ttype;
360: //if(type != 61)
361: // throw new ParseException("Parse Error", strTok.lineno());
362: String argtype = null;
363: while (strTok.ttype != 62) {
364: String tokVal = strTok.sval.trim();
365:
366: fileString += tokVal + "\n";
367:
368: if (tokVal.equals("TYPE")) {
369: strTok.nextToken();
370: int type = strTok.ttype;
371: if (type != 61)
372: throw new ParseException("Parse Error", strTok
373: .lineno());
374:
375: strTok.nextToken();
376: tokVal = strTok.sval.trim();
377: fileString += tokVal + "\n";
378: if (tokVal.startsWith("\"")) {
379: tokVal = tokVal.substring(1, tokVal.length() - 1);
380: }
381: tokVal = tokVal.replace('@', '_');
382:
383: argtype = tokVal;
384: } else if (tokVal.equals("VALUE")) {
385: strTok.nextToken();
386: int type = strTok.ttype;
387: if (type != 61)
388: throw new ParseException("Parse Error", strTok
389: .lineno());
390:
391: strTok.nextToken();
392:
393: if (strTok.ttype == strTok.TT_NUMBER) {
394: String temp = "" + strTok.nval;
395: if (!temp.endsWith(".0"))
396: tokVal = temp;
397: else
398: tokVal = "" + (int) strTok.nval;
399: } else
400: tokVal = strTok.sval.trim();
401:
402: //tokVal = strTok.sval.trim();
403:
404: fileString += tokVal + "\n";
405:
406: if (argtype == null)
407: new Exception("Wrong argList definition line no: "
408: + strTok.lineno());
409:
410: //argTable.put(argtype, tokVal);
411:
412: if (argtype.startsWith("\"")) {
413: argtype = argtype
414: .substring(1, argtype.length() - 1);
415: }
416: argtype.replace('@', '_');
417: argTypes.add(argtype);
418: if (tokVal.startsWith("\"")) {
419: tokVal = tokVal.substring(1, tokVal.length() - 1);
420: }
421: tokVal = tokVal.replace('@', '_');
422:
423: Object val = returnDefaultValue(tokVal, argtype);
424: argValues.add(val);
425: argtype = null;
426: } else {
427: throw new ParseException("Parse Error", strTok.lineno());
428: }
429: strTok.nextToken();
430: }
431:
432: strTok.wordChars('>', '>');
433: }
434:
435: private Object returnDefaultValue(String value, String type)
436: throws Exception {
437: if (type.indexOf("String") != -1)
438: return value;
439: if (type.indexOf("Integer") != -1 || type.indexOf("int") != -1)
440: return new Integer(value);
441: if (type.indexOf("Long") != -1 || type.indexOf("long") != -1)
442: return new Long(value);
443: if (type.indexOf("Boolean") != -1
444: || type.indexOf("boolean") != -1)
445: return new Boolean(value);
446: if (type.indexOf("Float") != -1 || type.indexOf("float") != -1)
447: return new Float(value);
448: if (type.indexOf("Double") != -1
449: || type.indexOf("double") != -1) {
450: return new Double(value);
451: }
452: if (type.indexOf("Byte") != -1 || type.indexOf("byte") != -1)
453: return new Byte(value);
454: if (type.indexOf("Character") != -1
455: || type.indexOf("char") != -1)
456: return new Character(value.charAt(0));
457: if (type.indexOf("Short") != -1 || type.indexOf("short") != -1)
458: return new Short(value);
459: return null;
460: }
461: }
|