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