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 Java Remote object storage and retrieval using an LDAP directory.
26 * The RMI object is supplied by a third-party module.
27 */
28
29 package test;
30
31 import java.io.*;
32 import java.net.*;
33 import java.rmi.Remote;
34 import java.rmi.server.UnicastRemoteObject;
35 import java.util.*;
36 import javax.naming.*;
37 import javax.naming.directory.*;
38
39 import org.example.hello.*;
40
41 public class StoreRemote {
42
43 // LDAP capture file
44 private static final String LDAP_CAPTURE_FILE =
45 System.getProperty("test.src") + "/src/test/test/StoreRemote.ldap";
46 // LDAPServer socket
47 private static ServerSocket serverSocket;
48
49 public static void main(String[] args) throws Exception {
50
51 /*
52 * Process arguments
53 */
54
55 int argc = args.length;
56 if ((argc < 1) ||
57 ((argc == 1) && (args[0].equalsIgnoreCase("-help")))) {
58
59 System.err.println("\nUsage: StoreRemote <ldapurl>\n");
60 System.err.println(" <ldapurl> is the LDAP URL of the parent entry\n");
61 System.err.println("example:");
62 System.err.println(" java StoreRemote ldap://oasis/o=airius.com");
63 return;
64 }
65
66 /*
67 * Launch the LDAP server with the StoreRemote.ldap capture file
68 */
69
70 serverSocket = new ServerSocket(0);
71 new Thread(new Runnable() {
72 @Override
73 public void run() {
74 try {
75 new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
76 } catch (Exception e) {
77 System.out.println("ERROR: unable to launch LDAP server");
78 e.printStackTrace();
79 }
80 }
81 }).start();
82
83 /*
84 * Store a Remote object in the LDAP directory
85 */
86
87 Hashtable<String,Object> env = new Hashtable<>();
88 env.put(Context.INITIAL_CONTEXT_FACTORY,
89 "com.sun.jndi.ldap.LdapCtxFactory");
90 URI ldapUri = new URI(args[0]);
121 */
122
123 try {
124 Hello obj = (Hello) ctx.lookup(dn);
125 System.out.println("StoreRemote: retrieved object: " + obj);
126 System.out.println("StoreRemote: calling Hello.sayHello()...\n" +
127 obj.sayHello());
128
129 // Explicitly release the RMI object
130 UnicastRemoteObject.unexportObject(obj, true);
131
132 } catch (NamingException e) {
133 System.err.println("StoreRemote: error retrieving entry '" +
134 dn + "' " + e);
135 e.printStackTrace();
136 cleanup(ctx, dn);
137 return;
138 }
139
140 cleanup(ctx, dn);
141 }
142
143 /*
144 * Remove objects from the LDAP directory
145 */
146 private static void cleanup(DirContext ctx, String... dns)
147 throws NamingException {
148
149 for (String dn : dns) {
150 try {
151 ctx.destroySubcontext(dn);
152 System.out.println("StoreRemote: removed entry '" + dn + "'");
153 } catch (NamingException e) {
154 System.err.println("StoreRemote: error removing entry '" + dn +
155 "' " + e);
156 }
157 }
158 ctx.close();
159 }
160 }
|
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 Java Remote object storage and retrieval using an LDAP directory.
26 * The RMI object is supplied by a third-party module.
27 */
28
29 package test;
30
31 import java.io.*;
32 import java.net.*;
33 import java.rmi.Remote;
34 import java.rmi.server.UnicastRemoteObject;
35 import java.util.*;
36 import javax.naming.*;
37 import javax.naming.directory.*;
38
39 import org.example.hello.*;
40
41 public class StoreRemote {
42
43 static {
44 final PrintStream out = new PrintStream(System.out, true);
45 final PrintStream err = new PrintStream(System.err, true);
46
47 System.setOut(out);
48 System.setErr(err);
49 }
50
51 // LDAP capture file
52 private static final String LDAP_CAPTURE_FILE =
53 System.getProperty("test.src") + "/src/test/test/StoreRemote.ldap";
54
55 public static void main(String[] args) throws Exception {
56
57 /*
58 * Process arguments
59 */
60
61 int argc = args.length;
62 if ((argc < 1) ||
63 ((argc == 1) && (args[0].equalsIgnoreCase("-help")))) {
64
65 System.err.println("\nUsage: StoreRemote <ldapurl>\n");
66 System.err.println(" <ldapurl> is the LDAP URL of the parent entry\n");
67 System.err.println("example:");
68 System.err.println(" java StoreRemote ldap://oasis/o=airius.com");
69 return;
70 }
71
72 /*
73 * Launch the LDAP server with the StoreRemote.ldap capture file
74 */
75
76 try (ServerSocket serverSocket = new ServerSocket()){
77 serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
78 new Thread(new Runnable() {
79 @Override
80 public void run() {
81 try {
82 new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
83 } catch (Exception e) {
84 System.out.println("ERROR: unable to launch LDAP server");
85 e.printStackTrace();
86 }
87 }
88 }).start();
89
90 /*
91 * Store a Remote object in the LDAP directory
92 */
93
94 Hashtable<String,Object> env = new Hashtable<>();
95 env.put(Context.INITIAL_CONTEXT_FACTORY,
96 "com.sun.jndi.ldap.LdapCtxFactory");
97 URI ldapUri = new URI(args[0]);
128 */
129
130 try {
131 Hello obj = (Hello) ctx.lookup(dn);
132 System.out.println("StoreRemote: retrieved object: " + obj);
133 System.out.println("StoreRemote: calling Hello.sayHello()...\n" +
134 obj.sayHello());
135
136 // Explicitly release the RMI object
137 UnicastRemoteObject.unexportObject(obj, true);
138
139 } catch (NamingException e) {
140 System.err.println("StoreRemote: error retrieving entry '" +
141 dn + "' " + e);
142 e.printStackTrace();
143 cleanup(ctx, dn);
144 return;
145 }
146
147 cleanup(ctx, dn);
148 }
149 }
150
151 /*
152 * Remove objects from the LDAP directory
153 */
154 private static void cleanup(DirContext ctx, String... dns)
155 throws NamingException {
156
157 for (String dn : dns) {
158 try {
159 ctx.destroySubcontext(dn);
160 System.out.println("StoreRemote: removed entry '" + dn + "'");
161 } catch (NamingException e) {
162 System.err.println("StoreRemote: error removing entry '" + dn +
163 "' " + e);
164 }
165 }
166 ctx.close();
167 }
168 }
|