OrbFocused.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* OrbFocused.java --
  2. Copyright (C) 2005 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;
  32. import gnu.CORBA.Poa.ORB_1_4;
  33. import org.omg.CORBA.BAD_INV_ORDER;
  34. import org.omg.CORBA.BAD_OPERATION;
  35. import org.omg.CORBA.BAD_PARAM;
  36. import org.omg.CORBA.CompletionStatus;
  37. import org.omg.CORBA.NO_RESOURCES;
  38. import org.omg.CORBA.portable.InvokeHandler;
  39. import java.applet.Applet;
  40. import java.net.ServerSocket;
  41. import java.util.Iterator;
  42. import java.util.Properties;
  43. import java.util.Random;
  44. import java.util.StringTokenizer;
  45. /**
  46. * This class implements the ORB that uses a single port or the restricted port
  47. * range for all its objects. It is required to for use together with various
  48. * firewalls that does not allow opening multiple randomly selected ports, as
  49. * the defauld CORBA implementation used to do. The firewal must be configured
  50. * to allow CORBA to work on one fixed port or (for better performance) on a
  51. * small fixed range of ports. This does not restrict the maximal number of the
  52. * connected objects as the objects can share the same port.
  53. *
  54. * The used port or the used port range can be specified via property
  55. * gnu.CORBA.ListenerPort. The value of this property is a single port or range
  56. * of ports, boundary values (inclusive) being separeted by dash (for instance,
  57. * "1245-1250").
  58. *
  59. * It is possible to instantiate multiple instances of the focused ORBs and
  60. * combine them with the ordinary ORBs. If you instantiate several instances of
  61. * the focused ORBs on the same host, they used port sets should not overlap.
  62. *
  63. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  64. */
  65. public class OrbFocused
  66. extends ORB_1_4
  67. {
  68. /**
  69. * The name of the fixed port range property. The presence of this property
  70. * indicates that the default focused ORB must be used.
  71. */
  72. public static final String LISTENER_PORT = "gnu.CORBA.ListenerPort";
  73. /**
  74. * The start of the range of the available ports, inclusive.
  75. */
  76. int m_ports_from = -1;
  77. /**
  78. * The end of the range of the available ports, inclusive.
  79. */
  80. int m_ports_to = -1;
  81. /**
  82. * The requests to port are served in parallel threads.
  83. */
  84. static final int PARALLEL = 0;
  85. /**
  86. * The requests to port are served in the same thread.
  87. */
  88. static final int SEQUENTIAL = 1;
  89. /**
  90. * The random number generator to get a random port in the valid range.
  91. */
  92. Random m_random = new Random();
  93. /**
  94. * Parse the "gnu.CORBA.ListenerPort" property and initialize the valid port
  95. * set range.
  96. */
  97. public void setPortRange(String property)
  98. {
  99. int from, to;
  100. try
  101. {
  102. StringTokenizer st = new StringTokenizer(property, " -");
  103. if (st.countTokens() == 1)
  104. from = to = Integer.parseInt(st.nextToken());
  105. else
  106. {
  107. from = Integer.parseInt(st.nextToken());
  108. to = Integer.parseInt(st.nextToken());
  109. m_random = new Random();
  110. }
  111. setPortRange(from, to);
  112. }
  113. catch (Exception ex)
  114. {
  115. throw new BAD_PARAM("Unable to parse port range '" + property + "'");
  116. }
  117. }
  118. /**
  119. * Set the port range.
  120. *
  121. * @param from - start of the port range, inclusive.
  122. * @param to - end of the port range, inclusive.
  123. */
  124. public void setPortRange(int from, int to)
  125. {
  126. if (from < 0 || to < 0 || to < from)
  127. throw new BAD_PARAM("Invalid range");
  128. m_ports_from = from;
  129. m_ports_to = to;
  130. }
  131. /**
  132. * Get the port from the previously specified usage range.
  133. */
  134. int getPortFromRange(int attempt)
  135. {
  136. if (m_ports_from == m_ports_to)
  137. return m_ports_from;
  138. else if (m_ports_to - m_ports_from < RANDOM_PORT_ATTEMPTS)
  139. return m_ports_from + (attempt % (m_ports_to - m_ports_from + 1));
  140. else
  141. return m_random.nextInt(m_ports_to - m_ports_from + 1) + m_ports_from;
  142. }
  143. /**
  144. * Get the shared port server where the new object can be added. This may
  145. * result reusing the existing server or instantiating a new server. If the
  146. * new server is instantiated and the ORB is already running, the server is
  147. * started.
  148. */
  149. protected portServer getPortServer(int type)
  150. {
  151. portServer p;
  152. int n;
  153. if (m_ports_from < m_ports_to)
  154. n = RANDOM_PORT_ATTEMPTS;
  155. else
  156. n = 1;
  157. Ports: for (int a = 0; a < n; a++)
  158. {
  159. int port = getPortFromRange(a);
  160. for (int i = 0; i < portServers.size(); i++)
  161. {
  162. p = (portServer) portServers.get(i);
  163. if (p.s_port == port)
  164. {
  165. return p;
  166. }
  167. }
  168. // The server is not yet instantiated. Instantiate.
  169. try
  170. {
  171. // Check if the port is ok:
  172. ServerSocket s = socketFactory.createServerSocket(port);
  173. s.close();
  174. portServer shared;
  175. switch (type)
  176. {
  177. case PARALLEL:
  178. shared = new portServer(port);
  179. break;
  180. case SEQUENTIAL:
  181. shared = new sharedPortServer(port);
  182. break;
  183. default:
  184. throw new InternalError("Invalid server type " + type);
  185. }
  186. portServers.add(shared);
  187. if (running)
  188. shared.start();
  189. return shared;
  190. }
  191. catch (Exception ex)
  192. {
  193. // Port is taken or probably other problems.
  194. continue Ports;
  195. }
  196. }
  197. throw new NO_RESOURCES("No free port available at " + m_ports_from + "-"
  198. + m_ports_to, Minor.Ports, CompletionStatus.COMPLETED_NO);
  199. }
  200. /**
  201. * Start the ORBs main working cycle (receive invocation - invoke on the local
  202. * object - send response - wait for another invocation).
  203. *
  204. * The method only returns after calling {@link #shutdown(boolean)}.
  205. */
  206. public void run()
  207. {
  208. if (m_ports_from < 0 || m_ports_to < 0)
  209. throw new BAD_INV_ORDER("For " + getClass().getName() + " "
  210. + LISTENER_PORT + " property must be set");
  211. running = true;
  212. // Start all port servers. In the current subclass, the portServers
  213. // collection must be already filled in.
  214. Iterator iter = portServers.iterator();
  215. while (iter.hasNext())
  216. {
  217. portServer subserver = (portServer) iter.next();
  218. if (!subserver.isAlive())
  219. {
  220. // Reuse the current thread for the last portServer.
  221. if (!iter.hasNext())
  222. {
  223. // Discard the iterator.
  224. iter = null;
  225. subserver.run();
  226. return;
  227. }
  228. else
  229. subserver.start();
  230. }
  231. }
  232. }
  233. /**
  234. * Get free port from the allowed range. This method instantiates the port
  235. * server for the returned port.
  236. */
  237. public int getFreePort()
  238. throws BAD_OPERATION
  239. {
  240. portServer s = getPortServer(PARALLEL);
  241. return s.s_port;
  242. }
  243. /**
  244. * Connect the given CORBA object to this ORB, explicitly specifying the
  245. * object key and the identity of the thread (and port), where the object must
  246. * be served. The identity is normally the POA.
  247. *
  248. * The new port server will be started only if there is no one already running
  249. * for the same identity. Otherwise, the task of the existing port server will
  250. * be widened, including duty to serve the given object. All objects,
  251. * connected to a single identity by this method, will process they requests
  252. * subsequently in the same thread. The method is used when the expected
  253. * number of the objects is too large to have a single port and thread per
  254. * object. This method is used by POAs, having a single thread policy.
  255. *
  256. * @param object the object, must implement the {@link InvokeHandler})
  257. * interface.
  258. * @param key the object key, usually used to identify the object from remote
  259. * side.
  260. * @param port the port, where the object must be connected.
  261. *
  262. * @throws BAD_PARAM if the object does not implement the
  263. * {@link InvokeHandler}).
  264. */
  265. public void connect_1_thread(org.omg.CORBA.Object object, byte[] key,
  266. java.lang.Object identity)
  267. {
  268. sharedPortServer shared = (sharedPortServer) identities.get(identity);
  269. if (shared == null)
  270. {
  271. shared = (sharedPortServer) getPortServer(SEQUENTIAL);
  272. identities.put(identity, shared);
  273. if (running)
  274. {
  275. shared.start();
  276. }
  277. }
  278. Connected_objects.cObject ref = connected_objects.add(key, object,
  279. shared.s_port, identity);
  280. IOR ior = createIOR(ref);
  281. prepareObject(object, ior);
  282. }
  283. /**
  284. * In this type of ORB, the service is started by {@link #getPortServer}. The
  285. * method below is not in use and should return without action.
  286. */
  287. public void startService(IOR ior)
  288. {
  289. }
  290. /**
  291. * Set parameters (additionally search for the port range property).
  292. */
  293. protected void set_parameters(Applet applet, Properties props)
  294. {
  295. super.set_parameters(applet, props);
  296. String lp = applet.getParameter(LISTENER_PORT);
  297. if (lp != null)
  298. setPortRange(lp);
  299. }
  300. /**
  301. * Set parameters (additionally search for the port range property).
  302. */
  303. protected void set_parameters(String[] args, Properties props)
  304. {
  305. super.set_parameters(args, props);
  306. String lp = null;
  307. String lpKey = "-" + LISTENER_PORT;
  308. if (args != null)
  309. if (args.length >= 2)
  310. {
  311. for (int i = 0; i < args.length - 1; i++)
  312. if (args[i].equals(lpKey))
  313. lp = args[i + 1];
  314. }
  315. if (lp != null)
  316. setPortRange(lp);
  317. }
  318. /**
  319. * Additionally set the port range property, when applicable.
  320. */
  321. protected void useProperties(Properties props)
  322. {
  323. super.useProperties(props);
  324. String lp = props.getProperty(LISTENER_PORT);
  325. if (lp != null)
  326. setPortRange(lp);
  327. }
  328. }