1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * Demonstrate JNDI using an LDAP connection control supplied by a third-party 26 * module. 27 */ 28 29 package test; 30 31 import java.net.*; 32 import java.util.*; 33 import javax.naming.*; 34 import javax.naming.directory.*; 35 import javax.naming.ldap.*; 36 37 import org.example.foo.FooControl; 38 39 public class ConnectWithFoo { 40 41 // LDAP capture file 42 private static final String LDAP_CAPTURE_FILE = 43 System.getProperty("test.src") + "/src/test/test/ConnectWithFoo.ldap"; 44 // LDAPServer socket 45 private static ServerSocket serverSocket; 46 47 public static void main(String[] args) throws Exception { 48 49 /* 50 * Process arguments 51 */ 52 53 int argc = args.length; 54 if ((argc < 1) || 55 ((argc == 1) && (args[0].equalsIgnoreCase("-help")))) { 56 57 System.err.println("\nUsage: ConnectWithFoo <ldapurl>\n"); 58 System.err.println(" <ldapurl> is the LDAP URL of the parent entry\n"); 59 System.err.println("example:"); 60 System.err.println(" java ConnectWithFoo ldap://oasis/o=airius.com"); 61 return; 62 } 63 64 /* 65 * Launch the LDAP server with the ConnectWithFoo.ldap capture file 66 */ 67 68 serverSocket = new ServerSocket(0); 69 new Thread(new Runnable() { 70 @Override 71 public void run() { 72 try { 73 new LDAPServer(serverSocket, LDAP_CAPTURE_FILE); 74 } catch (Exception e) { 75 System.out.println("ERROR: unable to launch LDAP server"); 76 e.printStackTrace(); 77 } 78 } 79 }).start(); 80 81 /* 82 * Connect to the LDAP directory 83 */ 84 85 Hashtable<String,Object> env = new Hashtable<>(); 86 env.put(Context.INITIAL_CONTEXT_FACTORY, 87 "com.sun.jndi.ldap.LdapCtxFactory"); 88 URI ldapUri = new URI(args[0]); 89 if (ldapUri.getPort() == -1) { 90 ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(), 91 serverSocket.getLocalPort(), ldapUri.getPath(), null, null); 92 } 93 env.put(Context.PROVIDER_URL, ldapUri.toString()); 94 if (args[args.length - 1].equalsIgnoreCase("-trace")) { 95 env.put("com.sun.jndi.ldap.trace.ber", System.out); 96 } 97 98 System.out.println("ConnectWithFoo: connecting to " + ldapUri); 99 LdapContext ctx = null; 100 Control[] connectionControls = { new FooControl(false) }; 101 102 try { 103 ctx = new InitialLdapContext(env, connectionControls); 104 System.out.println("ConnectWithFoo: connected"); 105 } catch (NamingException e) { 106 System.err.println("ConnectWithFoo: error connecting " + e); 107 } finally { 108 if (ctx != null) { 109 ctx.close(); 110 } 111 } 112 } 113 }