Path: blob/master/src/java.naming/share/classes/javax/naming/Reference.java
41152 views
/*1* Copyright (c) 1999, 2020, 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;2627import java.util.Vector;28import java.util.Enumeration;2930/**31* This class represents a reference to an object that is found outside of32* the naming/directory system.33*<p>34* Reference provides a way of recording address information about35* objects which themselves are not directly bound to the naming/directory system.36*<p>37* A Reference consists of an ordered list of addresses and class information38* about the object being referenced.39* Each address in the list identifies a communications endpoint40* for the same conceptual object. The "communications endpoint"41* is information that indicates how to contact the object. It could42* be, for example, a network address, a location in memory on the43* local machine, another process on the same machine, etc.44* The order of the addresses in the list may be of significance45* to object factories that interpret the reference.46*<p>47* Multiple addresses may arise for48* various reasons, such as replication or the object offering interfaces49* over more than one communication mechanism. The addresses are indexed50* starting with zero.51*<p>52* A Reference also contains information to assist in creating an instance53* of the object to which this Reference refers. It contains the class name54* of that object, and the class name and location of the factory to be used55* to create the object.56* The class factory location is a space-separated list of URLs representing57* the class path used to load the factory. When the factory class (or58* any class or resource upon which it depends) needs to be loaded,59* each URL is used (in order) to attempt to load the class.60*<p>61* A Reference instance is not synchronized against concurrent access by multiple62* threads. Threads that need to access a single Reference concurrently should63* synchronize amongst themselves and provide the necessary locking.64*65* @author Rosanna Lee66* @author Scott Seligman67*68* @see RefAddr69* @see StringRefAddr70* @see BinaryRefAddr71* @since 1.372*/7374/*<p>75* The serialized form of a Reference object consists of the class76* name of the object being referenced (a String), a Vector of the77* addresses (each a RefAddr), the name of the class factory (a78* String), and the location of the class factory (a String).79*/808182public class Reference implements Cloneable, java.io.Serializable {83/**84* Contains the fully-qualified name of the class of the object to which85* this Reference refers.86* @serial87* @see java.lang.Class#getName88*/89protected String className;90/**91* Contains the addresses contained in this Reference.92* Initialized by constructor.93* @serial94*/95protected Vector<RefAddr> addrs = null;9697/**98* Contains the name of the factory class for creating99* an instance of the object to which this Reference refers.100* Initialized to null.101* @serial102*/103protected String classFactory = null;104105/**106* Contains the location of the factory class.107* Initialized to null.108* @serial109*/110protected String classFactoryLocation = null;111112/**113* Constructs a new reference for an object with class name 'className'.114* Class factory and class factory location are set to null.115* The newly created reference contains zero addresses.116*117* @param className The non-null class name of the object to which118* this reference refers.119*/120public Reference(String className) {121this.className = className;122addrs = new Vector<>();123}124125/**126* Constructs a new reference for an object with class name 'className' and127* an address.128* Class factory and class factory location are set to null.129*130* @param className The non-null class name of the object to131* which this reference refers.132* @param addr The non-null address of the object.133*/134public Reference(String className, RefAddr addr) {135this.className = className;136addrs = new Vector<>();137addrs.addElement(addr);138}139140/**141* Constructs a new reference for an object with class name 'className',142* and the class name and location of the object's factory.143*144* @param className The non-null class name of the object to which145* this reference refers.146* @param factory The possibly null class name of the object's factory.147* @param factoryLocation148* The possibly null location from which to load149* the factory (e.g. URL)150* @see javax.naming.spi.ObjectFactory151* @see javax.naming.spi.NamingManager#getObjectInstance152*/153public Reference(String className, String factory, String factoryLocation) {154this(className);155classFactory = factory;156classFactoryLocation = factoryLocation;157}158159/**160* Constructs a new reference for an object with class name 'className',161* the class name and location of the object's factory, and the address for162* the object.163*164* @param className The non-null class name of the object to165* which this reference refers.166* @param factory The possibly null class name of the object's factory.167* @param factoryLocation The possibly null location from which168* to load the factory (e.g. URL)169* @param addr The non-null address of the object.170* @see javax.naming.spi.ObjectFactory171* @see javax.naming.spi.NamingManager#getObjectInstance172*/173public Reference(String className, RefAddr addr,174String factory, String factoryLocation) {175this(className, addr);176classFactory = factory;177classFactoryLocation = factoryLocation;178}179180/**181* Retrieves the class name of the object to which this reference refers.182*183* @return The non-null fully-qualified class name of the object.184* (e.g. "java.lang.String")185*/186public String getClassName() {187return className;188}189190/**191* Retrieves the class name of the factory of the object192* to which this reference refers.193*194* @return The possibly null fully-qualified class name of the factory.195* (e.g. "java.lang.String")196*/197public String getFactoryClassName() {198return classFactory;199}200201/**202* Retrieves the location of the factory of the object203* to which this reference refers.204* If it is a codebase, then it is an ordered list of URLs,205* separated by spaces, listing locations from where the factory206* class definition should be loaded.207*208* @return The possibly null string containing the209* location for loading in the factory's class.210*/211public String getFactoryClassLocation() {212return classFactoryLocation;213}214215/**216* Retrieves the first address that has the address type 'addrType'.217* String.compareTo() is used to test the equality of the address types.218*219* @param addrType The non-null address type for which to find the address.220* @return The address in this reference with address type 'addrType';221* null if no such address exists.222*/223public RefAddr get(String addrType) {224int len = addrs.size();225RefAddr addr;226for (int i = 0; i < len; i++) {227addr = addrs.elementAt(i);228if (addr.getType().compareTo(addrType) == 0)229return addr;230}231return null;232}233234/**235* Retrieves the address at index posn.236* @param posn The index of the address to retrieve.237* @return The address at the 0-based index posn. It must be in the238* range [0,getAddressCount()).239* @throws ArrayIndexOutOfBoundsException If posn not in the specified240* range.241*/242public RefAddr get(int posn) {243return addrs.elementAt(posn);244}245246/**247* Retrieves an enumeration of the addresses in this reference.248* When addresses are added, changed or removed from this reference,249* its effects on this enumeration are undefined.250*251* @return An non-null enumeration of the addresses252* ({@code RefAddr}) in this reference.253* If this reference has zero addresses, an enumeration with254* zero elements is returned.255*/256public Enumeration<RefAddr> getAll() {257return addrs.elements();258}259260/**261* Retrieves the number of addresses in this reference.262*263* @return The nonnegative number of addresses in this reference.264*/265public int size() {266return addrs.size();267}268269/**270* Adds an address to the end of the list of addresses.271*272* @param addr The non-null address to add.273*/274public void add(RefAddr addr) {275addrs.addElement(addr);276}277278/**279* Adds an address to the list of addresses at index posn.280* All addresses at index posn or greater are shifted up281* the list by one (away from index 0).282*283* @param posn The 0-based index of the list to insert addr.284* @param addr The non-null address to add.285* @throws ArrayIndexOutOfBoundsException If posn not in the specified286* range.287*/288public void add(int posn, RefAddr addr) {289addrs.insertElementAt(addr, posn);290}291292/**293* Deletes the address at index posn from the list of addresses.294* All addresses at index greater than posn are shifted down295* the list by one (towards index 0).296*297* @param posn The 0-based index of in address to delete.298* @return The address removed.299* @throws ArrayIndexOutOfBoundsException If posn not in the specified300* range.301*/302public Object remove(int posn) {303Object r = addrs.elementAt(posn);304addrs.removeElementAt(posn);305return r;306}307308/**309* Deletes all addresses from this reference.310*/311public void clear() {312addrs.setSize(0);313}314315/**316* Determines whether obj is a reference with the same addresses317* (in same order) as this reference.318* The addresses are checked using RefAddr.equals().319* In addition to having the same addresses, the Reference also needs to320* have the same class name as this reference.321* The class factory and class factory location are not checked.322* If obj is null or not an instance of Reference, null is returned.323*324* @param obj The possibly null object to check.325* @return true if obj is equal to this reference; false otherwise.326*/327public boolean equals(Object obj) {328if ((obj != null) && (obj instanceof Reference)) {329Reference target = (Reference)obj;330// ignore factory information331if (target.className.equals(this.className) &&332target.size() == this.size()) {333Enumeration<RefAddr> mycomps = getAll();334Enumeration<RefAddr> comps = target.getAll();335while (mycomps.hasMoreElements())336if (!(mycomps.nextElement().equals(comps.nextElement())))337return false;338return true;339}340}341return false;342}343344/**345* Computes the hash code of this reference.346* The hash code is the sum of the hash code of its addresses.347*348* @return A hash code of this reference as an int.349*/350public int hashCode() {351int hash = className.hashCode();352for (Enumeration<RefAddr> e = getAll(); e.hasMoreElements();)353hash += e.nextElement().hashCode();354return hash;355}356357/**358* Generates the string representation of this reference.359* The string consists of the class name to which this reference refers,360* and the string representation of each of its addresses.361* This representation is intended for display only and not to be parsed.362*363* @return The non-null string representation of this reference.364*/365public String toString() {366StringBuilder sb = new StringBuilder("Reference Class Name: " +367className + "\n");368int len = addrs.size();369for (int i = 0; i < len; i++)370sb.append(get(i).toString());371372return sb.toString();373}374375/**376* Makes a copy of this reference using its class name377* list of addresses, class factory name and class factory location.378* Changes to the newly created copy does not affect this Reference379* and vice versa.380*/381public Object clone() {382Reference r = new Reference(className, classFactory, classFactoryLocation);383Enumeration<RefAddr> a = getAll();384r.addrs = new Vector<>();385386while (a.hasMoreElements())387r.addrs.addElement(a.nextElement());388return r;389}390/**391* Use serialVersionUID from JNDI 1.1.1 for interoperability392*/393private static final long serialVersionUID = -1673475790065791735L;394};395396397