jdk/src/share/classes/sun/rmi/transport/proxy/RMIMasterSocketFactory.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.rmi.transport.proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.rmi.server.RMISocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.rmi.runtime.NewThreadAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.action.GetBooleanAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.action.GetLongAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * RMIMasterSocketFactory attempts to create a socket connection to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * specified host using successively less efficient mechanisms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * until one succeeds.  If the host is successfully connected to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the factory for the successful mechanism is stored in an internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * hash table keyed by the host name, so that future attempts to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * connect to the same host will automatically use the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class RMIMasterSocketFactory extends RMISocketFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** "proxy" package log level */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static int logLevel = LogStream.parseLevel(getLogLevel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static String getLogLevel() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    53
        return java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            new sun.security.action.GetPropertyAction("sun.rmi.transport.proxy.logLevel"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* proxy package log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final Log proxyLog =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Log.getLog("sun.rmi.transport.tcp.proxy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                   "transport", RMIMasterSocketFactory.logLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** timeout for attemping direct socket connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static long connectTimeout = getConnectTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static long getConnectTimeout() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    66
        return java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                new GetLongAction("sun.rmi.transport.proxy.connectTimeout",
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    68
                              15000)).longValue(); // default: 15 seconds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** whether to fallback to HTTP on general connect failures */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    72
    private static final boolean eagerHttpFallback =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        java.security.AccessController.doPrivileged(new GetBooleanAction(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    74
            "sun.rmi.transport.proxy.eagerHttpFallback")).booleanValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** table of hosts successfully connected to and the factory used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private Hashtable successTable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** maximum number of hosts to remember successful connection to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final int MaxRememberedHosts = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** list of the hosts in successTable in initial connection order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private Vector hostList = new Vector(MaxRememberedHosts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** default factory to initally use for direct socket connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected RMISocketFactory initialFactory = new RMIDirectSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** ordered list of factories to try as alternate connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * mechanisms if a direct socket connections fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected Vector altFactoryList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Create a RMIMasterSocketFactory object.  Establish order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * connection mechanisms to attempt on createSocket, if a direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * socket connection fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public RMIMasterSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        altFactoryList = new Vector(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        boolean setFactories = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            String proxyHost;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   103
            proxyHost = java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                new sun.security.action.GetPropertyAction("http.proxyHost"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (proxyHost == null)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   107
                proxyHost = java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    new sun.security.action.GetPropertyAction("proxyHost"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   110
            Boolean tmp = java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new sun.security.action.GetBooleanAction("java.rmi.server.disableHttp"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (!tmp.booleanValue() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                (proxyHost != null && proxyHost.length() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                setFactories = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // unable to obtain the properties, so assume default behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            setFactories = true;
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 (setFactories) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            altFactoryList.addElement(new RMIHttpToPortSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            altFactoryList.addElement(new RMIHttpToCGISocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Create a new client socket.  If we remember connecting to this host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * successfully before, then use the same factory again.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * try using a direct socket connection and then the alternate factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * in the order specified in altFactoryList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public Socket createSocket(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (proxyLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            proxyLog.log(Log.BRIEF, "host: " + host + ", port: " + port);
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
         * If we don't have any alternate factories to consult, short circuit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * the fallback procedure and delegate to the initial factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (altFactoryList.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return initialFactory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        RMISocketFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * If we remember successfully connecting to this host before,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * use the same factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        factory = (RMISocketFactory) successTable.get(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (proxyLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                proxyLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    "previously successful factory found: " + factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return factory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * Next, try a direct socket connection.  Open socket in another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * thread and only wait for specified timeout, in case the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * would otherwise spend minutes trying an unreachable host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Socket initialSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Socket fallbackSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        final AsyncConnector connector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            new AsyncConnector(initialFactory, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                AccessController.getContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                // connection must be attempted with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                // this thread's access control context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        IOException initialFailure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            synchronized (connector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   181
                Thread t = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   182
                    new NewThreadAction(connector, "AsyncConnector", true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    long deadline = now + connectTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        connector.wait(deadline - now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        initialSocket = checkConnector(connector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        if (initialSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    } while (now < deadline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    throw new InterruptedIOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        "interrupted while waiting for connector");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // assume no route to host (for now) if no connection yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (initialSocket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw new NoRouteToHostException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    "connect timed out: " + host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            proxyLog.log(Log.BRIEF, "direct socket connection successful");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return initialSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            initialFailure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } catch (NoRouteToHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            initialFailure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (eagerHttpFallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                initialFailure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (initialFailure != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (proxyLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    proxyLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        "direct socket connection failed: ", initialFailure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                // Finally, try any alternate connection mechanisms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                for (int i = 0; i < altFactoryList.size(); ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    factory = (RMISocketFactory) altFactoryList.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if (proxyLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            proxyLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                "trying with factory: " + factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        // For HTTP connections, the output (POST request) must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        // be sent before we verify a successful connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        // So, sacrifice a socket for the sake of testing...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        // The following sequence should verify a successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        // HTTP connection if no IOException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        Socket testSocket = factory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        InputStream in = testSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        int b = in.read(); // probably -1 for EOF...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        testSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        if (proxyLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            proxyLog.log(Log.BRIEF, "factory failed: ", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    proxyLog.log(Log.BRIEF, "factory succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    // factory succeeded, open new socket for caller's use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        fallbackSocket = factory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    } catch (IOException ex) {  // if it fails 2nd time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }                           // just give up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        synchronized (successTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                // check once again to see if direct connection succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                synchronized (connector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    initialSocket = checkConnector(connector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (initialSocket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    // if we had made another one as well, clean it up...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    if (fallbackSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        fallbackSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    return initialSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // if connector ever does get socket, it won't be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                connector.notUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                initialFailure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } catch (NoRouteToHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                initialFailure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (eagerHttpFallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    initialFailure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // if we had found an alternate mechanism, go and use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (fallbackSocket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                // remember this successful host/factory pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                rememberFactory(host, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                return fallbackSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw initialFailure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Remember a successful factory for connecting to host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Currently, excess hosts are removed from the remembered list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * using a Least Recently Created strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    void rememberFactory(String host, RMISocketFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        synchronized (successTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            while (hostList.size() >= MaxRememberedHosts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                successTable.remove(hostList.elementAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                hostList.removeElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            hostList.addElement(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            successTable.put(host, factory);
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
     * Check if an AsyncConnector succeeded.  If not, return socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * given to fall back to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    Socket checkConnector(AsyncConnector connector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Exception e = connector.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            e.fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
             * The AsyncConnector implementation guaranteed that the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
             * will be either an IOException or a RuntimeException, and we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
             * only throw one of those, so convince that compiler that it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
             * be one of those.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (e instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                throw (IOException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } else if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                throw (RuntimeException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                throw new Error("internal error: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    "unexpected checked exception: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return connector.getSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Create a new server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public ServerSocket createServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        //return new HttpAwareServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return initialFactory.createServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * AsyncConnector is used by RMIMasterSocketFactory to attempt socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * connections on a separate thread.  This allows RMIMasterSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * to control how long it will wait for the connection to succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private class AsyncConnector implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /** what factory to use to attempt connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        private RMISocketFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        /** the host to connect to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        private String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /** the port to connect to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        /** access control context to attempt connection within */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        private AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        /** exception that occurred during connection, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        private Exception exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        /** the connected socket, if successful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        private Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        /** socket should be closed after created, if ever */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        private boolean cleanUp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * Create a new asynchronous connector object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        AsyncConnector(RMISocketFactory factory, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                       AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            this.factory = factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            this.host    = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            this.port    = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            this.acc     = acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                security.checkConnect(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
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
         * Attempt socket connection in separate thread.  If successful,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * notify master waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                 * Using the privileges of the thread that wants to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                 * connection is tempting, but it will fail with applets with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                 * the current applet security manager because the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                 * network connection policy is not captured in the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                 * framework of the access control context we have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                 * java.security.AccessController.beginPrivileged(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    Socket temp = factory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        socket = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    rememberFactory(host, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        if (cleanUp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                          try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                              socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                          } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                     * Note that the only exceptions which could actually have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                     * occurred here are IOException or RuntimeException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                 * See above comments for matching beginPrivileged() call that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                 * is also commented out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                 * java.security.AccessController.endPrivileged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * Get exception that occurred during connection attempt, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * In the current implementation, this is guaranteed to be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * an IOException or a RuntimeException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        private synchronized Exception getException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * Get successful socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        private synchronized Socket getSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * Note that this connector's socket, if ever successfully created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * will not be used, so it should be cleaned up quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        synchronized void notUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            cleanUp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
}