Example usage for javax.naming.directory DirContext close

List of usage examples for javax.naming.directory DirContext close

Introduction

In this page you can find the example usage for javax.naming.directory DirContext close.

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this context.

Usage

From source file:org.springframework.ldap.pool.factory.DirContextPoolableObjectFactory.java

/**
 * @see org.apache.commons.pool.BaseKeyedPoolableObjectFactory#destroyObject(java.lang.Object,
 *      java.lang.Object)// ww  w .j a  va2 s.co m
 */
public void destroyObject(Object key, Object obj) throws Exception {
    Assert.isTrue(obj instanceof DirContext,
            "The Object to validate must be of type '" + DirContext.class + "'");

    try {
        final DirContext dirContext = (DirContext) obj;
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Closing " + key + " DirContext='" + dirContext + "'");
        }
        dirContext.close();
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Closed " + key + " DirContext='" + dirContext + "'");
        }
    } catch (Exception e) {
        this.logger.warn("An exception occured while closing '" + obj + "'", e);
    }
}

From source file:org.springframework.ldap.pool.factory.PoolingContextSourceTest.java

@Test
public void testGetReadOnlyContextPool() throws Exception {
    DirContext secondDirContextMock = mock(DirContext.class);

    when(contextSourceMock.getReadOnlyContext()).thenReturn(dirContextMock, secondDirContextMock);

    final PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setContextSource(contextSourceMock);

    //Get a context
    final DirContext readOnlyContext1 = poolingContextSource.getReadOnlyContext();
    assertThat(readOnlyContext1).isEqualTo(dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Close the context
    readOnlyContext1.close();
    assertThat(poolingContextSource.getNumActive()).isEqualTo(0);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(1);

    //Get the context again
    final DirContext readOnlyContext2 = poolingContextSource.getReadOnlyContext();
    assertThat(readOnlyContext2).isEqualTo(dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Get a new context
    final DirContext readOnlyContext3 = poolingContextSource.getReadOnlyContext();
    assertThat(readOnlyContext3).isEqualTo(secondDirContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(2);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Close context
    readOnlyContext2.close();//from   w  w  w.j  av a 2s  . co  m
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(1);

    //Close context
    readOnlyContext3.close();
    assertThat(poolingContextSource.getNumActive()).isEqualTo(0);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(2);
}

From source file:org.springframework.ldap.pool.factory.PoolingContextSourceTest.java

@Test
public void testGetReadWriteContextPool() throws Exception {
    DirContext secondDirContextMock = mock(DirContext.class);

    when(contextSourceMock.getReadWriteContext()).thenReturn(dirContextMock, secondDirContextMock);

    final PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setContextSource(contextSourceMock);

    //Get a context
    final DirContext readOnlyContext1 = poolingContextSource.getReadWriteContext();
    assertThat(readOnlyContext1).isEqualTo(dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Close the context
    readOnlyContext1.close();
    assertThat(poolingContextSource.getNumActive()).isEqualTo(0);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(1);

    //Get the context again
    final DirContext readOnlyContext2 = poolingContextSource.getReadWriteContext();
    assertThat(readOnlyContext2).isEqualTo(dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Get a new context
    final DirContext readOnlyContext3 = poolingContextSource.getReadWriteContext();
    assertThat(readOnlyContext3).isEqualTo(secondDirContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(2);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Close context
    readOnlyContext2.close();/*from   w  w w  .ja v  a2 s . co  m*/
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(1);

    //Close context
    readOnlyContext3.close();
    assertThat(poolingContextSource.getNumActive()).isEqualTo(0);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(2);
}

From source file:org.springframework.ldap.pool.factory.PoolingContextSourceTest.java

@Test
public void testGetReadOnlyLdapContext() throws Exception {
    LdapContext secondLdapContextMock = mock(LdapContext.class);

    when(contextSourceMock.getReadOnlyContext()).thenReturn(ldapContextMock, secondLdapContextMock);

    final PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setContextSource(contextSourceMock);

    //Get a context
    final DirContext readOnlyContext1 = poolingContextSource.getReadOnlyContext();
    assertThat(readOnlyContext1).isEqualTo(ldapContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Close the context
    readOnlyContext1.close();
    assertThat(poolingContextSource.getNumActive()).isEqualTo(0);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(1);

    //Get the context again
    final DirContext readOnlyContext2 = poolingContextSource.getReadOnlyContext();
    assertThat(readOnlyContext2).isEqualTo(ldapContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Get a new context
    final DirContext readOnlyContext3 = poolingContextSource.getReadOnlyContext();
    assertThat(readOnlyContext3).isEqualTo(secondLdapContextMock); //Order reversed because the 'wrapper' has the needed equals logic
    assertThat(poolingContextSource.getNumActive()).isEqualTo(2);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(0);

    //Close context
    readOnlyContext2.close();/*from   w  w  w.ja v  a 2 s.  c  o  m*/
    assertThat(poolingContextSource.getNumActive()).isEqualTo(1);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(1);

    //Close context
    readOnlyContext3.close();
    assertThat(poolingContextSource.getNumActive()).isEqualTo(0);
    assertThat(poolingContextSource.getNumIdle()).isEqualTo(2);
}

From source file:org.springframework.ldap.pool2.factory.DirContextPooledObjectFactory.java

/**
 * @see BaseKeyedPooledObjectFactory#destroyObject(Object, PooledObject)
 *
 * *//*from   w  w  w .  ja v a  2  s .c o  m*/
@Override
public void destroyObject(Object key, PooledObject<Object> pooledObject) throws Exception {
    Assert.notNull(pooledObject, "The Object to destroy must not be null");
    Assert.isTrue(pooledObject.getObject() instanceof DirContext,
            "The Object to destroy must be of type '" + DirContext.class + "'");

    try {
        final DirContext dirContext = (DirContext) pooledObject.getObject();
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Closing " + key + " DirContext='" + dirContext + "'");
        }
        dirContext.close();
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Closed " + key + " DirContext='" + dirContext + "'");
        }
    } catch (Exception e) {
        this.logger.warn("An exception occured while closing '" + pooledObject.getObject() + "'", e);
    }
}

From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java

public List getAllPersonNames() {
    DirContext ctx = createAnonymousContext();

    LinkedList list = new LinkedList();
    NamingEnumeration results = null;
    try {/* w  w w . j  a  va  2 s.  co m*/
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        results = ctx.search("", "(objectclass=person)", controls);

        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get("cn");
            String cn = (String) attr.get();
            list.add(cn);
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
    return list;
}

From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java

public List findAll() {
    DirContext ctx = createAnonymousContext();

    LinkedList list = new LinkedList();
    NamingEnumeration results = null;
    try {/*ww  w . j  a  va 2s .c om*/
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        results = ctx.search("", "(objectclass=person)", controls);

        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            String dn = searchResult.getName();
            Attributes attributes = searchResult.getAttributes();
            list.add(mapToPerson(dn, attributes));
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
    return list;
}

From source file:org.springframework.ldap.support.LdapUtils.java

/**
 * Close the given JNDI Context and ignore any thrown exception. This is
 * useful for typical <code>finally</code> blocks in JNDI code.
 * //from   ww w .ja v  a 2s  . c  o m
 * @param context the JNDI Context to close (may be <code>null</code>)
 */
public static void closeContext(DirContext context) {
    if (context != null) {
        try {
            context.close();
        } catch (NamingException ex) {
            logger.debug("Could not close JNDI DirContext", ex);
        } catch (Throwable ex) {
            // We don't trust the JNDI provider: It might throw
            // RuntimeException or Error.
            logger.debug("Unexpected exception on closing JNDI DirContext", ex);
        }
    }
}

From source file:org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManagerDelegate.java

protected void closeTargetResource(CompensatingTransactionHolderSupport transactionHolderSupport) {
    DirContextHolder contextHolder = (DirContextHolder) transactionHolderSupport;
    DirContext ctx = contextHolder.getCtx();

    try {//from  w  w  w  . j  av  a 2  s. c  o  m
        log.debug("Closing target context");
        ctx.close();
    } catch (NamingException e) {
        log.warn("Failed to close target context", e);
    }
}

From source file:org.springframework.ldap.transaction.compensating.manager.TransactionAwareDirContextInvocationHandler.java

/**
 * Close the supplied context, but only if it is not associated with the
 * current transaction.//from  w  ww.j a  va  2s  .  c o m
 * 
 * @param context
 *            the DirContext to close.
 * @param contextSource
 *            the ContextSource bound to the transaction.
 * @throws NamingException
 */
void doCloseConnection(DirContext context, ContextSource contextSource) throws javax.naming.NamingException {
    DirContextHolder transactionContextHolder = (DirContextHolder) TransactionSynchronizationManager
            .getResource(contextSource);
    if (transactionContextHolder == null || transactionContextHolder.getCtx() != context) {
        log.debug("Closing context");
        // This is not the transactional context or the transaction is
        // no longer active - we should close it.
        context.close();
    } else {
        log.debug("Leaving transactional context open");
    }
}