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