gnuIcCurrent.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* gnuIcCurrent.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.Interceptor;
  32. import gnu.CORBA.CDR.BufferedCdrOutput;
  33. import gnu.CORBA.Poa.ORB_1_4;
  34. import org.omg.CORBA.Any;
  35. import org.omg.CORBA.BAD_INV_ORDER;
  36. import org.omg.CORBA.TCKind;
  37. import org.omg.CORBA.portable.InputStream;
  38. import org.omg.CORBA.portable.ObjectImpl;
  39. import org.omg.PortableInterceptor.Current;
  40. import org.omg.PortableInterceptor.CurrentHelper;
  41. import org.omg.PortableInterceptor.InvalidSlot;
  42. import java.util.Hashtable;
  43. import java.util.Iterator;
  44. import java.util.Map;
  45. /**
  46. * Supports the "Interceptor current" concept, providing the slot value
  47. * information for the current thread. When making the invocation, this
  48. * information is copied to the Current, returned by ClientRequestInfo.
  49. *
  50. * There is only one instance of this class per ORB. It maintains a thread to
  51. * information map.
  52. *
  53. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  54. */
  55. public class gnuIcCurrent extends ObjectImpl implements Current
  56. {
  57. /**
  58. * Use serialVersionUID for interoperability.
  59. */
  60. private static final long serialVersionUID = 1;
  61. /**
  62. * The ORB, controllin this Current. It provides data about the required size
  63. * of the slot array.
  64. */
  65. final ORB_1_4 orb;
  66. /**
  67. * The table, mapping threads to records.
  68. */
  69. private Hashtable threads = new Hashtable();
  70. /**
  71. * An empty array when no slots are defined, computed once.
  72. */
  73. static final Any[] NO_SLOTS = new Any[ 0 ];
  74. /**
  75. * Create the IC current.
  76. */
  77. public gnuIcCurrent(ORB_1_4 an_orb)
  78. {
  79. orb = an_orb;
  80. }
  81. /**
  82. * Get the array of POA current repository ids.
  83. *
  84. * @return a single member array, containing value, returned by the
  85. * {@link CurrentHelper#id}, normally
  86. * "IDL:omg.org/PortableInterceptor/Current:1.0".
  87. */
  88. public String[] _ids()
  89. {
  90. return new String[] { CurrentHelper.id() };
  91. }
  92. /**
  93. * Add the entry to the map.
  94. */
  95. public void put(Thread t, Any[] record)
  96. {
  97. synchronized (threads)
  98. {
  99. threads.put(t, record);
  100. // Remove non-running threads, avoiding memory leak.
  101. if (threads.size() > 12)
  102. {
  103. Iterator it = threads.entrySet().iterator();
  104. while (it.hasNext())
  105. {
  106. Map.Entry e = (Map.Entry) it.next();
  107. Thread tx = (Thread) e.getKey();
  108. if (!tx.isAlive())
  109. {
  110. it.remove();
  111. }
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Check if this thread is registered.
  118. */
  119. public boolean has(Thread t)
  120. {
  121. synchronized (threads)
  122. {
  123. return threads.containsKey(t);
  124. }
  125. }
  126. /**
  127. * Remove the entry from the map.
  128. */
  129. public void remove(Thread t)
  130. {
  131. synchronized (threads)
  132. {
  133. threads.remove(t);
  134. }
  135. }
  136. /**
  137. * Get array of all slots, as it is applicable for the current thread. If the
  138. * slots were not previously allocated, they are allocated during this call.
  139. */
  140. Any[] get_slots()
  141. {
  142. Any[] r;
  143. synchronized (threads)
  144. {
  145. r = (Any[]) threads.get(Thread.currentThread());
  146. if (r == null)
  147. {
  148. r = new Any[ orb.icSlotSize ];
  149. for (int i = 0; i < r.length; i++)
  150. {
  151. Any a = orb.create_any();
  152. a.type(orb.get_primitive_tc(TCKind.tk_null));
  153. r [ i ] = a;
  154. }
  155. put(Thread.currentThread(), r);
  156. }
  157. return r;
  158. }
  159. }
  160. /**
  161. * Get copu array of all slots, as it is applicable for the current thread. If
  162. * the slots were not previously allocated, they are allocated during this
  163. * call.
  164. */
  165. public Any[] clone_slots()
  166. {
  167. if (orb.icSlotSize == 0)
  168. {
  169. return NO_SLOTS;
  170. }
  171. else
  172. {
  173. Any[] r = get_slots();
  174. Any[] copy = new Any[ r.length ];
  175. BufferedCdrOutput buf = new BufferedCdrOutput();
  176. buf.setOrb(orb);
  177. for (int i = 0; i < copy.length; i++)
  178. {
  179. r [ i ].write_value(buf);
  180. }
  181. InputStream input = buf.create_input_stream();
  182. for (int i = 0; i < copy.length; i++)
  183. {
  184. copy [ i ] = orb.create_any();
  185. copy [ i ].read_value(input, r [ i ].type());
  186. }
  187. return copy;
  188. }
  189. }
  190. /**
  191. * Get value for the slot with the given id. If the array of Currents has not
  192. * been yet allocated for the current thread, it is allocated during the
  193. * invocation of this method.
  194. */
  195. public Any get_slot(int slot_id) throws InvalidSlot, BAD_INV_ORDER
  196. {
  197. try
  198. {
  199. return get_slots() [ slot_id ];
  200. }
  201. catch (ArrayIndexOutOfBoundsException e)
  202. {
  203. throw new InvalidSlot("Slot " + slot_id);
  204. }
  205. }
  206. /**
  207. * Set value for the slot with the given id. If the array of Currents has not
  208. * been yet allocated for the current thread, it is allocated during the
  209. * invocation of this method.
  210. */
  211. public void set_slot(int slot_id, Any data)
  212. throws InvalidSlot, BAD_INV_ORDER
  213. {
  214. try
  215. {
  216. get_slots() [ slot_id ] = data;
  217. }
  218. catch (ArrayIndexOutOfBoundsException e)
  219. {
  220. throw new InvalidSlot("Slot " + slot_id);
  221. }
  222. }
  223. }