Search in sources :

Example 1 with NewThreadAction

use of sun.rmi.runtime.NewThreadAction in project jdk8u_jdk by JetBrains.

the class RMIMasterSocketFactory method createSocket.

/**
     * Create a new client socket.  If we remember connecting to this host
     * successfully before, then use the same factory again.  Otherwise,
     * try using a direct socket connection and then the alternate factories
     * in the order specified in altFactoryList.
     */
public Socket createSocket(String host, int port) throws IOException {
    if (proxyLog.isLoggable(Log.BRIEF)) {
        proxyLog.log(Log.BRIEF, "host: " + host + ", port: " + port);
    }
    /*
         * If we don't have any alternate factories to consult, short circuit
         * the fallback procedure and delegate to the initial factory.
         */
    if (altFactoryList.size() == 0) {
        return initialFactory.createSocket(host, port);
    }
    RMISocketFactory factory;
    /*
         * If we remember successfully connecting to this host before,
         * use the same factory.
         */
    factory = successTable.get(host);
    if (factory != null) {
        if (proxyLog.isLoggable(Log.BRIEF)) {
            proxyLog.log(Log.BRIEF, "previously successful factory found: " + factory);
        }
        return factory.createSocket(host, port);
    }
    /*
         * Next, try a direct socket connection.  Open socket in another
         * thread and only wait for specified timeout, in case the socket
         * would otherwise spend minutes trying an unreachable host.
         */
    Socket initialSocket = null;
    Socket fallbackSocket = null;
    final AsyncConnector connector = new AsyncConnector(initialFactory, host, port, AccessController.getContext());
    // connection must be attempted with
    // this thread's access control context
    IOException initialFailure = null;
    try {
        synchronized (connector) {
            Thread t = java.security.AccessController.doPrivileged(new NewThreadAction(connector, "AsyncConnector", true));
            t.start();
            try {
                long now = System.currentTimeMillis();
                long deadline = now + connectTimeout;
                do {
                    connector.wait(deadline - now);
                    initialSocket = checkConnector(connector);
                    if (initialSocket != null)
                        break;
                    now = System.currentTimeMillis();
                } while (now < deadline);
            } catch (InterruptedException e) {
                throw new InterruptedIOException("interrupted while waiting for connector");
            }
        }
        // assume no route to host (for now) if no connection yet
        if (initialSocket == null)
            throw new NoRouteToHostException("connect timed out: " + host);
        proxyLog.log(Log.BRIEF, "direct socket connection successful");
        return initialSocket;
    } catch (UnknownHostException | NoRouteToHostException e) {
        initialFailure = e;
    } catch (SocketException e) {
        if (eagerHttpFallback) {
            initialFailure = e;
        } else {
            throw e;
        }
    } finally {
        if (initialFailure != null) {
            if (proxyLog.isLoggable(Log.BRIEF)) {
                proxyLog.log(Log.BRIEF, "direct socket connection failed: ", initialFailure);
            }
            // Finally, try any alternate connection mechanisms.
            for (int i = 0; i < altFactoryList.size(); ++i) {
                factory = altFactoryList.elementAt(i);
                if (proxyLog.isLoggable(Log.BRIEF)) {
                    proxyLog.log(Log.BRIEF, "trying with factory: " + factory);
                }
                try (Socket testSocket = factory.createSocket(host, port)) {
                    // For HTTP connections, the output (POST request) must
                    // be sent before we verify a successful connection.
                    // So, sacrifice a socket for the sake of testing...
                    // The following sequence should verify a successful
                    // HTTP connection if no IOException is thrown.
                    InputStream in = testSocket.getInputStream();
                    // probably -1 for EOF...
                    int b = in.read();
                } catch (IOException ex) {
                    if (proxyLog.isLoggable(Log.BRIEF)) {
                        proxyLog.log(Log.BRIEF, "factory failed: ", ex);
                    }
                    continue;
                }
                proxyLog.log(Log.BRIEF, "factory succeeded");
                // factory succeeded, open new socket for caller's use
                try {
                    fallbackSocket = factory.createSocket(host, port);
                } catch (IOException ex) {
                // if it fails 2nd time,
                }
                // just give up
                break;
            }
        }
    }
    synchronized (successTable) {
        try {
            // check once again to see if direct connection succeeded
            synchronized (connector) {
                initialSocket = checkConnector(connector);
            }
            if (initialSocket != null) {
                // if we had made another one as well, clean it up...
                if (fallbackSocket != null)
                    fallbackSocket.close();
                return initialSocket;
            }
            // if connector ever does get socket, it won't be used
            connector.notUsed();
        } catch (UnknownHostException | NoRouteToHostException e) {
            initialFailure = e;
        } catch (SocketException e) {
            if (eagerHttpFallback) {
                initialFailure = e;
            } else {
                throw e;
            }
        }
        // if we had found an alternate mechanism, go and use it
        if (fallbackSocket != null) {
            // remember this successful host/factory pair
            rememberFactory(host, factory);
            return fallbackSocket;
        }
        throw initialFailure;
    }
}
Also used : RMISocketFactory(java.rmi.server.RMISocketFactory) NewThreadAction(sun.rmi.runtime.NewThreadAction)

Example 2 with NewThreadAction

use of sun.rmi.runtime.NewThreadAction in project jdk8u_jdk by JetBrains.

the class TCPTransport method listen.

/**
     * Listen on transport's endpoint.
     */
private void listen() throws RemoteException {
    assert Thread.holdsLock(this);
    TCPEndpoint ep = getEndpoint();
    int port = ep.getPort();
    if (server == null) {
        if (tcpLog.isLoggable(Log.BRIEF)) {
            tcpLog.log(Log.BRIEF, "(port " + port + ") create server socket");
        }
        try {
            server = ep.newServerSocket();
            /*
                 * Don't retry ServerSocket if creation fails since
                 * "port in use" will cause export to hang if an
                 * RMIFailureHandler is not installed.
                 */
            Thread t = AccessController.doPrivileged(new NewThreadAction(new AcceptLoop(server), "TCP Accept-" + port, true));
            t.start();
        } catch (java.net.BindException e) {
            throw new ExportException("Port already in use: " + port, e);
        } catch (IOException e) {
            throw new ExportException("Listen failed on port: " + port, e);
        }
    } else {
        // otherwise verify security access to existing server socket
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkListen(port);
        }
    }
}
Also used : IOException(java.io.IOException) Endpoint(sun.rmi.transport.Endpoint) NewThreadAction(sun.rmi.runtime.NewThreadAction) ExportException(java.rmi.server.ExportException)

Example 3 with NewThreadAction

use of sun.rmi.runtime.NewThreadAction in project jdk8u_jdk by JetBrains.

the class ConnectionAcceptor method startNewAcceptor.

/**
     * Start a new thread to accept connections.
     */
public void startNewAcceptor() {
    Thread t = AccessController.doPrivileged(new NewThreadAction(ConnectionAcceptor.this, "Multiplex Accept-" + ++threadNum, true));
    t.start();
}
Also used : NewThreadAction(sun.rmi.runtime.NewThreadAction)

Example 4 with NewThreadAction

use of sun.rmi.runtime.NewThreadAction in project jdk8u_jdk by JetBrains.

the class SequenceEntry method refSetRemove.

/**
     * Remove endpoint from the reference set.
     */
private synchronized void refSetRemove(VMID vmid) {
    // remove notification request
    DGCImpl.getDGCImpl().unregisterTarget(vmid, this);
    if (refSet.removeElement(vmid) && refSet.isEmpty()) {
        // remove object from table.
        if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
            DGCImpl.dgcLog.log(Log.VERBOSE, "reference set is empty: target = " + this);
        }
        /*
             * If the remote object implements the Unreferenced interface,
             * invoke its unreferenced callback in a separate thread.
             */
        Remote obj = getImpl();
        if (obj instanceof Unreferenced) {
            final Unreferenced unrefObj = (Unreferenced) obj;
            final Thread t = java.security.AccessController.doPrivileged(new NewThreadAction(new Runnable() {

                public void run() {
                    unrefObj.unreferenced();
                }
            }, "Unreferenced-" + nextThreadNum++, false, true));
            // REMIND: access to nextThreadNum not synchronized; you care?
            /*
                 * We must manually set the context class loader appropriately
                 * for threads that may invoke user code (see bugid 4171278).
                 */
            java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {

                public Void run() {
                    t.setContextClassLoader(ccl);
                    return null;
                }
            });
            t.start();
        }
        unpinImpl();
    }
}
Also used : Remote(java.rmi.Remote) Unreferenced(java.rmi.server.Unreferenced) NewThreadAction(sun.rmi.runtime.NewThreadAction)

Aggregations

NewThreadAction (sun.rmi.runtime.NewThreadAction)4 IOException (java.io.IOException)1 Remote (java.rmi.Remote)1 ExportException (java.rmi.server.ExportException)1 RMISocketFactory (java.rmi.server.RMISocketFactory)1 Unreferenced (java.rmi.server.Unreferenced)1 Endpoint (sun.rmi.transport.Endpoint)1