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]);
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 }
|
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]);
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 }
|