1 /* 2 * Copyright (c) 2015, 2018, 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.io.PrintStream; 32 import java.net.*; 33 import java.util.*; 34 import javax.naming.*; 35 import javax.naming.directory.*; 36 import javax.naming.ldap.*; 37 38 import org.example.foo.FooControl; 39 40 public class ConnectWithFoo { 41 42 static { 43 final PrintStream out = new PrintStream(System.out, true); 44 final PrintStream err = new PrintStream(System.err, true); 45 46 System.setOut(out); 47 System.setErr(err); 48 } 49 50 // LDAP capture file 51 private static final String LDAP_CAPTURE_FILE = 52 System.getProperty("test.src") + "/src/test/test/ConnectWithFoo.ldap"; 53 54 public static void main(String[] args) throws Exception { 55 56 /* 57 * Process arguments 58 */ 59 60 int argc = args.length; 61 if ((argc < 1) || 62 ((argc == 1) && (args[0].equalsIgnoreCase("-help")))) { 63 64 System.err.println("\nUsage: ConnectWithFoo <ldapurl>\n"); 65 System.err.println(" <ldapurl> is the LDAP URL of the parent entry\n"); 66 System.err.println("example:"); 67 System.err.println(" java ConnectWithFoo ldap://oasis/o=airius.com"); 68 return; 69 } 70 71 /* 72 * Launch the LDAP server with the ConnectWithFoo.ldap capture file 73 */ 74 75 try (ServerSocket serverSocket = new ServerSocket()) { 76 serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); 77 new Thread(new Runnable() { 78 @Override 79 public void run() { 80 try { 81 new LDAPServer(serverSocket, LDAP_CAPTURE_FILE); 82 } catch (Exception e) { 83 System.out.println("ERROR: unable to launch LDAP server"); 84 e.printStackTrace(); 85 } 86 } 87 }).start(); 88 89 /* 90 * Connect to the LDAP directory 91 */ 92 93 Hashtable<String,Object> env = new Hashtable<>(); 94 env.put(Context.INITIAL_CONTEXT_FACTORY, 95 "com.sun.jndi.ldap.LdapCtxFactory"); 96 URI ldapUri = new URI(args[0]); 97 if (ldapUri.getPort() == -1) { 98 ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(), 99 serverSocket.getLocalPort(), ldapUri.getPath(), null, null); 100 } 101 env.put(Context.PROVIDER_URL, ldapUri.toString()); 102 if (args[args.length - 1].equalsIgnoreCase("-trace")) { 103 env.put("com.sun.jndi.ldap.trace.ber", System.out); 104 } 105 106 System.out.println("ConnectWithFoo: connecting to " + ldapUri); 107 LdapContext ctx = null; 108 Control[] connectionControls = { new FooControl(false) }; 109 110 try { 111 ctx = new InitialLdapContext(env, connectionControls); 112 System.out.println("ConnectWithFoo: connected"); 113 } catch (NamingException e) { 114 System.err.println("ConnectWithFoo: error connecting " + e); 115 } finally { 116 if (ctx != null) { 117 ctx.close(); 118 } 119 } 120 } 121 } 122 }