Search in sources :

Example 1 with Endpoint

use of sun.rmi.transport.Endpoint in project jdk8u_jdk by JetBrains.

the class ConnectionAcceptor method newConnection.

/**
     * Supplies a connection to the endpoint of the address space
     * for which this is a channel.  The returned connection may
     * be one retrieved from a cache of idle connections.
     */
public Connection newConnection() throws RemoteException {
    TCPConnection conn;
    // the loop exits)
    do {
        conn = null;
        // try to get a free connection
        synchronized (freeList) {
            int elementPos = freeList.size() - 1;
            if (elementPos >= 0) {
                // If there is a security manager, make sure
                // the caller is allowed to connect to the
                // requested endpoint.
                checkConnectPermission();
                conn = freeList.get(elementPos);
                freeList.remove(elementPos);
            }
        }
        if (conn != null) {
            // check to see if the connection has closed since last use
            if (!conn.isDead()) {
                TCPTransport.tcpLog.log(Log.BRIEF, "reuse connection");
                return conn;
            }
            // conn is dead, and cannot be reused (reuse => false)
            this.free(conn, false);
        }
    } while (conn != null);
    // none free, so create a new connection
    return (createConnection());
}
Also used : Endpoint(sun.rmi.transport.Endpoint)

Example 2 with Endpoint

use of sun.rmi.transport.Endpoint in project jdk8u_jdk by JetBrains.

the class ConnectionAcceptor method createConnection.

/**
     * Create a new connection to the remote endpoint of this channel.
     * The returned connection is new.  The caller must already have
     * passed a security checkConnect or equivalent.
     */
private Connection createConnection() throws RemoteException {
    Connection conn;
    TCPTransport.tcpLog.log(Log.BRIEF, "create connection");
    if (!usingMultiplexer) {
        Socket sock = ep.newSocket();
        conn = new TCPConnection(this, sock);
        try {
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            writeTransportHeader(out);
            // choose protocol (single op if not reusable socket)
            if (!conn.isReusable()) {
                out.writeByte(TransportConstants.SingleOpProtocol);
            } else {
                out.writeByte(TransportConstants.StreamProtocol);
                out.flush();
                /*
                     * Set socket read timeout to configured value for JRMP
                     * connection handshake; this also serves to guard against
                     * non-JRMP servers that do not respond (see 4322806).
                     */
                int originalSoTimeout = 0;
                try {
                    originalSoTimeout = sock.getSoTimeout();
                    sock.setSoTimeout(handshakeTimeout);
                } catch (Exception e) {
                // if we fail to set this, ignore and proceed anyway
                }
                DataInputStream in = new DataInputStream(conn.getInputStream());
                byte ack = in.readByte();
                if (ack != TransportConstants.ProtocolAck) {
                    throw new ConnectIOException(ack == TransportConstants.ProtocolNack ? "JRMP StreamProtocol not supported by server" : "non-JRMP server at remote endpoint");
                }
                String suggestedHost = in.readUTF();
                int suggestedPort = in.readInt();
                if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
                    TCPTransport.tcpLog.log(Log.VERBOSE, "server suggested " + suggestedHost + ":" + suggestedPort);
                }
                // set local host name, if unknown
                TCPEndpoint.setLocalHost(suggestedHost);
                // do NOT set the default port, because we don't
                // know if we can't listen YET...
                // write out default endpoint to match protocol
                // (but it serves no purpose)
                TCPEndpoint localEp = TCPEndpoint.getLocalEndpoint(0, null, null);
                out.writeUTF(localEp.getHost());
                out.writeInt(localEp.getPort());
                if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
                    TCPTransport.tcpLog.log(Log.VERBOSE, "using " + localEp.getHost() + ":" + localEp.getPort());
                }
                /*
                     * After JRMP handshake, set socket read timeout to value
                     * configured for the rest of the lifetime of the
                     * connection.  NOTE: this timeout, if configured to a
                     * finite duration, places an upper bound on the time
                     * that a remote method call is permitted to execute.
                     */
                try {
                    /*
                         * If socket factory had set a non-zero timeout on its
                         * own, then restore it instead of using the property-
                         * configured value.
                         */
                    sock.setSoTimeout((originalSoTimeout != 0 ? originalSoTimeout : responseTimeout));
                } catch (Exception e) {
                // if we fail to set this, ignore and proceed anyway
                }
                out.flush();
            }
        } catch (IOException e) {
            if (e instanceof RemoteException)
                throw (RemoteException) e;
            else
                throw new ConnectIOException("error during JRMP connection establishment", e);
        }
    } else {
        try {
            conn = multiplexer.openConnection();
        } catch (IOException e) {
            synchronized (this) {
                usingMultiplexer = false;
                multiplexer = null;
            }
            throw new ConnectIOException("error opening virtual connection " + "over multiplexed connection", e);
        }
    }
    return conn;
}
Also used : DataOutputStream(java.io.DataOutputStream) ConnectIOException(java.rmi.ConnectIOException) Connection(sun.rmi.transport.Connection) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Endpoint(sun.rmi.transport.Endpoint) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException) Socket(java.net.Socket)

Example 3 with Endpoint

use of sun.rmi.transport.Endpoint in project jdk8u_jdk by JetBrains.

the class TCPEndpoint method readHostPortFormat.

/**
     * Create a new endpoint from input stream data.
     * @param in the input stream
     */
public static TCPEndpoint readHostPortFormat(DataInput in) throws IOException {
    String host = in.readUTF();
    int port = in.readInt();
    return new TCPEndpoint(host, port);
}
Also used : Endpoint(sun.rmi.transport.Endpoint)

Example 4 with Endpoint

use of sun.rmi.transport.Endpoint in project jdk8u_jdk by JetBrains.

the class TCPEndpoint method read.

/**
     * Get the endpoint from the input stream.
     * @param in the input stream
     * @exception IOException If id could not be read (due to stream failure)
     */
public static TCPEndpoint read(ObjectInput in) throws IOException, ClassNotFoundException {
    String host;
    int port;
    RMIClientSocketFactory csf = null;
    byte format = in.readByte();
    switch(format) {
        case FORMAT_HOST_PORT:
            host = in.readUTF();
            port = in.readInt();
            break;
        case FORMAT_HOST_PORT_FACTORY:
            host = in.readUTF();
            port = in.readInt();
            csf = (RMIClientSocketFactory) in.readObject();
            break;
        default:
            throw new IOException("invalid endpoint format");
    }
    return new TCPEndpoint(host, port, csf, null);
}
Also used : ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) Endpoint(sun.rmi.transport.Endpoint) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory)

Example 5 with Endpoint

use of sun.rmi.transport.Endpoint 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)

Aggregations

Endpoint (sun.rmi.transport.Endpoint)8 IOException (java.io.IOException)4 ConnectIOException (java.rmi.ConnectIOException)2 RemoteException (java.rmi.RemoteException)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 Socket (java.net.Socket)1 NoSuchObjectException (java.rmi.NoSuchObjectException)1 ExportException (java.rmi.server.ExportException)1 RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)1 RemoteRef (java.rmi.server.RemoteRef)1 RegistryImpl (sun.rmi.registry.RegistryImpl)1 NewThreadAction (sun.rmi.runtime.NewThreadAction)1 UnicastServerRef (sun.rmi.server.UnicastServerRef)1 Connection (sun.rmi.transport.Connection)1 LiveRef (sun.rmi.transport.LiveRef)1 TCPEndpoint (sun.rmi.transport.tcp.TCPEndpoint)1