src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPEndpoint.java
author rriggs
Fri, 07 Dec 2018 11:51:17 -0500
changeset 52902 e3398b2e1ab0
parent 47216 71c04702a3d5
permissions -rw-r--r--
8214971: Replace use of string.equals("") with isEmpty() Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12040
diff changeset
     2
 * Copyright (c) 1996, 2012, 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
package sun.rmi.transport.tcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.DataInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.DataOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.ConnectIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.rmi.server.RMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.server.RMIServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.server.RMISocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.AccessController;
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 14342
diff changeset
    41
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.rmi.runtime.NewThreadAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.rmi.transport.Channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.rmi.transport.Endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.rmi.transport.Target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.rmi.transport.Transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * TCPEndpoint represents some communication endpoint for an address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * space (VM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class TCPEndpoint implements Endpoint {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** IP address or host name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** custom client socket factory (null if not custom factory) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final RMIClientSocketFactory csf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** custom server socket factory (null if not custom factory) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final RMIServerSocketFactory ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** if local, the port number to listen on */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int listenPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** if local, the transport object associated with this endpoint */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private TCPTransport transport = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** the local host name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static String localHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** true if real local host name is known yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static boolean localHostKnown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // this should be a *private* method since it is privileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static int getInt(String name, int def) {
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 14342
diff changeset
    83
        return AccessController.doPrivileged(
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 14342
diff changeset
    84
                (PrivilegedAction<Integer>) () -> Integer.getInteger(name, def));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // this should be a *private* method since it is privileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static boolean getBoolean(String name) {
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 14342
diff changeset
    89
        return AccessController.doPrivileged(
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 14342
diff changeset
    90
                (PrivilegedAction<Boolean>) () -> Boolean.getBoolean(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns the value of the java.rmi.server.hostname property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static String getHostnameProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 14342
diff changeset
    98
            (PrivilegedAction<String>) () -> System.getProperty("java.rmi.server.hostname"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Find host name of local machine.  Property "java.rmi.server.hostname"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * is used if set, so server administrator can compensate for the possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * inablility to get fully qualified host name from VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        localHostKnown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        localHost = getHostnameProperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // could try querying CGI program here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (localHost == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                InetAddress localAddr = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                byte[] raw = localAddr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if ((raw[0] == 127) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    (raw[1] ==   0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    (raw[2] ==   0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    (raw[3] ==   1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    localHostKnown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                /* if the user wishes to use a fully qualified domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                 * name then attempt to find one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (getBoolean("java.rmi.server.useLocalHostName")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    localHost = FQDN.attemptFQDN(localAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    /* default to using ip addresses, names will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                     * work across seperate domains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    localHost = localAddr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                localHostKnown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                localHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            TCPTransport.tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                "localHostKnown = " + localHostKnown +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                ", localHost = " + localHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /** maps an endpoint key containing custom socket factories to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * their own unique endpoint */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // TBD: should this be a weak hash table?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Map<TCPEndpoint,LinkedList<TCPEndpoint>> localEndpoints =
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   151
        new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Create an endpoint for a specified host and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * This should not be used by external classes to create endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * for servers in this VM; use getLocalEndpoint instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public TCPEndpoint(String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this(host, port, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Create a custom socket factory endpoint for a specified host and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * This should not be used by external classes to create endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * for servers in this VM; use getLocalEndpoint instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public TCPEndpoint(String host, int port, RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                       RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (host == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.csf = csf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.ssf = ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Get an endpoint for the local address space on specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * If port number is 0, it returns shared default endpoint object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * whose host name and port may or may not have been determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static TCPEndpoint getLocalEndpoint(int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return getLocalEndpoint(port, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public static TCPEndpoint getLocalEndpoint(int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                               RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                               RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * Find mapping for an endpoint key to the list of local unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * endpoints for this client/server socket factory pair (perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * null) for the specific port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        TCPEndpoint ep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        synchronized (localEndpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            TCPEndpoint endpointKey = new TCPEndpoint(null, port, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            String localHost = resampleLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (epList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                 * Create new endpoint list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                ep = new TCPEndpoint(localHost, port, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                epList = new LinkedList<TCPEndpoint>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                epList.add(ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                ep.listenPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                ep.transport = new TCPTransport(epList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                localEndpoints.put(endpointKey, epList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    TCPTransport.tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        "created local endpoint for socket factory " + ssf +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        " on port " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                synchronized (epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    ep = epList.getLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    String lastHost = ep.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    int lastPort =  ep.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    TCPTransport lastTransport = ep.transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    // assert (localHost == null ^ lastHost != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    if (localHost != null && !localHost.equals(lastHost)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                         * Hostname has been updated; add updated endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                         * to list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        if (lastPort != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                             * Remove outdated endpoints only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                             * port has already been set on those endpoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            epList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        ep = new TCPEndpoint(localHost, lastPort, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        ep.listenPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        ep.transport = lastTransport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        epList.add(ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return ep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Resamples the local hostname and returns the possibly-updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * local hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private static String resampleLocalHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        String hostnameProperty = getHostnameProperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        synchronized (localEndpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // assert(localHostKnown ^ (localHost == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (hostnameProperty != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (!localHostKnown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                     * If the local hostname is unknown, update ALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                     * existing endpoints with the new hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    setLocalHost(hostnameProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                } else if (!hostnameProperty.equals(localHost)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                     * Only update the localHost field for reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                     * in future endpoint creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    localHost = hostnameProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        TCPTransport.tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            "updated local hostname to: " + localHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return localHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Set the local host name, if currently unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static void setLocalHost(String host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // assert (host != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        synchronized (localEndpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             * If host is not known, change the host field of ALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
             * the local endpoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (!localHostKnown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                localHost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                localHostKnown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    TCPTransport.tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        "local host set to " + host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                for (LinkedList<TCPEndpoint> epList : localEndpoints.values())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    synchronized (epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        for (TCPEndpoint ep : epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            ep.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Set the port of the (shared) default endpoint object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * When first created, it contains port 0 because the transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * hasn't tried to listen to get assigned a port, or if listening
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * failed, a port hasn't been assigned from the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    static void setDefaultPort(int port, RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                               RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        TCPEndpoint endpointKey = new TCPEndpoint(null, 0, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        synchronized (localEndpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            synchronized (epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                int size = epList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                TCPEndpoint lastEp = epList.getLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                for (TCPEndpoint ep : epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    ep.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (size > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                     * Remove all but the last element of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                     * (which contains the most recent hostname).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    epList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    epList.add(lastEp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
             * Allow future exports to use the actual bound port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
             * explicitly (see 6269166).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            TCPEndpoint newEndpointKey = new TCPEndpoint(null, port, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            localEndpoints.put(newEndpointKey, epList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                TCPTransport.tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    "default port for server socket factory " + ssf +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    " and client socket factory " + csf +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    " set to " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Returns transport for making connections to remote endpoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * (here, the default transport at port 0 is used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public Transport getOutboundTransport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        TCPEndpoint localEndpoint = getLocalEndpoint(0, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return localEndpoint.transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Returns the current list of known transports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * The returned list is an unshared collection of Transports,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * including all transports which may have channels to remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * endpoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private static Collection<TCPTransport> allKnownTransports() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // Loop through local endpoints, getting the transport of each one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        Set<TCPTransport> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        synchronized (localEndpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // presize s to number of localEndpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            s = new HashSet<TCPTransport>(localEndpoints.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            for (LinkedList<TCPEndpoint> epList : localEndpoints.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                 * Each local endpoint has its transport added to s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                 * Note: the transport is the same for all endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                 * in the list, so it is okay to pick any one of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                TCPEndpoint ep = epList.getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                s.add(ep.transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Release idle outbound connections to reduce demand on I/O resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * All transports are asked to release excess connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public static void shedConnectionCaches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        for (TCPTransport transport : allKnownTransports()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            transport.shedConnectionCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Export the object to accept incoming calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public void exportObject(Target target) throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        transport.exportObject(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns a channel for this (remote) endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public Channel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return getOutboundTransport().getChannel(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Returns address for endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Returns the port for this endpoint.  If this endpoint was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * created as a server endpoint (using getLocalEndpoint) for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * default/anonymous port and its inbound transport has started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * listening, this method returns (instead of zero) the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * bound port suitable for passing to clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Returns the port that this endpoint's inbound transport listens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * on, if this endpoint was created as a server endpoint (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * getLocalEndpoint).  If this endpoint was created for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * default/anonymous port, then this method returns zero even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * the transport has started listening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public int getListenPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return listenPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Returns the transport for incoming connections to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * endpoint, if this endpoint was created as a server endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * (using getLocalEndpoint).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public Transport getInboundTransport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Get the client socket factory associated with this endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public RMIClientSocketFactory getClientSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return csf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Get the server socket factory associated with this endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public RMIServerSocketFactory getServerSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Return string representation for endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return "[" + host + ":" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            (ssf != null ? "," + ssf : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            (csf != null ? "," + csf : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if ((obj != null) && (obj instanceof TCPEndpoint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            TCPEndpoint ep = (TCPEndpoint) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (port != ep.port || !host.equals(ep.host))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (((csf == null) ^ (ep.csf == null)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                ((ssf == null) ^ (ep.ssf == null)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
             * Fix for 4254510: perform socket factory *class* equality check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
             * before socket factory equality check to avoid passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
             * a potentially naughty socket factory to this endpoint's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
             * {client,server} socket factory equals method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if ((csf != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                !(csf.getClass() == ep.csf.getClass() && csf.equals(ep.csf)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if ((ssf != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                !(ssf.getClass() == ep.ssf.getClass() && ssf.equals(ep.ssf)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /* codes for the self-describing formats of wire representation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private static final int FORMAT_HOST_PORT           = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private static final int FORMAT_HOST_PORT_FACTORY   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Write endpoint to output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public void write(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (csf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            out.writeByte(FORMAT_HOST_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            out.writeUTF(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            out.writeInt(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            out.writeByte(FORMAT_HOST_PORT_FACTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            out.writeUTF(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            out.writeInt(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            out.writeObject(csf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Get the endpoint from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @param in the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @exception IOException If id could not be read (due to stream failure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public static TCPEndpoint read(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        RMIClientSocketFactory csf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        byte format = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        switch (format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
          case FORMAT_HOST_PORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            host = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            port = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
          case FORMAT_HOST_PORT_FACTORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            host = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            port = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            csf = (RMIClientSocketFactory) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            throw new IOException("invalid endpoint format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return new TCPEndpoint(host, port, csf, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Write endpoint to output stream in older format used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * UnicastRef for JDK1.1 compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public void writeHostPortFormat(DataOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (csf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            throw new InternalError("TCPEndpoint.writeHostPortFormat: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                "called for endpoint with non-null socket factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        out.writeUTF(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        out.writeInt(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Create a new endpoint from input stream data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param in the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public static TCPEndpoint readHostPortFormat(DataInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        String host = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int port = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return new TCPEndpoint(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private static RMISocketFactory chooseFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        RMISocketFactory sf = RMISocketFactory.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (sf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            sf = TCPTransport.defaultSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Open and return new client socket connection to endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    Socket newSocket() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            TCPTransport.tcpLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                "opening socket to " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            RMIClientSocketFactory clientFactory = csf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if (clientFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                clientFactory = chooseFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            socket = clientFactory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            throw new java.rmi.UnknownHostException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                "Unknown host: " + host, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        } catch (java.net.ConnectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            throw new java.rmi.ConnectException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                "Connection refused to host: " + host, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            // We might have simply run out of file descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                TCPEndpoint.shedConnectionCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                // REMIND: should we retry createSocket?
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   626
            } catch (OutOfMemoryError | Exception mem) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                // don't quit if out of memory
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   628
                // or shed fails non-catastrophically
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            throw new ConnectIOException("Exception creating connection to: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                host, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // set socket to disable Nagle's algorithm (always send immediately)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        // TBD: should this be left up to socket factory instead?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            socket.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // if we fail to set this, ignore and proceed anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // fix 4187495: explicitly set SO_KEEPALIVE to prevent client hangs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            socket.setKeepAlive(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            // ignore and proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Return new server socket to listen for connections on this endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    ServerSocket newServerSocket() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            TCPTransport.tcpLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                "creating server socket on " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        RMIServerSocketFactory serverFactory = ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (serverFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            serverFactory = chooseFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        ServerSocket server = serverFactory.createServerSocket(listenPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        // if we listened on an anonymous port, set the default port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        // (for this socket factory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (listenPort == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            setDefaultPort(server.getLocalPort(), csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * The class FQDN encapsulates a routine that makes a best effort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * attempt to retrieve the fully qualified domain name of the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @author  Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    private static class FQDN implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         * strings in which we can store discovered fqdn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        private String reverseLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        private String hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        private FQDN(String hostAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            this.hostAddress = hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * Do our best to obtain a fully qualified hostname for the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * host.  Perform the following steps to get a localhostname:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * 1. InetAddress.getLocalHost().getHostName() - if contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         *    '.' use as FQDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * 2. if no '.' query name service for FQDN in a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         *    Note: We query the name service for an FQDN by creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         *    an InetAddress via a stringified copy of the local ip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         *    address; this creates an InetAddress with a null hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         *    Asking for the hostname of this InetAddress causes a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         *    service lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         * 3. if name service takes too long to return, use ip address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * 4. if name service returns but response contains no '.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         *    default to ipaddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        static String attemptFQDN(InetAddress localAddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            throws java.net.UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            String hostName = localAddr.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if (hostName.indexOf('.') < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                String hostAddress = localAddr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                FQDN f = new FQDN(hostAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                int nameServiceTimeOut =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    TCPEndpoint.getInt("sun.rmi.transport.tcp.localHostNameTimeOut",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                                       10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    synchronized(f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        f.getFQDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        /* wait to obtain an FQDN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        f.wait(nameServiceTimeOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    /* propagate the exception to the caller */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                hostName = f.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   741
                if ((hostName == null) || (hostName.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    || (hostName.indexOf('.') < 0 )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    hostName = hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            return hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * Method that that will start a thread to wait to retrieve a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * fully qualified domain name from a name service.  The spawned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * thread may never return but we have marked it as a daemon so the vm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * will terminate appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        private void getFQDN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            /* FQDN finder will run in RMI threadgroup. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            Thread t = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                new NewThreadAction(FQDN.this, "FQDN Finder", true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        private synchronized String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            return reverseLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         * thread to query a name service for the fqdn of this host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        public void run()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                name = InetAddress.getByName(hostAddress).getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    reverseLookup = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    this.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
}