< prev index next >

test/jdk/javax/naming/module/src/test/test/StoreObject.java

Print this page


   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]);


 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 }
   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]);


 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 }
< prev index next >