List of usage examples for javax.naming.directory DirContext close
public void close() throws NamingException;
From source file:RenameDiffParent.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {// ww w . j a va2s . c om // Create initial context DirContext ctx = new InitialDirContext(env); // Perform rename ctx.rename("cn=C. User, ou=NewHires", "cn=C. User, ou=People"); // Check that it worked System.out.println(ctx.lookup("cn=C. User, ou=People")); // Revert change ctx.rename("cn=C. User, ou=People", "cn=C. User, ou=NewHires"); // Check that we are back at our original setup System.out.println(ctx.lookup("cn=C. User, ou=NewHires")); // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:SerObjWithCodebase.java
public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: java SerObjWithCodebase <codebase URL>"); System.exit(-1);/*from w ww .ja va2 s. c om*/ } String codebase = args[0]; // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try { // Create the initial context DirContext ctx = new InitialDirContext(env); // Create object to be bound Flower f = new Flower("rose", "pink"); // Perform bind and specify codebase ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase)); // Check that it is bound Flower f2 = (Flower) ctx.lookup("cn=Flower"); System.out.println(f2); // Close the context when we're done ctx.close(); } catch (NamingException e) { System.out.println("Operation failed: " + e); } }
From source file:NewConn.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/* w w w .j av a2 s . c o m*/ // Create initial context (first connection) DirContext ctx = new InitialDirContext(env); // Get a copy of the same context DirContext ctx2 = (DirContext) ctx.lookup(""); // Change authentication properties in ctx2 ctx2.addToEnvironment(Context.SECURITY_PRINCIPAL, "cn=C. User, ou=NewHires, o=JNDITutorial"); ctx2.addToEnvironment(Context.SECURITY_CREDENTIALS, "mysecret"); // Method on ctx2 will use new connection System.out.println(ctx2.getAttributes("ou=NewHires")); // Close the contexts when we're done ctx.close(); ctx2.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:SearchTimeLimit.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*from w w w . j av a 2 s .c o m*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Set search controls to limit count to 'timeout' SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctls.setTimeLimit(timeout); // // Search for objects with those matching attributes NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls); // Print the answer printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (TimeLimitExceededException e) { System.out.println("time limit exceeded"); } catch (Exception e) { System.err.println(e); } }
From source file:SearchWithFilterRetAll.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {// w w w . ja va 2 s . c o m // Create initial context DirContext ctx = new InitialDirContext(env); // Create default search controls SearchControls ctls = new SearchControls(); // Specify the search filter to match // Ask for objects with attribute sn == Geisel and which have // the "mail" attribute. String filter = "(&(sn=Geisel)(mail=*))"; // Search for objects using filter NamingEnumeration answer = ctx.search("ou=People", filter, ctls); // Print the answer // Search.printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:LdapSearch.java
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); String sp = "com.sun.jndi.ldap.LdapCtxFactory"; env.put(Context.INITIAL_CONTEXT_FACTORY, sp); String ldapUrl = "ldap://localhost:389/dc=yourName, dc=com"; env.put(Context.PROVIDER_URL, ldapUrl); DirContext dctx = new InitialDirContext(env); String base = "ou=People"; SearchControls sc = new SearchControls(); String[] attributeFilter = { "cn", "mail" }; sc.setReturningAttributes(attributeFilter); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(&(sn=W*)(l=Criteria*))"; NamingEnumeration results = dctx.search(base, filter, sc); while (results.hasMore()) { SearchResult sr = (SearchResult) results.next(); Attributes attrs = sr.getAttributes(); Attribute attr = attrs.get("cn"); System.out.print(attr.get() + ": "); attr = attrs.get("mail"); System.out.println(attr.get()); }/*from w w w . j av a2 s.c o m*/ dctx.close(); }
From source file:RenameKeepRDN.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); // Set property to keep RDN env.put("java.naming.ldap.deleteRDN", "false"); try {/*from ww w. j ava 2s . c om*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Perform rename ctx.rename("cn=C. User, ou=NewHires", "cn=Claude User,ou=NewHires"); // Check that it worked System.out.println(ctx.getAttributes("cn=Claude User,ou=NewHires")); // Revert change // Make sure new name doesn't get converted into attribute ctx.removeFromEnvironment("java.naming.ldap.deleteRDN"); ctx.rename("cn=Claude User, ou=NewHires", "cn=C. User,ou=NewHires"); // Check that we are back at our original setup System.out.println(ctx.getAttributes("cn=C. User,ou=NewHires")); // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:SearchWithFilter.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*from w w w. ja v a 2 s . c om*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Specify the ids of the attributes to return String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes(attrIDs); // Specify the search filter to match // Ask for objects with attribute sn == Geisel and which have // the "mail" attribute. String filter = "(&(sn=Geisel)(mail=*))"; // Search for objects using filter NamingEnumeration answer = ctx.search("ou=People", filter, ctls); // Print the answer // Search.printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:SearchWithFilterObjs.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {//w ww . jav a 2 s .co m // Create initial context DirContext ctx = new InitialDirContext(env); // Specify the ids of the attributes to return String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes(attrIDs); // Specify the search filter to match // Ask for objects with attribute sn == Geisel and which have // the "mail" attribute. String filter = "(&(sn={0})(mail=*))"; // Search for objects using filter NamingEnumeration answer = ctx.search("ou=People", filter, new Object[] { "Geisel" }, ctls); // Print the answer // Search.printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:SearchSubtree.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*ww w .j ava 2 s.co m*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Specify the ids of the attributes to return String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes(attrIDs); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify the search filter to match // Ask for objects with attribute sn == Geisel and which have // the "mail" attribute. String filter = "(&(sn=Geisel)(mail=*))"; // Search subtree for objects using filter NamingEnumeration answer = ctx.search("", filter, ctls); // Print the answer // Search.printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }