List of usage examples for org.apache.thrift TBase isSet
public boolean isSet(F field);
From source file:com.alibaba.jstorm.utils.JStormUtils.java
License:Apache License
public static Map<String, Object> thriftToMap(org.apache.thrift.TBase thriftObj) { Map<String, Object> ret = new HashMap<String, Object>(); int i = 1;//from w w w . java2s . c o m TFieldIdEnum field = thriftObj.fieldForId(i); while (field != null) { if (thriftObj.isSet(field)) { Object obj = thriftObj.getFieldValue(field); ret.put(field.getFieldName(), thriftToObject(obj)); } field = thriftObj.fieldForId(++i); } return ret; }
From source file:com.linecorp.armeria.client.thrift.ThriftClientCodec.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from w w w . ja v a2 s . c om*/
public <T> T decodeResponse(ServiceInvocationContext ctx, ByteBuf content, Object originalResponse)
throws Exception {
if (content == null) {
return null;
}
if (!content.isReadable()) {
ThriftMethod thriftMethod = getThriftMethod(ctx);
if (thriftMethod != null && thriftMethod.isOneWay()) {
return null;
}
throw new TApplicationException(TApplicationException.MISSING_RESULT, ctx.toString());
}
TByteBufInputTransport inputTransport = new TByteBufInputTransport(content);
TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport);
TMessage msg = inputProtocol.readMessageBegin();
if (msg.type == TMessageType.EXCEPTION) {
TApplicationException ex = TApplicationException.read(inputProtocol);
inputProtocol.readMessageEnd();
throw ex;
}
ThriftMethod method = methodMap.get(msg.name);
if (method == null) {
throw new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name);
}
TBase<? extends TBase, TFieldIdEnum> result = method.createResult();
result.read(inputProtocol);
inputProtocol.readMessageEnd();
for (TFieldIdEnum fieldIdEnum : method.getExceptionFields()) {
if (result.isSet(fieldIdEnum)) {
throw (TException) result.getFieldValue(fieldIdEnum);
}
}
TFieldIdEnum successField = method.successField();
if (successField == null) { //void method
return null;
}
if (result.isSet(successField)) {
return (T) result.getFieldValue(successField);
}
throw new TApplicationException(TApplicationException.MISSING_RESULT,
result.getClass().getName() + '.' + successField.getFieldName());
}
From source file:com.linecorp.armeria.client.thrift.ThriftClientDelegate.java
License:Apache License
private Object decodeResponse(ThriftFunction method, HttpData content) throws TException { if (content.isEmpty()) { if (method.isOneway()) { return null; }//from w w w . j a va 2 s .c om throw new TApplicationException(TApplicationException.MISSING_RESULT); } final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(), content.length()); final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport); final TMessage msg = inputProtocol.readMessageBegin(); if (msg.type == TMessageType.EXCEPTION) { TApplicationException ex = TApplicationException.read(inputProtocol); inputProtocol.readMessageEnd(); throw ex; } if (!method.name().equals(msg.name)) { throw new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name); } TBase<? extends TBase<?, ?>, TFieldIdEnum> result = method.newResult(); result.read(inputProtocol); inputProtocol.readMessageEnd(); for (TFieldIdEnum fieldIdEnum : method.exceptionFields()) { if (result.isSet(fieldIdEnum)) { throw (TException) result.getFieldValue(fieldIdEnum); } } TFieldIdEnum successField = method.successField(); if (successField == null) { // void method return null; } if (result.isSet(successField)) { return result.getFieldValue(successField); } throw new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName()); }
From source file:com.linecorp.armeria.client.thrift.THttpClientDelegate.java
License:Apache License
private void handle(ClientRequestContext ctx, DefaultRpcResponse reply, ThriftFunction func, HttpData content) throws TException { if (func.isOneWay()) { handleSuccess(ctx, reply, null, null); return;/*from w w w . j a v a 2 s .com*/ } if (content.isEmpty()) { throw new TApplicationException(TApplicationException.MISSING_RESULT); } final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(), content.length()); final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport); final TMessage header = inputProtocol.readMessageBegin(); final TApplicationException appEx = readApplicationException(func, inputProtocol, header); if (appEx != null) { handleException(ctx, reply, new ThriftReply(header, appEx), appEx); return; } TBase<? extends TBase<?, ?>, TFieldIdEnum> result = func.newResult(); result.read(inputProtocol); inputProtocol.readMessageEnd(); final ThriftReply rawResponseContent = new ThriftReply(header, result); for (TFieldIdEnum fieldIdEnum : func.exceptionFields()) { if (result.isSet(fieldIdEnum)) { final TException cause = (TException) ThriftFieldAccess.get(result, fieldIdEnum); handleException(ctx, reply, rawResponseContent, cause); return; } } final TFieldIdEnum successField = func.successField(); if (successField == null) { // void method handleSuccess(ctx, reply, null, rawResponseContent); return; } if (result.isSet(successField)) { final Object returnValue = ThriftFieldAccess.get(result, successField); handleSuccess(ctx, reply, returnValue, rawResponseContent); return; } handleException(ctx, reply, rawResponseContent, new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName())); }
From source file:com.linecorp.armeria.internal.thrift.ThriftFunction.java
License:Apache License
/** * Converts the specified {@code result} into a Java object. *///from www . jav a 2s. co m public Object getResult(TBase<TBase<?, ?>, TFieldIdEnum> result) throws TException { for (TFieldIdEnum fieldIdEnum : exceptionFields()) { if (result.isSet(fieldIdEnum)) { throw (TException) ThriftFieldAccess.get(result, fieldIdEnum); } } final TFieldIdEnum successField = successField(); if (successField == null) { //void method return null; } else if (result.isSet(successField)) { return ThriftFieldAccess.get(result, successField); } else { throw new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName()); } }
From source file:com.sleepycat.server.util.Adapters.java
License:Open Source License
private static <F extends TFieldIdEnum, B> B updateBdbObject(B bdbObj, TBase<?, F> tObj, F[] fields, Map<F, BiConsumer<B, Object>> setters) { if (tObj == null) return bdbObj; try {//www.ja v a2 s . c o m Map<String, PropertyDescriptor> properties = getProperties(bdbObj); for (F f : fields) { if (tObj.isSet(f)) { Object value = tObj.getFieldValue(f); if (setters != null && setters.containsKey(f)) { setters.get(f).accept(bdbObj, value); } else { PropertyDescriptor p = properties.get(f.getFieldName()); p.getWriteMethod().invoke(bdbObj, value); } } } return bdbObj; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:ezbake.deployer.utilities.Utilities.java
License:Apache License
private static String ppTBaseField(TBase base, Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry, int depth) { String strValue;/* www. ja v a 2s . com*/ //noinspection unchecked if (base.isSet(entry.getKey())) { @SuppressWarnings("unchecked") Object value = base.getFieldValue(entry.getKey()); strValue = ppObject(value, depth); } else { strValue = "not set"; } return strValue; }
From source file:org.apache.aurora.common.thrift.Util.java
License:Apache License
/** * Prints a TBase.//from www . j a v a 2s.com * * @param t The object to print. * @param depth The print nesting level. * @return The pretty-printed version of the TBase. */ private static String printTbase(TBase t, int depth) { List<String> fields = Lists.newArrayList(); for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry : FieldMetaData .getStructMetaDataMap(t.getClass()).entrySet()) { @SuppressWarnings("unchecked") boolean fieldSet = t.isSet(entry.getKey()); String strValue; if (fieldSet) { @SuppressWarnings("unchecked") Object value = t.getFieldValue(entry.getKey()); strValue = printValue(value, depth); } else { strValue = "not set"; } fields.add(tabs(depth) + entry.getValue().fieldName + ": " + strValue); } return Joiner.on("\n").join(fields); }
From source file:org.apache.avro.thrift.ThriftData.java
License:Apache License
@Override protected Object getField(Object record, String name, int pos, Object state) { TFieldIdEnum f = ((TFieldIdEnum[]) state)[pos]; TBase struct = (TBase) record; if (struct.isSet(f)) return struct.getFieldValue(f); return null;/*from w w w . j ava 2 s . c o m*/ }