Path: blob/master/src/java.naming/share/classes/javax/naming/NameClassPair.java
41152 views
/*1* Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming;2627/**28* This class represents the object name and class name pair of a binding29* found in a context.30*<p>31* A context consists of name-to-object bindings.32* The NameClassPair class represents the name and the33* class of the bound object. It consists34* of a name and a string representing the35* package-qualified class name.36*<p>37* Use subclassing for naming systems that generate contents of38* a name/class pair dynamically.39*<p>40* A NameClassPair instance is not synchronized against concurrent41* access by multiple threads. Threads that need to access a NameClassPair42* concurrently should synchronize amongst themselves and provide43* the necessary locking.44*45* @author Rosanna Lee46* @author Scott Seligman47*48* @see Context#list49* @since 1.350*/5152/*53* <p>54* The serialized form of a NameClassPair object consists of the name (a55* String), class name (a String), and isRelative flag (a boolean).56*/5758public class NameClassPair implements java.io.Serializable {59/**60* Contains the name of this NameClassPair.61* It is initialized by the constructor and can be updated using62* {@code setName()}.63* @serial64* @see #getName65* @see #setName66*/67private String name;6869/**70*Contains the class name contained in this NameClassPair.71* It is initialized by the constructor and can be updated using72* {@code setClassName()}.73* @serial74* @see #getClassName75* @see #setClassName76*/77private String className;7879/**80* Contains the full name of this NameClassPair within its81* own namespace.82* It is initialized using {@code setNameInNamespace()}83* @serial84* @see #getNameInNamespace85* @see #setNameInNamespace86*/87private String fullName = null;888990/**91* Records whether the name of this {@code NameClassPair}92* is relative to the target context.93* It is initialized by the constructor and can be updated using94* {@code setRelative()}.95* @serial96* @see #isRelative97* @see #setRelative98* @see #getName99* @see #setName100*/101private boolean isRel = true;102103/**104* Constructs an instance of a NameClassPair given its105* name and class name.106*107* @param name The non-null name of the object. It is relative108* to the <em>target context</em> (which is109* named by the first parameter of the <code>list()</code> method)110* @param className The possibly null class name of the object111* bound to name. It is null if the object bound is null.112* @see #getClassName113* @see #setClassName114* @see #getName115* @see #setName116*/117public NameClassPair(String name, String className) {118this.name = name;119this.className = className;120}121122/**123* Constructs an instance of a NameClassPair given its124* name, class name, and whether it is relative to the listing context.125*126* @param name The non-null name of the object.127* @param className The possibly null class name of the object128* bound to name. It is null if the object bound is null.129* @param isRelative true if <code>name</code> is a name relative130* to the target context (which is named by the first parameter131* of the <code>list()</code> method); false if <code>name</code>132* is a URL string.133* @see #getClassName134* @see #setClassName135* @see #getName136* @see #setName137* @see #isRelative138* @see #setRelative139*/140public NameClassPair(String name, String className, boolean isRelative) {141this.name = name;142this.className = className;143this.isRel = isRelative;144}145146/**147* Retrieves the class name of the object bound to the name of this binding.148* If a reference or some other indirect information is bound,149* retrieves the class name of the eventual object that150* will be returned by {@code Binding.getObject()}.151*152* @return The possibly null class name of object bound.153* It is null if the object bound is null.154* @see Binding#getObject155* @see Binding#getClassName156* @see #setClassName157*/158public String getClassName() {159return className;160}161162/**163* Retrieves the name of this binding.164* If {@code isRelative()} is true, this name is relative to the165* target context (which is named by the first parameter of the166* {@code list()}).167* If {@code isRelative()} is false, this name is a URL string.168*169* @return The non-null name of this binding.170* @see #isRelative171* @see #setName172*/173public String getName() {174return name;175}176177/**178* Sets the name of this binding.179*180* @param name the non-null string to use as the name.181* @see #getName182* @see #setRelative183*/184public void setName(String name) {185this.name = name;186}187188/**189* Sets the class name of this binding.190*191* @param name the possibly null string to use as the class name.192* If null, {@code Binding.getClassName()} will return193* the actual class name of the object in the binding.194* The class name will be null if the object bound is null.195* @see #getClassName196* @see Binding#getClassName197*/198public void setClassName(String name) {199this.className = name;200}201202/**203* Determines whether the name of this binding is204* relative to the target context (which is named by205* the first parameter of the <code>list()</code> method).206*207* @return true if the name of this binding is relative to the208* target context;209* false if the name of this binding is a URL string.210* @see #setRelative211* @see #getName212*/213public boolean isRelative() {214return isRel;215}216217/**218* Sets whether the name of this binding is relative to the target219* context (which is named by the first parameter of the <code>list()</code>220* method).221*222* @param r If true, the name of binding is relative to the target context;223* if false, the name of binding is a URL string.224* @see #isRelative225* @see #setName226*/227public void setRelative(boolean r) {228isRel = r;229}230231/**232* Retrieves the full name of this binding.233* The full name is the absolute name of this binding within234* its own namespace. See {@link Context#getNameInNamespace()}.235* <p>236*237* In naming systems for which the notion of full name does not238* apply to this binding an {@code UnsupportedOperationException}239* is thrown.240* This exception is also thrown when a service provider written before241* the introduction of the method is in use.242* <p>243* The string returned by this method is not a JNDI composite name and244* should not be passed directly to context methods.245*246* @return The full name of this binding.247* @throws UnsupportedOperationException if the notion of full name248* does not apply to this binding in the naming system.249* @since 1.5250* @see #setNameInNamespace251* @see #getName252*/253public String getNameInNamespace() {254if (fullName == null) {255throw new UnsupportedOperationException();256}257return fullName;258}259260/**261* Sets the full name of this binding.262* This method must be called to set the full name whenever a263* {@code NameClassPair} is created and a full name is264* applicable to this binding.265* <p>266* Setting the full name to null, or not setting it at all, will267* cause {@code getNameInNamespace()} to throw an exception.268*269* @param fullName The full name to use.270* @since 1.5271* @see #getNameInNamespace272* @see #setName273*/274public void setNameInNamespace(String fullName) {275this.fullName = fullName;276}277278/**279* Generates the string representation of this name/class pair.280* The string representation consists of the name and class name separated281* by a colon (':').282* The contents of this string is useful283* for debugging and is not meant to be interpreted programmatically.284*285* @return The string representation of this name/class pair.286*/287public String toString() {288return (isRelative() ? "" : "(not relative)") + getName() + ": " +289getClassName();290}291292293/**294* Use serialVersionUID from JNDI 1.1.1 for interoperability295*/296private static final long serialVersionUID = 5620776610160863339L;297}298299300