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