8211920: Close server socket and cleanups in test/jdk/javax/naming/module/RunBasic.java

Reviewed-by: dfuchs
This commit is contained in:
Chris Hegarty 2018-10-09 11:44:00 +01:00
parent 2db7ed1895
commit e61252dc27
8 changed files with 541 additions and 472 deletions

View File

@ -27,8 +27,10 @@ import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.process.ProcessTools;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -65,6 +67,8 @@ public class RunBasic {
private static final List<String> JAVA_CMDS;
static final String HOST_NAME = InetAddress.getLoopbackAddress().getHostName();
static {
String javaPath = JDKToolFinder.getJDKTool("java");
@ -85,6 +89,8 @@ public class RunBasic {
prepareModule("test", "--module-source-path",
Path.of(TEST_SRC, "src").toString());
System.out.println("Hostname: [" + HOST_NAME + "]");
// run tests
runTest("java.desktop", "test.StoreObject");
runTest("person", "test.StorePerson");
@ -98,9 +104,12 @@ public class RunBasic {
private static void prepareModule(String mod, String... opts)
throws IOException {
System.out.println("Preparing the '" + mod + "' module...");
long start = System.nanoTime();
makeDir("mods", mod);
CompilerUtils.compile(Path.of(TEST_SRC, "src", mod),
Path.of("mods", (mod.equals("test") ? "" : mod)), opts);
Duration duration = Duration.ofNanos(System.nanoTime() - start);
System.out.println("completed: duration - " + duration );
}
private static void makeDir(String first, String... more)
@ -111,7 +120,7 @@ public class RunBasic {
private static void runTest(String desc, String clsName) throws Throwable {
System.out.println("Running with the '" + desc + "' module...");
runJava("-Dtest.src=" + TEST_SRC, "-p", "mods", "-m", "test/" + clsName,
"ldap://localhost/dc=ie,dc=oracle,dc=com");
"ldap://" + HOST_NAME + "/dc=ie,dc=oracle,dc=com");
}
private static void runJava(String... opts) throws Throwable {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -29,6 +29,7 @@
package test;
import java.io.PrintStream;
import java.net.*;
import java.util.*;
import javax.naming.*;
@ -40,12 +41,18 @@ import org.example.authz.AuthzIdResponseControl;
public class ConnectWithAuthzId {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") +
"/src/test/test/ConnectWithAuthzId.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
@ -68,67 +75,69 @@ public class ConnectWithAuthzId {
* Launch the LDAP server with the ConnectWithAuthzId.ldap capture file
*/
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Connect to the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ie,dc=oracle,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "changeit");
env.put(LdapContext.CONTROL_FACTORIES,
"org.example.authz.AuthzIdResponseControlFactory");
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
System.out.println("ConnectWithAuthzId: connecting to " + ldapUri);
LdapContext ctx = null;
Control[] connectionControls = { new AuthzIdRequestControl(false) };
try {
ctx = new InitialLdapContext(env, connectionControls);
System.out.println("ConnectWithAuthzId: connected");
// Retrieve the response controls
Control[] responseControls = ctx.getResponseControls();
if (responseControls != null) {
for (Control responseControl : responseControls) {
System.out.println("ConnectWithAuthzId: received response" +
" control: " + responseControl.getID());
if (responseControl instanceof AuthzIdResponseControl) {
AuthzIdResponseControl authzId =
(AuthzIdResponseControl)responseControl;
System.out.println("ConnectWithAuthzId: identity is " +
authzId.getIdentity());
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Connect to the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
} catch (NamingException e) {
System.err.println("ConnectWithAuthzId: error connecting " + e);
} finally {
if (ctx != null) {
ctx.close();
env.put(Context.PROVIDER_URL, ldapUri.toString());
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=ie,dc=oracle,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "changeit");
env.put(LdapContext.CONTROL_FACTORIES,
"org.example.authz.AuthzIdResponseControlFactory");
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
System.out.println("ConnectWithAuthzId: connecting to " + ldapUri);
LdapContext ctx = null;
Control[] connectionControls = { new AuthzIdRequestControl(false) };
try {
ctx = new InitialLdapContext(env, connectionControls);
System.out.println("ConnectWithAuthzId: connected");
// Retrieve the response controls
Control[] responseControls = ctx.getResponseControls();
if (responseControls != null) {
for (Control responseControl : responseControls) {
System.out.println("ConnectWithAuthzId: received response" +
" control: " + responseControl.getID());
if (responseControl instanceof AuthzIdResponseControl) {
AuthzIdResponseControl authzId =
(AuthzIdResponseControl)responseControl;
System.out.println("ConnectWithAuthzId: identity is " +
authzId.getIdentity());
}
}
}
} catch (NamingException e) {
System.err.println("ConnectWithAuthzId: error connecting " + e);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,7 @@
package test;
import java.io.PrintStream;
import java.net.*;
import java.util.*;
import javax.naming.*;
@ -38,11 +39,17 @@ import org.example.foo.FooControl;
public class ConnectWithFoo {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") + "/src/test/test/ConnectWithFoo.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
@ -65,48 +72,50 @@ public class ConnectWithFoo {
* Launch the LDAP server with the ConnectWithFoo.ldap capture file
*/
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Connect to the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
}).start();
/*
* Connect to the LDAP directory
*/
System.out.println("ConnectWithFoo: connecting to " + ldapUri);
LdapContext ctx = null;
Control[] connectionControls = { new FooControl(false) };
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
System.out.println("ConnectWithFoo: connecting to " + ldapUri);
LdapContext ctx = null;
Control[] connectionControls = { new FooControl(false) };
try {
ctx = new InitialLdapContext(env, connectionControls);
System.out.println("ConnectWithFoo: connected");
} catch (NamingException e) {
System.err.println("ConnectWithFoo: error connecting " + e);
} finally {
if (ctx != null) {
ctx.close();
try {
ctx = new InitialLdapContext(env, connectionControls);
System.out.println("ConnectWithFoo: connected");
} catch (NamingException e) {
System.err.println("ConnectWithFoo: error connecting " + e);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,7 @@
package test;
import java.io.PrintStream;
import java.net.*;
import java.util.*;
import javax.naming.*;
@ -36,11 +37,17 @@ import javax.naming.ldap.*;
public class ReadByUrl {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") + "/src/test/test/ReadByUrl.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
@ -63,50 +70,52 @@ public class ReadByUrl {
* Launch the LDAP server with the ReadByUrl.ldap capture file
*/
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Connect to the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI("ldapv4", null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
}).start();
/*
* Connect to the LDAP directory
*/
// URL context factory location for 'ldapv4://'
env.put(Context.URL_PKG_PREFIXES, "org.example");
Hashtable<String,Object> env = new Hashtable<>();
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI("ldapv4", null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
System.out.println("ReadByUrl: connecting to " + ldapUri);
DirContext ctx = null;
// URL context factory location for 'ldapv4://'
env.put(Context.URL_PKG_PREFIXES, "org.example");
System.out.println("ReadByUrl: connecting to " + ldapUri);
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
System.out.println("ReadByUrl: connected");
DirContext entry = (DirContext) ctx.lookup(ldapUri.toString());
entry.close();
} catch (NamingException e) {
System.err.println("ReadByUrl: error connecting " + e);
} finally {
if (ctx != null) {
ctx.close();
try {
ctx = new InitialDirContext(env);
System.out.println("ReadByUrl: connected");
DirContext entry = (DirContext) ctx.lookup(ldapUri.toString());
entry.close();
} catch (NamingException e) {
System.err.println("ReadByUrl: error connecting " + e);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -29,6 +29,7 @@
package test;
import java.io.PrintStream;
import java.net.*;
import java.util.*;
import javax.naming.*;
@ -38,18 +39,24 @@ import org.example.fruit.Fruit;
public class StoreFruit {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") + "/src/test/test/StoreFruit.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
/*
* Process arguments
*/
int argc = args.length;
if ((argc < 1) ||
((argc == 1) && (args[0].equalsIgnoreCase("-help")))) {
@ -58,97 +65,98 @@ public class StoreFruit {
System.err.println(" <ldapurl> is the LDAP URL of the parent entry\n");
System.err.println("example:");
System.err.println(" java StoreFruit ldap://oasis/o=airius.com");
return;
return;
}
/*
* Launch the LDAP server with the StoreFruit.ldap capture file
*/
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
/*
* Store fruit objects in the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
}).start();
/*
* Store fruit objects in the LDAP directory
*/
System.out.println("StoreFruit: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
Fruit fruit = null;
String dn = "cn=myfruit";
String dn2 = "cn=myapple";
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
try {
fruit = new Fruit("orange");
ctx.bind(dn, fruit);
System.out.println("StoreFruit: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreFruit: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
System.out.println("StoreFruit: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
Fruit fruit = null;
String dn = "cn=myfruit";
String dn2 = "cn=myapple";
try {
ctx.bind(dn2, new Fruit("apple"));
System.out.println("StoreFruit: created entry '" + dn2 + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreFruit: entry '" + dn2 +
"' already exists");
cleanup(ctx, dn);
return;
}
try {
fruit = new Fruit("orange");
ctx.bind(dn, fruit);
System.out.println("StoreFruit: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreFruit: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
/*
* Retrieve fruit objects from the LDAP directory
*/
try {
ctx.bind(dn2, new Fruit("apple"));
System.out.println("StoreFruit: created entry '" + dn2 + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreFruit: entry '" + dn2 +
"' already exists");
cleanup(ctx, dn);
return;
}
try {
Fruit fruit2 = (Fruit) ctx.lookup(dn);
System.out.println("StoreFruit: retrieved object: " + fruit2);
} catch (NamingException e) {
System.err.println("StoreFruit: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
/*
* Retrieve fruit objects from the LDAP directory
*/
try {
Fruit fruit3 = (Fruit) ctx.lookup(dn2);
System.out.println("StoreFruit: retrieved object: " + fruit3);
} catch (NamingException e) {
System.err.println("StoreFruit: error retrieving entry '" +
dn2 + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
try {
Fruit fruit2 = (Fruit) ctx.lookup(dn);
System.out.println("StoreFruit: retrieved object: " + fruit2);
} catch (NamingException e) {
System.err.println("StoreFruit: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
try {
Fruit fruit3 = (Fruit) ctx.lookup(dn2);
System.out.println("StoreFruit: retrieved object: " + fruit3);
} catch (NamingException e) {
System.err.println("StoreFruit: error retrieving entry '" +
dn2 + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
cleanup(ctx, dn, dn2);
}
/*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,7 @@
package test;
import java.awt.event.ActionEvent;
import java.io.PrintStream;
import java.net.*;
import java.util.*;
import javax.naming.*;
@ -37,11 +38,17 @@ import javax.naming.directory.*;
public class StoreObject {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") + "/src/test/test/StoreObject.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
@ -64,89 +71,91 @@ public class StoreObject {
* Launch the LDAP server with the StoreObject.ldap capture file
*/
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Store objects in the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
}).start();
/*
* Store objects in the LDAP directory
*/
System.out.println("StoreObject: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
String dn = "cn=myevent";
String dn2 = "cn=myevent2";
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
try {
ctx.bind(dn, new ActionEvent("", 1, "Hello1"));
System.out.println("StoreObject: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreObject: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
System.out.println("StoreObject: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
String dn = "cn=myevent";
String dn2 = "cn=myevent2";
try {
ctx.bind(dn2, new ActionEvent("", 2, "Hello2"));
System.out.println("StoreObject: created entry '" + dn2 + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreObject: entry '" + dn2 +
"' already exists");
cleanup(ctx, dn);
return;
}
try {
ctx.bind(dn, new ActionEvent("", 1, "Hello1"));
System.out.println("StoreObject: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreObject: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
/*
* Retrieve objects from the LDAP directory
*/
try {
ctx.bind(dn2, new ActionEvent("", 2, "Hello2"));
System.out.println("StoreObject: created entry '" + dn2 + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreObject: entry '" + dn2 +
"' already exists");
cleanup(ctx, dn);
return;
}
try {
ActionEvent b = (ActionEvent) ctx.lookup(dn);
System.out.println("StoreObject: retrieved object: " + b);
} catch (NamingException e) {
System.err.println("StoreObject: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
/*
* Retrieve objects from the LDAP directory
*/
try {
ActionEvent t = (ActionEvent) ctx.lookup(dn2);
System.out.println("StoreObject: retrieved object: " + t);
} catch (NamingException e) {
System.err.println("StoreObject: error retrieving entry '" +
dn2 + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
try {
ActionEvent b = (ActionEvent) ctx.lookup(dn);
System.out.println("StoreObject: retrieved object: " + b);
} catch (NamingException e) {
System.err.println("StoreObject: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
ctx.close();
}
try {
ActionEvent t = (ActionEvent) ctx.lookup(dn2);
System.out.println("StoreObject: retrieved object: " + t);
} catch (NamingException e) {
System.err.println("StoreObject: error retrieving entry '" +
dn2 + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
cleanup(ctx, dn, dn2);
ctx.close();
}
/*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,6 +31,7 @@
package test;
import java.io.PrintStream;
import java.net.*;
import java.util.*;
import javax.naming.*;
@ -40,11 +41,17 @@ import org.example.person.Person;
public class StorePerson {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") + "/src/test/test/StorePerson.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
@ -67,115 +74,116 @@ public class StorePerson {
* Launch the LDAP server with the StorePerson.ldap capture file
*/
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Store Person objects in the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
}).start();
/*
* Store Person objects in the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
// Specify the factory classname explicitly
env.put(Context.STATE_FACTORIES, "org.example.person.PersonFactory");
env.put(Context.OBJECT_FACTORIES, "org.example.person.PersonFactory");
System.out.println("StorePerson: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
Person person = null;
String name = "John Smith";
String dn = "cn=" + name;
try {
person = new Person(name, "Smith");
person.setMailAddress("jsmith@smith.com");
ctx.bind(dn, person);
System.out.println("StorePerson: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StorePerson: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
name = "Jill Smyth";
String dn2 = "cn=" + name;
Person person2 = new Person(name, "Smyth");
person2.setMailAddress("jsmyth@smith.com");
try {
ctx.bind(dn2, person2);
System.out.println("StorePerson: created entry '" + dn2 + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StorePerson: entry '" + dn2 +
"' already exists");
cleanup(ctx, dn);
return;
}
/*
* Retrieve Person objects from the LDAP directory
*/
try {
Person person3 = (Person) ctx.lookup(dn);
System.out.println("StorePerson: retrieved object: " + person3);
if (person.getAttributes().equals(person3.getAttributes())) {
System.out.println(
"StorePerson: retrieved person matches original");
} else {
System.out.println(
"StorePerson: retrieved person does NOT match original");
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
} catch (NamingException e) {
System.err.println("StorePerson: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
// Specify the factory classname explicitly
env.put(Context.STATE_FACTORIES, "org.example.person.PersonFactory");
env.put(Context.OBJECT_FACTORIES, "org.example.person.PersonFactory");
System.out.println("StorePerson: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
Person person = null;
String name = "John Smith";
String dn = "cn=" + name;
try {
person = new Person(name, "Smith");
person.setMailAddress("jsmith@smith.com");
ctx.bind(dn, person);
System.out.println("StorePerson: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StorePerson: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
name = "Jill Smyth";
String dn2 = "cn=" + name;
Person person2 = new Person(name, "Smyth");
person2.setMailAddress("jsmyth@smith.com");
try {
ctx.bind(dn2, person2);
System.out.println("StorePerson: created entry '" + dn2 + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StorePerson: entry '" + dn2 +
"' already exists");
cleanup(ctx, dn);
return;
}
/*
* Retrieve Person objects from the LDAP directory
*/
try {
Person person3 = (Person) ctx.lookup(dn);
System.out.println("StorePerson: retrieved object: " + person3);
if (person.getAttributes().equals(person3.getAttributes())) {
System.out.println(
"StorePerson: retrieved person matches original");
} else {
System.out.println(
"StorePerson: retrieved person does NOT match original");
}
} catch (NamingException e) {
System.err.println("StorePerson: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
try {
Person person4 = (Person) ctx.lookup(dn2);
System.out.println("StorePerson: retrieved object: " + person4);
if (person2.getAttributes().equals(person4.getAttributes())) {
System.out.println(
"StorePerson: retrieved person matches original");
} else {
System.out.println(
"StorePerson: retrieved person does NOT match original");
}
} catch (NamingException e) {
System.err.println("StorePerson: error retrieving entry '" +
dn2 + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
cleanup(ctx, dn, dn2);
return;
}
try {
Person person4 = (Person) ctx.lookup(dn2);
System.out.println("StorePerson: retrieved object: " + person4);
if (person2.getAttributes().equals(person4.getAttributes())) {
System.out.println(
"StorePerson: retrieved person matches original");
} else {
System.out.println(
"StorePerson: retrieved person does NOT match original");
}
} catch (NamingException e) {
System.err.println("StorePerson: error retrieving entry '" +
dn2 + "' " + e);
e.printStackTrace();
cleanup(ctx, dn, dn2);
return;
}
cleanup(ctx, dn, dn2);
return;
}
/*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -40,11 +40,17 @@ import org.example.hello.*;
public class StoreRemote {
static {
final PrintStream out = new PrintStream(System.out, true);
final PrintStream err = new PrintStream(System.err, true);
System.setOut(out);
System.setErr(err);
}
// LDAP capture file
private static final String LDAP_CAPTURE_FILE =
System.getProperty("test.src") + "/src/test/test/StoreRemote.ldap";
// LDAPServer socket
private static ServerSocket serverSocket;
public static void main(String[] args) throws Exception {
@ -67,77 +73,79 @@ public class StoreRemote {
* Launch the LDAP server with the StoreRemote.ldap capture file
*/
serverSocket = new ServerSocket(0);
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
try (ServerSocket serverSocket = new ServerSocket()){
serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
new Thread(new Runnable() {
@Override
public void run() {
try {
new LDAPServer(serverSocket, LDAP_CAPTURE_FILE);
} catch (Exception e) {
System.out.println("ERROR: unable to launch LDAP server");
e.printStackTrace();
}
}
}).start();
/*
* Store a Remote object in the LDAP directory
*/
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
}).start();
/*
* Store a Remote object in the LDAP directory
*/
System.out.println("StoreRemote: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
String dn = "cn=myremote";
Hashtable<String,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
URI ldapUri = new URI(args[0]);
if (ldapUri.getPort() == -1) {
ldapUri = new URI(ldapUri.getScheme(), null, ldapUri.getHost(),
serverSocket.getLocalPort(), ldapUri.getPath(), null, null);
}
env.put(Context.PROVIDER_URL, ldapUri.toString());
if (args[args.length - 1].equalsIgnoreCase("-trace")) {
env.put("com.sun.jndi.ldap.trace.ber", System.out);
}
try {
Hello hello = new HelloImpl();
ctx.bind(dn, hello);
System.out.println("StoreRemote: created entry '" + dn + "'");
System.out.println("StoreRemote: connecting to " + ldapUri);
DirContext ctx = new InitialDirContext(env);
String dn = "cn=myremote";
// Explicitly release the RMI object
UnicastRemoteObject.unexportObject(hello, true);
try {
Hello hello = new HelloImpl();
ctx.bind(dn, hello);
System.out.println("StoreRemote: created entry '" + dn + "'");
} catch (NameAlreadyBoundException e) {
System.err.println("StoreRemote: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
// Explicitly release the RMI object
UnicastRemoteObject.unexportObject(hello, true);
/*
* Retrieve the Remote object from the LDAP directory
*/
} catch (NameAlreadyBoundException e) {
System.err.println("StoreRemote: entry '" + dn +
"' already exists");
cleanup(ctx, (String)null);
return;
}
try {
Hello obj = (Hello) ctx.lookup(dn);
System.out.println("StoreRemote: retrieved object: " + obj);
System.out.println("StoreRemote: calling Hello.sayHello()...\n" +
obj.sayHello());
/*
* Retrieve the Remote object from the LDAP directory
*/
// Explicitly release the RMI object
UnicastRemoteObject.unexportObject(obj, true);
try {
Hello obj = (Hello) ctx.lookup(dn);
System.out.println("StoreRemote: retrieved object: " + obj);
System.out.println("StoreRemote: calling Hello.sayHello()...\n" +
obj.sayHello());
} catch (NamingException e) {
System.err.println("StoreRemote: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn);
return;
}
// Explicitly release the RMI object
UnicastRemoteObject.unexportObject(obj, true);
} catch (NamingException e) {
System.err.println("StoreRemote: error retrieving entry '" +
dn + "' " + e);
e.printStackTrace();
cleanup(ctx, dn);
return;
}
cleanup(ctx, dn);
}
/*