CollocatedOrbs.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* CollocatedOrbs.java -- Handles collocations
  2. Copyright (C) 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;
  32. import gnu.CORBA.Poa.gnuServantObject;
  33. import java.net.InetAddress;
  34. import java.net.UnknownHostException;
  35. import java.util.ArrayList;
  36. /**
  37. * This class provides support for the direct method invocations without
  38. * involving the network in the case when both ORBs run on the same java
  39. * virtual machine. Special attention is only needed when call is made
  40. * between two independent ORBs, instantiated via ORB.init. The call to the
  41. * object, obtained via IOR reference from the ORB where it was locally
  42. * connected is always local anyway.
  43. *
  44. * For security reasons it may be sensible to keep this class and all support
  45. * package private.
  46. *
  47. * @author Audrius Meskauskas
  48. */
  49. class CollocatedOrbs
  50. {
  51. /**
  52. * This field is used in automated Classpath specific testing to disable
  53. * the direct calls.
  54. */
  55. static boolean DIRECT_CALLS_ALLOWED = true;
  56. /**
  57. * Containts the references of the all running GNU Classpath ORBs in the
  58. * local virtual machine. GNU Classpath ORBs register themselves here when
  59. * created and unregister when either ORB.destroy is called or in the
  60. * finalizer.
  61. */
  62. private static ArrayList orbs = new ArrayList();
  63. /**
  64. * The address of the local host.
  65. */
  66. static String localHost;
  67. static
  68. {
  69. try
  70. {
  71. localHost = InetAddress.getLocalHost().getHostAddress();
  72. }
  73. catch (UnknownHostException ex)
  74. {
  75. throw new InternalError("Local host is not accessible:" + ex);
  76. }
  77. }
  78. /**
  79. * Register the new ORB
  80. *
  81. * @param orb the orb to register
  82. */
  83. static void registerOrb(OrbFunctional orb)
  84. {
  85. if (DIRECT_CALLS_ALLOWED)
  86. synchronized (orbs)
  87. {
  88. assert ! orbs.contains(orb);
  89. orbs.add(orb);
  90. }
  91. }
  92. /**
  93. * Unregister the ORB. The ORB will no longer be reacheable locally but may
  94. * be reacheable via network as if it would be remote.
  95. *
  96. * @param orb the orb to unregister
  97. */
  98. static void unregisterOrb(OrbFunctional orb)
  99. {
  100. if (DIRECT_CALLS_ALLOWED)
  101. synchronized (orbs)
  102. {
  103. assert orbs.contains(orb);
  104. orbs.remove(orb);
  105. }
  106. }
  107. /**
  108. * Search the possibly local object. If the IOR is not local or none of the
  109. * found ORBs of this virtual machine knows about it, null is returned.
  110. *
  111. * @param ior the IOR to search
  112. * @return the found local CORBA object or null in not found.
  113. */
  114. static org.omg.CORBA.Object searchLocalObject(IOR ior)
  115. {
  116. if (! DIRECT_CALLS_ALLOWED && ! ior.Internet.host.equals(localHost))
  117. return null;
  118. synchronized (orbs)
  119. {
  120. OrbFunctional orb;
  121. org.omg.CORBA.Object object;
  122. for (int i = 0; i < orbs.size(); i++)
  123. {
  124. orb = (OrbFunctional) orbs.get(i);
  125. object = orb.find_connected_object(ior.key, ior.Internet.port);
  126. if (object != null)
  127. {
  128. if (object instanceof SafeForDirectCalls)
  129. {
  130. return object;
  131. }
  132. else if (object instanceof gnuServantObject)
  133. {
  134. return object;
  135. }
  136. }
  137. }
  138. }
  139. return null;
  140. }
  141. }