NameParser.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* NameParser.java --
  2. Copyright (C) 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.NamingService;
  32. import gnu.CORBA.Minor;
  33. import gnu.CORBA.OrbFunctional;
  34. import gnu.CORBA.IOR;
  35. import gnu.CORBA.Unexpected;
  36. import gnu.CORBA.Version;
  37. import gnu.java.lang.CPStringBuilder;
  38. import org.omg.CORBA.BAD_PARAM;
  39. import org.omg.CORBA.DATA_CONVERSION;
  40. import org.omg.CORBA.ORB;
  41. import org.omg.CORBA.Object;
  42. import org.omg.CORBA.ORBPackage.InvalidName;
  43. import org.omg.CORBA.portable.Delegate;
  44. import org.omg.CORBA.portable.ObjectImpl;
  45. import org.omg.CosNaming.NamingContext;
  46. import org.omg.CosNaming._NamingContextStub;
  47. import java.io.File;
  48. import java.io.FileReader;
  49. import java.io.IOException;
  50. import java.io.InputStreamReader;
  51. import java.io.UnsupportedEncodingException;
  52. import java.net.MalformedURLException;
  53. import java.net.URL;
  54. import java.net.URLDecoder;
  55. import java.util.StringTokenizer;
  56. /**
  57. * Parses the alternative IOR representations into our IOR structure.
  58. *
  59. * TODO This parser currently supports only one address per target string. A
  60. * string with the multiple addresses will be accepted, but only the last
  61. * address will be taken into consideration. The fault tolerance is not yet
  62. * implemented.
  63. *
  64. * The key string is filtered using {@link java.net.URLDecoder} that replaces
  65. * the agreed escape sequences by the corresponding non alphanumeric characters.
  66. *
  67. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  68. */
  69. public class NameParser
  70. extends NameTransformer
  71. {
  72. /**
  73. * The corbaloc prefix.
  74. */
  75. public static final String pxCORBALOC = "corbaloc";
  76. /**
  77. * The corbaname prefix.
  78. */
  79. public static final String pxCORBANAME = "corbaname";
  80. /**
  81. * The IOR prefix.
  82. */
  83. public static final String pxIOR = "ior";
  84. /**
  85. * The file:// prefix.
  86. */
  87. public static final String pxFILE = "file://";
  88. /**
  89. * The ftp:// prefix.
  90. */
  91. public static final String pxFTP = "ftp://";
  92. /**
  93. * The http:// prefix.
  94. */
  95. public static final String pxHTTP = "http://";
  96. /**
  97. * Marks iiop protocol.
  98. */
  99. public static final String IIOP = "iiop";
  100. /**
  101. * Marks rir protocol.
  102. */
  103. public static final String RIR = "rir";
  104. /**
  105. * The default port value, as specified in OMG documentation.
  106. */
  107. public static final int DEFAULT_PORT = 2809;
  108. /**
  109. * The default name.
  110. */
  111. public static final String DEFAULT_NAME = "NameService";
  112. /**
  113. * The string to name converter, initialized on demand.
  114. */
  115. static NameTransformer converter;
  116. /**
  117. * The current position.
  118. */
  119. int p;
  120. /**
  121. * The address being parsed, splitted into tokens.
  122. */
  123. String[] t;
  124. /**
  125. * Parse CORBALOC.
  126. *
  127. * The expected format is: <br>
  128. * 1. corbaloc:[iiop][version.subversion@]:host[:port]/key <br>
  129. * 2. corbaloc:rir:[/key] <br>
  130. * 3. corbaname:[iiop][version.subversion@]:host[:port]/key <br>
  131. * 4. corbaname:rir:[/key] <br>
  132. * 5. file://[file name]<br>
  133. * 6. http://[url]<br>
  134. * 7. ftp://[url]<br>
  135. *
  136. * Protocol defaults to IOP, the object key defaults to the NameService.
  137. *
  138. * @param corbaloc the string to parse.
  139. * @param orb the ORB, needed to create IORs and resolve rir references.
  140. *
  141. * @return the resolved object.
  142. */
  143. public synchronized org.omg.CORBA.Object corbaloc(String corbaloc,
  144. OrbFunctional orb)
  145. throws BAD_PARAM
  146. {
  147. return corbaloc(corbaloc, orb, 0);
  148. }
  149. /**
  150. * Parse controlling against the infinite recursion loop.
  151. */
  152. private org.omg.CORBA.Object corbaloc(String corbaloc,
  153. OrbFunctional orb, int recursion)
  154. {
  155. // The used CORBA specification does not state how many times we should to
  156. //redirect, but the infinite loop may be used to knock out the system.
  157. // by malicious attempt.
  158. if (recursion > 10)
  159. throw new DATA_CONVERSION("More than 10 redirections");
  160. if (corbaloc.startsWith(pxFILE))
  161. return corbaloc(readFile(corbaloc.substring(pxFILE.length())), orb, recursion+1);
  162. else if (corbaloc.startsWith(pxHTTP))
  163. return corbaloc(readUrl(corbaloc), orb, recursion+1);
  164. else if (corbaloc.startsWith(pxFTP))
  165. return corbaloc(readUrl(corbaloc), orb, recursion+1);
  166. boolean corbaname;
  167. // The version numbers with default values.
  168. int major = 1;
  169. int minor = 0;
  170. // The host address.
  171. String host;
  172. // The port.
  173. int port = DEFAULT_PORT;
  174. // The object key as string.
  175. String key;
  176. StringTokenizer st = new StringTokenizer(corbaloc, ":@/.,#", true);
  177. t = new String[st.countTokens()];
  178. for (int i = 0; i < t.length; i++)
  179. {
  180. t[i] = st.nextToken();
  181. }
  182. p = 0;
  183. if (t[p].startsWith(pxCORBANAME))
  184. corbaname = true;
  185. else if (t[p].equalsIgnoreCase(pxCORBALOC))
  186. corbaname = false;
  187. else if (t[p].equalsIgnoreCase(pxIOR))
  188. {
  189. IOR ior = IOR.parse(corbaloc);
  190. return orb.ior_to_object(ior);
  191. }
  192. else
  193. throw new DATA_CONVERSION("Unsupported protocol: '" + t[p] + "'");
  194. p++;
  195. if (!t[p++].equals(":"))
  196. throw new BAD_PARAM("Syntax (':' expected after name prefix)");
  197. // Check for rir:
  198. if (t[p].equals(RIR))
  199. {
  200. p++;
  201. if (!t[p++].equals(":"))
  202. throw new BAD_PARAM("':' expected after 'rir'");
  203. key = readKey("/");
  204. Object object;
  205. try
  206. {
  207. object = orb.resolve_initial_references(key);
  208. return corbaname ? resolve(object) : object;
  209. }
  210. catch (InvalidName e)
  211. {
  212. throw new BAD_PARAM("Unknown initial reference '" + key + "'");
  213. }
  214. }
  215. else
  216. // Check for iiop.
  217. if (t[p].equals(IIOP) || t[p].equals(":"))
  218. {
  219. IOR ior = new IOR();
  220. Addresses: do
  221. { // Read addresses.
  222. if (t[p].equals(":"))
  223. {
  224. p++;
  225. }
  226. else
  227. {
  228. p++;
  229. if (!t[p++].equals(":"))
  230. throw new BAD_PARAM("':' expected after 'iiop'");
  231. // Check if version is present.
  232. if (t[p + 1].equals("."))
  233. if (t[p + 3].equals("@"))
  234. {
  235. // Version info present.
  236. try
  237. {
  238. major = Integer.parseInt(t[p++]);
  239. }
  240. catch (NumberFormatException e)
  241. {
  242. throw new BAD_PARAM("Major version number '"
  243. + t[p - 1] + "'");
  244. }
  245. p++; // '.' at this point.
  246. try
  247. {
  248. minor = Integer.parseInt(t[p++]);
  249. }
  250. catch (NumberFormatException e)
  251. {
  252. throw new BAD_PARAM("Major version number '"
  253. + t[p - 1] + "'");
  254. }
  255. p++; // '@' at this point.
  256. }
  257. }
  258. ior.Internet.version = new Version(major, minor);
  259. // Then host data goes till '/' or ':'.
  260. CPStringBuilder bhost = new CPStringBuilder(corbaloc.length());
  261. while (!t[p].equals(":") && !t[p].equals("/") && !t[p].equals(","))
  262. bhost.append(t[p++]);
  263. host = bhost.toString();
  264. ior.Internet.host = host;
  265. if (t[p].equals(":"))
  266. {
  267. // Port specified.
  268. p++;
  269. try
  270. {
  271. port = Integer.parseInt(t[p++]);
  272. }
  273. catch (NumberFormatException e)
  274. {
  275. throw new BAD_PARAM("Invalid port '" + t[p - 1] + "'");
  276. }
  277. }
  278. ior.Internet.port = port;
  279. // Id is not listed.
  280. ior.Id = "";
  281. if (t[p].equals(","))
  282. p++;
  283. else
  284. break Addresses;
  285. }
  286. while (true);
  287. key = readKey("/");
  288. ior.key = key.getBytes();
  289. org.omg.CORBA.Object object = orb.ior_to_object(ior);
  290. return corbaname ? resolve(object) : object;
  291. }
  292. else
  293. throw new DATA_CONVERSION("Unsupported protocol '" + t[p] + "'");
  294. }
  295. /**
  296. * Read IOR from the file in the local file system.
  297. */
  298. String readFile(String file)
  299. {
  300. File f = new File(file);
  301. if (!f.exists())
  302. {
  303. DATA_CONVERSION err = new DATA_CONVERSION(f.getAbsolutePath()
  304. + " does not exist.");
  305. err.minor = Minor.Missing_IOR;
  306. }
  307. try
  308. {
  309. char[] c = new char[(int) f.length()];
  310. FileReader fr = new FileReader(f);
  311. fr.read(c);
  312. fr.close();
  313. return new String(c).trim();
  314. }
  315. catch (IOException ex)
  316. {
  317. DATA_CONVERSION d = new DATA_CONVERSION();
  318. d.initCause(ex);
  319. d.minor = Minor.Missing_IOR;
  320. throw (d);
  321. }
  322. }
  323. /**
  324. * Read IOR from the remote URL.
  325. */
  326. String readUrl(String url)
  327. {
  328. URL u;
  329. try
  330. {
  331. u = new URL(url);
  332. }
  333. catch (MalformedURLException mex)
  334. {
  335. throw new BAD_PARAM("Malformed URL: '" + url + "'");
  336. }
  337. try
  338. {
  339. InputStreamReader r = new InputStreamReader(u.openStream());
  340. CPStringBuilder b = new CPStringBuilder();
  341. int c;
  342. while ((c = r.read()) > 0)
  343. b.append((char) c);
  344. return b.toString().trim();
  345. }
  346. catch (Exception exc)
  347. {
  348. DATA_CONVERSION d = new DATA_CONVERSION("Reading " + url + " failed.");
  349. d.minor = Minor.Missing_IOR;
  350. throw d;
  351. }
  352. }
  353. private org.omg.CORBA.Object resolve(org.omg.CORBA.Object object)
  354. {
  355. NamingContext ns;
  356. String key = "?";
  357. try
  358. {
  359. if (object instanceof NamingContext)
  360. ns = (NamingContext) object;
  361. else
  362. {
  363. Delegate delegate = ((ObjectImpl) object)._get_delegate();
  364. ns = new _NamingContextStub();
  365. ((_NamingContextStub) ns)._set_delegate(delegate);
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. BAD_PARAM bad = new BAD_PARAM("The CORBANAME target " + object
  371. + " is not a NamingContext");
  372. bad.minor = 10;
  373. bad.initCause(ex);
  374. throw bad;
  375. }
  376. if (converter == null)
  377. converter = new NameTransformer();
  378. try
  379. {
  380. key = readKey("#");
  381. object = ns.resolve(converter.toName(key));
  382. return object;
  383. }
  384. catch (Exception ex)
  385. {
  386. BAD_PARAM bad = new BAD_PARAM("Wrong CORBANAME '" + key + "'");
  387. bad.minor = 10;
  388. bad.initCause(ex);
  389. throw bad;
  390. }
  391. }
  392. private String readKey(String delimiter)
  393. throws BAD_PARAM
  394. {
  395. if (p < t.length)
  396. if (!t[p].equals(delimiter))
  397. {
  398. if (t[p].equals("#"))
  399. return DEFAULT_NAME;
  400. else
  401. throw new BAD_PARAM("'" + delimiter + "String' expected '" + t[p]
  402. + "' found");
  403. }
  404. CPStringBuilder bKey = new CPStringBuilder();
  405. p++;
  406. while (p < t.length && !t[p].equals("#"))
  407. bKey.append(t[p++]);
  408. if (bKey.length() == 0)
  409. return DEFAULT_NAME;
  410. try
  411. {
  412. return URLDecoder.decode(bKey.toString(), "UTF-8");
  413. }
  414. catch (UnsupportedEncodingException e)
  415. {
  416. throw new Unexpected("URLDecoder does not support UTF-8", e);
  417. }
  418. }
  419. static NameParser n = new NameParser();
  420. static void corbalocT(String ior, OrbFunctional orb)
  421. {
  422. System.out.println(ior);
  423. System.out.println(n.corbaloc(ior, orb));
  424. System.out.println();
  425. }
  426. public static void main(String[] args)
  427. {
  428. try
  429. {
  430. OrbFunctional orb = (OrbFunctional) ORB.init(args, null);
  431. corbalocT("corbaloc:iiop:1.3@155axyz.com/Prod/aTradingService", orb);
  432. corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
  433. corbalocT("corbaloc:iiop:355cxyz.com/Prod/cTradingService", orb);
  434. corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb);
  435. corbalocT("corbaloc:iiop:355cxyz.com:7777/Prod/cTradingService", orb);
  436. corbalocT("corbaloc::556xyz.com:80/Dev/NameService", orb);
  437. corbalocT("corbaloc:iiop:1.2@host1:3076/0", orb);
  438. corbalocT("corbaloc:rir:/NameService", orb);
  439. corbalocT("corbaloc:rir:/", orb);
  440. corbalocT("corbaloc:rir:", orb);
  441. corbalocT("corbaloc:rir:/NameService", orb);
  442. corbalocT("corbaloc:rir:/", orb);
  443. corbalocT("corbaloc:rir:", orb);
  444. corbalocT("corbaloc::555xyz.com,:556xyz.com:80/Dev/NameService", orb);
  445. }
  446. catch (BAD_PARAM e)
  447. {
  448. e.printStackTrace(System.out);
  449. }
  450. }
  451. }