001 /* NameClassPair.java --
002 Copyright (C) 2001, 2005, 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.naming;
040
041 import java.io.Serializable;
042
043 /**
044 * <code>NameClassPair</code> represents the name-classname mapping pair
045 * of a binding in a context.
046 * <p>
047 * Bindings are mappings of a name to an object and this class is used to
048 * specify the mapping of the name to the class type of the bound object.
049 * As classname the fully qualified classname is used.
050 * </p>
051 *
052 * @author Tom Tromey (tromey@redhat.com)
053 * @since 1.3
054 */
055 public class NameClassPair implements Serializable
056 {
057 private static final long serialVersionUID = 5620776610160863339L;
058
059 /**
060 * Constructs an instance with the given name and classname.
061 *
062 * @param name the name of the binding relative to the target context
063 * (may not be <code>null</code>)
064 * @param className the name of the class. If <code>null</code> the bound
065 * object is also <code>null</code>
066 */
067 public NameClassPair (String name, String className)
068 {
069 this (name, className, true);
070 }
071
072 /**
073 * Constructs an instance with the given name and classname and a
074 * flag indicating if the name is relative to the target context.
075 *
076 * @param name the name of the binding (may not be <code>null</code>)
077 * @param className the name of the class. If <code>null</code> the bound
078 * object is also <code>null</code>
079 * @param isRelative flag indicating if the name is relative or not
080 */
081 public NameClassPair (String name, String className, boolean isRelative)
082 {
083 this.name = name;
084 this.className = className;
085 this.isRel = isRelative;
086 }
087
088 /**
089 * Returns the classname of the binding.
090 * @return The fully qualified classname or <code>null</code> if the
091 * bound object is null.
092 */
093 public String getClassName ()
094 {
095 return className;
096 }
097
098 /**
099 * Returns the name of the binding.
100 * @return The name.
101 */
102 public String getName ()
103 {
104 return name;
105 }
106
107 /**
108 * Checks whether the name is relative to the target context or not.
109 * @return <code>true</code> if the name is relative,
110 * <code>false</code> otherwise.
111 */
112 public boolean isRelative ()
113 {
114 return isRel;
115 }
116
117 /**
118 * Sets the classname of the bound object.
119 * @param name the classname to set (maybe <code>null</code>)
120 */
121 public void setClassName (String name)
122 {
123 this.className = name;
124 }
125
126 /**
127 * Sets the name of the binding.
128 * @param name the name to set
129 */
130 public void setName (String name)
131 {
132 this.name = name;
133 }
134
135 /**
136 * Sets if the name is relative to the target context.
137 * @param r <code>true</code> to mark as relative
138 */
139 public void setRelative (boolean r)
140 {
141 this.isRel = r;
142 }
143
144 /**
145 * Sets the full name for this binding. Setting the full name by this
146 * method is the only way to initialize full names of bindings if
147 * supported by a specific naming system.
148 *
149 * @param fullName the full name of this binding. If not set or set to
150 * <code>null</code> the <code>getNameInNamespace()</code> method will
151 * throw an exception
152 *
153 * @see #getNameInNamespace()
154 *
155 * @since 1.5
156 */
157 public void setNameInNamespace(String fullName)
158 {
159 this.fullName = fullName;
160 }
161
162 /**
163 * Returns the full name for this binding. The full name of a binding is
164 * defined as the absolute name in its own namespace and is not valid
165 * outside.
166 *
167 * @return The full name in the bindings namespace.
168 * @throws UnsupportedOperationException if no full name is applicable in
169 * the specific naming system.
170 *
171 * @see Context#getNameInNamespace()
172 *
173 * @since 1.5
174 */
175 public String getNameInNamespace()
176 {
177 if (this.fullName == null)
178 throw new UnsupportedOperationException();
179
180 return this.fullName;
181 }
182
183 /**
184 * Returns the string representation.
185 * @return The string <code>getName() + ":" + getClassName()</code>.
186 */
187 public String toString ()
188 {
189 // Specified by class documentation.
190 return name + ":" + className;
191 }
192
193 // These field names are fixed by the serialization spec.
194 private String name;
195 private String className;
196 private boolean isRel;
197 private String fullName;
198 }