throw new IOException( e.getMessage(), e );
}
if ( message.type == TMessageType.CALL ) {
RpcInvocation result = new RpcInvocation();
result.setAttachment(Constants.INTERFACE_KEY, serviceName );
result.setMethodName( message.name );
String argsClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class)
.getExtension(ThriftClassNameGenerator.NAME).generateArgsClassName( serviceName, message.name );
if ( StringUtils.isEmpty( argsClassName ) ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION,
"The specified interface name incorrect." );
}
Class clazz = cachedClass.get( argsClassName );
if ( clazz == null ) {
try {
clazz = ClassHelper.forNameWithThreadContextClassLoader( argsClassName );
cachedClass.putIfAbsent( argsClassName, clazz );
} catch ( ClassNotFoundException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
}
TBase args;
try {
args = ( 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 );
}
try{
args.read( protocol );
protocol.readMessageEnd();
} catch ( TException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
List<Object> parameters = new ArrayList<Object>();
List<Class<?>> parameterTypes =new ArrayList<Class<?>>();
int index = 1;
while ( true ) {
TFieldIdEnum fieldIdEnum = args.fieldForId( index++ );
if ( fieldIdEnum == null ) { break; }
String fieldName = fieldIdEnum.getFieldName();
String getMethodName = ThriftUtils.generateGetMethodName( fieldName );
Method getMethod;
try {
getMethod = clazz.getMethod( getMethodName );
} catch ( NoSuchMethodException e ) {
throw new RpcException(
RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
parameterTypes.add( getMethod.getReturnType() );
try {
parameters.add( getMethod.invoke( args ) );
} catch ( IllegalAccessException e ) {
throw new RpcException(
RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
} catch ( InvocationTargetException e ) {
throw new RpcException(
RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
}
result.setArguments( parameters.toArray() );
result.setParameterTypes(parameterTypes.toArray(new Class[parameterTypes.size()]));
Request request = new Request( id );
request.setData( result );
cachedRequest.putIfAbsent( id,
RequestData.create( message.seqid, serviceName, message.name ) );
return request;
} else if ( message.type == TMessageType.EXCEPTION ) {
TApplicationException exception;
try {
exception = TApplicationException.read( protocol );
protocol.readMessageEnd();
} catch ( TException e ) {
throw new IOException( e.getMessage(), e );
}
RpcResult result = new RpcResult();
result.setException( new RpcException( exception.getMessage() ) );
Response response = new Response();
response.setResult( result );
response.setId( id );
return response;
} else if ( message.type == TMessageType.REPLY ) {
String resultClassName = ExtensionLoader.getExtensionLoader( ClassNameGenerator.class )
.getExtension(ThriftClassNameGenerator.NAME).generateResultClassName( serviceName, message.name );
if ( StringUtils.isEmpty( resultClassName ) ) {
throw new IllegalArgumentException(
new StringBuilder( 32 )
.append( "Could not infer service result class name from service name " )
.append( serviceName )
.append( ", the service name you specified may not generated by thrift idl compiler" )
.toString() );
}
Class<?> clazz = cachedClass.get( resultClassName );
if ( clazz == null ) {
try {
clazz = ClassHelper.forNameWithThreadContextClassLoader( resultClassName );
cachedClass.putIfAbsent( resultClassName, clazz );
} catch ( ClassNotFoundException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
}
TBase<?,? extends TFieldIdEnum> result;
try {
result = ( 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 );
}
try {
result.read( protocol );
protocol.readMessageEnd();
} catch ( TException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
Object realResult = null;
int index = 0;
while ( true ) {
TFieldIdEnum fieldIdEnum = result.fieldForId( index++ );
if ( fieldIdEnum == null ) { break ; }
Field field;