1. /*
  2. * @(#)Binding.java 1.8 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 a name-to-object binding found in a context.
  10. *<p>
  11. * A context consists of name-to-object bindings.
  12. * The Binding class represents such a binding. It consists
  13. * of a name and an object. The <code>Context.listBindings()</code>
  14. * method returns an enumeration of Binding.
  15. *<p>
  16. * Use subclassing for naming systems that generate contents of
  17. * a binding dynamically.
  18. *<p>
  19. * A Binding instance is not synchronized against concurrent access by multiple
  20. * threads. Threads that need to access a Binding concurrently should
  21. * synchronize amongst themselves and provide the necessary locking.
  22. *
  23. * @author Rosanna Lee
  24. * @author Scott Seligman
  25. * @version 1.8 03/12/19
  26. * @since 1.3
  27. */
  28. public class Binding extends NameClassPair {
  29. /**
  30. * Contains this binding's object.
  31. * It is initialized by the constuctor and can be updated using
  32. * <tt>setObject</tt>.
  33. * @serial
  34. * @see #getObject
  35. * @see #setObject
  36. */
  37. private Object boundObj;
  38. /**
  39. * Constructs an instance of a Binding given its name and object.
  40. *<p>
  41. * <tt>getClassName()</tt> will return
  42. * the class name of <tt>obj</tt> (or null if <tt>obj</tt> is null)
  43. * unless the class name has been explicitly set using <tt>setClassName()</tt>
  44. *
  45. * @param name The non-null name of the object. It is relative
  46. * to the <em>target context</em> (which is
  47. * named by the first parameter of the <code>listBindings()</code> method)
  48. * @param obj The possibly null object bound to name.
  49. * @see NameClassPair#setClassName
  50. */
  51. public Binding(String name, Object obj) {
  52. super(name, null);
  53. this.boundObj = obj;
  54. }
  55. /**
  56. * Constructs an instance of a Binding given its name, object, and whether
  57. * the name is relative.
  58. *<p>
  59. * <tt>getClassName()</tt> will return the class name of <tt>obj</tt>
  60. * (or null if <tt>obj</tt> is null) unless the class name has been
  61. * explicitly set using <tt>setClassName()</tt>
  62. *
  63. * @param name The non-null string name of the object.
  64. * @param obj The possibly null object bound to name.
  65. * @param isRelative true if <code>name</code> is a name relative
  66. * to the target context (which is named by
  67. * the first parameter of the <code>listBindings()</code> method);
  68. * false if <code>name</code> is a URL string.
  69. * @see NameClassPair#isRelative
  70. * @see NameClassPair#setRelative
  71. * @see NameClassPair#setClassName
  72. */
  73. public Binding(String name, Object obj, boolean isRelative) {
  74. super(name, null, isRelative);
  75. this.boundObj = obj;
  76. }
  77. /**
  78. * Constructs an instance of a Binding given its name, class name, and object.
  79. *
  80. * @param name The non-null name of the object. It is relative
  81. * to the <em>target context</em> (which is
  82. * named by the first parameter of the <code>listBindings()</code> method)
  83. * @param className The possibly null class name of the object
  84. * bound to <tt>name</tt>. If null, the class name of <tt>obj</tt> is
  85. * returned by <tt>getClassName()</tt>. If <tt>obj</tt> is also
  86. * null, <tt>getClassName()</tt> will return null.
  87. * @param obj The possibly null object bound to name.
  88. * @see NameClassPair#setClassName
  89. */
  90. public Binding(String name, String className, Object obj) {
  91. super(name, className);
  92. this.boundObj = obj;
  93. }
  94. /**
  95. * Constructs an instance of a Binding given its
  96. * name, class name, object, and whether the name is relative.
  97. *
  98. * @param name The non-null string name of the object.
  99. * @param className The possibly null class name of the object
  100. * bound to <tt>name</tt>. If null, the class name of <tt>obj</tt> is
  101. * returned by <tt>getClassName()</tt>. If <tt>obj</tt> is also
  102. * null, <tt>getClassName()</tt> will return null.
  103. * @param obj The possibly null object bound to name.
  104. * @param isRelative true if <code>name</code> is a name relative
  105. * to the target context (which is named by
  106. * the first parameter of the <code>listBindings()</code> method);
  107. * false if <code>name</code> is a URL string.
  108. * @see NameClassPair#isRelative
  109. * @see NameClassPair#setRelative
  110. * @see NameClassPair#setClassName
  111. */
  112. public Binding(String name, String className, Object obj, boolean isRelative) {
  113. super(name, className, isRelative);
  114. this.boundObj = obj;
  115. }
  116. /**
  117. * Retrieves the class name of the object bound to the name of this binding.
  118. * If the class name has been set explicitly, return it.
  119. * Otherwise, if this binding contains a non-null object,
  120. * that object's class name is used. Otherwise, null is returned.
  121. *
  122. * @return A possibly null string containing class name of object bound.
  123. */
  124. public String getClassName() {
  125. String cname = super.getClassName();
  126. if (cname != null) {
  127. return cname;
  128. }
  129. if (boundObj != null)
  130. return boundObj.getClass().getName();
  131. else
  132. return null;
  133. }
  134. /**
  135. * Retrieves the object bound to the name of this binding.
  136. *
  137. * @return The object bound; null if this binding does not contain an object.
  138. * @see #setObject
  139. */
  140. public Object getObject() {
  141. return boundObj;
  142. }
  143. /**
  144. * Sets the object associated with this binding.
  145. * @param obj The possibly null object to use.
  146. * @see #getObject
  147. */
  148. public void setObject(Object obj) {
  149. boundObj = obj;
  150. }
  151. /**
  152. * Generates the string representation of this binding.
  153. * The string representation consists of the string representation
  154. * of the name/class pair and the string representation of
  155. * this binding's object, separated by ':'.
  156. * The contents of this string is useful
  157. * for debugging and is not meant to be interpreted programmatically.
  158. *
  159. * @return The non-null string representation of this binding.
  160. */
  161. public String toString() {
  162. return super.toString() + ":" + getObject();
  163. }
  164. /**
  165. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  166. */
  167. private static final long serialVersionUID = 8839217842691845890L;
  168. };