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