1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67:
68:
81: public class NameParser
82: extends NameTransformer
83: {
84:
87: public static final String pxCORBALOC = "corbaloc";
88:
89:
92: public static final String pxCORBANAME = "corbaname";
93:
94:
97: public static final String pxIOR = "ior";
98:
99:
102: public static final String pxFILE = "file://";
103:
104:
107: public static final String pxFTP = "ftp://";
108:
109:
112: public static final String pxHTTP = "http://";
113:
114:
117: public static final String IIOP = "iiop";
118:
119:
122: public static final String RIR = "rir";
123:
124:
127: public static final int DEFAULT_PORT = 2809;
128:
129:
132: public static final String DEFAULT_NAME = "NameService";
133:
134:
137: static NameTransformer converter;
138:
139:
142: int p;
143:
144:
147: String[] t;
148:
149:
168: public synchronized org.omg.CORBA.Object corbaloc(String corbaloc,
169: OrbFunctional orb)
170: throws BAD_PARAM
171: {
172: return corbaloc(corbaloc, orb, 0);
173: }
174:
175:
178: private org.omg.CORBA.Object corbaloc(String corbaloc,
179: OrbFunctional orb, int recursion)
180: {
181:
182:
183:
184: if (recursion > 10)
185: throw new DATA_CONVERSION("More than 10 redirections");
186:
187: if (corbaloc.startsWith(pxFILE))
188: return corbaloc(readFile(corbaloc.substring(pxFILE.length())), orb, recursion+1);
189: else if (corbaloc.startsWith(pxHTTP))
190: return corbaloc(readUrl(corbaloc), orb, recursion+1);
191: else if (corbaloc.startsWith(pxFTP))
192: return corbaloc(readUrl(corbaloc), orb, recursion+1);
193:
194: boolean corbaname;
195:
196:
197: ArrayList alt_addr = new ArrayList();
198:
199:
200: int major = 1;
201: int minor = 0;
202:
203:
204: String host;
205:
206:
207: int port = DEFAULT_PORT;
208:
209:
210: String key;
211:
212: StringTokenizer st = new StringTokenizer(corbaloc, ":@/.,#", true);
213:
214: t = new String[st.countTokens()];
215:
216: for (int i = 0; i < t.length; i++)
217: {
218: t[i] = st.nextToken();
219: }
220:
221: p = 0;
222:
223: if (t[p].startsWith(pxCORBANAME))
224: corbaname = true;
225: else if (t[p].equalsIgnoreCase(pxCORBALOC))
226: corbaname = false;
227: else if (t[p].equalsIgnoreCase(pxIOR))
228: {
229: IOR ior = IOR.parse(corbaloc);
230: return orb.ior_to_object(ior);
231: }
232: else
233: throw new DATA_CONVERSION("Unsupported protocol: '" + t[p] + "'");
234:
235: p++;
236:
237: if (!t[p++].equals(":"))
238: throw new BAD_PARAM("Syntax (':' expected after name prefix)");
239:
240:
241: if (t[p].equals(RIR))
242: {
243: p++;
244: if (!t[p++].equals(":"))
245: throw new BAD_PARAM("':' expected after 'rir'");
246:
247: key = readKey("/");
248:
249: Object object;
250: try
251: {
252: object = orb.resolve_initial_references(key);
253: return corbaname ? resolve(object) : object;
254: }
255: catch (InvalidName e)
256: {
257: throw new BAD_PARAM("Unknown initial reference '" + key + "'");
258: }
259: }
260: else
261:
262: if (t[p].equals(IIOP) || t[p].equals(":"))
263: {
264: IOR ior = new IOR();
265:
266: Addresses: do
267: {
268: if (t[p].equals(":"))
269: {
270: p++;
271: }
272: else
273: {
274: p++;
275: if (!t[p++].equals(":"))
276: throw new BAD_PARAM("':' expected after 'iiop'");
277:
278: if (t[p + 1].equals("."))
279: if (t[p + 3].equals("@"))
280: {
281:
282: try
283: {
284: major = Integer.parseInt(t[p++]);
285: }
286: catch (NumberFormatException e)
287: {
288: throw new BAD_PARAM("Major version number '"
289: + t[p - 1] + "'");
290: }
291: p++;
292: try
293: {
294: minor = Integer.parseInt(t[p++]);
295: }
296: catch (NumberFormatException e)
297: {
298: throw new BAD_PARAM("Major version number '"
299: + t[p - 1] + "'");
300: }
301: p++;
302: }
303: }
304:
305: ior.Internet.version = new Version(major, minor);
306:
307:
308: StringBuffer bhost = new StringBuffer(corbaloc.length());
309: while (!t[p].equals(":") && !t[p].equals("/") && !t[p].equals(","))
310: bhost.append(t[p++]);
311:
312: host = bhost.toString();
313:
314: ior.Internet.host = host;
315:
316: if (t[p].equals(":"))
317: {
318:
319: p++;
320: try
321: {
322: port = Integer.parseInt(t[p++]);
323: }
324: catch (NumberFormatException e)
325: {
326: throw new BAD_PARAM("Invalid port '" + t[p - 1] + "'");
327: }
328: }
329:
330: ior.Internet.port = port;
331:
332:
333: ior.Id = "";
334:
335: if (t[p].equals(","))
336: p++;
337: else
338: break Addresses;
339: }
340: while (true);
341:
342: key = readKey("/");
343: ior.key = key.getBytes();
344:
345: org.omg.CORBA.Object object = orb.ior_to_object(ior);
346: return corbaname ? resolve(object) : object;
347: }
348:
349: else
350: throw new DATA_CONVERSION("Unsupported protocol '" + t[p] + "'");
351: }
352:
353:
356: String readFile(String file)
357: {
358: File f = new File(file);
359: if (!f.exists())
360: {
361: DATA_CONVERSION err = new DATA_CONVERSION(f.getAbsolutePath()
362: + " does not exist.");
363: err.minor = Minor.Missing_IOR;
364: }
365: try
366: {
367: char[] c = new char[(int) f.length()];
368: FileReader fr = new FileReader(f);
369: fr.read(c);
370: fr.close();
371: return new String(c).trim();
372: }
373: catch (IOException ex)
374: {
375: DATA_CONVERSION d = new DATA_CONVERSION();
376: d.initCause(ex);
377: d.minor = Minor.Missing_IOR;
378: throw (d);
379: }
380: }
381:
382:
385: String readUrl(String url)
386: {
387: URL u;
388: try
389: {
390: u = new URL(url);
391: }
392: catch (MalformedURLException mex)
393: {
394: throw new BAD_PARAM("Malformed URL: '" + url + "'");
395: }
396:
397: try
398: {
399: InputStreamReader r = new InputStreamReader(u.openStream());
400:
401: StringBuffer b = new StringBuffer();
402: int c;
403:
404: while ((c = r.read()) > 0)
405: b.append((char) c);
406:
407: return b.toString().trim();
408: }
409: catch (Exception exc)
410: {
411: DATA_CONVERSION d = new DATA_CONVERSION("Reading " + url + " failed.");
412: d.minor = Minor.Missing_IOR;
413: throw d;
414: }
415: }
416:
417: private org.omg.CORBA.Object resolve(org.omg.CORBA.Object object)
418: {
419: NamingContext ns;
420: String key = "?";
421: try
422: {
423: if (object instanceof NamingContext)
424: ns = (NamingContext) object;
425: else
426: {
427: Delegate delegate = ((ObjectImpl) object)._get_delegate();
428: ns = new _NamingContextStub();
429: ((_NamingContextStub) ns)._set_delegate(delegate);
430: }
431: }
432: catch (Exception ex)
433: {
434: BAD_PARAM bad = new BAD_PARAM("The CORBANAME target " + object
435: + " is not a NamingContext");
436: bad.minor = 10;
437: bad.initCause(ex);
438: throw bad;
439: }
440:
441: if (converter == null)
442: converter = new NameTransformer();
443:
444: try
445: {
446: key = readKey("#");
447: object = ns.resolve(converter.toName(key));
448: return object;
449: }
450: catch (Exception ex)
451: {
452: BAD_PARAM bad = new BAD_PARAM("Wrong CORBANAME '" + key + "'");
453: bad.minor = 10;
454: bad.initCause(ex);
455: throw bad;
456: }
457: }
458:
459: private String readKey(String delimiter)
460: throws BAD_PARAM
461: {
462: if (p < t.length)
463: if (!t[p].equals(delimiter))
464: {
465: if (t[p].equals("#"))
466: return DEFAULT_NAME;
467: else
468: throw new BAD_PARAM("'" + delimiter + "String' expected '" + t[p]
469: + "' found");
470: }
471:
472: StringBuffer bKey = new StringBuffer();
473: p++;
474:
475: while (p < t.length && !t[p].equals("#"))
476: bKey.append(t[p++]);
477:
478: if (bKey.length() == 0)
479: return DEFAULT_NAME;
480:
481: try
482: {
483: return URLDecoder.decode(bKey.toString(), "UTF-8");
484: }
485: catch (UnsupportedEncodingException e)
486: {
487: throw new Unexpected("URLDecoder does not support UTF-8", e);
488: }
489: }
490:
491: static NameParser n = new NameParser();
492:
493: static void corbalocT(String ior, OrbFunctional orb)
494: {
495: System.out.println(ior);
496: System.out.println(n.corbaloc(ior, orb));
497: System.out.println();
498: }
499:
500: public static void main(String[] args)
501: {
502: try
503: {
504: OrbFunctional orb = (OrbFunctional) ORB.init(args, null);
505: corbalocT("corbaloc:iiop:1.3@155axyz.com/Prod/aTradingService", orb);
506: corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
507: corbalocT("corbaloc:iiop:355cxyz.com/Prod/cTradingService", orb);
508: corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
509: corbalocT("corbaloc:iiop:355cxyz.com:7777/Prod/cTradingService", orb);
510:
511: corbalocT("corbaloc::556xyz.com:80/Dev/NameService", orb);
512: corbalocT("corbaloc:iiop:1.2@host1:3076/0", orb);
513:
514: corbalocT("corbaloc:rir:/NameService", orb);
515: corbalocT("corbaloc:rir:/", orb);
516: corbalocT("corbaloc:rir:", orb);
517:
518: corbalocT("corbaloc:rir:/NameService", orb);
519: corbalocT("corbaloc:rir:/", orb);
520: corbalocT("corbaloc:rir:", orb);
521:
522: corbalocT("corbaloc::555xyz.com,:556xyz.com:80/Dev/NameService", orb);
523: }
524: catch (BAD_PARAM e)
525: {
526: e.printStackTrace(System.out);
527: }
528: }
529: }