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 Fruit object and its associated object factory is supplied by a 27 * third-party module. The Fruit object implements javax.naming.Referenceable. 28 */ 29 30 package test; 31 32 import java.io.PrintStream; 33 import java.net.*; 34 import java.util.*; 35 import javax.naming.*; 36 import javax.naming.directory.*; 37 38 import org.example.fruit.Fruit; 39 40 public class StoreFruit { 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 51 // LDAP capture file 52 private static final String LDAP_CAPTURE_FILE = 53 System.getProperty("test.src") + "/src/test/test/StoreFruit.ldap"; 54 55 public static void main(String[] args) throws Exception { 56 57 /* 58 * Process arguments 59 */ 60 int argc = args.length; 61 if ((argc < 1) || 62 ((argc == 1) && (args[0].equalsIgnoreCase("-help")))) { 63 64 System.err.println("\nUsage: StoreFruit <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 StoreFruit ldap://oasis/o=airius.com"); 68 return; 69 } 70 71 /* 72 * Launch the LDAP server with the StoreFruit.ldap capture file 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 fruit 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("StoreFruit: connecting to " + ldapUri); 106 DirContext ctx = new InitialDirContext(env); 107 Fruit fruit = null; 108 String dn = "cn=myfruit"; 109 String dn2 = "cn=myapple"; 110 111 try { 112 fruit = new Fruit("orange"); 113 ctx.bind(dn, fruit); 114 System.out.println("StoreFruit: created entry '" + dn + "'"); 115 } catch (NameAlreadyBoundException e) { 116 System.err.println("StoreFruit: entry '" + dn + 117 "' already exists"); 118 cleanup(ctx, (String)null); 119 return; 120 } 121 122 try { 123 ctx.bind(dn2, new Fruit("apple")); 124 System.out.println("StoreFruit: created entry '" + dn2 + "'"); 125 } catch (NameAlreadyBoundException e) { 126 System.err.println("StoreFruit: entry '" + dn2 + 127 "' already exists"); 128 cleanup(ctx, dn); 129 return; 130 } 131 132 /* 133 * Retrieve fruit objects from the LDAP directory 134 */ 135 136 try { 137 Fruit fruit2 = (Fruit) ctx.lookup(dn); 138 System.out.println("StoreFruit: retrieved object: " + fruit2); 139 } catch (NamingException e) { 140 System.err.println("StoreFruit: error retrieving entry '" + 141 dn + "' " + e); 142 e.printStackTrace(); 143 cleanup(ctx, dn, dn2); 144 return; 145 } 146 147 try { 148 Fruit fruit3 = (Fruit) ctx.lookup(dn2); 149 System.out.println("StoreFruit: retrieved object: " + fruit3); 150 } catch (NamingException e) { 151 System.err.println("StoreFruit: error retrieving entry '" + 152 dn2 + "' " + e); 153 e.printStackTrace(); 154 cleanup(ctx, dn, dn2); 155 return; 156 } 157 158 cleanup(ctx, dn, dn2); 159 } 160 } 161 162 /* 163 * Remove objects from the LDAP directory 164 */ 165 private static void cleanup(DirContext ctx, String... dns) 166 throws NamingException { 167 168 for (String dn : dns) { 169 try { 170 ctx.destroySubcontext(dn); 171 System.out.println("StoreFruit: removed entry '" + dn + "'"); 172 } catch (NamingException e) { 173 System.err.println("StoreFruit: error removing entry '" + dn + 174 "' " + e); 175 } 176 } 177 ctx.close(); 178 } 179 }