1 /* 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.naming; 27 28 /** 29 * This class represents a name-to-object binding found in a context. 30 *<p> 31 * A context consists of name-to-object bindings. 32 * The Binding class represents such a binding. It consists 33 * of a name and an object. The <code>Context.listBindings()</code> 34 * method returns an enumeration of Binding. 35 *<p> 36 * Use subclassing for naming systems that generate contents of 37 * a binding dynamically. 38 *<p> 39 * A Binding instance is not synchronized against concurrent access by multiple 40 * threads. Threads that need to access a Binding concurrently should 41 * synchronize amongst themselves and provide the necessary locking. 42 * 43 * @author Rosanna Lee 44 * @author Scott Seligman 45 * @since 1.3 46 */ 47 48 public class Binding extends NameClassPair { 49 /** 50 * Contains this binding's object. 51 * It is initialized by the constructor and can be updated using 52 * {@code setObject}. 53 * @serial 54 * @see #getObject 55 * @see #setObject 56 */ 57 private Object boundObj; 58 59 /** 60 * Constructs an instance of a Binding given its name and object. 61 *<p> 62 * {@code getClassName()} will return 63 * 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 relative 67 * to the <em>target context</em> (which is 68 * named by the first parameter of the <code>listBindings()</code> method) 69 * @param obj The possibly null object bound to name. 70 * @see NameClassPair#setClassName 71 */ 72 public Binding(String name, Object obj) { 73 super(name, null); 74 this.boundObj = obj; 75 } 76 77 /** 78 * Constructs an instance of a Binding given its name, object, and whether 79 * 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 been 83 * 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 relative 88 * to the target context (which is named by 89 * the first parameter of the <code>listBindings()</code> method); 90 * false if <code>name</code> is a URL string. 91 * @see NameClassPair#isRelative 92 * @see NameClassPair#setRelative 93 * @see NameClassPair#setClassName 94 */ 95 public Binding(String name, Object obj, boolean isRelative) { 96 super(name, null, isRelative); 97 this.boundObj = obj; 98 } 99 100 /** 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 relative 104 * to the <em>target context</em> (which is 105 * named by the first parameter of the <code>listBindings()</code> method) 106 * @param className The possibly null class name of the object 107 * bound to {@code name}. If null, the class name of {@code obj} is 108 * returned by {@code getClassName()}. If {@code obj} is also 109 * null, {@code getClassName()} will return null. 110 * @param obj The possibly null object bound to name. 111 * @see NameClassPair#setClassName 112 */ 113 public Binding(String name, String className, Object obj) { 114 super(name, className); 115 this.boundObj = obj; 116 } 117 118 /** 119 * Constructs an instance of a Binding given its 120 * 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 object 124 * bound to {@code name}. If null, the class name of {@code obj} is 125 * returned by {@code getClassName()}. If {@code obj} is also 126 * 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 relative 129 * to the target context (which is named by 130 * the first parameter of the <code>listBindings()</code> method); 131 * false if <code>name</code> is a URL string. 132 * @see NameClassPair#isRelative 133 * @see NameClassPair#setRelative 134 * @see NameClassPair#setClassName 135 */ 136 public Binding(String name, String className, Object obj, boolean isRelative) { 137 super(name, className, isRelative); 138 this.boundObj = obj; 139 } 140 141 /** 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 */ 149 public String getClassName() { 150 String cname = super.getClassName(); 151 if (cname != null) { 152 return cname; 153 } 154 if (boundObj != null) 155 return boundObj.getClass().getName(); 156 else 157 return null; 158 } 159 160 /** 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 #setObject 165 */ 166 167 public Object getObject() { 168 return boundObj; 169 } 170 171 /** 172 * Sets the object associated with this binding. 173 * @param obj The possibly null object to use. 174 * @see #getObject 175 */ 176 public void setObject(Object obj) { 177 boundObj = obj; 178 } 179 180 /** 181 * Generates the string representation of this binding. 182 * The string representation consists of the string representation 183 * of the name/class pair and the string representation of 184 * this binding's object, separated by ':'. 185 * The contents of this string is useful 186 * for debugging and is not meant to be interpreted programmatically. 187 * 188 * @return The non-null string representation of this binding. 189 */ 190 191 public String toString() { 192 return super.toString() + ":" + getObject(); 193 } 194 195 /** 196 * Use serialVersionUID from JNDI 1.1.1 for interoperability 197 */ 198 private static final long serialVersionUID = 8839217842691845890L; 199 };