Path: blob/master/src/java.naming/share/classes/javax/naming/Binding.java
41152 views
/*1* Copyright (c) 1999, 2019, 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 a name-to-object binding found in a context.29*<p>30* A context consists of name-to-object bindings.31* The Binding class represents such a binding. It consists32* of a name and an object. The <code>Context.listBindings()</code>33* method returns an enumeration of Binding.34*<p>35* Use subclassing for naming systems that generate contents of36* a binding dynamically.37*<p>38* A Binding instance is not synchronized against concurrent access by multiple39* threads. Threads that need to access a Binding concurrently should40* synchronize amongst themselves and provide the necessary locking.41*42* @author Rosanna Lee43* @author Scott Seligman44* @since 1.345*/4647public class Binding extends NameClassPair {48/**49* Contains this binding's object.50* It is initialized by the constructor and can be updated using51* {@code setObject}.52* @serial53* @see #getObject54* @see #setObject55*/56@SuppressWarnings("serial") // Not statically typed as Serializable57private Object boundObj;5859/**60* Constructs an instance of a Binding given its name and object.61*<p>62* {@code getClassName()} will return63* the class name of {@code obj} (or null if {@code obj} is null)64* unless the class name has been explicitly set using {@code setClassName()}65*66* @param name The non-null name of the object. It is relative67* to the <em>target context</em> (which is68* named by the first parameter of the <code>listBindings()</code> method)69* @param obj The possibly null object bound to name.70* @see NameClassPair#setClassName71*/72public Binding(String name, Object obj) {73super(name, null);74this.boundObj = obj;75}7677/**78* Constructs an instance of a Binding given its name, object, and whether79* the name is relative.80*<p>81* {@code getClassName()} will return the class name of {@code obj}82* (or null if {@code obj} is null) unless the class name has been83* explicitly set using {@code setClassName()}84*85* @param name The non-null string name of the object.86* @param obj The possibly null object bound to name.87* @param isRelative true if <code>name</code> is a name relative88* to the target context (which is named by89* the first parameter of the <code>listBindings()</code> method);90* false if <code>name</code> is a URL string.91* @see NameClassPair#isRelative92* @see NameClassPair#setRelative93* @see NameClassPair#setClassName94*/95public Binding(String name, Object obj, boolean isRelative) {96super(name, null, isRelative);97this.boundObj = obj;98}99100/**101* Constructs an instance of a Binding given its name, class name, and object.102*103* @param name The non-null name of the object. It is relative104* to the <em>target context</em> (which is105* named by the first parameter of the <code>listBindings()</code> method)106* @param className The possibly null class name of the object107* bound to {@code name}. If null, the class name of {@code obj} is108* returned by {@code getClassName()}. If {@code obj} is also109* null, {@code getClassName()} will return null.110* @param obj The possibly null object bound to name.111* @see NameClassPair#setClassName112*/113public Binding(String name, String className, Object obj) {114super(name, className);115this.boundObj = obj;116}117118/**119* Constructs an instance of a Binding given its120* name, class name, object, and whether the name is relative.121*122* @param name The non-null string name of the object.123* @param className The possibly null class name of the object124* bound to {@code name}. If null, the class name of {@code obj} is125* returned by {@code getClassName()}. If {@code obj} is also126* null, {@code getClassName()} will return null.127* @param obj The possibly null object bound to name.128* @param isRelative true if <code>name</code> is a name relative129* to the target context (which is named by130* the first parameter of the <code>listBindings()</code> method);131* false if <code>name</code> is a URL string.132* @see NameClassPair#isRelative133* @see NameClassPair#setRelative134* @see NameClassPair#setClassName135*/136public Binding(String name, String className, Object obj, boolean isRelative) {137super(name, className, isRelative);138this.boundObj = obj;139}140141/**142* Retrieves the class name of the object bound to the name of this binding.143* If the class name has been set explicitly, return it.144* Otherwise, if this binding contains a non-null object,145* that object's class name is used. Otherwise, null is returned.146*147* @return A possibly null string containing class name of object bound.148*/149public String getClassName() {150String cname = super.getClassName();151if (cname != null) {152return cname;153}154if (boundObj != null)155return boundObj.getClass().getName();156else157return null;158}159160/**161* Retrieves the object bound to the name of this binding.162*163* @return The object bound; null if this binding does not contain an object.164* @see #setObject165*/166167public Object getObject() {168return boundObj;169}170171/**172* Sets the object associated with this binding.173* @param obj The possibly null object to use.174* @see #getObject175*/176public void setObject(Object obj) {177boundObj = obj;178}179180/**181* Generates the string representation of this binding.182* The string representation consists of the string representation183* of the name/class pair and the string representation of184* this binding's object, separated by ':'.185* The contents of this string is useful186* for debugging and is not meant to be interpreted programmatically.187*188* @return The non-null string representation of this binding.189*/190191public String toString() {192return super.toString() + ":" + getObject();193}194195/**196* Use serialVersionUID from JNDI 1.1.1 for interoperability197*/198private static final long serialVersionUID = 8839217842691845890L;199};200201202