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.orbeon.oxf.processor.LDAPProcessor.java

private void disconnect(DirContext ctx) {
    try {/*from w  w  w  .  j  a  va 2 s. c  om*/
        if (ctx != null)
            ctx.close();
    } catch (NamingException e) {
        throw new OXFException("LDAP disconnect Failed", e);
    }
}

From source file:org.pegadi.server.user.LDAPUserServerImpl.java

/**
 * Can probably be done more elegant too.
 *
 * @param userDN   real dn to the user.//from   ww w  .  j  a  v  a2 s .  c o m
 * @param password the user's password
 * @return
 */
public boolean checkAuthentication(String userDN, String password) {
    if (password.trim().equals(""))
        return false;
    DirContext ctx2 = null;
    try {
        // See if the user authenticates.
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, url + "/" + ldapBaseDN);
        env.put(Context.SECURITY_AUTHENTICATION, auth);
        env.put(Context.SECURITY_PRINCIPAL, userDN);
        env.put(Context.SECURITY_CREDENTIALS, password);
        env.put("com.sun.jndi.ldap.connect.timeout", "10000");
        // Specify timeout to be 10 seconds, only on non SSL since SSL connections
        // break with a timeout.
        ctx2 = new InitialDirContext(env);
        log.info("Successfully logged in... " + userDN);
    } catch (Exception e) {
        log.error("Exception during login", e);
        return false;
    }

    finally {
        try {
            ctx2.close();
        } catch (NamingException ignore) {
        }
    }

    return true;
}

From source file:org.springframework.ldap.core.LdapTemplate.java

/**
 * Close the supplied DirContext if it is not null. Swallow any exceptions,
 * as this is only for cleanup.//from www .jav a  2s. co  m
 * 
 * @param ctx the context to close.
 */
private void closeContext(DirContext ctx) {
    if (ctx != null) {
        try {
            ctx.close();
        } catch (Exception e) {
            // Never mind this.
        }
    }
}

From source file:org.springframework.ldap.core.support.AbstractContextSource.java

/**
 * Close the context and swallow any exceptions.
 * /*from ww  w .  j a v a 2s.co m*/
 * @param ctx the DirContext to close.
 */
private void closeContext(DirContext ctx) {
    if (ctx != null) {
        try {
            ctx.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java

public void create(Person person) {
    DirContext ctx = createAuthenticatedContext();
    String dn = buildDn(person);/*from   w ww.j a v  a2s .  c o  m*/
    try {
        Attributes attrs = getAttributesToBind(person);
        ctx.bind(dn, null, attrs);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
}

From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java

public void update(Person person) {
    DirContext ctx = createAuthenticatedContext();
    String dn = buildDn(person);/*from  ww w  .j av  a  2 s.c o m*/
    try {
        ctx.rebind(dn, null, getAttributesToBind(person));
    } catch (NamingException e) {

        throw new RuntimeException(e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
}

From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java

public void delete(Person person) {
    DirContext ctx = createAuthenticatedContext();
    String dn = buildDn(person);//from  w  ww .j a  va 2  s .com
    try {
        ctx.unbind(dn);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
}

From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java

public List<String> getAllPersonNames() {
    DirContext ctx = createAnonymousContext();

    LinkedList<String> list = new LinkedList<String>();
    NamingEnumeration<?> results = null;
    try {//from  ww w .  ja v  a2  s  . 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();
            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.demo.dao.PersonDaoImpl.java

public List<Person> findAll() {
    DirContext ctx = createAnonymousContext();

    LinkedList<Person> list = new LinkedList<Person>();
    NamingEnumeration<?> results = null;
    try {//from w w  w  .  j a v  a  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();
            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.demo.dao.PersonDaoImpl.java

public Person findByPrimaryKey(String country, String company, String fullname) {

    DirContext ctx = createAnonymousContext();
    String dn = buildDn(country, company, fullname);
    try {// w w  w .  ja v  a2  s.c o  m
        Attributes attributes = ctx.getAttributes(dn);
        return mapToPerson(dn, attributes);
    } catch (NameNotFoundException e) {
        throw new RuntimeException("Did not find entry with primary key '" + dn + "'", e);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
}