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]); 98 if (ldapUri.getPort() == -1) { 99 ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(), 100 serverSocket.getLocalPort(), ldapUri.getPath(), null, null); 101 } 102 env.put(Context.PROVIDER_URL, ldapUri.toString()); 103 if (args[args.length - 1].equalsIgnoreCase("-trace")) { 104 env.put("com.sun.jndi.ldap.trace.ber", System.out); 105 } 106 107 System.out.println("StoreRemote: connecting to " + ldapUri); 108 DirContext ctx = new InitialDirContext(env); 109 String dn = "cn=myremote"; 110 111 try { 112 Hello hello = new HelloImpl(); 113 ctx.bind(dn, hello); 114 System.out.println("StoreRemote: created entry '" + dn + "'"); 115 116 // Explicitly release the RMI object 117 UnicastRemoteObject.unexportObject(hello, true); 118 119 } catch (NameAlreadyBoundException e) { 120 System.err.println("StoreRemote: entry '" + dn + 121 "' already exists"); 122 cleanup(ctx, (String)null); 123 return; 124 } 125 126 /* 127 * Retrieve the Remote object from the LDAP directory 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 }