}
private void encodeRequest( Channel channel, ChannelBuffer buffer, Request request )
throws IOException {
RpcInvocation inv = ( RpcInvocation ) request.getData();
int seqId = nextSeqId();
String serviceName = inv.getAttachment(Constants.INTERFACE_KEY);
if ( StringUtils.isEmpty( serviceName ) ) {
throw new IllegalArgumentException(
new StringBuilder( 32 )
.append( "Could not find service name in attachment with key " )
.append(Constants.INTERFACE_KEY)
.toString() );
}
TMessage message = new TMessage(
inv.getMethodName(),
TMessageType.CALL,
seqId );
String methodArgs = ExtensionLoader.getExtensionLoader( ClassNameGenerator.class )
.getExtension(channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME))
.generateArgsClassName(serviceName, inv.getMethodName());
if ( StringUtils.isEmpty( methodArgs ) ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION,
new StringBuilder(32).append(
"Could not encode request, the specified interface may be incorrect." ).toString() );
}
Class<?> clazz = cachedClass.get( methodArgs );
if ( clazz == null ) {
try {
clazz = ClassHelper.forNameWithThreadContextClassLoader( methodArgs );
cachedClass.putIfAbsent( methodArgs, 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 );
}
for( int i = 0; i < inv.getArguments().length; i++ ) {
Object obj = inv.getArguments()[i];
if ( obj == null ) { continue; }
TFieldIdEnum field = args.fieldForId( i + 1 );
String setMethodName = ThriftUtils.generateSetMethodName( field.getFieldName() );
Method method;
try {
method = clazz.getMethod( setMethodName, inv.getParameterTypes()[i] );
} catch ( NoSuchMethodException e ) {
throw new RpcException( RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e );
}
try {