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 object storage and retrieval using an LDAP directory. 26 * The ActionEvent object is serializable and is supplied by the java.desktop 27 * module. 28 */ 29 30 package test; 31 32 import java.awt.event.ActionEvent; 33 import java.io.PrintStream; 34 import java.net.*; 35 import java.util.*; 36 import javax.naming.*; 37 import javax.naming.directory.*; 38 39 public class StoreObject { 40 41 static { 42 final PrintStream out = new PrintStream(System.out, true); 43 final PrintStream err = new PrintStream(System.err, true); 44 45 System.setOut(out); 46 System.setErr(err); 47 } 48 49 // LDAP capture file 50 private static final String LDAP_CAPTURE_FILE = 51 System.getProperty("test.src") + "/src/test/test/StoreObject.ldap"; 52 53 public static void main(String[] args) throws Exception { 54 55 /* 56 * Process arguments 57 */ 58 59 int argc = args.length; 60 if ((argc < 1) || 61 ((argc == 1) && (args[0].equalsIgnoreCase("-help")))) { 62 63 System.err.println("\nUsage: StoreObject <ldapurl>\n"); 64 System.err.println(" <ldapurl> is the LDAP URL of the parent entry\n"); 65 System.err.println("example:"); 66 System.err.println(" java StoreObject ldap://oasis/o=airius.com"); 67 return; 68 } 69 70 /* 71 * Launch the LDAP server with the StoreObject.ldap capture file 72 */ 73 74 try (ServerSocket serverSocket = new ServerSocket()) { 75 serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); 76 new Thread(new Runnable() { 77 @Override 78 public void run() { 79 try { 80 new LDAPServer(serverSocket, LDAP_CAPTURE_FILE); 81 } catch (Exception e) { 82 System.out.println("ERROR: unable to launch LDAP server"); 83 e.printStackTrace(); 84 } 85 } 86 }).start(); 87 88 /* 89 * Store objects in the LDAP directory 90 */ 91 92 Hashtable<String,Object> env = new Hashtable<>(); 93 env.put(Context.INITIAL_CONTEXT_FACTORY, 94 "com.sun.jndi.ldap.LdapCtxFactory"); 95 URI ldapUri = new URI(args[0]); 96 if (ldapUri.getPort() == -1) { 97 ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(), 98 serverSocket.getLocalPort(), ldapUri.getPath(), null, null); 99 } 100 env.put(Context.PROVIDER_URL, ldapUri.toString()); 101 if (args[args.length - 1].equalsIgnoreCase("-trace")) { 102 env.put("com.sun.jndi.ldap.trace.ber", System.out); 103 } 104 105 System.out.println("StoreObject: connecting to " + ldapUri); 106 DirContext ctx = new InitialDirContext(env); 107 String dn = "cn=myevent"; 108 String dn2 = "cn=myevent2"; 109 110 try { 111 ctx.bind(dn, new ActionEvent("", 1, "Hello1")); 112 System.out.println("StoreObject: created entry '" + dn + "'"); 113 } catch (NameAlreadyBoundException e) { 114 System.err.println("StoreObject: entry '" + dn + 115 "' already exists"); 116 cleanup(ctx, (String)null); 117 return; 118 } 119 120 try { 121 ctx.bind(dn2, new ActionEvent("", 2, "Hello2")); 122 System.out.println("StoreObject: created entry '" + dn2 + "'"); 123 } catch (NameAlreadyBoundException e) { 124 System.err.println("StoreObject: entry '" + dn2 + 125 "' already exists"); 126 cleanup(ctx, dn); 127 return; 128 } 129 130 /* 131 * Retrieve objects from the LDAP directory 132 */ 133 134 try { 135 ActionEvent b = (ActionEvent) ctx.lookup(dn); 136 System.out.println("StoreObject: retrieved object: " + b); 137 } catch (NamingException e) { 138 System.err.println("StoreObject: error retrieving entry '" + 139 dn + "' " + e); 140 e.printStackTrace(); 141 cleanup(ctx, dn, dn2); 142 return; 143 } 144 145 try { 146 ActionEvent t = (ActionEvent) ctx.lookup(dn2); 147 System.out.println("StoreObject: retrieved object: " + t); 148 } catch (NamingException e) { 149 System.err.println("StoreObject: error retrieving entry '" + 150 dn2 + "' " + e); 151 e.printStackTrace(); 152 cleanup(ctx, dn, dn2); 153 return; 154 } 155 156 cleanup(ctx, dn, dn2); 157 ctx.close(); 158 } 159 } 160 161 /* 162 * Remove objects from the LDAP directory 163 */ 164 private static void cleanup(DirContext ctx, String... dns) 165 throws NamingException { 166 167 for (String dn : dns) { 168 try { 169 ctx.destroySubcontext(dn); 170 System.out.println("StoreObject: removed entry '" + dn + "'"); 171 } catch (NamingException e) { 172 System.err.println("StoreObject: error removing entry '" + dn + 173 "' " + e); 174 } 175 } 176 ctx.close(); 177 } 178 }