javax.naming.CommunicationException Java Examples

The following examples show how to use javax.naming.CommunicationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LdapRequest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
BerDecoder getReplyBer(long millis) throws CommunicationException,
                                           InterruptedException {
    if (cancelled) {
        throw new CommunicationException("Request: " + msgId +
            " cancelled");
    }
    if (isClosed()) {
        return null;
    }

    BerDecoder result = millis > 0 ?
            replies.poll(millis, TimeUnit.MILLISECONDS) : replies.take();

    if (cancelled) {
        throw new CommunicationException("Request: " + msgId +
            " cancelled");
    }

    return result == EOF ? null : result;
}
 
Example #2
Source File: ResourceRecord.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private String decodeNaptr(int pos) throws CommunicationException {
    int order = getUShort(pos);
    pos += 2;
    int preference = getUShort(pos);
    pos += 2;
    StringBuffer flags = new StringBuffer();
    pos += decodeCharString(pos, flags);
    StringBuffer services = new StringBuffer();
    pos += decodeCharString(pos, services);
    StringBuffer regexp = new StringBuffer(rdlen);
    pos += decodeCharString(pos, regexp);
    DnsName replacement = decodeName(pos);

    return (order + " " + preference + " " + flags + " " +
            services + " " + regexp + " " + replacement);
}
 
Example #3
Source File: ResourceRecord.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private String decodeSoa(int pos) throws CommunicationException {
    DnsName mname = new DnsName();
    pos = decodeName(pos, mname);
    DnsName rname = new DnsName();
    pos = decodeName(pos, rname);

    long serial = getUInt(pos);
    pos += 4;
    long refresh = getUInt(pos);
    pos += 4;
    long retry = getUInt(pos);
    pos += 4;
    long expire = getUInt(pos);
    pos += 4;
    long minimum = getUInt(pos);    // now used as negative TTL
    pos += 4;

    return (mname + " " + rname + " " + serial + " " +
            refresh + " " + retry + " " + expire + " " + minimum);
}
 
Example #4
Source File: ResourceRecord.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private String decodeNaptr(int pos) throws CommunicationException {
    int order = getUShort(pos);
    pos += 2;
    int preference = getUShort(pos);
    pos += 2;
    StringBuffer flags = new StringBuffer();
    pos += decodeCharString(pos, flags);
    StringBuffer services = new StringBuffer();
    pos += decodeCharString(pos, services);
    StringBuffer regexp = new StringBuffer(rdlen);
    pos += decodeCharString(pos, regexp);
    DnsName replacement = decodeName(pos);

    return (order + " " + preference + " " + flags + " " +
            services + " " + regexp + " " + replacement);
}
 
Example #5
Source File: ResourceRecord.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private String decodeNaptr(int pos) throws CommunicationException {
    int order = getUShort(pos);
    pos += 2;
    int preference = getUShort(pos);
    pos += 2;
    StringBuffer flags = new StringBuffer();
    pos += decodeCharString(pos, flags);
    StringBuffer services = new StringBuffer();
    pos += decodeCharString(pos, services);
    StringBuffer regexp = new StringBuffer(rdlen);
    pos += decodeCharString(pos, regexp);
    DnsName replacement = decodeName(pos);

    return (order + " " + preference + " " + flags + " " +
            services + " " + regexp + " " + replacement);
}
 
Example #6
Source File: LdapSearch.java    From cxf with Apache License 2.0 6 votes vote down vote up
public Attributes getAttributes(String dn) throws NamingException {
    int retry = 0;
    while (true) {
        try {
            if (this.dirContext == null) {
                this.dirContext = createInitialContext();
            }
            return dirContext.getAttributes(dn);
        } catch (CommunicationException e) {
            LOG.log(Level.WARNING, "Error in ldap search: " + e.getMessage(), e);
            this.dirContext = null;
            retry++;
            if (retry >= numRetries) {
                throw new XKMSException(ResultMajorEnum.HTTP_WWW_W_3_ORG_2002_03_XKMS_RECEIVER,
                                        ResultMinorEnum.HTTP_WWW_W_3_ORG_2002_03_XKMS_FAILURE, "Backend failure");
            }
        }
    }
}
 
Example #7
Source File: ResourceRecord.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private String decodeNaptr(int pos) throws CommunicationException {
    int order = getUShort(pos);
    pos += 2;
    int preference = getUShort(pos);
    pos += 2;
    StringBuffer flags = new StringBuffer();
    pos += decodeCharString(pos, flags);
    StringBuffer services = new StringBuffer();
    pos += decodeCharString(pos, services);
    StringBuffer regexp = new StringBuffer(rdlen);
    pos += decodeCharString(pos, regexp);
    DnsName replacement = decodeName(pos);

    return (order + " " + preference + " " + flags + " " +
            services + " " + regexp + " " + replacement);
}
 
Example #8
Source File: ResourceRecord.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private String decodeNaptr(int pos) throws CommunicationException {
    int order = getUShort(pos);
    pos += 2;
    int preference = getUShort(pos);
    pos += 2;
    StringBuffer flags = new StringBuffer();
    pos += decodeCharString(pos, flags);
    StringBuffer services = new StringBuffer();
    pos += decodeCharString(pos, services);
    StringBuffer regexp = new StringBuffer(rdlen);
    pos += decodeCharString(pos, regexp);
    DnsName replacement = decodeName(pos);

    return (order + " " + preference + " " + flags + " " +
            services + " " + regexp + " " + replacement);
}
 
Example #9
Source File: ResourceRecord.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private String decodeSoa(int pos) throws CommunicationException {
    DnsName mname = new DnsName();
    pos = decodeName(pos, mname);
    DnsName rname = new DnsName();
    pos = decodeName(pos, rname);

    long serial = getUInt(pos);
    pos += 4;
    long refresh = getUInt(pos);
    pos += 4;
    long retry = getUInt(pos);
    pos += 4;
    long expire = getUInt(pos);
    pos += 4;
    long minimum = getUInt(pos);    // now used as negative TTL
    pos += 4;

    return (mname + " " + rname + " " + serial + " " +
            refresh + " " + retry + " " + expire + " " + minimum);
}
 
Example #10
Source File: LdapSearch.java    From cxf with Apache License 2.0 6 votes vote down vote up
public NamingEnumeration<SearchResult> searchSubTree(String rootEntry, String filter) throws NamingException {
    int retry = 0;
    while (true) {
        try {
            if (this.dirContext == null) {
                this.dirContext = createInitialContext();
            }
            SearchControls ctls = new SearchControls();
            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            return dirContext.search(rootEntry, filter, ctls);
        } catch (CommunicationException e) {
            LOG.log(Level.WARNING, "Error in ldap search: " + e.getMessage(), e);
            this.dirContext = null;
            retry++;
            if (retry >= numRetries) {
                throw new XKMSException(ResultMajorEnum.HTTP_WWW_W_3_ORG_2002_03_XKMS_RECEIVER,
                                        ResultMinorEnum.HTTP_WWW_W_3_ORG_2002_03_XKMS_FAILURE, "Backend failure");
            }
        }
    }
}
 
Example #11
Source File: ResourceRecord.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private int decodeName(int pos, DnsName n) throws CommunicationException {
    int endPos = -1;
    int level = 0;
    try {
        while (true) {
            if (level > MAXIMUM_COMPRESSION_REFERENCES)
                throw new IOException("Too many compression references");
            int typeAndLen = msg[pos] & 0xFF;
            if (typeAndLen == 0) {                  // end of name
                ++pos;
                n.add(0, "");
                break;
            } else if (typeAndLen <= 63) {          // regular label
                ++pos;
                n.add(0, new String(msg, pos, typeAndLen,
                    StandardCharsets.ISO_8859_1));
                pos += typeAndLen;
            } else if ((typeAndLen & 0xC0) == 0xC0) { // name compression
                ++level;
                endPos = pos + 2;
                pos = getUShort(pos) & 0x3FFF;
            } else
                throw new IOException("Invalid label type: " + typeAndLen);
        }
    } catch (IOException | InvalidNameException e) {
        CommunicationException ce =new CommunicationException(
            "DNS error: malformed packet");
        ce.initCause(e);
        throw ce;
    }
    if (endPos == -1)
        endPos = pos;
    return endPos;
}
 
Example #12
Source File: TestLdapGroupsMapping.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGroupsWithConnectionClosed() throws IOException, NamingException {
  // The case mocks connection is closed/gc-ed, so the first search call throws CommunicationException,
  // then after reconnected return the user NamingEnumeration first, and then the group
  when(mockContext.search(anyString(), anyString(), any(Object[].class),
      any(SearchControls.class)))
      .thenThrow(new CommunicationException("Connection is closed"))
      .thenReturn(mockUserNamingEnum, mockGroupNamingEnum);
  
  // Although connection is down but after reconnected it still should retrieve the result groups
  doTestGetGroups(Arrays.asList(testGroups), 1 + 2); // 1 is the first failure call 
}
 
Example #13
Source File: ResourceRecord.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private String decodeSrv(int pos) throws CommunicationException {
    int priority = getUShort(pos);
    pos += 2;
    int weight =   getUShort(pos);
    pos += 2;
    int port =     getUShort(pos);
    pos += 2;
    DnsName target = decodeName(pos);
    return (priority + " " + weight + " " + port + " " + target);
}
 
Example #14
Source File: LdapRequest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
synchronized BerDecoder getReplyBer() throws CommunicationException {
    if (cancelled) {
        throw new CommunicationException("Request: " + msgId +
            " cancelled");
    }

    /*
     * Remove a reply if the queue is not empty.
     * poll returns null if queue is empty.
     */
    BerDecoder reply = replies.poll();
    return reply;
}
 
Example #15
Source File: TestLdapGroupsMapping.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGroupsWithLdapDown() throws IOException, NamingException {
  // This mocks the case where Ldap server is down, and always throws CommunicationException 
  when(mockContext.search(anyString(), anyString(), any(Object[].class),
      any(SearchControls.class)))
      .thenThrow(new CommunicationException("Connection is closed"));
  
  // Ldap server is down, no groups should be retrieved
  doTestGetGroups(Arrays.asList(new String[] {}), 
      1 + LdapGroupsMapping.RECONNECT_RETRY_COUNT); // 1 is the first normal call
}
 
Example #16
Source File: ResourceRecord.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
ResourceRecord(byte[] msg, int msgLen, int offset,
               boolean qSection, boolean decodeRdata)
        throws CommunicationException {

    this.msg = msg;
    this.msgLen = msgLen;
    this.offset = offset;
    this.qSection = qSection;
    decode(decodeRdata);
}
 
Example #17
Source File: LDAPCertStoreImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a map containing the values for this request. The first time
 * this method is called on an object, the LDAP request is sent,
 * the results parsed and added to a private map and also to the
 * cache of this LDAPCertStore. Subsequent calls return the private
 * map immediately.
 *
 * The map contains an entry for each requested attribute. The
 * attribute name is the key, values are byte[][]. If there are no
 * values for that attribute, values are byte[0][].
 *
 * @return                      the value Map
 * @throws NamingException      if a naming exception occurs
 */
private Map<String, byte[][]> getValueMap() throws NamingException {
    if (valueMap != null) {
        return valueMap;
    }
    if (DEBUG) {
        System.out.println("Request: " + name + ":" + requestedAttributes);
        requests++;
        if (requests % 5 == 0) {
            System.out.println("LDAP requests: " + requests);
        }
    }
    valueMap = new HashMap<>(8);
    String[] attrIds = requestedAttributes.toArray(STRING0);
    Attributes attrs;

    if (communicationError) {
        ctx.reconnect(null);
        communicationError = false;
    }

    try {
        attrs = ctx.getAttributes(name, attrIds);
    } catch (CommunicationException ce) {
        communicationError = true;
        throw ce;
    } catch (NameNotFoundException e) {
        // name does not exist on this LDAP server
        // treat same as not attributes found
        attrs = EMPTY_ATTRIBUTES;
    }
    for (String attrId : requestedAttributes) {
        Attribute attr = attrs.get(attrId);
        byte[][] values = getAttributeValues(attr);
        cacheAttribute(attrId, values);
        valueMap.put(attrId, values);
    }
    return valueMap;
}
 
Example #18
Source File: ResourceRecord.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
ResourceRecord(byte[] msg, int msgLen, int offset,
               boolean qSection, boolean decodeRdata)
        throws CommunicationException {

    this.msg = msg;
    this.msgLen = msgLen;
    this.offset = offset;
    this.qSection = qSection;
    decode(decodeRdata);
}
 
Example #19
Source File: TestLdapGroupsMapping.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGroupsWithConnectionClosed() throws IOException, NamingException {
  // The case mocks connection is closed/gc-ed, so the first search call throws CommunicationException,
  // then after reconnected return the user NamingEnumeration first, and then the group
  when(mockContext.search(anyString(), anyString(), any(Object[].class),
      any(SearchControls.class)))
      .thenThrow(new CommunicationException("Connection is closed"))
      .thenReturn(mockUserNamingEnum, mockGroupNamingEnum);
  
  // Although connection is down but after reconnected it still should retrieve the result groups
  doTestGetGroups(Arrays.asList(testGroups), 1 + 2); // 1 is the first failure call 
}
 
Example #20
Source File: Connections.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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(waittime);  // Wait until one is released or removed
            } 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;
}
 
Example #21
Source File: Connections.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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(waittime);  // Wait until one is released or removed
            } 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;
}
 
Example #22
Source File: ResourceRecord.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String decodeSrv(int pos) throws CommunicationException {
    int priority = getUShort(pos);
    pos += 2;
    int weight =   getUShort(pos);
    pos += 2;
    int port =     getUShort(pos);
    pos += 2;
    DnsName target = decodeName(pos);
    return (priority + " " + weight + " " + port + " " + target);
}
 
Example #23
Source File: ResourceRecord.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
ResourceRecord(byte[] msg, int msgLen, int offset,
               boolean qSection, boolean decodeRdata)
        throws CommunicationException {

    this.msg = msg;
    this.msgLen = msgLen;
    this.offset = offset;
    this.qSection = qSection;
    decode(decodeRdata);
}
 
Example #24
Source File: Connections.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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(waittime);  // Wait until one is released or removed
            } 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;
}
 
Example #25
Source File: LdapTemplateNamespaceHandlerTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void verifyParsePoolingDefaults() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-defaults.xml");

    ContextSource outerContextSource = ctx.getBean(ContextSource.class);
    assertThat(outerContextSource).isNotNull();
    assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue();

    ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
    assertThat(pooledContextSource).isNotNull();
    assertThat(pooledContextSource instanceof PoolingContextSource).isTrue();

    Object objectFactory = getInternalState(pooledContextSource, "dirContextPoolableObjectFactory");
    assertThat(getInternalState(objectFactory, "contextSource")).isNotNull();
    assertThat(getInternalState(objectFactory, "dirContextValidator")).isNull();
    Set<Class<? extends Throwable>> nonTransientExceptions =
            (Set<Class<? extends Throwable>>) getInternalState(objectFactory, "nonTransientExceptions");
    assertThat(nonTransientExceptions).hasSize(1);
    assertThat(nonTransientExceptions.contains(CommunicationException.class)).isTrue();

    GenericKeyedObjectPool objectPool = (GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
    assertThat(objectPool.getMaxActive()).isEqualTo(8);
    assertThat(objectPool.getMaxTotal()).isEqualTo(-1);
    assertThat(objectPool.getMaxIdle()).isEqualTo(8);
    assertThat(objectPool.getMaxWait()).isEqualTo(-1);
    assertThat(objectPool.getMinIdle()).isEqualTo(0);
    assertThat(objectPool.getWhenExhaustedAction()).isEqualTo((byte)1);
}
 
Example #26
Source File: ResourceRecord.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private Object decodeRdata(int pos) throws CommunicationException {
    if (rrclass == CLASS_INTERNET) {
        switch (rrtype) {
        case TYPE_A:
            return decodeA(pos);
        case TYPE_AAAA:
            return decodeAAAA(pos);
        case TYPE_CNAME:
        case TYPE_NS:
        case TYPE_PTR:
            return decodeName(pos);
        case TYPE_MX:
            return decodeMx(pos);
        case TYPE_SOA:
            return decodeSoa(pos);
        case TYPE_SRV:
            return decodeSrv(pos);
        case TYPE_NAPTR:
            return decodeNaptr(pos);
        case TYPE_TXT:
            return decodeTxt(pos);
        case TYPE_HINFO:
            return decodeHinfo(pos);
        }
    }
    // Unknown RR type/class
    if (debug) {
        dprint("Unknown RR type for RR data: " + rrtype + " rdlen=" + rdlen
               + ", pos=" + pos +", msglen=" + msg.length + ", remaining="
               + (msg.length-pos));
    }
    byte[] rd = new byte[rdlen];
    System.arraycopy(msg, pos, rd, 0, rdlen);
    return rd;
}
 
Example #27
Source File: ResourceRecord.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
ResourceRecord(byte[] msg, int msgLen, int offset,
               boolean qSection, boolean decodeRdata)
        throws CommunicationException {

    this.msg = msg;
    this.msgLen = msgLen;
    this.offset = offset;
    this.qSection = qSection;
    decode(decodeRdata);
}
 
Example #28
Source File: Connections.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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(waittime);  // Wait until one is released or removed
            } 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;
}
 
Example #29
Source File: ResourceRecord.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Object decodeRdata(int pos) throws CommunicationException {
    if (rrclass == CLASS_INTERNET) {
        switch (rrtype) {
        case TYPE_A:
            return decodeA(pos);
        case TYPE_AAAA:
            return decodeAAAA(pos);
        case TYPE_CNAME:
        case TYPE_NS:
        case TYPE_PTR:
            return decodeName(pos);
        case TYPE_MX:
            return decodeMx(pos);
        case TYPE_SOA:
            return decodeSoa(pos);
        case TYPE_SRV:
            return decodeSrv(pos);
        case TYPE_NAPTR:
            return decodeNaptr(pos);
        case TYPE_TXT:
            return decodeTxt(pos);
        case TYPE_HINFO:
            return decodeHinfo(pos);
        }
    }
    // Unknown RR type/class
    if (debug) {
        dprint("Unknown RR type for RR data: " + rrtype + " rdlen=" + rdlen
               + ", pos=" + pos +", msglen=" + msg.length + ", remaining="
               + (msg.length-pos));
    }
    byte[] rd = new byte[rdlen];
    System.arraycopy(msg, pos, rd, 0, rdlen);
    return rd;
}
 
Example #30
Source File: Connections.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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(waittime);  // Wait until one is released or removed
            } 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;
}