Checker.notNull("parameter:serviceProvider", serviceProvider);
ServerSerializationFactory serializationFactory = createSerializationFactory();
final ObjectInputStream inputStream = serializationFactory.createObjectInputStream(input);
final ObjectOutputStream outputStream = serializationFactory.createObjectOutputStream();
Object result = null;
boolean exceptionWasThrown = false;
Method method = null;
// read in the interface...
final String interfaceName = (String) inputStream.readObject();
final Class interfacee = this.getRequestedInterface(interfaceName);
// verify the serviceProvider actually implements $interface
this.checkServiceProvider(interfacee, serviceProvider);
// the method name...
final String methodName = (String) inputStream.readObject();
// the parameter types...
final int parameterCount = inputStream.readInt();
final String[] parameterTypes = new String[parameterCount];
for (int i = 0; i < parameterTypes.length; i++) {
parameterTypes[i] = (String) inputStream.readObject();
}
// attempt to find a method on the given interface that matches the
// method signature...
method = this.getMethod(serviceProvider.getClass(), methodName, parameterTypes);
// deserialize parameters...
final Object[] parameters = new Object[parameterCount];
for (int i = 0; i < parameterCount; i++) {
parameters[i] = inputStream.readObject();
}
// any exceptions that are thrown will be serialized and included in the
// response...
try {
// prepare to invoke method...
result = this.invoke(serviceProvider, method, parameters);
} catch (final InvocationTargetException invocationTargetException) {
final Throwable caught = invocationTargetException.getTargetException();
// if method throws exception leave...
boolean thrown = false;
final Class caughtType = caught.getClass();
final Class[] thrownExceptions = method.getExceptionTypes();
for (int i = 0; i < thrownExceptions.length; i++) {
final Class thrownException = thrownExceptions[i];
if (caughtType.equals(thrownException)) {
thrown = true;
result = caught;
break;
}
}
if (false == thrown) {
result = new RpcException(caught.getMessage());
}
exceptionWasThrown = true;
} catch (final RuntimeException runtimeException) {
exceptionWasThrown = true;
result = new RpcException(runtimeException.getMessage());
} catch (final Throwable throwable) {
exceptionWasThrown = true;
result = new RpcException(throwable.getMessage());
}
// write the result...
outputStream.writeBoolean(exceptionWasThrown);
outputStream.writeObject(result);
return outputStream.getText();
}