publicclass BalancedParentheses { // Should we run the client or server in a separate thread? // // Both sides can throw exceptions, but do you have a preference // as to which side should be the main thread. staticboolean separateServerThread = true;
// use any free port by default volatileint serverPort = 0;
// Is the server ready to serve? volatilestaticboolean serverReady = false;
// Define the server side of the test. // // If the server prematurely exits, serverReady will be set to true // to avoid infinite hangs. void doServerSide() throws Exception { // Create unbound server socket
ServerSocket serverSock = new ServerSocket();
// And bind it to the loopback address
SocketAddress sockAddr = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0);
serverSock.bind(sockAddr);
// signal client, it's ready to accecpt connection
serverPort = serverSock.getLocalPort();
serverReady = true;
// Define the client side of the test. // // If the server prematurely exits, serverReady will be set to true // to avoid infinite hangs. void doClientSide() throws Exception { // Wait for server to get started. while (!serverReady) { Thread.sleep(50);
}
// set up the environment for creating the initial context
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // Construct the provider URL
String providerURL = URIBuilder.newBuilder()
.scheme("ldap")
.loopback()
.port(serverPort)
.build().toString();
env.put(Context.PROVIDER_URL, providerURL);
env.put("com.sun.jndi.ldap.read.timeout", "1000");
// create initial context
DirContext context = new InitialDirContext(env);
// searching
SearchControls scs = new SearchControls();
scs.setSearchScope(SearchControls.SUBTREE_SCOPE);
try {
NamingEnumeration<SearchResult> answer = context.search( "o=sun,c=us", "(&(cn=Bob)))", scs);
} catch (InvalidSearchFilterException isfe) { // ignore, it is the expected filter exception.
System.out.println("Expected exception: " + isfe.getMessage());
} catch (NamingException ne) { // maybe a read timeout exception, as the server does not response. thrownew Exception("Expect a InvalidSearchFilterException", ne);
}
try {
NamingEnumeration<SearchResult> answer = context.search( "o=sun,c=us", ")(&(cn=Bob)", scs);
} catch (InvalidSearchFilterException isfe) { // ignore, it is the expected filter exception.
System.out.println("Expected exception: " + isfe.getMessage());
} catch (NamingException ne) { // maybe a read timeout exception, as the server does not response. thrownew Exception("Expect a InvalidSearchFilterException", ne);
}
try {
NamingEnumeration<SearchResult> answer = context.search( "o=sun,c=us", "(&(cn=Bob))", scs);
} catch (InvalidSearchFilterException isfe) { // ignore, it is the expected filter exception. thrownew Exception("Unexpected ISFE", isfe);
} catch (NamingException ne) { // maybe a read timeout exception, as the server does not response.
System.out.println("Expected exception: " + ne.getMessage());
}
// Primary constructor, used to drive remainder of the test.
BalancedParentheses() throws Exception { if (separateServerThread) {
startServer(true);
startClient(false);
} else {
startClient(true);
startServer(false);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.