KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > naming > NameClassPair


1 /*
2  * @(#)NameClassPair.java 1.10 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 the object name and class name pair of a binding
12  * found in a context.
13  *<p>
14  * A context consists of name-to-object bindings.
15  * The NameClassPair class represents the name and the
16  * class of the bound object. It consists
17  * of a name and a string representing the
18  * package-qualified class name.
19  *<p>
20  * Use subclassing for naming systems that generate contents of
21  * a name/class pair dynamically.
22  *<p>
23  * A NameClassPair instance is not synchronized against concurrent
24  * access by multiple threads. Threads that need to access a NameClassPair
25  * concurrently should synchronize amongst themselves and provide
26  * the necessary locking.
27  *
28  * @author Rosanna Lee
29  * @author Scott Seligman
30  * @version 1.10 03/12/19
31  *
32  * @see Context#list
33  * @since 1.3
34  */

35
36  /*
37   * <p>
38   * The serialized form of a NameClassPair object consists of the name (a
39   * String), class name (a String), and isRelative flag (a boolean).
40   */

41
42 public class NameClassPair implements java.io.Serializable JavaDoc {
43     /**
44      * Contains the name of this NameClassPair.
45      * It is initialized by the constructor and can be updated using
46      * <tt>setName()</tt>.
47      * @serial
48      * @see #getName
49      * @see #setName
50      */

51     private String JavaDoc name;
52
53     /**
54      *Contains the class name contained in this NameClassPair.
55      * It is initialized by the constructor and can be updated using
56      * <tt>setClassName()</tt>.
57      * @serial
58      * @see #getClassName
59      * @see #setClassName
60      */

61     private String JavaDoc className;
62
63     /**
64      * Contains the full name of this NameClassPair within its
65      * own namespace.
66      * It is initialized using <tt>setNameInNamespace()</tt>
67      * @serial
68      * @see #getNameInNamespace
69      * @see #setNameInNamespace
70      */

71     private String JavaDoc fullName = null;
72
73
74     /**
75      * Records whether the name of this <tt>NameClassPair</tt>
76      * is relative to the target context.
77      * It is initialized by the constructor and can be updated using
78      * <tt>setRelative()</tt>.
79      * @serial
80      * @see #isRelative
81      * @see #setRelative
82      * @see #getName
83      * @see #setName
84      */

85     private boolean isRel = true;
86
87     /**
88      * Constructs an instance of a NameClassPair given its
89      * name and class name.
90      *
91      * @param name The non-null name of the object. It is relative
92      * to the <em>target context</em> (which is
93      * named by the first parameter of the <code>list()</code> method)
94      * @param className The possibly null class name of the object
95      * bound to name. It is null if the object bound is null.
96      * @see #getClassName
97      * @see #setClassName
98      * @see #getName
99      * @see #setName
100      */

101     public NameClassPair(String JavaDoc name, String JavaDoc className) {
102     this.name = name;
103     this.className = className;
104     }
105
106     /**
107      * Constructs an instance of a NameClassPair given its
108      * name, class name, and whether it is relative to the listing context.
109      *
110      * @param name The non-null name of the object.
111      * @param className The possibly null class name of the object
112      * bound to name. It is null if the object bound is null.
113      * @param isRelative true if <code>name</code> is a name relative
114      * to the target context (which is named by the first parameter
115      * of the <code>list()</code> method); false if <code>name</code>
116      * is a URL string.
117      * @see #getClassName
118      * @see #setClassName
119      * @see #getName
120      * @see #setName
121      * @see #isRelative
122      * @see #setRelative
123      */

124     public NameClassPair(String JavaDoc name, String JavaDoc className, boolean isRelative) {
125     this.name = name;
126     this.className = className;
127     this.isRel = isRelative;
128     }
129
130     /**
131      * Retrieves the class name of the object bound to the name of this binding.
132      * If a reference or some other indirect information is bound,
133      * retrieves the class name of the eventual object that
134      * will be returned by <tt>Binding.getObject()</tt>.
135      *
136      * @return The possibly null class name of object bound.
137      * It is null if the object bound is null.
138      * @see Binding#getObject
139      * @see Binding#getClassName
140      * @see #setClassName
141      */

142     public String JavaDoc getClassName() {
143     return className;
144     }
145
146     /**
147      * Retrieves the name of this binding.
148      * If <tt>isRelative()</tt> is true, this name is relative to the
149      * target context (which is named by the first parameter of the
150      * <tt>list()</tt>).
151      * If <tt>isRelative()</tt> is false, this name is a URL string.
152      *
153      * @return The non-null name of this binding.
154      * @see #isRelative
155      * @see #setName
156      */

157     public String JavaDoc getName() {
158     return name;
159     }
160
161     /**
162      * Sets the name of this binding.
163      *
164      * @param name the non-null string to use as the name.
165      * @see #getName
166      * @see #setRelative
167      */

168     public void setName(String JavaDoc name) {
169     this.name = name;
170     }
171
172     /**
173      * Sets the class name of this binding.
174      *
175      * @param name the possibly null string to use as the class name.
176      * If null, <tt>Binding.getClassName()</tt> will return
177      * the actual class name of the object in the binding.
178      * The class name will be null if the object bound is null.
179      * @see #getClassName
180      * @see Binding#getClassName
181      */

182     public void setClassName(String JavaDoc name) {
183     this.className = name;
184     }
185
186     /**
187      * Determines whether the name of this binding is
188      * relative to the target context (which is named by
189      * the first parameter of the <code>list()</code> method).
190      *
191      * @return true if the name of this binding is relative to the
192      * target context;
193      * false if the name of this binding is a URL string.
194      * @see #setRelative
195      * @see #getName
196      */

197     public boolean isRelative() {
198     return isRel;
199     }
200
201     /**
202      * Sets whether the name of this binding is relative to the target
203      * context (which is named by the first parameter of the <code>list()</code>
204      * method).
205      *
206      * @param r If true, the name of binding is relative to the target context;
207      * if false, the name of binding is a URL string.
208      * @see #isRelative
209      * @see #setName
210      */

211     public void setRelative(boolean r) {
212     isRel = r;
213     }
214
215     /**
216      * Retrieves the full name of this binding.
217      * The full name is the absolute name of this binding within
218      * its own namespace. See {@link Context#getNameInNamespace()}.
219      * <p>
220      *
221      * In naming systems for which the notion of full name does not
222      * apply to this binding an <tt>UnsupportedOperationException</tt>
223      * is thrown.
224      * This exception is also thrown when a service provider written before
225      * the introduction of the method is in use.
226      * <p>
227      * The string returned by this method is not a JNDI composite name and
228      * should not be passed directly to context methods.
229      *
230      * @return The full name of this binding.
231      * @throws UnsupportedOperationException if the notion of full name
232      * does not apply to this binding in the naming system.
233      * @since 1.5
234      * @see #setNameInNamespace
235      * @see #getName
236      */

237     public String JavaDoc getNameInNamespace() {
238     if (fullName == null) {
239         throw new UnsupportedOperationException JavaDoc();
240     }
241         return fullName;
242     }
243
244     /**
245      * Sets the full name of this binding.
246      * This method must be called to set the full name whenever a
247      * <tt>NameClassPair</tt> is created and a full name is
248      * applicable to this binding.
249      * <p>
250      * Setting the full name to null, or not setting it at all, will
251      * cause <tt>getNameInNamespace()</tt> to throw an exception.
252      *
253      * @param fullName The full name to use.
254      * @since 1.5
255      * @see #getNameInNamespace
256      * @see #setName
257      */

258     public void setNameInNamespace(String JavaDoc fullName) {
259         this.fullName = fullName;
260     }
261
262     /**
263      * Generates the string representation of this name/class pair.
264      * The string representation consists of the name and class name separated
265      * by a colon (':').
266      * The contents of this string is useful
267      * for debugging and is not meant to be interpreted programmatically.
268      *
269      * @return The string representation of this name/class pair.
270      */

271     public String JavaDoc toString() {
272     return (isRelative() ? "" : "(not relative)") + getName() + ": " +
273         getClassName();
274     }
275
276
277     /**
278      * Use serialVersionUID from JNDI 1.1.1 for interoperability
279      */

280     private static final long serialVersionUID = 5620776610160863339L;
281 }
282
Popular Tags