use of java.lang.reflect.InvocationHandler in project jdbc-shards by wplatform.
the class SmartConnection method newInstance.
/**
* Creates a exception trace version of a connection
*
* @param conn - the original connection
* @return - the connection with exception trace
* @throws SQLException
*/
public static Connection newInstance(DataSourceRepository database, SmartDataSource dataSource) {
InvocationHandler handler = new SmartConnection(database, dataSource);
ClassLoader cl = Connection.class.getClassLoader();
return (Connection) Proxy.newProxyInstance(cl, new Class[] { Connection.class }, handler);
}
use of java.lang.reflect.InvocationHandler in project jdbc-shards by wplatform.
the class BaseTestCase method assertThrows.
/**
* Verify the next method call on the object will throw an exception.
*
* @param <T> the class of the object
* @param verifier the result verifier to call
* @param obj the object to wrap
* @return a proxy for the object
*/
@SuppressWarnings("unchecked")
protected <T> T assertThrows(final ResultVerifier verifier, final T obj) {
Class<?> c = obj.getClass();
InvocationHandler ih = new InvocationHandler() {
private Exception called = new Exception("No method called");
@Override
protected void finalize() {
if (called != null) {
called.printStackTrace(System.err);
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
try {
called = null;
Object ret = method.invoke(obj, args);
verifier.verify(ret, null, method, args);
return ret;
} catch (InvocationTargetException e) {
verifier.verify(null, e.getTargetException(), method, args);
Class<?> retClass = method.getReturnType();
if (!retClass.isPrimitive()) {
return null;
}
if (retClass == boolean.class) {
return false;
} else if (retClass == byte.class) {
return (byte) 0;
} else if (retClass == char.class) {
return (char) 0;
} else if (retClass == short.class) {
return (short) 0;
} else if (retClass == int.class) {
return 0;
} else if (retClass == long.class) {
return 0L;
} else if (retClass == float.class) {
return 0F;
} else if (retClass == double.class) {
return 0D;
}
return null;
}
}
};
if (!ProxyCodeGenerator.isGenerated(c)) {
Class<?>[] interfaces = c.getInterfaces();
if (Modifier.isFinal(c.getModifiers()) || (interfaces.length > 0 && getClass() != c)) {
// interface class proxies
if (interfaces.length == 0) {
throw new RuntimeException("Can not create a proxy for the class " + c.getSimpleName() + " because it doesn't implement any interfaces and is final");
}
return (T) Proxy.newProxyInstance(c.getClassLoader(), interfaces, ih);
}
}
try {
Class<?> pc = ProxyCodeGenerator.getClassProxy(c);
Constructor<?> cons = pc.getConstructor(new Class<?>[] { InvocationHandler.class });
return (T) cons.newInstance(new Object[] { ih });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.lang.reflect.InvocationHandler in project cxf by apache.
the class SOAPBindingUtil method getProxy.
public static <T> T getProxy(Class<T> cls, Object obj) {
InvocationHandler ih = new ExtensionInvocationHandler(obj);
/*
* If we put proxies into the loader of the proxied class, they'll just pile up.
*/
Object proxy = null;
try {
proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { cls }, ih);
} catch (Throwable ex) {
// Using cls classloader as a fallback to make it work within OSGi
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
if (contextLoader != cls.getClassLoader()) {
proxy = Proxy.newProxyInstance(cls.getClassLoader(), new Class[] { cls }, ih);
} else {
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new RuntimeException(ex);
}
}
return cls.cast(proxy);
}
use of java.lang.reflect.InvocationHandler in project cxf by apache.
the class HttpsURLConnectionFactory method decorateWithTLS.
/**
* This method assigns the various TLS parameters on the HttpsURLConnection
* from the TLS Client Parameters. Connection parameter is of supertype HttpURLConnection,
* which allows internal cast to potentially divergent subtype (https) implementations.
*/
protected synchronized void decorateWithTLS(TLSClientParameters tlsClientParameters, HttpURLConnection connection) throws GeneralSecurityException {
int hash = tlsClientParameters.hashCode();
if (hash != lastTlsHash) {
lastTlsHash = hash;
socketFactory = null;
}
// tlsClientParameters.sslSocketFactory to allow runtime configuration change
if (tlsClientParameters.isUseHttpsURLConnectionDefaultSslSocketFactory()) {
socketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
} else if (tlsClientParameters.getSSLSocketFactory() != null) {
// see if an SSLSocketFactory was set. This allows easy interop
// with not-yet-commons-ssl.jar, or even just people who like doing their
// own JSSE.
socketFactory = tlsClientParameters.getSSLSocketFactory();
} else if (socketFactory == null) {
// ssl socket factory not yet instantiated, create a new one with tlsClientParameters's Trust
// Managers, Key Managers, etc
SSLContext ctx = org.apache.cxf.transport.https.SSLUtils.getSSLContext(tlsClientParameters);
String[] cipherSuites = SSLUtils.getCiphersuitesToInclude(tlsClientParameters.getCipherSuites(), tlsClientParameters.getCipherSuitesFilter(), ctx.getSocketFactory().getDefaultCipherSuites(), SSLUtils.getSupportedCipherSuites(ctx), LOG);
// The SSLSocketFactoryWrapper enables certain cipher suites
// from the policy.
String protocol = tlsClientParameters.getSecureSocketProtocol() != null ? tlsClientParameters.getSecureSocketProtocol() : "TLS";
socketFactory = new SSLSocketFactoryWrapper(ctx.getSocketFactory(), cipherSuites, protocol);
// recalc the hashcode since some of the above MAY have changed the tlsClientParameters
lastTlsHash = tlsClientParameters.hashCode();
} else {
// ssl socket factory already initialized, reuse it to benefit of keep alive
}
HostnameVerifier verifier = org.apache.cxf.transport.https.SSLUtils.getHostnameVerifier(tlsClientParameters);
if (connection instanceof HttpsURLConnection) {
// handle the expected case (javax.net.ssl)
HttpsURLConnection conn = (HttpsURLConnection) connection;
conn.setHostnameVerifier(verifier);
conn.setSSLSocketFactory(socketFactory);
} else {
// that are similar to the Sun cases
try {
Method method = connection.getClass().getMethod("getHostnameVerifier");
InvocationHandler handler = new ReflectionInvokationHandler(verifier) {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return super.invoke(proxy, method, args);
} catch (Exception ex) {
return true;
}
}
};
Object proxy = java.lang.reflect.Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { method.getReturnType() }, handler);
method = connection.getClass().getMethod("setHostnameVerifier", method.getReturnType());
method.invoke(connection, proxy);
} catch (Exception ex) {
// Ignore this one
}
try {
Method getSSLSocketFactory = connection.getClass().getMethod("getSSLSocketFactory");
Method setSSLSocketFactory = connection.getClass().getMethod("setSSLSocketFactory", getSSLSocketFactory.getReturnType());
if (getSSLSocketFactory.getReturnType().isInstance(socketFactory)) {
setSSLSocketFactory.invoke(connection, socketFactory);
} else {
// need to see if we can create one - mostly the weblogic case. The
// weblogic SSLSocketFactory has a protected constructor that can take
// a JSSE SSLSocketFactory so we'll try and use that
Constructor<?> c = getSSLSocketFactory.getReturnType().getDeclaredConstructor(SSLSocketFactory.class);
ReflectionUtil.setAccessible(c);
setSSLSocketFactory.invoke(connection, c.newInstance(socketFactory));
}
} catch (Exception ex) {
if (connection.getClass().getName().contains("weblogic")) {
if (!weblogicWarned) {
weblogicWarned = true;
LOG.warning("Could not configure SSLSocketFactory on Weblogic. " + " Use the Weblogic control panel to configure the SSL settings.");
}
return;
}
// if we cannot set the SSLSocketFactory, we're in serious trouble.
throw new IllegalArgumentException("Error decorating connection class " + connection.getClass().getName(), ex);
}
}
}
use of java.lang.reflect.InvocationHandler in project cxf by apache.
the class ManagedConnectionImplTest method testAssociateConnectionThrowsException.
@Test
public void testAssociateConnectionThrowsException() throws Throwable {
InvocationHandler ih = EasyMock.createMock(InvocationHandler.class);
Object dodgyHandle = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Foo.class }, ih);
try {
mci.associateConnection(dodgyHandle);
fail("Except exception on call with ClassCast Exception");
} catch (ResourceAdapterInternalException raie) {
assertTrue(true);
}
}
Aggregations