8048175: Remove redundant use of reflection on core classes from JNDI

Reviewed-by: msheppar, vinnie
This commit is contained in:
Pavel Rappo 2014-07-15 16:46:43 +01:00
parent 26689adbd2
commit 73f2d27bdf
5 changed files with 49 additions and 186 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -71,17 +71,10 @@ public class RemoteToCorba implements StateFactory {
if (orig instanceof Remote) { if (orig instanceof Remote) {
// Turn remote object into org.omg.CORBA.Object // Turn remote object into org.omg.CORBA.Object
try {
// Returns null if JRMP; let next factory try // Returns null if JRMP; let next factory try
// CNCtx will eventually throw IllegalArgumentException if // CNCtx will eventually throw IllegalArgumentException if
// no CORBA object gotten // no CORBA object gotten
return return CorbaUtils.remoteToCorba((Remote)orig, ((CNCtx)ctx)._orb);
CorbaUtils.remoteToCorba((Remote)orig, ((CNCtx)ctx)._orb);
} catch (ClassNotFoundException e) {
// RMI-IIOP library not available
throw new ConfigurationException(
"javax.rmi packages not available");
}
} }
return null; // pass and let next state factory try return null; // pass and let next state factory try
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -86,9 +86,8 @@ class ClientId {
try { try {
Class<?> socketFactoryClass = Class<?> socketFactoryClass =
Obj.helper.loadClass(socketFactory); Obj.helper.loadClass(socketFactory);
Class<?> objClass = Class.forName("java.lang.Object");
this.sockComparator = socketFactoryClass.getMethod( this.sockComparator = socketFactoryClass.getMethod(
"compare", new Class<?>[]{objClass, objClass}); "compare", new Class<?>[]{Object.class, Object.class});
Method getDefault = socketFactoryClass.getMethod( Method getDefault = socketFactoryClass.getMethod(
"getDefault", new Class<?>[]{}); "getDefault", new Class<?>[]{});
this.factory = this.factory =

View File

@ -31,6 +31,7 @@ import java.io.InterruptedIOException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
@ -42,11 +43,10 @@ import javax.naming.InterruptedNamingException;
import javax.naming.ldap.Control; import javax.naming.ldap.Control;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import sun.misc.IOUtils; import sun.misc.IOUtils;
//import javax.net.SocketFactory; import javax.net.SocketFactory;
/** /**
* A thread that creates a connection to an LDAP server. * A thread that creates a connection to an LDAP server.
@ -219,9 +219,7 @@ public final class Connection implements Runnable {
ce.setRootCause(realException); ce.setRootCause(realException);
throw ce; throw ce;
} catch (Exception e) { } catch (Exception e) {
// Class.forName() seems to do more error checking // We need to have a catch all here and
// and will throw IllegalArgumentException and such.
// That's why we need to have a catch all here and
// ignore generic exceptions. // ignore generic exceptions.
// Also catches all IO errors generated by socket creation. // Also catches all IO errors generated by socket creation.
CommunicationException ce = CommunicationException ce =
@ -238,27 +236,8 @@ public final class Connection implements Runnable {
/* /*
* Create an InetSocketAddress using the specified hostname and port number. * Create an InetSocketAddress using the specified hostname and port number.
*/ */
private Object createInetSocketAddress(String host, int port) private InetSocketAddress createInetSocketAddress(String host, int port) {
throws NoSuchMethodException { return new InetSocketAddress(host, port);
try {
Class<?> inetSocketAddressClass =
Class.forName("java.net.InetSocketAddress");
Constructor<?> inetSocketAddressCons =
inetSocketAddressClass.getConstructor(new Class<?>[]{
String.class, int.class});
return inetSocketAddressCons.newInstance(new Object[]{
host, new Integer(port)});
} catch (ClassNotFoundException |
InstantiationException |
InvocationTargetException |
IllegalAccessException e) {
throw new NoSuchMethodException();
}
} }
/* /*
@ -279,29 +258,22 @@ public final class Connection implements Runnable {
// create the factory // create the factory
Class<?> socketFactoryClass = Obj.helper.loadClass(socketFactory); Class<? extends SocketFactory> socketFactoryClass =
(Class<? extends SocketFactory>) Obj.helper.loadClass
(socketFactory);
Method getDefault = Method getDefault =
socketFactoryClass.getMethod("getDefault", new Class<?>[]{}); socketFactoryClass.getMethod("getDefault", new Class<?>[]{});
Object factory = getDefault.invoke(null, new Object[]{}); SocketFactory factory = (SocketFactory) getDefault.invoke(null, new Object[]{});
// create the socket // create the socket
Method createSocket = null;
if (connectTimeout > 0) { if (connectTimeout > 0) {
try { InetSocketAddress endpoint =
createSocket = socketFactoryClass.getMethod("createSocket", createInetSocketAddress(host, port);
new Class<?>[]{});
Method connect = Socket.class.getMethod("connect",
new Class<?>[]{Class.forName("java.net.SocketAddress"),
int.class});
Object endpoint = createInetSocketAddress(host, port);
// unconnected socket // unconnected socket
socket = socket = factory.createSocket();
(Socket)createSocket.invoke(factory, new Object[]{});
if (debug) { if (debug) {
System.err.println("Connection: creating socket with " + System.err.println("Connection: creating socket with " +
@ -309,52 +281,34 @@ public final class Connection implements Runnable {
} }
// connected socket // connected socket
connect.invoke(socket, new Object[]{ socket.connect(endpoint, connectTimeout);
endpoint, new Integer(connectTimeout)}); }
} catch (NoSuchMethodException e) {
// continue (but ignore connectTimeout) // continue (but ignore connectTimeout)
}
}
if (socket == null) { if (socket == null) {
createSocket = socketFactoryClass.getMethod("createSocket",
new Class<?>[]{String.class, int.class});
if (debug) { if (debug) {
System.err.println("Connection: creating socket using " + System.err.println("Connection: creating socket using " +
"supplied socket factory"); "supplied socket factory");
} }
// connected socket // connected socket
socket = (Socket) createSocket.invoke(factory, socket = factory.createSocket(host, port);
new Object[]{host, new Integer(port)});
} }
} else { } else {
if (connectTimeout > 0) { if (connectTimeout > 0) {
try { InetSocketAddress endpoint = createInetSocketAddress(host, port);
Constructor<Socket> socketCons =
Socket.class.getConstructor(new Class<?>[]{});
Method connect = Socket.class.getMethod("connect", socket = new Socket();
new Class<?>[]{Class.forName("java.net.SocketAddress"),
int.class});
Object endpoint = createInetSocketAddress(host, port);
socket = socketCons.newInstance(new Object[]{});
if (debug) { if (debug) {
System.err.println("Connection: creating socket with " + System.err.println("Connection: creating socket with " +
"a timeout"); "a timeout");
} }
connect.invoke(socket, new Object[]{ socket.connect(endpoint, connectTimeout);
endpoint, new Integer(connectTimeout)}); }
} catch (NoSuchMethodException e) {
// continue (but ignore connectTimeout) // continue (but ignore connectTimeout)
}
}
if (socket == null) { if (socket == null) {
if (debug) { if (debug) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -30,32 +30,10 @@ import java.net.URL;
abstract class VersionHelper { abstract class VersionHelper {
private static VersionHelper helper = null; private static final VersionHelper helper = new VersionHelper12();
VersionHelper() {} // Disallow anyone from creating one of these. VersionHelper() {} // Disallow anyone from creating one of these.
static {
try {
Class.forName("java.net.URLClassLoader"); // 1.2 test
Class.forName("java.security.PrivilegedAction"); // 1.2 test
helper = (VersionHelper)
Class.forName(
"com.sun.jndi.ldap.VersionHelper12").newInstance();
} catch (Exception e) {
}
// Use 1.1 helper if 1.2 test fails, or if we cannot create 1.2 helper
if (helper == null) {
try {
helper = (VersionHelper)
Class.forName(
"com.sun.jndi.ldap.VersionHelper11").newInstance();
} catch (Exception e) {
// should never happen
}
}
}
static VersionHelper getVersionHelper() { static VersionHelper getVersionHelper() {
return helper; return helper;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,7 @@ import java.rmi.Remote;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Properties; import java.util.Properties;
import java.util.Enumeration; import java.util.Enumeration;
@ -38,6 +39,8 @@ import org.omg.CORBA.ORB;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import javax.rmi.CORBA.Stub;
import javax.rmi.PortableRemoteObject;
/** /**
* Contains utilities for performing CORBA-related tasks: * Contains utilities for performing CORBA-related tasks:
@ -76,71 +79,43 @@ public class CorbaUtils {
* @param orb The non-null ORB to connect the remote object to * @param orb The non-null ORB to connect the remote object to
* @return The CORBA Object for remoteObj; null if <tt>remoteObj</tt> * @return The CORBA Object for remoteObj; null if <tt>remoteObj</tt>
* is a JRMP implementation or JRMP stub. * is a JRMP implementation or JRMP stub.
* @exception ClassNotFoundException The RMI-IIOP package is not available
* @exception ConfigurationException The CORBA Object cannot be obtained * @exception ConfigurationException The CORBA Object cannot be obtained
* because of configuration problems. * because of configuration problems.
*/ */
public static org.omg.CORBA.Object remoteToCorba(Remote remoteObj, ORB orb) public static org.omg.CORBA.Object remoteToCorba(Remote remoteObj, ORB orb)
throws ClassNotFoundException, ConfigurationException { throws ConfigurationException {
synchronized (CorbaUtils.class) {
if (toStubMethod == null) {
initMethodHandles();
}
}
// First, get remoteObj's stub // First, get remoteObj's stub
// javax.rmi.CORBA.Stub stub = PortableRemoteObject.toStub(remoteObj); // javax.rmi.CORBA.Stub stub = PortableRemoteObject.toStub(remoteObj);
java.lang.Object stub; Remote stub;
try { try {
stub = toStubMethod.invoke(null, new java.lang.Object[]{remoteObj}); stub = PortableRemoteObject.toStub(remoteObj);
} catch (Throwable t) {
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
// realException.printStackTrace();
ConfigurationException ce = new ConfigurationException( ConfigurationException ce = new ConfigurationException(
"Problem with PortableRemoteObject.toStub(); object not exported or stub not found"); "Problem with PortableRemoteObject.toStub(); object not exported or stub not found");
ce.setRootCause(realException); ce.setRootCause(t);
throw ce;
} catch (IllegalAccessException e) {
ConfigurationException ce = new ConfigurationException(
"Cannot invoke javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)");
ce.setRootCause(e);
throw ce; throw ce;
} }
// Next, make sure that the stub is javax.rmi.CORBA.Stub // Next, make sure that the stub is javax.rmi.CORBA.Stub
if (!corbaStubClass.isInstance(stub)) { if (!(stub instanceof Stub)) {
return null; // JRMP implementation or JRMP stub return null; // JRMP implementation or JRMP stub
} }
// Next, make sure that the stub is connected // Next, make sure that the stub is connected
// Invoke stub.connect(orb)
try { try {
connectMethod.invoke(stub, new java.lang.Object[]{orb}); ((Stub) stub).connect(orb);
} catch (RemoteException e) {
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
// realException.printStackTrace();
if (!(realException instanceof java.rmi.RemoteException)) {
ConfigurationException ce = new ConfigurationException(
"Problem invoking javax.rmi.CORBA.Stub.connect()");
ce.setRootCause(realException);
throw ce;
}
// ignore RemoteException because stub might have already // ignore RemoteException because stub might have already
// been connected // been connected
} catch (IllegalAccessException e) { } catch (Throwable t) {
ConfigurationException ce = new ConfigurationException( ConfigurationException ce = new ConfigurationException(
"Cannot invoke javax.rmi.CORBA.Stub.connect()"); "Problem invoking javax.rmi.CORBA.Stub.connect()");
ce.setRootCause(e); ce.setRootCause(t);
throw ce; throw ce;
} }
// Finally, return stub // Finally, return stub
@ -235,40 +210,4 @@ public class CorbaUtils {
throw new AssertionError(iae); throw new AssertionError(iae);
} }
} }
// Fields used for reflection of RMI-IIOP
private static Method toStubMethod = null;
private static Method connectMethod = null;
private static Class<?> corbaStubClass = null;
/**
* Initializes reflection method handles for RMI-IIOP.
* @exception ClassNotFoundException javax.rmi.CORBA.* not available
*/
private static void initMethodHandles() throws ClassNotFoundException {
// Get javax.rmi.CORBA.Stub class
corbaStubClass = Class.forName("javax.rmi.CORBA.Stub");
// Get javax.rmi.CORBA.Stub.connect(org.omg.CORBA.ORB) method
try {
connectMethod = corbaStubClass.getMethod("connect",
new Class<?>[] {org.omg.CORBA.ORB.class});
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"No method definition for javax.rmi.CORBA.Stub.connect(org.omg.CORBA.ORB)");
}
// Get javax.rmi.PortableRemoteObject class
Class<?> proClass = Class.forName("javax.rmi.PortableRemoteObject");
// Get javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote) method
try {
toStubMethod = proClass.getMethod("toStub",
new Class<?>[] {java.rmi.Remote.class});
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"No method definition for javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)");
}
}
} }