Connected_objects.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Connected_objects.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 java.util.Iterator;
  33. import java.util.Map;
  34. import java.util.Set;
  35. import java.util.TreeMap;
  36. /**
  37. * The repository of objects, that have been connected to the
  38. * {@link FunctionalORB} by the method
  39. * {@link ORB.connect(org.omg.CORBA.Object)}.
  40. *
  41. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  42. */
  43. public class Connected_objects
  44. {
  45. /**
  46. * The reference data about the connected object.
  47. */
  48. public class cObject
  49. {
  50. /**
  51. * Create an initialised instance.
  52. */
  53. cObject(org.omg.CORBA.Object _object, int _port, byte[] _key,
  54. java.lang.Object an_identity
  55. )
  56. {
  57. object = _object;
  58. port = _port;
  59. key = _key;
  60. identity = an_identity;
  61. }
  62. /**
  63. * The object.
  64. */
  65. public final org.omg.CORBA.Object object;
  66. /**
  67. * The port on that the object is connected.
  68. */
  69. public final int port;
  70. /**
  71. * The object key.
  72. */
  73. public final byte[] key;
  74. /**
  75. * The shared serving identity (usually POA) or null if no such
  76. * applicable.
  77. */
  78. public final java.lang.Object identity;
  79. }
  80. /**
  81. * The free number to give for the next instance.
  82. * This field is incremented each time the
  83. * new collection of the connected objects is created.
  84. * Each collection has its own unique instance number.
  85. */
  86. private static long free_object_number;
  87. /**
  88. * The map of the all connected objects, maps the object key to the
  89. * object.
  90. */
  91. private Map objects = new TreeMap(new ByteArrayComparator());
  92. /**
  93. * Get the record of the stored object.
  94. *
  95. * @param stored_object the stored object
  96. *
  97. * @return the record about the stored object, null if
  98. * this object is not stored here.
  99. */
  100. public cObject getKey(org.omg.CORBA.Object stored_object)
  101. {
  102. synchronized (objects)
  103. {
  104. Map.Entry item;
  105. Iterator iter = objects.entrySet().iterator();
  106. cObject ref;
  107. while (iter.hasNext())
  108. {
  109. item = (Map.Entry) iter.next();
  110. ref = (cObject) item.getValue();
  111. if (stored_object.equals(ref.object) ||
  112. stored_object._is_equivalent(ref.object)
  113. )
  114. return ref;
  115. }
  116. }
  117. return null;
  118. }
  119. /**
  120. * Add the new object to the repository. The object key is
  121. * generated automatically.
  122. *
  123. * @param object the object to add.
  124. * @param port on that the ORB will be listening to the remote
  125. * invocations.
  126. *
  127. * @return the newly created object record.
  128. */
  129. public cObject add(org.omg.CORBA.Object object, int port)
  130. {
  131. return add(generateObjectKey(object), object, port, null);
  132. }
  133. /**
  134. * Add the new object to the repository.
  135. *
  136. * @param key the object key.
  137. * @param object the object to add.
  138. * @param port the port, on that the ORB will be listening on the
  139. * remote invocations.
  140. */
  141. public cObject add(byte[] key, org.omg.CORBA.Object object, int port,
  142. java.lang.Object identity
  143. )
  144. {
  145. cObject rec = new cObject(object, port, key, identity);
  146. synchronized (objects)
  147. {
  148. objects.put(key, rec);
  149. }
  150. return rec;
  151. }
  152. /**
  153. * Get the stored object.
  154. *
  155. * @param key the key (in the byte array form).
  156. *
  157. * @return the matching object, null if none is matching.
  158. */
  159. public cObject get(byte[] key)
  160. {
  161. synchronized (objects)
  162. {
  163. return (cObject) objects.get(key);
  164. }
  165. }
  166. /**
  167. * Get the map entry set.
  168. */
  169. public Set entrySet()
  170. {
  171. return objects.entrySet();
  172. }
  173. /**
  174. * Remove the given object.
  175. *
  176. * @param object the object to remove.
  177. */
  178. public void remove(org.omg.CORBA.Object object)
  179. {
  180. synchronized (objects)
  181. {
  182. cObject ref = getKey(object);
  183. if (ref != null)
  184. objects.remove(ref.key);
  185. }
  186. }
  187. /**
  188. * Remove the given object, indiciating it by the key.
  189. *
  190. * @param object the object to remove.
  191. */
  192. public void remove(byte[] key)
  193. {
  194. objects.remove(key);
  195. }
  196. /**
  197. * Generate the object key, unique in the currently
  198. * running java virtual machine.
  199. *
  200. * The generated key includes the object class name
  201. * and the absolute instance number.
  202. *
  203. * @return the generated key.
  204. */
  205. protected byte[] generateObjectKey(org.omg.CORBA.Object object)
  206. {
  207. return (object.getClass().getName() + ":" + getFreeInstanceNumber()).getBytes();
  208. }
  209. /**
  210. * Get next free instance number.
  211. */
  212. private static synchronized long getFreeInstanceNumber()
  213. {
  214. long instance_number = free_object_number;
  215. free_object_number++;
  216. return instance_number;
  217. }
  218. /**
  219. * Get the number of the connected objects.
  220. *
  221. * @return the size of the internal map.
  222. */
  223. public int size()
  224. {
  225. return objects.size();
  226. }
  227. }