Reference.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Reference.java --
  2. Copyright (C) 2000, 2001, 2005, 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 javax.naming;
  32. import java.io.Serializable;
  33. import java.util.Enumeration;
  34. import java.util.Vector;
  35. /**
  36. * This class represents a reference to an object that is located outside of the
  37. * naming/directory system.
  38. *
  39. * @see Referenceable
  40. *
  41. * @author Tom Tromey (tromey@redhat.com)
  42. */
  43. public class Reference implements Cloneable, Serializable
  44. {
  45. private static final long serialVersionUID = - 1673475790065791735L;
  46. /**
  47. * The list of addresses, stored in this reference. The object may be
  48. * have by several different addresses.
  49. */
  50. protected Vector<RefAddr> addrs;
  51. /**
  52. * The name of the class factory to create an instance of the object,
  53. * referenced by this reference.
  54. */
  55. protected String classFactory;
  56. /**
  57. * The location, from where the class factory should be loaded.
  58. */
  59. protected String classFactoryLocation;
  60. /**
  61. * The name of the class of the object, to that this reference refers.
  62. */
  63. protected String className;
  64. /**
  65. * Create a new reference that is referencting to the object of the
  66. * specified class.
  67. */
  68. public Reference (String className)
  69. {
  70. this.className = className;
  71. addrs = new Vector<RefAddr> ();
  72. }
  73. /**
  74. * Create a new reference that is referencing to the object of the
  75. * specified class with the given address.
  76. */
  77. public Reference (String className, RefAddr addr)
  78. {
  79. this.className = className;
  80. addrs = new Vector<RefAddr> ();
  81. addrs.add (addr);
  82. }
  83. /**
  84. * Create a new reference that is referencing to the object of the
  85. * specified class, specifying the class and location of the factory that
  86. * produces these objects.
  87. *
  88. * @param className the object class name
  89. * @param factoryClassName the object factory class name
  90. * @param factoryLocation the object factory location
  91. */
  92. public Reference (String className, String factoryClassName,
  93. String factoryLocation)
  94. {
  95. this.className = className;
  96. this.classFactory = factoryClassName;
  97. this.classFactoryLocation = factoryLocation;
  98. addrs = new Vector<RefAddr> ();
  99. }
  100. /**
  101. * Create a new reference that is referencing to the object of the
  102. * specified class, specifying the class and location of the factory that
  103. * produces these objects and also the address of this object.
  104. *
  105. * @param className the object class name
  106. * @param addr the address of the object
  107. * @param factoryClassName the object factory class name
  108. * @param factoryLocation the object factory location
  109. */
  110. public Reference (String className, RefAddr addr,
  111. String factoryClassName, String factoryLocation)
  112. {
  113. this.className = className;
  114. this.classFactory = factoryClassName;
  115. this.classFactoryLocation = factoryLocation;
  116. addrs = new Vector<RefAddr> ();
  117. addrs.add (addr);
  118. }
  119. /**
  120. * Add the new address for this object at the given position of the
  121. * address list.
  122. */
  123. public void add (int posn, RefAddr addr)
  124. {
  125. addrs.add (posn, addr);
  126. }
  127. /**
  128. * Appends the new object address to the end of the address list.
  129. */
  130. public void add (RefAddr addr)
  131. {
  132. addrs.add (addr);
  133. }
  134. /**
  135. * Removes all defined addresses of the object.
  136. */
  137. public void clear ()
  138. {
  139. addrs.clear ();
  140. }
  141. public Object clone ()
  142. {
  143. Reference r = new Reference (className, classFactory,
  144. classFactoryLocation);
  145. r.addrs = (Vector<RefAddr>) addrs.clone ();
  146. return r;
  147. }
  148. // Convenience function.
  149. private boolean equals (String a, String b)
  150. {
  151. return (a == null) ? (b == null) : a.equals (b);
  152. }
  153. /**
  154. * Compares two addresses for equality, by value.
  155. */
  156. public boolean equals (Object obj)
  157. {
  158. if (! (obj instanceof Reference))
  159. return false;
  160. Reference r = (Reference) obj;
  161. return (equals (classFactory, r.classFactory)
  162. && equals (classFactoryLocation, r.classFactoryLocation)
  163. && equals (className, r.className)
  164. && addrs.equals (r.addrs));
  165. }
  166. /**
  167. * Get the address of this object at the given position.
  168. */
  169. public RefAddr get (int posn)
  170. {
  171. return addrs.get (posn);
  172. }
  173. /**
  174. * Get the given type of address for this object.
  175. *
  176. * @param addrType the needed type of address
  177. *
  178. * @return the address of this object, having the specified type. If there
  179. * is no address of such type, null is returned.
  180. */
  181. public RefAddr get (String addrType)
  182. {
  183. for (int i = 0; i < addrs.size (); ++i)
  184. {
  185. RefAddr r = addrs.get (i);
  186. if (addrType.equals (r.getType ()))
  187. return r;
  188. }
  189. return null;
  190. }
  191. /**
  192. * Get the enumeration over all defined addresses of the object.
  193. */
  194. public Enumeration<RefAddr> getAll ()
  195. {
  196. return addrs.elements ();
  197. }
  198. /**
  199. * Get the name of the class of the referenced object.
  200. *
  201. * @see #className
  202. */
  203. public String getClassName ()
  204. {
  205. return className;
  206. }
  207. /**
  208. * Get the location of the factory class of the referenced object.
  209. *
  210. * @see #classFactoryLocation
  211. */
  212. public String getFactoryClassLocation ()
  213. {
  214. return classFactoryLocation;
  215. }
  216. /**
  217. * Get the name of the factory class of the referenced object
  218. *
  219. * @see #classFactory
  220. */
  221. public String getFactoryClassName ()
  222. {
  223. return classFactory;
  224. }
  225. /**
  226. * Get the hashcode of this reference.
  227. *
  228. * @return the sum of the hash codes of the addresses.
  229. */
  230. public int hashCode ()
  231. {
  232. // The spec says the hash code is the sum of the hash codes of the
  233. // addresses. It does not mention the other fields.
  234. int h = 0;
  235. for (int i = 0; i < addrs.size (); ++i)
  236. h += addrs.get (i).hashCode ();
  237. return h;
  238. }
  239. /**
  240. * Remove the address at the given position.
  241. *
  242. * @param posn the position of the address to remove
  243. *
  244. * @return the removed address
  245. */
  246. public Object remove (int posn)
  247. {
  248. return addrs.remove (posn);
  249. }
  250. /**
  251. * Return the number of the defined addresses.
  252. */
  253. public int size ()
  254. {
  255. return addrs.size ();
  256. }
  257. /**
  258. * Return the string representation.
  259. */
  260. public String toString ()
  261. {
  262. String x = getClass ().toString () + "[";
  263. for (int i = 0; i < addrs.size (); ++i)
  264. {
  265. if (i > 0)
  266. x += ",";
  267. x += addrs.get (i).toString ();
  268. }
  269. return x + "]";
  270. }
  271. }