Author: david.lloyd(a)jboss.com
Date: 2009-04-30 22:17:23 -0400 (Thu, 30 Apr 2009)
New Revision: 5110
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/SpiUtils.java
Log:
remove some duplicate code
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/SpiUtils.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/SpiUtils.java 2009-05-01
01:11:18 UTC (rev 5109)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/SpiUtils.java 2009-05-01
02:17:23 UTC (rev 5110)
@@ -141,20 +141,8 @@
public static <T extends ConnectionHandler> ConnectionHandlerFactory
connectionHandlerFactory(final Class<T> handlerClass) throws
IllegalArgumentException {
return AccessController.doPrivileged(new
PrivilegedAction<ConnectionHandlerFactory>() {
public ConnectionHandlerFactory run() {
- final Constructor<T> constructor;
- try {
- constructor = handlerClass.getConstructor(ConnectionHandler.class);
- } catch (NoSuchMethodException e) {
- throw new IllegalArgumentException("No valid constructor is
present");
- }
- if ((handlerClass.getModifiers() & constructor.getModifiers() &
Modifier.PUBLIC) == 0) {
- throw new IllegalArgumentException("Class or constructor is not
public");
- }
- for (Class<?> exceptionType : constructor.getExceptionTypes()) {
- if (Exception.class.isAssignableFrom(exceptionType) && !
RuntimeException.class.isAssignableFrom(exceptionType)) {
- throw new IllegalArgumentException("Constructor may not
throw checked exceptions");
- }
- }
+ final Constructor<T> constructor =
getPutOneArgConstructor(ConnectionHandler.class, handlerClass);
+ checkForCheckedExceptions(constructor);
return new ConnectionHandlerFactory() {
public ConnectionHandler createInstance(final ConnectionHandler
localConnectionHandler) {
return AccessController.doPrivileged(new
PrivilegedAction<ConnectionHandler>() {
@@ -176,6 +164,19 @@
});
}
+ private static <T> Constructor<T> getPutOneArgConstructor(final
Class<?> argType, final Class<T> targetClass) {
+ final Constructor<T> constructor;
+ try {
+ constructor = targetClass.getConstructor(argType);
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException("No valid constructor is
present");
+ }
+ if ((targetClass.getModifiers() & constructor.getModifiers() &
Modifier.PUBLIC) == 0) {
+ throw new IllegalArgumentException("Class or constructor is not
public");
+ }
+ return constructor;
+ }
+
/**
* Create a connection provider factory for a public class which implements {@code
ConnectionProvider} and has a
* public constructor which accepts a {@code ConnectionProviderContext} as its sole
parameter.
@@ -188,20 +189,8 @@
public static <T extends ConnectionProvider> ConnectionProviderFactory
connectionProviderFactory(final Class<T> providerClass) throws
IllegalArgumentException {
return AccessController.doPrivileged(new
PrivilegedAction<ConnectionProviderFactory>() {
public ConnectionProviderFactory run() {
- final Constructor<T> constructor;
- try {
- constructor =
providerClass.getConstructor(ConnectionProviderContext.class);
- } catch (NoSuchMethodException e) {
- throw new IllegalArgumentException("No valid constructor is
present");
- }
- if ((providerClass.getModifiers() & constructor.getModifiers() &
Modifier.PUBLIC) == 0) {
- throw new IllegalArgumentException("Class or constructor is not
public");
- }
- for (Class<?> exceptionType : constructor.getExceptionTypes()) {
- if (Exception.class.isAssignableFrom(exceptionType) && !
RuntimeException.class.isAssignableFrom(exceptionType)) {
- throw new IllegalArgumentException("Constructor may not
throw checked exceptions");
- }
- }
+ final Constructor<T> constructor =
getPutOneArgConstructor(ConnectionProviderContext.class, providerClass);
+ checkForCheckedExceptions(constructor);
return new ConnectionProviderFactory() {
public ConnectionProvider createInstance(final
ConnectionProviderContext context) {
return AccessController.doPrivileged(new
PrivilegedAction<ConnectionProvider>() {
@@ -223,6 +212,14 @@
});
}
+ private static void checkForCheckedExceptions(final Constructor<?> constructor)
{
+ for (Class<?> exceptionType : constructor.getExceptionTypes()) {
+ if (! Error.class.isAssignableFrom(exceptionType) && !
RuntimeException.class.isAssignableFrom(exceptionType)) {
+ throw new IllegalArgumentException("Constructor may not throw
checked exceptions");
+ }
+ }
+ }
+
/**
* Get a remote request context that simply ignores a cancel request.
*