Search in sources :

Example 1 with InterruptedNamingException

use of javax.naming.InterruptedNamingException in project jdk8u_jdk by JetBrains.

the class Connection method readReply.

/**
     * Reads a reply; waits until one is ready.
     */
BerDecoder readReply(LdapRequest ldr) throws IOException, NamingException {
    BerDecoder rber;
    // Track down elapsed time to workaround spurious wakeups
    long elapsedMilli = 0;
    long elapsedNano = 0;
    while (((rber = ldr.getReplyBer()) == null) && (readTimeout <= 0 || elapsedMilli < readTimeout)) {
        try {
            // If socket closed, don't even try
            synchronized (this) {
                if (sock == null) {
                    throw new ServiceUnavailableException(host + ":" + port + "; socket closed");
                }
            }
            synchronized (ldr) {
                // check if condition has changed since our last check
                rber = ldr.getReplyBer();
                if (rber == null) {
                    if (readTimeout > 0) {
                        // Socket read timeout is specified
                        long beginNano = System.nanoTime();
                        // will be woken up before readTimeout if reply is
                        // available
                        ldr.wait(readTimeout - elapsedMilli);
                        elapsedNano += (System.nanoTime() - beginNano);
                        elapsedMilli += elapsedNano / 1000_000;
                        elapsedNano %= 1000_000;
                    } else {
                        // no timeout is set so we wait infinitely until
                        // a response is received
                        // https://docs.oracle.com/javase/8/docs/technotes/guides/jndi/jndi-ldap.html#PROP
                        ldr.wait();
                    }
                } else {
                    break;
                }
            }
        } catch (InterruptedException ex) {
            throw new InterruptedNamingException("Interrupted during LDAP operation");
        }
    }
    if ((rber == null) && (elapsedMilli >= readTimeout)) {
        abandonRequest(ldr, null);
        throw new NamingException("LDAP response read timed out, timeout used:" + readTimeout + "ms.");
    }
    return rber;
}
Also used : InterruptedNamingException(javax.naming.InterruptedNamingException) NamingException(javax.naming.NamingException) InterruptedNamingException(javax.naming.InterruptedNamingException) ServiceUnavailableException(javax.naming.ServiceUnavailableException)

Example 2 with InterruptedNamingException

use of javax.naming.InterruptedNamingException in project jdk8u_jdk by JetBrains.

the class Connections method get.

/**
     * Retrieves a PooledConnection from this list of connections.
     * Use an existing one if one is idle, or create one if the list's
     * max size hasn't been reached. If max size has been reached, wait
     * for a PooledConnection to be returned, or one to be removed (thus
     * not reaching the max size any longer).
     *
     * @param timeout if > 0, msec to wait until connection is available
     * @param factory creates the PooledConnection if one needs to be created
     *
     * @return A non-null PooledConnection
     * @throws NamingException PooledConnection cannot be created, because this
     * thread was interrupted while it waited for an available connection,
     * or if it timed out while waiting, or the creation of a connection
     * resulted in an error.
     */
synchronized PooledConnection get(long timeout, PooledConnectionFactory factory) throws NamingException {
    PooledConnection conn;
    long start = (timeout > 0 ? System.currentTimeMillis() : 0);
    long waittime = timeout;
    d("get(): before");
    while ((conn = getOrCreateConnection(factory)) == null) {
        if (timeout > 0 && waittime <= 0) {
            throw new CommunicationException("Timeout exceeded while waiting for a connection: " + timeout + "ms");
        }
        try {
            d("get(): waiting");
            if (waittime > 0) {
                // Wait until one is released or removed
                wait(waittime);
            } else {
                wait();
            }
        } catch (InterruptedException e) {
            throw new InterruptedNamingException("Interrupted while waiting for a connection");
        }
        // Check whether we timed out
        if (timeout > 0) {
            long now = System.currentTimeMillis();
            waittime = timeout - (now - start);
        }
    }
    d("get(): after");
    return conn;
}
Also used : CommunicationException(javax.naming.CommunicationException) InterruptedNamingException(javax.naming.InterruptedNamingException)

Aggregations

InterruptedNamingException (javax.naming.InterruptedNamingException)2 CommunicationException (javax.naming.CommunicationException)1 NamingException (javax.naming.NamingException)1 ServiceUnavailableException (javax.naming.ServiceUnavailableException)1