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(delegate);
429: }
430: }
431: catch (Exception ex)
432: {
433: BAD_PARAM bad = new BAD_PARAM("The CORBANAME target " + object
434: + " is not a NamingContext");
435: bad.minor = 10;
436: bad.initCause(ex);
437: throw bad;
438: }
439:
440: if (converter == null)
441: converter = new NameTransformer();
442:
443: try
444: {
445: key = readKey("#");
446: object = ns.resolve(converter.toName(key));
447: return object;
448: }
449: catch (Exception ex)
450: {
451: BAD_PARAM bad = new BAD_PARAM("Wrong CORBANAME '" + key + "'");
452: bad.minor = 10;
453: bad.initCause(ex);
454: throw bad;
455: }
456: }
457:
458: private String readKey(String delimiter)
459: throws BAD_PARAM
460: {
461: if (p < t.length)
462: if (!t[p].equals(delimiter))
463: {
464: if (t[p].equals("#"))
465: return DEFAULT_NAME;
466: else
467: throw new BAD_PARAM("'" + delimiter + "String' expected '" + t[p]
468: + "' found");
469: }
470:
471: StringBuffer bKey = new StringBuffer();
472: p++;
473:
474: while (p < t.length && !t[p].equals("#"))
475: bKey.append(t[p++]);
476:
477: if (bKey.length() == 0)
478: return DEFAULT_NAME;
479:
480: try
481: {
482: return URLDecoder.decode(bKey.toString(), "UTF-8");
483: }
484: catch (UnsupportedEncodingException e)
485: {
486: throw new Unexpected("URLDecoder does not support UTF-8", e);
487: }
488: }
489:
490: static NameParser n = new NameParser();
491:
492: static void corbalocT(String ior, OrbFunctional orb)
493: {
494: System.out.println(ior);
495: System.out.println(n.corbaloc(ior, orb));
496: System.out.println();
497: }
498:
499: public static void main(String[] args)
500: {
501: try
502: {
503: OrbFunctional orb = (OrbFunctional) ORB.init(args, null);
504: corbalocT("corbaloc:iiop:1.3@155axyz.com/Prod/aTradingService", orb);
505: corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
506: corbalocT("corbaloc:iiop:355cxyz.com/Prod/cTradingService", orb);
507: corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
508: corbalocT("corbaloc:iiop:355cxyz.com:7777/Prod/cTradingService", orb);
509:
510: corbalocT("corbaloc::556xyz.com:80/Dev/NameService", orb);
511: corbalocT("corbaloc:iiop:1.2@host1:3076/0", orb);
512:
513: corbalocT("corbaloc:rir:/NameService", orb);
514: corbalocT("corbaloc:rir:/", orb);
515: corbalocT("corbaloc:rir:", orb);
516:
517: corbalocT("corbaloc:rir:/NameService", orb);
518: corbalocT("corbaloc:rir:/", orb);
519: corbalocT("corbaloc:rir:", orb);
520:
521: corbalocT("corbaloc::555xyz.com,:556xyz.com:80/Dev/NameService", orb);
522: }
523: catch (BAD_PARAM e)
524: {
525: e.printStackTrace(System.out);
526: }
527: }
528: }