1. /*
  2. * @(#)NameClassPair.java 1.10 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming;
  8. /**
  9. * This class represents the object name and class name pair of a binding
  10. * found in a context.
  11. *<p>
  12. * A context consists of name-to-object bindings.
  13. * The NameClassPair class represents the name and the
  14. * class of the bound object. It consists
  15. * of a name and a string representing the
  16. * package-qualified class name.
  17. *<p>
  18. * Use subclassing for naming systems that generate contents of
  19. * a name/class pair dynamically.
  20. *<p>
  21. * A NameClassPair instance is not synchronized against concurrent
  22. * access by multiple threads. Threads that need to access a NameClassPair
  23. * concurrently should synchronize amongst themselves and provide
  24. * the necessary locking.
  25. *
  26. * @author Rosanna Lee
  27. * @author Scott Seligman
  28. * @version 1.10 03/12/19
  29. *
  30. * @see Context#list
  31. * @since 1.3
  32. */
  33. /*
  34. * <p>
  35. * The serialized form of a NameClassPair object consists of the name (a
  36. * String), class name (a String), and isRelative flag (a boolean).
  37. */
  38. public class NameClassPair implements java.io.Serializable {
  39. /**
  40. * Contains the name of this NameClassPair.
  41. * It is initialized by the constructor and can be updated using
  42. * <tt>setName()</tt>.
  43. * @serial
  44. * @see #getName
  45. * @see #setName
  46. */
  47. private String name;
  48. /**
  49. *Contains the class name contained in this NameClassPair.
  50. * It is initialized by the constructor and can be updated using
  51. * <tt>setClassName()</tt>.
  52. * @serial
  53. * @see #getClassName
  54. * @see #setClassName
  55. */
  56. private String className;
  57. /**
  58. * Contains the full name of this NameClassPair within its
  59. * own namespace.
  60. * It is initialized using <tt>setNameInNamespace()</tt>
  61. * @serial
  62. * @see #getNameInNamespace
  63. * @see #setNameInNamespace
  64. */
  65. private String fullName = null;
  66. /**
  67. * Records whether the name of this <tt>NameClassPair</tt>
  68. * is relative to the target context.
  69. * It is initialized by the constructor and can be updated using
  70. * <tt>setRelative()</tt>.
  71. * @serial
  72. * @see #isRelative
  73. * @see #setRelative
  74. * @see #getName
  75. * @see #setName
  76. */
  77. private boolean isRel = true;
  78. /**
  79. * Constructs an instance of a NameClassPair given its
  80. * name and class name.
  81. *
  82. * @param name The non-null name of the object. It is relative
  83. * to the <em>target context</em> (which is
  84. * named by the first parameter of the <code>list()</code> method)
  85. * @param className The possibly null class name of the object
  86. * bound to name. It is null if the object bound is null.
  87. * @see #getClassName
  88. * @see #setClassName
  89. * @see #getName
  90. * @see #setName
  91. */
  92. public NameClassPair(String name, String className) {
  93. this.name = name;
  94. this.className = className;
  95. }
  96. /**
  97. * Constructs an instance of a NameClassPair given its
  98. * name, class name, and whether it is relative to the listing context.
  99. *
  100. * @param name The non-null name of the object.
  101. * @param className The possibly null class name of the object
  102. * bound to name. It is null if the object bound is null.
  103. * @param isRelative true if <code>name</code> is a name relative
  104. * to the target context (which is named by the first parameter
  105. * of the <code>list()</code> method); false if <code>name</code>
  106. * is a URL string.
  107. * @see #getClassName
  108. * @see #setClassName
  109. * @see #getName
  110. * @see #setName
  111. * @see #isRelative
  112. * @see #setRelative
  113. */
  114. public NameClassPair(String name, String className, boolean isRelative) {
  115. this.name = name;
  116. this.className = className;
  117. this.isRel = isRelative;
  118. }
  119. /**
  120. * Retrieves the class name of the object bound to the name of this binding.
  121. * If a reference or some other indirect information is bound,
  122. * retrieves the class name of the eventual object that
  123. * will be returned by <tt>Binding.getObject()</tt>.
  124. *
  125. * @return The possibly null class name of object bound.
  126. * It is null if the object bound is null.
  127. * @see Binding#getObject
  128. * @see Binding#getClassName
  129. * @see #setClassName
  130. */
  131. public String getClassName() {
  132. return className;
  133. }
  134. /**
  135. * Retrieves the name of this binding.
  136. * If <tt>isRelative()</tt> is true, this name is relative to the
  137. * target context (which is named by the first parameter of the
  138. * <tt>list()</tt>).
  139. * If <tt>isRelative()</tt> is false, this name is a URL string.
  140. *
  141. * @return The non-null name of this binding.
  142. * @see #isRelative
  143. * @see #setName
  144. */
  145. public String getName() {
  146. return name;
  147. }
  148. /**
  149. * Sets the name of this binding.
  150. *
  151. * @param name the non-null string to use as the name.
  152. * @see #getName
  153. * @see #setRelative
  154. */
  155. public void setName(String name) {
  156. this.name = name;
  157. }
  158. /**
  159. * Sets the class name of this binding.
  160. *
  161. * @param name the possibly null string to use as the class name.
  162. * If null, <tt>Binding.getClassName()</tt> will return
  163. * the actual class name of the object in the binding.
  164. * The class name will be null if the object bound is null.
  165. * @see #getClassName
  166. * @see Binding#getClassName
  167. */
  168. public void setClassName(String name) {
  169. this.className = name;
  170. }
  171. /**
  172. * Determines whether the name of this binding is
  173. * relative to the target context (which is named by
  174. * the first parameter of the <code>list()</code> method).
  175. *
  176. * @return true if the name of this binding is relative to the
  177. * target context;
  178. * false if the name of this binding is a URL string.
  179. * @see #setRelative
  180. * @see #getName
  181. */
  182. public boolean isRelative() {
  183. return isRel;
  184. }
  185. /**
  186. * Sets whether the name of this binding is relative to the target
  187. * context (which is named by the first parameter of the <code>list()</code>
  188. * method).
  189. *
  190. * @param r If true, the name of binding is relative to the target context;
  191. * if false, the name of binding is a URL string.
  192. * @see #isRelative
  193. * @see #setName
  194. */
  195. public void setRelative(boolean r) {
  196. isRel = r;
  197. }
  198. /**
  199. * Retrieves the full name of this binding.
  200. * The full name is the absolute name of this binding within
  201. * its own namespace. See {@link Context#getNameInNamespace()}.
  202. * <p>
  203. *
  204. * In naming systems for which the notion of full name does not
  205. * apply to this binding an <tt>UnsupportedOperationException</tt>
  206. * is thrown.
  207. * This exception is also thrown when a service provider written before
  208. * the introduction of the method is in use.
  209. * <p>
  210. * The string returned by this method is not a JNDI composite name and
  211. * should not be passed directly to context methods.
  212. *
  213. * @return The full name of this binding.
  214. * @throws UnsupportedOperationException if the notion of full name
  215. * does not apply to this binding in the naming system.
  216. * @since 1.5
  217. * @see #setNameInNamespace
  218. * @see #getName
  219. */
  220. public String getNameInNamespace() {
  221. if (fullName == null) {
  222. throw new UnsupportedOperationException();
  223. }
  224. return fullName;
  225. }
  226. /**
  227. * Sets the full name of this binding.
  228. * This method must be called to set the full name whenever a
  229. * <tt>NameClassPair</tt> is created and a full name is
  230. * applicable to this binding.
  231. * <p>
  232. * Setting the full name to null, or not setting it at all, will
  233. * cause <tt>getNameInNamespace()</tt> to throw an exception.
  234. *
  235. * @param fullName The full name to use.
  236. * @since 1.5
  237. * @see #getNameInNamespace
  238. * @see #setName
  239. */
  240. public void setNameInNamespace(String fullName) {
  241. this.fullName = fullName;
  242. }
  243. /**
  244. * Generates the string representation of this name/class pair.
  245. * The string representation consists of the name and class name separated
  246. * by a colon (':').
  247. * The contents of this string is useful
  248. * for debugging and is not meant to be interpreted programmatically.
  249. *
  250. * @return The string representation of this name/class pair.
  251. */
  252. public String toString() {
  253. return (isRelative() ? "" : "(not relative)") + getName() + ": " +
  254. getClassName();
  255. }
  256. /**
  257. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  258. */
  259. private static final long serialVersionUID = 5620776610160863339L;
  260. }