001 /*
002 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.naming;
027
028 /**
029 * This class represents a name-to-object binding found in a context.
030 *<p>
031 * A context consists of name-to-object bindings.
032 * The Binding class represents such a binding. It consists
033 * of a name and an object. The <code>Context.listBindings()</code>
034 * method returns an enumeration of Binding.
035 *<p>
036 * Use subclassing for naming systems that generate contents of
037 * a binding dynamically.
038 *<p>
039 * A Binding instance is not synchronized against concurrent access by multiple
040 * threads. Threads that need to access a Binding concurrently should
041 * synchronize amongst themselves and provide the necessary locking.
042 *
043 * @author Rosanna Lee
044 * @author Scott Seligman
045 * @version 1.15 07/05/05
046 * @since 1.3
047 */
048
049 public class Binding extends NameClassPair {
050 /**
051 * Contains this binding's object.
052 * It is initialized by the constuctor and can be updated using
053 * <tt>setObject</tt>.
054 * @serial
055 * @see #getObject
056 * @see #setObject
057 */
058 private Object boundObj;
059
060 /**
061 * Constructs an instance of a Binding given its name and object.
062 *<p>
063 * <tt>getClassName()</tt> will return
064 * the class name of <tt>obj</tt> (or null if <tt>obj</tt> is null)
065 * unless the class name has been explicitly set using <tt>setClassName()</tt>
066 *
067 * @param name The non-null name of the object. It is relative
068 * to the <em>target context</em> (which is
069 * named by the first parameter of the <code>listBindings()</code> method)
070 * @param obj The possibly null object bound to name.
071 * @see NameClassPair#setClassName
072 */
073 public Binding(String name, Object obj) {
074 super (name, null);
075 this .boundObj = obj;
076 }
077
078 /**
079 * Constructs an instance of a Binding given its name, object, and whether
080 * the name is relative.
081 *<p>
082 * <tt>getClassName()</tt> will return the class name of <tt>obj</tt>
083 * (or null if <tt>obj</tt> is null) unless the class name has been
084 * explicitly set using <tt>setClassName()</tt>
085 *
086 * @param name The non-null string name of the object.
087 * @param obj The possibly null object bound to name.
088 * @param isRelative true if <code>name</code> is a name relative
089 * to the target context (which is named by
090 * the first parameter of the <code>listBindings()</code> method);
091 * false if <code>name</code> is a URL string.
092 * @see NameClassPair#isRelative
093 * @see NameClassPair#setRelative
094 * @see NameClassPair#setClassName
095 */
096 public Binding(String name, Object obj, boolean isRelative) {
097 super (name, null, isRelative);
098 this .boundObj = obj;
099 }
100
101 /**
102 * Constructs an instance of a Binding given its name, class name, and object.
103 *
104 * @param name The non-null name of the object. It is relative
105 * to the <em>target context</em> (which is
106 * named by the first parameter of the <code>listBindings()</code> method)
107 * @param className The possibly null class name of the object
108 * bound to <tt>name</tt>. If null, the class name of <tt>obj</tt> is
109 * returned by <tt>getClassName()</tt>. If <tt>obj</tt> is also
110 * null, <tt>getClassName()</tt> will return null.
111 * @param obj The possibly null object bound to name.
112 * @see NameClassPair#setClassName
113 */
114 public Binding(String name, String className, Object obj) {
115 super (name, className);
116 this .boundObj = obj;
117 }
118
119 /**
120 * Constructs an instance of a Binding given its
121 * name, class name, object, and whether the name is relative.
122 *
123 * @param name The non-null string name of the object.
124 * @param className The possibly null class name of the object
125 * bound to <tt>name</tt>. If null, the class name of <tt>obj</tt> is
126 * returned by <tt>getClassName()</tt>. If <tt>obj</tt> is also
127 * null, <tt>getClassName()</tt> will return null.
128 * @param obj The possibly null object bound to name.
129 * @param isRelative true if <code>name</code> is a name relative
130 * to the target context (which is named by
131 * the first parameter of the <code>listBindings()</code> method);
132 * false if <code>name</code> is a URL string.
133 * @see NameClassPair#isRelative
134 * @see NameClassPair#setRelative
135 * @see NameClassPair#setClassName
136 */
137 public Binding(String name, String className, Object obj,
138 boolean isRelative) {
139 super (name, className, isRelative);
140 this .boundObj = obj;
141 }
142
143 /**
144 * Retrieves the class name of the object bound to the name of this binding.
145 * If the class name has been set explicitly, return it.
146 * Otherwise, if this binding contains a non-null object,
147 * that object's class name is used. Otherwise, null is returned.
148 *
149 * @return A possibly null string containing class name of object bound.
150 */
151 public String getClassName() {
152 String cname = super .getClassName();
153 if (cname != null) {
154 return cname;
155 }
156 if (boundObj != null)
157 return boundObj.getClass().getName();
158 else
159 return null;
160 }
161
162 /**
163 * Retrieves the object bound to the name of this binding.
164 *
165 * @return The object bound; null if this binding does not contain an object.
166 * @see #setObject
167 */
168
169 public Object getObject() {
170 return boundObj;
171 }
172
173 /**
174 * Sets the object associated with this binding.
175 * @param obj The possibly null object to use.
176 * @see #getObject
177 */
178 public void setObject(Object obj) {
179 boundObj = obj;
180 }
181
182 /**
183 * Generates the string representation of this binding.
184 * The string representation consists of the string representation
185 * of the name/class pair and the string representation of
186 * this binding's object, separated by ':'.
187 * The contents of this string is useful
188 * for debugging and is not meant to be interpreted programmatically.
189 *
190 * @return The non-null string representation of this binding.
191 */
192
193 public String toString() {
194 return super .toString() + ":" + getObject();
195 }
196
197 /**
198 * Use serialVersionUID from JNDI 1.1.1 for interoperability
199 */
200 private static final long serialVersionUID = 8839217842691845890L;
201 };
|