throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
}
TBase resultObj;
try {
resultObj = ( TBase ) clazz.newInstance();
} catch ( InstantiationException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
} catch ( IllegalAccessException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
TApplicationException applicationException = null;
TMessage message;
if ( result.hasException() ) {
Throwable throwable = result.getException();
int index = 1;
boolean found = false;
while ( true ) {
TFieldIdEnum fieldIdEnum = resultObj.fieldForId( index++ );
if ( fieldIdEnum == null ) { break; }
String fieldName = fieldIdEnum.getFieldName();
String getMethodName = ThriftUtils.generateGetMethodName( fieldName );
String setMethodName = ThriftUtils.generateSetMethodName( fieldName );
Method getMethod;
Method setMethod;
try {
getMethod = clazz.getMethod( getMethodName );
if ( getMethod.getReturnType().equals( throwable.getClass() ) ) {
found = true;
setMethod = clazz.getMethod( setMethodName, throwable.getClass() );
setMethod.invoke( resultObj, throwable );
}
} catch ( NoSuchMethodException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
} catch ( InvocationTargetException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
} catch ( IllegalAccessException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
}
if ( !found ) {
applicationException = new TApplicationException( throwable.getMessage() );
}
} else {
Object realResult = result.getResult();
// result field id is 0
String fieldName = resultObj.fieldForId( 0 ).getFieldName();
String setMethodName = ThriftUtils.generateSetMethodName( fieldName );
String getMethodName = ThriftUtils.generateGetMethodName( fieldName );
Method getMethod;
Method setMethod;
try {
getMethod = clazz.getMethod( getMethodName );
setMethod = clazz.getMethod( setMethodName, getMethod.getReturnType() );
setMethod.invoke( resultObj, realResult );
} catch ( NoSuchMethodException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
} catch ( InvocationTargetException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
} catch ( IllegalAccessException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
}
if ( applicationException != null ) {
message = new TMessage( rd.methodName, TMessageType.EXCEPTION, rd.id );
} else {
message = new TMessage( rd.methodName, TMessageType.REPLY, rd.id );
}
RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 1024 );
TIOStreamTransport transport = new TIOStreamTransport( bos );
TBinaryProtocol protocol = new TBinaryProtocol( transport );
int messageLength;
int headerLength;
byte[] bytes = new byte[4];
try {
// magic
protocol.writeI16( MAGIC );
// message length
protocol.writeI32( Integer.MAX_VALUE );
// message header length
protocol.writeI16( Short.MAX_VALUE );
// version
protocol.writeByte( VERSION );
// service name
protocol.writeString( rd.serviceName );
// id
protocol.writeI64( response.getId() );
protocol.getTransport().flush();
headerLength = bos.size();
// message
protocol.writeMessageBegin( message );
switch ( message.type ) {
case TMessageType.EXCEPTION:
applicationException.write( protocol );
break;
case TMessageType.REPLY:
resultObj.write( protocol );
break;
}
protocol.writeMessageEnd();
protocol.getTransport().flush();
int oldIndex = messageLength = bos.size();