/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package Framework.remoting;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.rmi.Remote;
import java.rmi.RemoteException;
import Framework.DistributedAccessException;
import Framework.ErrorMgr;
public class RemoteExceptionTransformingProxyFactory {
/**
* Wraps a remote object whose methods throw RemoteExceptions into a dynamic proxy that doesn't.
* So the remote object must be retrieved first, and then passed into this method to create a proxy for it.
* Note that if the target object doesn't implement methods with the same signature as the target interface,
* a NoSuchMethodException will be thrown when these methods are invoked.
*
* @param targetObject a remote object whose methods throw the checked RemoteException
* @param targetInterface an interface with the same method signatures as the target object, except that they don't throw RemoteExceptions
* @return a dynamic proxy for the target object implementing the target interface, that converts RemoteExceptions in DistributedAccessExceptions
*/
@SuppressWarnings("unchecked") //JVM 1.5
public static <T> T transform(Remote targetObject, Class<T> targetInterface) {
return (T)Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
new Class[] {targetInterface},
new RemoteExceptionTransformingInvocationHandler(targetObject));
}
private static class RemoteExceptionTransformingInvocationHandler implements InvocationHandler {
private Remote targetObject;
public RemoteExceptionTransformingInvocationHandler(Remote targetObject) {
this.targetObject = targetObject;
}
public Object invoke(Object proxy, Method interfaceMethod, Object[] args) throws Throwable {
Method targetObjectMethod = targetObject.getClass().getMethod(interfaceMethod.getName(),
interfaceMethod.getParameterTypes());
try {
return targetObjectMethod.invoke(targetObject, args);
}
catch (InvocationTargetException e) {
if (e.getCause() instanceof RemoteException) {
DistributedAccessException errorVar = new DistributedAccessException(e.getCause());
ErrorMgr.addError(errorVar);
throw errorVar;
}
throw e.getCause();
}
}
}
}