1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57:
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64:
77: public class CorbalocParser
78: extends NameTransformer
79: {
80:
83: public static final String pxCORBALOC = "corbaloc";
84:
85:
88: public static final String pxCORBANAME = "corbaname";
89:
90:
93: public static final String pxIOR = "ior";
94:
95:
98: public static final String pxFILE = "file://";
99:
100:
103: public static final String pxFTP = "ftp://";
104:
105:
108: public static final String pxHTTP = "http://";
109:
110:
113: public static final String IIOP = "iiop";
114:
115:
118: public static final String RIR = "rir";
119:
120:
123: public static final int DEFAULT_PORT = 2809;
124:
125:
128: public static final String DEFAULT_NAME = "NameService";
129:
130:
133: static NameTransformer converter;
134:
135:
138: int p;
139:
140:
143: String[] t;
144:
145:
165: public synchronized String[] corbaloc(String corbaloc,
166: ORB orb)
167: throws InvalidNameException
168: {
169: return corbaloc(corbaloc, orb, 0);
170: }
171:
172:
175: private String[] corbaloc(String corbaloc,
176: ORB orb, int recursion) throws InvalidNameException
177: {
178:
179:
180:
181: if (recursion > 10)
182: throw new DATA_CONVERSION("More than 10 redirections");
183:
184: if (corbaloc.startsWith(pxFILE))
185: return corbaloc(readFile(corbaloc.substring(pxFILE.length())), orb, recursion+1);
186: else if (corbaloc.startsWith(pxHTTP))
187: return corbaloc(readUrl(corbaloc), orb, recursion+1);
188: else if (corbaloc.startsWith(pxFTP))
189: return corbaloc(readUrl(corbaloc), orb, recursion+1);
190:
191:
192: int major = 1;
193: int minor = 0;
194:
195:
196: String host;
197:
198:
199: int port = DEFAULT_PORT;
200:
201:
202: String key;
203:
204: StringTokenizer st = new StringTokenizer(corbaloc, ":@/.,#", true);
205:
206: t = new String[st.countTokens()];
207:
208: for (int i = 0; i < t.length; i++)
209: {
210: t[i] = st.nextToken();
211: }
212:
213: p = 0;
214:
215: if (!t[p].startsWith(pxCORBANAME))
216: throw new InvalidNameException(corbaloc+" must start with "+pxCORBANAME);
217:
218: p++;
219:
220: if (!t[p++].equals(":"))
221: throw new BAD_PARAM("Syntax (':' expected after name prefix)");
222:
223:
224: if (t[p].equals(RIR))
225: {
226: p++;
227: if (!t[p++].equals(":"))
228: throw new BAD_PARAM("':' expected after 'rir'");
229:
230: key = readKey("/");
231:
232: Object object;
233: try
234: {
235: object = orb.resolve_initial_references(key);
236: return resolve(orb.object_to_string(object));
237: }
238: catch (InvalidName e)
239: {
240: throw new BAD_PARAM("Unknown initial reference '" + key + "'");
241: }
242: }
243: else
244:
245: if (t[p].equals(IIOP) || t[p].equals(":"))
246: {
247: IOR ior = new IOR();
248:
249: Addresses: do
250: {
251: if (t[p].equals(":"))
252: {
253: p++;
254: }
255: else
256: {
257: p++;
258: if (!t[p++].equals(":"))
259: throw new BAD_PARAM("':' expected after 'iiop'");
260:
261: if (t[p + 1].equals("."))
262: if (t[p + 3].equals("@"))
263: {
264:
265: try
266: {
267: major = Integer.parseInt(t[p++]);
268: }
269: catch (NumberFormatException e)
270: {
271: throw new BAD_PARAM("Major version number '"
272: + t[p - 1] + "'");
273: }
274: p++;
275: try
276: {
277: minor = Integer.parseInt(t[p++]);
278: }
279: catch (NumberFormatException e)
280: {
281: throw new BAD_PARAM("Major version number '"
282: + t[p - 1] + "'");
283: }
284: p++;
285: }
286: }
287:
288: ior.Internet.version = new Version(major, minor);
289:
290:
291: StringBuffer bhost = new StringBuffer(corbaloc.length());
292: while (!t[p].equals(":") && !t[p].equals("/") && !t[p].equals(","))
293: bhost.append(t[p++]);
294:
295: host = bhost.toString();
296:
297: ior.Internet.host = host;
298:
299: if (t[p].equals(":"))
300: {
301:
302: p++;
303: try
304: {
305: port = Integer.parseInt(t[p++]);
306: }
307: catch (NumberFormatException e)
308: {
309: throw new BAD_PARAM("Invalid port '" + t[p - 1] + "'");
310: }
311: }
312:
313: ior.Internet.port = port;
314:
315:
316: ior.Id = "";
317:
318: if (t[p].equals(","))
319: p++;
320: else
321: break Addresses;
322: }
323: while (true);
324:
325: key = readKey("/");
326: ior.key = key.getBytes();
327:
328: return resolve(ior.toStringifiedReference());
329: }
330:
331: else
332: throw new InvalidNameException("Unsupported protocol '" + t[p] +
333: "' (iiop expected)");
334: }
335:
336:
339: String readFile(String file)
340: {
341: File f = new File(file);
342: if (!f.exists())
343: {
344: DATA_CONVERSION err = new DATA_CONVERSION(f.getAbsolutePath()
345: + " does not exist.");
346: err.minor = Minor.Missing_IOR;
347: }
348: try
349: {
350: char[] c = new char[(int) f.length()];
351: FileReader fr = new FileReader(f);
352: fr.read(c);
353: fr.close();
354: return new String(c).trim();
355: }
356: catch (IOException ex)
357: {
358: DATA_CONVERSION d = new DATA_CONVERSION();
359: d.initCause(ex);
360: d.minor = Minor.Missing_IOR;
361: throw (d);
362: }
363: }
364:
365:
368: String readUrl(String url)
369: {
370: URL u;
371: try
372: {
373: u = new URL(url);
374: }
375: catch (MalformedURLException mex)
376: {
377: throw new BAD_PARAM("Malformed URL: '" + url + "'");
378: }
379:
380: try
381: {
382: InputStreamReader r = new InputStreamReader(u.openStream());
383:
384: StringBuffer b = new StringBuffer();
385: int c;
386:
387: while ((c = r.read()) > 0)
388: b.append((char) c);
389:
390: return b.toString().trim();
391: }
392: catch (Exception exc)
393: {
394: DATA_CONVERSION d = new DATA_CONVERSION("Reading " + url + " failed.");
395: d.minor = Minor.Missing_IOR;
396: throw d;
397: }
398: }
399:
400: private String[] resolve(String nsIor)
401: {
402: String [] n = new String[2];
403: n[0] = nsIor;
404: n[1] = readKey("#");
405: return n;
406: }
407:
408: private String readKey(String delimiter)
409: throws BAD_PARAM
410: {
411: if (p < t.length)
412: if (!t[p].equals(delimiter))
413: {
414: if (t[p].equals("#"))
415: return DEFAULT_NAME;
416: else
417: throw new BAD_PARAM("'" + delimiter + "String' expected '" + t[p]
418: + "' found");
419: }
420:
421: StringBuffer bKey = new StringBuffer();
422: p++;
423:
424: while (p < t.length && !t[p].equals("#"))
425: bKey.append(t[p++]);
426:
427: if (bKey.length() == 0)
428: return DEFAULT_NAME;
429:
430: try
431: {
432: return URLDecoder.decode(bKey.toString(), "UTF-8");
433: }
434: catch (UnsupportedEncodingException e)
435: {
436: throw new Unexpected("URLDecoder does not support UTF-8", e);
437: }
438: }
439: }