KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > naming > Binding


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
8 package javax.naming;
9
10 /**
11   * This class represents a name-to-object binding found in a context.
12   *<p>
13   * A context consists of name-to-object bindings.
14   * The Binding class represents such a binding. It consists
15   * of a name and an object. The <code>Context.listBindings()</code>
16   * method returns an enumeration of Binding.
17   *<p>
18   * Use subclassing for naming systems that generate contents of
19   * a binding dynamically.
20   *<p>
21   * A Binding instance is not synchronized against concurrent access by multiple
22   * threads. Threads that need to access a Binding concurrently should
23   * synchronize amongst themselves and provide the necessary locking.
24   *
25   * @author Rosanna Lee
26   * @author Scott Seligman
27   * @version 1.8 03/12/19
28   * @since 1.3
29   */

30
31 public class Binding extends NameClassPair JavaDoc {
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 JavaDoc boundObj;
41
42     /**
43       * Constructs an instance of a Binding given its name and object.
44       *<p>
45       * <tt>getClassName()</tt> will return
46       * the class name of <tt>obj</tt> (or null if <tt>obj</tt> is null)
47       * unless the class name has been explicitly set using <tt>setClassName()</tt>
48       *
49       * @param name The non-null name of the object. It is relative
50       * to the <em>target context</em> (which is
51       * named by the first parameter of the <code>listBindings()</code> method)
52       * @param obj The possibly null object bound to name.
53       * @see NameClassPair#setClassName
54       */

55     public Binding(String JavaDoc name, Object JavaDoc obj) {
56     super(name, null);
57     this.boundObj = obj;
58     }
59
60     /**
61       * Constructs an instance of a Binding given its name, object, and whether
62       * the name is relative.
63       *<p>
64       * <tt>getClassName()</tt> will return the class name of <tt>obj</tt>
65       * (or null if <tt>obj</tt> is null) unless the class name has been
66       * explicitly set using <tt>setClassName()</tt>
67       *
68       * @param name The non-null string name of the object.
69       * @param obj The possibly null object bound to name.
70       * @param isRelative true if <code>name</code> is a name relative
71       * to the target context (which is named by
72       * the first parameter of the <code>listBindings()</code> method);
73       * false if <code>name</code> is a URL string.
74       * @see NameClassPair#isRelative
75       * @see NameClassPair#setRelative
76       * @see NameClassPair#setClassName
77       */

78     public Binding(String JavaDoc name, Object JavaDoc obj, boolean isRelative) {
79     super(name, null, isRelative);
80     this.boundObj = obj;
81     }
82
83     /**
84       * Constructs an instance of a Binding given its name, class name, and object.
85       *
86       * @param name The non-null name of the object. It is relative
87       * to the <em>target context</em> (which is
88       * named by the first parameter of the <code>listBindings()</code> method)
89       * @param className The possibly null class name of the object
90       * bound to <tt>name</tt>. If null, the class name of <tt>obj</tt> is
91       * returned by <tt>getClassName()</tt>. If <tt>obj</tt> is also
92       * null, <tt>getClassName()</tt> will return null.
93       * @param obj The possibly null object bound to name.
94       * @see NameClassPair#setClassName
95       */

96     public Binding(String JavaDoc name, String JavaDoc className, Object JavaDoc obj) {
97     super(name, className);
98     this.boundObj = obj;
99     }
100
101     /**
102       * Constructs an instance of a Binding given its
103       * name, class name, object, and whether the name is relative.
104       *
105       * @param name The non-null string name of the object.
106       * @param className The possibly null class name of the object
107       * bound to <tt>name</tt>. If null, the class name of <tt>obj</tt> is
108       * returned by <tt>getClassName()</tt>. If <tt>obj</tt> is also
109       * null, <tt>getClassName()</tt> will return null.
110       * @param obj The possibly null object bound to name.
111       * @param isRelative true if <code>name</code> is a name relative
112       * to the target context (which is named by
113       * the first parameter of the <code>listBindings()</code> method);
114       * false if <code>name</code> is a URL string.
115       * @see NameClassPair#isRelative
116       * @see NameClassPair#setRelative
117       * @see NameClassPair#setClassName
118       */

119     public Binding(String JavaDoc name, String JavaDoc className, Object JavaDoc obj, boolean isRelative) {
120     super(name, className, isRelative);
121     this.boundObj = obj;
122     }
123
124     /**
125       * Retrieves the class name of the object bound to the name of this binding.
126       * If the class name has been set explicitly, return it.
127       * Otherwise, if this binding contains a non-null object,
128       * that object's class name is used. Otherwise, null is returned.
129       *
130       * @return A possibly null string containing class name of object bound.
131       */

132     public String JavaDoc getClassName() {
133     String JavaDoc cname = super.getClassName();
134     if (cname != null) {
135         return cname;
136     }
137     if (boundObj != null)
138         return boundObj.getClass().getName();
139     else
140         return null;
141     }
142
143     /**
144       * Retrieves the object bound to the name of this binding.
145       *
146       * @return The object bound; null if this binding does not contain an object.
147       * @see #setObject
148       */

149
150     public Object JavaDoc getObject() {
151     return boundObj;
152     }
153
154     /**
155      * Sets the object associated with this binding.
156      * @param obj The possibly null object to use.
157      * @see #getObject
158      */

159     public void setObject(Object JavaDoc obj) {
160     boundObj = obj;
161     }
162
163     /**
164       * Generates the string representation of this binding.
165       * The string representation consists of the string representation
166       * of the name/class pair and the string representation of
167       * this binding's object, separated by ':'.
168       * The contents of this string is useful
169       * for debugging and is not meant to be interpreted programmatically.
170       *
171       * @return The non-null string representation of this binding.
172       */

173
174     public String JavaDoc toString() {
175     return super.toString() + ":" + getObject();
176     }
177
178     /**
179      * Use serialVersionUID from JNDI 1.1.1 for interoperability
180      */

181     private static final long serialVersionUID = 8839217842691845890L;
182 };
183
184
Popular Tags