001 /*
002 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.naming;
027
028 /**
029 * This class represents the binary form of the address of
030 * a communications end-point.
031 *<p>
032 * A BinaryRefAddr consists of a type that describes the communication mechanism
033 * and an opaque buffer containing the address description
034 * specific to that communication mechanism. The format and interpretation of
035 * the address type and the contents of the opaque buffer are based on
036 * the agreement of three parties: the client that uses the address,
037 * the object/server that can be reached using the address,
038 * and the administrator or program that creates the address.
039 *<p>
040 * An example of a binary reference address is an BER X.500 presentation address.
041 * Another example of a binary reference address is a serialized form of
042 * a service's object handle.
043 *<p>
044 * A binary reference address is immutable in the sense that its fields
045 * once created, cannot be replaced. However, it is possible to access
046 * the byte array used to hold the opaque buffer. Programs are strongly
047 * recommended against changing this byte array. Changes to this
048 * byte array need to be explicitly synchronized.
049 *
050 * @author Rosanna Lee
051 * @author Scott Seligman
052 * @version 1.14 07/05/05
053 *
054 * @see RefAddr
055 * @see StringRefAddr
056 * @since 1.3
057 */
058
059 /*
060 * The serialized form of a BinaryRefAddr object consists of its type
061 * name String and a byte array containing its "contents".
062 */
063
064 public class BinaryRefAddr extends RefAddr {
065 /**
066 * Contains the bytes of the address.
067 * This field is initialized by the constructor and returned
068 * using getAddressBytes() and getAddressContents().
069 * @serial
070 */
071 private byte[] buf = null;
072
073 /**
074 * Constructs a new instance of BinaryRefAddr using its address type and a byte
075 * array for contents.
076 *
077 * @param addrType A non-null string describing the type of the address.
078 * @param src The non-null contents of the address as a byte array.
079 * The contents of src is copied into the new BinaryRefAddr.
080 */
081 public BinaryRefAddr(String addrType, byte[] src) {
082 this (addrType, src, 0, src.length);
083 }
084
085 /**
086 * Constructs a new instance of BinaryRefAddr using its address type and
087 * a region of a byte array for contents.
088 *
089 * @param addrType A non-null string describing the type of the address.
090 * @param src The non-null contents of the address as a byte array.
091 * The contents of src is copied into the new BinaryRefAddr.
092 * @param offset The starting index in src to get the bytes.
093 * 0 <= offset <= src.length.
094 * @param count The number of bytes to extract from src.
095 * 0 <= count <= src.length-offset.
096 */
097 public BinaryRefAddr(String addrType, byte[] src, int offset,
098 int count) {
099 super (addrType);
100 buf = new byte[count];
101 System.arraycopy(src, offset, buf, 0, count);
102 }
103
104 /**
105 * Retrieves the contents of this address as an Object.
106 * The result is a byte array.
107 * Changes to this array will affect this BinaryRefAddr's contents.
108 * Programs are recommended against changing this array's contents
109 * and to lock the buffer if they need to change it.
110 *
111 * @return The non-null buffer containing this address's contents.
112 */
113 public Object getContent() {
114 return buf;
115 }
116
117 /**
118 * Determines whether obj is equal to this address. It is equal if
119 * it contains the same address type and their contents are byte-wise
120 * equivalent.
121 * @param obj The possibly null object to check.
122 * @return true if the object is equal; false otherwise.
123 */
124 public boolean equals(Object obj) {
125 if ((obj != null) && (obj instanceof BinaryRefAddr)) {
126 BinaryRefAddr target = (BinaryRefAddr) obj;
127 if (addrType.compareTo(target.addrType) == 0) {
128 if (buf == null && target.buf == null)
129 return true;
130 if (buf == null || target.buf == null
131 || buf.length != target.buf.length)
132 return false;
133 for (int i = 0; i < buf.length; i++)
134 if (buf[i] != target.buf[i])
135 return false;
136 return true;
137 }
138 }
139 return false;
140 }
141
142 /**
143 * Computes the hash code of this address using its address type and contents.
144 * Two BinaryRefAddrs have the same hash code if they have
145 * the same address type and the same contents.
146 * It is also possible for different BinaryRefAddrs to have
147 * the same hash code.
148 *
149 * @return The hash code of this address as an int.
150 */
151 public int hashCode() {
152 int hash = addrType.hashCode();
153 for (int i = 0; i < buf.length; i++) {
154 hash += buf[i]; // %%% improve later
155 }
156 return hash;
157 }
158
159 /**
160 * Generates the string representation of this address.
161 * The string consists of the address's type and contents with labels.
162 * The first 32 bytes of contents are displayed (in hexadecimal).
163 * If there are more than 32 bytes, "..." is used to indicate more.
164 * This string is meant to used for debugging purposes and not
165 * meant to be interpreted programmatically.
166 * @return The non-null string representation of this address.
167 */
168 public String toString() {
169 StringBuffer str = new StringBuffer("Address Type: " + addrType
170 + "\n");
171
172 str.append("AddressContents: ");
173 for (int i = 0; i < buf.length && i < 32; i++) {
174 str.append(Integer.toHexString(buf[i]) + " ");
175 }
176 if (buf.length >= 32)
177 str.append(" ...\n");
178 return (str.toString());
179 }
180
181 /**
182 * Use serialVersionUID from JNDI 1.1.1 for interoperability
183 */
184 private static final long serialVersionUID = -3415254970957330361L;
185 }
|