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.
*
* 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) {
// Turn remote object into org.omg.CORBA.Object
try {
// Returns null if JRMP; let next factory try
// CNCtx will eventually throw IllegalArgumentException if
// no CORBA object gotten
return
CorbaUtils.remoteToCorba((Remote)orig, ((CNCtx)ctx)._orb);
} catch (ClassNotFoundException e) {
// RMI-IIOP library not available
throw new ConfigurationException(
"javax.rmi packages not available");
}
// Returns null if JRMP; let next factory try
// CNCtx will eventually throw IllegalArgumentException if
// no CORBA object gotten
return CorbaUtils.remoteToCorba((Remote)orig, ((CNCtx)ctx)._orb);
}
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -86,9 +86,8 @@ class ClientId {
try {
Class<?> socketFactoryClass =
Obj.helper.loadClass(socketFactory);
Class<?> objClass = Class.forName("java.lang.Object");
this.sockComparator = socketFactoryClass.getMethod(
"compare", new Class<?>[]{objClass, objClass});
"compare", new Class<?>[]{Object.class, Object.class});
Method getDefault = socketFactoryClass.getMethod(
"getDefault", new Class<?>[]{});
this.factory =

View File

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

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.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,32 +30,10 @@ import java.net.URL;
abstract class VersionHelper {
private static VersionHelper helper = null;
private static final VersionHelper helper = new VersionHelper12();
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() {
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.
*
* 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.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Enumeration;
@ -38,6 +39,8 @@ import org.omg.CORBA.ORB;
import javax.naming.Context;
import javax.naming.ConfigurationException;
import javax.rmi.CORBA.Stub;
import javax.rmi.PortableRemoteObject;
/**
* 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
* @return The CORBA Object for remoteObj; null if <tt>remoteObj</tt>
* is a JRMP implementation or JRMP stub.
* @exception ClassNotFoundException The RMI-IIOP package is not available
* @exception ConfigurationException The CORBA Object cannot be obtained
* because of configuration problems.
*/
public static org.omg.CORBA.Object remoteToCorba(Remote remoteObj, ORB orb)
throws ClassNotFoundException, ConfigurationException {
synchronized (CorbaUtils.class) {
if (toStubMethod == null) {
initMethodHandles();
}
}
throws ConfigurationException {
// First, get remoteObj's stub
// javax.rmi.CORBA.Stub stub = PortableRemoteObject.toStub(remoteObj);
java.lang.Object stub;
Remote stub;
try {
stub = toStubMethod.invoke(null, new java.lang.Object[]{remoteObj});
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
// realException.printStackTrace();
stub = PortableRemoteObject.toStub(remoteObj);
} catch (Throwable t) {
ConfigurationException ce = new ConfigurationException(
"Problem with PortableRemoteObject.toStub(); object not exported or stub not found");
ce.setRootCause(realException);
throw ce;
} catch (IllegalAccessException e) {
ConfigurationException ce = new ConfigurationException(
"Cannot invoke javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)");
ce.setRootCause(e);
ce.setRootCause(t);
throw ce;
}
// 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
}
// Next, make sure that the stub is connected
// Invoke stub.connect(orb)
try {
connectMethod.invoke(stub, new java.lang.Object[]{orb});
} 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;
}
((Stub) stub).connect(orb);
} catch (RemoteException e) {
// ignore RemoteException because stub might have already
// been connected
} catch (IllegalAccessException e) {
} catch (Throwable t) {
ConfigurationException ce = new ConfigurationException(
"Cannot invoke javax.rmi.CORBA.Stub.connect()");
ce.setRootCause(e);
"Problem invoking javax.rmi.CORBA.Stub.connect()");
ce.setRootCause(t);
throw ce;
}
// Finally, return stub
@ -235,40 +210,4 @@ public class CorbaUtils {
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)");
}
}
}