gnuPOAManager.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* gnuPOAManager.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.Poa;
  32. import org.omg.CORBA.BAD_INV_ORDER;
  33. import org.omg.CORBA.LocalObject;
  34. import org.omg.PortableInterceptor.NON_EXISTENT;
  35. import org.omg.PortableServer.POAManager;
  36. import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
  37. import org.omg.PortableServer.POAManagerPackage.State;
  38. import java.util.HashSet;
  39. import java.util.Iterator;
  40. /**
  41. * The implementation of the POA manager. The manager is a controlled
  42. * switch that can change its states in response to the method calls
  43. * and throw exceptions if the requested change is invalid. It is possible
  44. * to check the state this switch. It does not do anything else.
  45. *
  46. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  47. */
  48. public class gnuPOAManager
  49. extends LocalObject
  50. implements POAManager
  51. {
  52. /**
  53. * Use serialVersionUID for interoperability.
  54. */
  55. private static final long serialVersionUID = 1;
  56. /**
  57. * The POAs, controlled by this manager.
  58. */
  59. private HashSet POAs = new HashSet();
  60. /**
  61. * The state of the manager. The newly created manager is always
  62. * in the holding state.
  63. */
  64. State state = State.HOLDING;
  65. /**
  66. * Get the state of the POA manager.
  67. */
  68. public State get_state()
  69. {
  70. return state;
  71. }
  72. /**
  73. * Turns the associated POAs into active state, allowing them to receive
  74. * and process requests.
  75. *
  76. * @throws AdapterInactive if the POAs are in the inactive state.
  77. * If once inactivated, the POA cannot be activated again. This
  78. * method can only be called to leave the holding or discarding state.
  79. */
  80. public void activate()
  81. throws AdapterInactive
  82. {
  83. if (state != State.INACTIVE)
  84. state = State.ACTIVE;
  85. else
  86. throw new AdapterInactive();
  87. notifyInterceptors(state.value());
  88. }
  89. /**
  90. * Turns the associated POAs into holding state. In this state, the POAs
  91. * queue incoming requests but do not process them.
  92. *
  93. * @param wait_for_completion if true, the method call suspends the current
  94. * thread till POAs complete the requests they are currently processing. If
  95. * false, the method returns immediately.
  96. * @throws AdapterInactive if the POAs are in the inactive state.
  97. */
  98. public void hold_requests(boolean wait_for_completion)
  99. throws AdapterInactive
  100. {
  101. if (state != State.INACTIVE)
  102. state = State.HOLDING;
  103. else
  104. throw new AdapterInactive();
  105. notifyInterceptors(state.value());
  106. if (wait_for_completion)
  107. waitForIdle();
  108. }
  109. /**
  110. *
  111. * Turns the asociated POAs into inactive state. The POAs in the incative
  112. * state will reject new requests. If the POA is once inactivated, it
  113. * cannot be activated again. The operation is used when
  114. * the associated POAs are to be shut down.
  115. *
  116. * @param etherealize_objects if true, the servant managers of the
  117. * associated POAs, having RETAIN and USE_SERVANT_MANAGER policies,
  118. * will receive a call of {@link ServantActivatorOperations#etherealize}.
  119. *
  120. * @param wait_for_completion if true, the method call suspends the current
  121. * thread till POAs complete the requests they are currently processing. If
  122. * false, the method returns immediately.
  123. *
  124. * @throws AdapterInactive if the POAs are already in the inactive state.
  125. *
  126. * @see POAOperations#destroy
  127. */
  128. public void deactivate(boolean etherealize_objects,
  129. boolean wait_for_completion
  130. )
  131. throws AdapterInactive
  132. {
  133. if (state == State.INACTIVE)
  134. throw new AdapterInactive("Repetetive inactivation");
  135. state = State.INACTIVE;
  136. notifyInterceptors(state.value());
  137. if (wait_for_completion)
  138. waitForIdle();
  139. Iterator iter = POAs.iterator();
  140. while (iter.hasNext())
  141. {
  142. gnuPOA poa = (gnuPOA) iter.next();
  143. // If the servant activator is non null, this means it has been
  144. // set - hence the policies are appropriate.
  145. if (poa.servant_activator != null)
  146. poa.etherealizeAll();
  147. }
  148. }
  149. /**
  150. * Turns the associated POAs into discaring state. In this state, the POAs
  151. * discard the incoming requests. This mode is used in situations when
  152. * the server is flooded with requests. The client receives remote exception
  153. * ({@link org.omg.CORBA.TRANSIENT}, minor code 1).
  154. *
  155. * @param wait_for_completion if true, the method call suspends the current
  156. * thread till POAs complete the requests they are currently processing. If
  157. * false, the method returns immediately.
  158. * @throws AdapterInactive if the POAs are in the inactive state.
  159. */
  160. public void discard_requests(boolean wait_for_completion)
  161. throws AdapterInactive
  162. {
  163. if (state != State.INACTIVE)
  164. state = State.DISCARDING;
  165. else
  166. throw new AdapterInactive();
  167. notifyInterceptors(state.value());
  168. if (wait_for_completion)
  169. waitForIdle();
  170. }
  171. /**
  172. * Suspend the current thread while at least one of the associated POA is
  173. * actively processing some requests. The method assumes that the POAs
  174. * are not accepting the <i>new</i> requests due manager state.
  175. *
  176. * @throws BAD_INV_ORDER if the POAs are in the active state.
  177. */
  178. public void waitForIdle()
  179. {
  180. if (state == State.ACTIVE)
  181. throw new BAD_INV_ORDER("The state is active");
  182. gnuPOA poa;
  183. Iterator iter = POAs.iterator();
  184. while (iter.hasNext())
  185. {
  186. poa = (gnuPOA) iter.next();
  187. poa.waitWhileRunning();
  188. }
  189. }
  190. /**
  191. * Add the POA that will be controlled by this manager.
  192. *
  193. * @param poa the POA.
  194. */
  195. public void addPoa(gnuPOA poa)
  196. {
  197. POAs.add(poa);
  198. }
  199. /**
  200. * Remove the POA, releasing it from the control of this manager.
  201. * Called in POA finaliser.
  202. *
  203. * @param poa the POA to remove.
  204. */
  205. public void removePOA(gnuPOA poa)
  206. {
  207. POAs.remove(poa);
  208. }
  209. /**
  210. * This method is called when POA is destryed. The interceptors are
  211. * notified.
  212. */
  213. public void poaDestroyed(gnuPOA poa)
  214. {
  215. notifyInterceptors(NON_EXISTENT.value);
  216. }
  217. /**
  218. * Notify CORBA 3.0 interceptors about the status change.
  219. */
  220. public synchronized void notifyInterceptors(int new_state)
  221. {
  222. gnuPOA poa;
  223. Iterator iter = POAs.iterator();
  224. // The System.identityHashCode is also called in gnuIorInfo.
  225. while (iter.hasNext())
  226. {
  227. poa = (gnuPOA) iter.next();
  228. if (poa.m_orb.iIor != null)
  229. {
  230. poa.m_orb.iIor.adapter_manager_state_changed(
  231. System.identityHashCode(this), (short) new_state);
  232. }
  233. }
  234. }
  235. }