jdk/src/java.naming/share/classes/javax/naming/BinaryRefAddr.java
author igerasim
Tue, 09 Sep 2014 19:02:36 +0400
changeset 26458 d6a5aed9204b
parent 25859 3317bb8137f4
permissions -rw-r--r--
7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.naming;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  * This class represents the binary form of the address of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
  * a communications end-point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  * A BinaryRefAddr consists of a type that describes the communication mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  * and an opaque buffer containing the address description
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  * specific to that communication mechanism. The format and interpretation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * the address type and the contents of the opaque buffer are based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  * the agreement of three parties: the client that uses the address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * the object/server that can be reached using the address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * and the administrator or program that creates the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * An example of a binary reference address is an BER X.500 presentation address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * Another example of a binary reference address is a serialized form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * a service's object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * A binary reference address is immutable in the sense that its fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * once created, cannot be replaced. However, it is possible to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  * the byte array used to hold the opaque buffer. Programs are strongly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * recommended against changing this byte array. Changes to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  * byte array need to be explicitly synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * @see RefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * @see StringRefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  * The serialized form of a BinaryRefAddr object consists of its type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  * name String and a byte array containing its "contents".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class BinaryRefAddr extends RefAddr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Contains the bytes of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * This field is initialized by the constructor and returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * using getAddressBytes() and getAddressContents().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private byte[] buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      * Constructs a new instance of BinaryRefAddr using its address type and a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      * array for contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
      * @param addrType A non-null string describing the type of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
      * @param src      The non-null contents of the address as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      *                 The contents of src is copied into the new BinaryRefAddr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public BinaryRefAddr(String addrType, byte[] src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this(addrType, src, 0, src.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      * Constructs a new instance of BinaryRefAddr using its address type and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      * a region of a byte array for contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      * @param addrType A non-null string describing the type of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * @param src      The non-null contents of the address as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      *                 The contents of src is copied into the new BinaryRefAddr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      * @param offset   The starting index in src to get the bytes.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
    92
      *                 {@code 0 <= offset <= src.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      * @param count    The number of bytes to extract from src.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
    94
      *                 {@code 0 <= count <= src.length-offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public BinaryRefAddr(String addrType, byte[] src, int offset, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super(addrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        buf = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.arraycopy(src, offset, buf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      * Retrieves the contents of this address as an Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      * The result is a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      * Changes to this array will affect this BinaryRefAddr's contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      * Programs are recommended against changing this array's contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      * and to lock the buffer if they need to change it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      * @return The non-null buffer containing this address's contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public Object getContent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      * Determines whether obj is equal to this address.  It is equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      * it contains the same address type and their contents are byte-wise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      * equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      * @param obj      The possibly null object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      * @return true if the object is equal; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if ((obj != null) && (obj instanceof BinaryRefAddr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            BinaryRefAddr target = (BinaryRefAddr)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (addrType.compareTo(target.addrType) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (buf == null && target.buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (buf == null || target.buf == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    buf.length != target.buf.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                for (int i = 0; i < buf.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    if (buf[i] != target.buf[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      * Computes the hash code of this address using its address type and contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      * Two BinaryRefAddrs have the same hash code if they have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      * the same address type and the same contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
      * It is also possible for different BinaryRefAddrs to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      * the same hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      * @return The hash code of this address as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int hash = addrType.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for (int i = 0; i < buf.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            hash += buf[i];     // %%% improve later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      * Generates the string representation of this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      * The string consists of the address's type and contents with labels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
      * The first 32 bytes of contents are displayed (in hexadecimal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      * If there are more than 32 bytes, "..." is used to indicate more.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      * This string is meant to used for debugging purposes and not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      * meant to be interpreted programmatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      * @return The non-null string representation of this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public String toString(){
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   168
        StringBuilder str = new StringBuilder("Address Type: " + addrType + "\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        str.append("AddressContents: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        for (int i = 0; i<buf.length && i < 32; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            str.append(Integer.toHexString(buf[i]) +" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (buf.length >= 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            str.append(" ...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return (str.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Use serialVersionUID from JNDI 1.1.1 for interoperability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static final long serialVersionUID = -3415254970957330361L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}