1. /*
  2. * @(#)NameClassPair.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 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.7 03/01/23
  29. *
  30. * @see Context#list
  31. * @since 1.3
  32. */
  33. /*<p>
  34. * The serialized form of a NameClassPair object consists of the name (a
  35. * String), class name (a String), and isRelative flag (a boolean).
  36. */
  37. public class NameClassPair implements java.io.Serializable {
  38. /**
  39. * Contains the name of this NameClassPair.
  40. * It is initialized by the constructor and can be updated using
  41. * <tt>setName()</tt>.
  42. * @serial
  43. * @see #getName
  44. * @see #setName
  45. */
  46. private String name;
  47. /**
  48. *Contains the class name contained in this NameClassPair.
  49. * It is initialized by the constructor and can be updated using
  50. * <tt>setClassName()</tt>.
  51. * @serial
  52. * @see #getClassName
  53. * @see #setClassName
  54. */
  55. private String className;
  56. /**
  57. * Records whether the name of this <tt>NameClassPair</tt>
  58. * is relative to the target context.
  59. * It is initialized by the constructor and can be updated using
  60. * <tt>setRelative()</tt>.
  61. * @serial
  62. * @see #isRelative
  63. * @see #setRelative
  64. * @see #getName
  65. * @see #setName
  66. */
  67. private boolean isRel = true;
  68. /**
  69. * Constructs an instance of a NameClassPair given its
  70. * name and class name.
  71. *
  72. * @param name The non-null name of the object. It is relative
  73. * to the <em>target context</em> (which is
  74. * named by the first parameter of the <code>list()</code> method)
  75. * @param className The possibly null class name of the object
  76. * bound to name. It is null if the object bound is null.
  77. * @see #getClassName
  78. * @see #setClassName
  79. * @see #getName
  80. * @see #setName
  81. */
  82. public NameClassPair(String name, String className) {
  83. this.name = name;
  84. this.className = className;
  85. }
  86. /**
  87. * Constructs an instance of a NameClassPair given its
  88. * name, class name, and whether it is relative to the listing context.
  89. *
  90. * @param name The non-null name of the object.
  91. * @param className The possibly null class name of the object
  92. * bound to name. It is null if the object bound is null.
  93. * @param isRelative true if <code>name</code> is a name relative
  94. * to the target context (which is named by
  95. * the first parameter of the <code>list()</code> method);
  96. * false if <code>name</code> is a URL string.
  97. * @see #getClassName
  98. * @see #setClassName
  99. * @see #getName
  100. * @see #setName
  101. * @see #isRelative
  102. * @see #setRelative
  103. */
  104. public NameClassPair(String name, String className, boolean isRelative) {
  105. this.name = name;
  106. this.className = className;
  107. this.isRel = isRelative;
  108. }
  109. /**
  110. * Retrieves the class name of the object bound to the name of this binding.
  111. * If a reference or some other indirect information is bound,
  112. * retrieves the class name of the eventual object that
  113. * will be returned by <tt>Binding.getObject()</tt>.
  114. *
  115. * @return The possibly null class name of object bound.
  116. * It is null if the object bound is null.
  117. * @see Binding#getObject
  118. * @see Binding#getClassName
  119. * @see #setClassName
  120. */
  121. public String getClassName() {
  122. return className;
  123. }
  124. /**
  125. * Retrieves the name of this binding.
  126. * If <tt>isRelative()</tt> is true, this name is relative to the target context
  127. * (which is named by the first parameter of the <tt>list()</tt>).
  128. * If <tt>isRelative()</tt> is false, this name is a URL string.
  129. *
  130. * @return The non-null name of this binding.
  131. * @see #isRelative
  132. * @see #setName
  133. */
  134. public String getName() {
  135. return name;
  136. }
  137. /**
  138. * Sets the name of this binding.
  139. *
  140. * @param name the non-null string to use as the name.
  141. * @see #getName
  142. * @see #setRelative
  143. */
  144. public void setName(String name) {
  145. this.name = name;
  146. }
  147. /**
  148. * Sets the class name of this binding.
  149. *
  150. * @param name the possibly null string to use as the class name.
  151. * If null, <tt>Binding.getClassName()</tt> will return
  152. * the actual class name of the object in the binding.
  153. * The class name will be null if the object bound is null.
  154. * @see #getClassName
  155. * @see Binding#getClassName
  156. */
  157. public void setClassName(String name) {
  158. this.className = name;
  159. }
  160. /**
  161. * Determines whether the name of this binding is
  162. * relative to the target context (which is named by
  163. * the first parameter of the <code>list()</code> method).
  164. * @return true if the name of this binding is relative to the target context;
  165. * false if the name of this binding is a URL string.
  166. * @see #setRelative
  167. * @see #getName
  168. */
  169. public boolean isRelative() {
  170. return isRel;
  171. }
  172. /**
  173. * Sets whether the name of this binding is relative to the target
  174. * context (which is named by the first parameter of the <code>list()</code>
  175. * method).
  176. * @param r If true, the name of binding is relative to the target context;
  177. * if false, the name of binding is a URL string.
  178. * @see #isRelative
  179. * @see #setName
  180. */
  181. public void setRelative(boolean r) {
  182. isRel = r;
  183. }
  184. /**
  185. * Generates the string representation of this name/class pair.
  186. * The string representation consists of the name and class name separated
  187. * by a colon (':').
  188. * The contents of this string is useful
  189. * for debugging and is not meant to be interpreted programmatically.
  190. *
  191. * @return The string representation of this name/class pair.
  192. */
  193. public String toString() {
  194. return (isRelative() ? "" : "(not relative)") + getName() + ": " +
  195. getClassName();
  196. }
  197. /**
  198. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  199. */
  200. private static final long serialVersionUID = 5620776610160863339L;
  201. }