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: int major = 1;
198: int minor = 0;
199:
200:
201: String host;
202:
203:
204: int port = DEFAULT_PORT;
205:
206:
207: String key;
208:
209: StringTokenizer st = new StringTokenizer(corbaloc, ":@/.,#", true);
210:
211: t = new String[st.countTokens()];
212:
213: for (int i = 0; i < t.length; i++)
214: {
215: t[i] = st.nextToken();
216: }
217:
218: p = 0;
219:
220: if (t[p].startsWith(pxCORBANAME))
221: corbaname = true;
222: else if (t[p].equalsIgnoreCase(pxCORBALOC))
223: corbaname = false;
224: else if (t[p].equalsIgnoreCase(pxIOR))
225: {
226: IOR ior = IOR.parse(corbaloc);
227: return orb.ior_to_object(ior);
228: }
229: else
230: throw new DATA_CONVERSION("Unsupported protocol: '" + t[p] + "'");
231:
232: p++;
233:
234: if (!t[p++].equals(":"))
235: throw new BAD_PARAM("Syntax (':' expected after name prefix)");
236:
237:
238: if (t[p].equals(RIR))
239: {
240: p++;
241: if (!t[p++].equals(":"))
242: throw new BAD_PARAM("':' expected after 'rir'");
243:
244: key = readKey("/");
245:
246: Object object;
247: try
248: {
249: object = orb.resolve_initial_references(key);
250: return corbaname ? resolve(object) : object;
251: }
252: catch (InvalidName e)
253: {
254: throw new BAD_PARAM("Unknown initial reference '" + key + "'");
255: }
256: }
257: else
258:
259: if (t[p].equals(IIOP) || t[p].equals(":"))
260: {
261: IOR ior = new IOR();
262:
263: Addresses: do
264: {
265: if (t[p].equals(":"))
266: {
267: p++;
268: }
269: else
270: {
271: p++;
272: if (!t[p++].equals(":"))
273: throw new BAD_PARAM("':' expected after 'iiop'");
274:
275: if (t[p + 1].equals("."))
276: if (t[p + 3].equals("@"))
277: {
278:
279: try
280: {
281: major = Integer.parseInt(t[p++]);
282: }
283: catch (NumberFormatException e)
284: {
285: throw new BAD_PARAM("Major version number '"
286: + t[p - 1] + "'");
287: }
288: p++;
289: try
290: {
291: minor = Integer.parseInt(t[p++]);
292: }
293: catch (NumberFormatException e)
294: {
295: throw new BAD_PARAM("Major version number '"
296: + t[p - 1] + "'");
297: }
298: p++;
299: }
300: }
301:
302: ior.Internet.version = new Version(major, minor);
303:
304:
305: StringBuffer bhost = new StringBuffer(corbaloc.length());
306: while (!t[p].equals(":") && !t[p].equals("/") && !t[p].equals(","))
307: bhost.append(t[p++]);
308:
309: host = bhost.toString();
310:
311: ior.Internet.host = host;
312:
313: if (t[p].equals(":"))
314: {
315:
316: p++;
317: try
318: {
319: port = Integer.parseInt(t[p++]);
320: }
321: catch (NumberFormatException e)
322: {
323: throw new BAD_PARAM("Invalid port '" + t[p - 1] + "'");
324: }
325: }
326:
327: ior.Internet.port = port;
328:
329:
330: ior.Id = "";
331:
332: if (t[p].equals(","))
333: p++;
334: else
335: break Addresses;
336: }
337: while (true);
338:
339: key = readKey("/");
340: ior.key = key.getBytes();
341:
342: org.omg.CORBA.Object object = orb.ior_to_object(ior);
343: return corbaname ? resolve(object) : object;
344: }
345:
346: else
347: throw new DATA_CONVERSION("Unsupported protocol '" + t[p] + "'");
348: }
349:
350:
353: String readFile(String file)
354: {
355: File f = new File(file);
356: if (!f.exists())
357: {
358: DATA_CONVERSION err = new DATA_CONVERSION(f.getAbsolutePath()
359: + " does not exist.");
360: err.minor = Minor.Missing_IOR;
361: }
362: try
363: {
364: char[] c = new char[(int) f.length()];
365: FileReader fr = new FileReader(f);
366: fr.read(c);
367: fr.close();
368: return new String(c).trim();
369: }
370: catch (IOException ex)
371: {
372: DATA_CONVERSION d = new DATA_CONVERSION();
373: d.initCause(ex);
374: d.minor = Minor.Missing_IOR;
375: throw (d);
376: }
377: }
378:
379:
382: String readUrl(String url)
383: {
384: URL u;
385: try
386: {
387: u = new URL(url);
388: }
389: catch (MalformedURLException mex)
390: {
391: throw new BAD_PARAM("Malformed URL: '" + url + "'");
392: }
393:
394: try
395: {
396: InputStreamReader r = new InputStreamReader(u.openStream());
397:
398: StringBuffer b = new StringBuffer();
399: int c;
400:
401: while ((c = r.read()) > 0)
402: b.append((char) c);
403:
404: return b.toString().trim();
405: }
406: catch (Exception exc)
407: {
408: DATA_CONVERSION d = new DATA_CONVERSION("Reading " + url + " failed.");
409: d.minor = Minor.Missing_IOR;
410: throw d;
411: }
412: }
413:
414: private org.omg.CORBA.Object resolve(org.omg.CORBA.Object object)
415: {
416: NamingContext ns;
417: String key = "?";
418: try
419: {
420: if (object instanceof NamingContext)
421: ns = (NamingContext) object;
422: else
423: {
424: Delegate delegate = ((ObjectImpl) object)._get_delegate();
425: ns = new _NamingContextStub();
426: ((_NamingContextStub) ns)._set_delegate(delegate);
427: }
428: }
429: catch (Exception ex)
430: {
431: BAD_PARAM bad = new BAD_PARAM("The CORBANAME target " + object
432: + " is not a NamingContext");
433: bad.minor = 10;
434: bad.initCause(ex);
435: throw bad;
436: }
437:
438: if (converter == null)
439: converter = new NameTransformer();
440:
441: try
442: {
443: key = readKey("#");
444: object = ns.resolve(converter.toName(key));
445: return object;
446: }
447: catch (Exception ex)
448: {
449: BAD_PARAM bad = new BAD_PARAM("Wrong CORBANAME '" + key + "'");
450: bad.minor = 10;
451: bad.initCause(ex);
452: throw bad;
453: }
454: }
455:
456: private String readKey(String delimiter)
457: throws BAD_PARAM
458: {
459: if (p < t.length)
460: if (!t[p].equals(delimiter))
461: {
462: if (t[p].equals("#"))
463: return DEFAULT_NAME;
464: else
465: throw new BAD_PARAM("'" + delimiter + "String' expected '" + t[p]
466: + "' found");
467: }
468:
469: StringBuffer bKey = new StringBuffer();
470: p++;
471:
472: while (p < t.length && !t[p].equals("#"))
473: bKey.append(t[p++]);
474:
475: if (bKey.length() == 0)
476: return DEFAULT_NAME;
477:
478: try
479: {
480: return URLDecoder.decode(bKey.toString(), "UTF-8");
481: }
482: catch (UnsupportedEncodingException e)
483: {
484: throw new Unexpected("URLDecoder does not support UTF-8", e);
485: }
486: }
487:
488: static NameParser n = new NameParser();
489:
490: static void corbalocT(String ior, OrbFunctional orb)
491: {
492: System.out.println(ior);
493: System.out.println(n.corbaloc(ior, orb));
494: System.out.println();
495: }
496:
497: public static void main(String[] args)
498: {
499: try
500: {
501: OrbFunctional orb = (OrbFunctional) ORB.init(args, null);
502: corbalocT("corbaloc:iiop:1.3@155axyz.com/Prod/aTradingService", orb);
503: corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
504: corbalocT("corbaloc:iiop:355cxyz.com/Prod/cTradingService", orb);
505: corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
506: corbalocT("corbaloc:iiop:355cxyz.com:7777/Prod/cTradingService", orb);
507:
508: corbalocT("corbaloc::556xyz.com:80/Dev/NameService", orb);
509: corbalocT("corbaloc:iiop:1.2@host1:3076/0", orb);
510:
511: corbalocT("corbaloc:rir:/NameService", orb);
512: corbalocT("corbaloc:rir:/", orb);
513: corbalocT("corbaloc:rir:", orb);
514:
515: corbalocT("corbaloc:rir:/NameService", orb);
516: corbalocT("corbaloc:rir:/", orb);
517: corbalocT("corbaloc:rir:", orb);
518:
519: corbalocT("corbaloc::555xyz.com,:556xyz.com:80/Dev/NameService", orb);
520: }
521: catch (BAD_PARAM e)
522: {
523: e.printStackTrace(System.out);
524: }
525: }
526: }