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