Java Code Examples for org.apache.thrift.TBase#getClass()

The following examples show how to use org.apache.thrift.TBase#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftDocServicePlugin.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Nullable
private static TBase<?, ?> asTBase(Object exampleRequest) {
    final TBase<?, ?> exampleTBase = (TBase<?, ?>) exampleRequest;
    final Class<?> type = exampleTBase.getClass();
    if (!type.getName().endsWith(REQUEST_STRUCT_SUFFIX)) {
        return null;
    }

    final Class<?> serviceType = type.getEnclosingClass();
    if (serviceType == null) {
        return null;
    }

    if (serviceType.getEnclosingClass() != null) {
        return null;
    }

    return exampleTBase;
}
 
Example 2
Source File: TestParquetWriteProtocol.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void validateThrift(String[] expectations, TBase<?, ?> a)
      throws TException {
    final ThriftSchemaConverter thriftSchemaConverter = new ThriftSchemaConverter();
//      System.out.println(a);
    final Class<TBase<?,?>> class1 = (Class<TBase<?,?>>)a.getClass();
    final MessageType schema = thriftSchemaConverter.convert(class1);
    LOG.info("{}", schema);
    final StructType structType = thriftSchemaConverter.toStructType(class1);
    ExpectationValidatingRecordConsumer recordConsumer = new ExpectationValidatingRecordConsumer(new ArrayDeque<String>(Arrays.asList(expectations)));
    final MessageColumnIO columnIO = new ColumnIOFactory().getColumnIO(schema);
    ParquetWriteProtocol p = new ParquetWriteProtocol(new RecordConsumerLoggingWrapper(recordConsumer), columnIO, structType);
    a.write(p);
  }
 
Example 3
Source File: TestParquetWriteProtocol.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private MessageType validatePig(String[] expectations, TBase<?, ?> a) {
  ThriftToPig<TBase<?,?>> thriftToPig = new ThriftToPig(a.getClass());
  ExpectationValidatingRecordConsumer recordConsumer = new ExpectationValidatingRecordConsumer(new ArrayDeque<String>(Arrays.asList(expectations)));
  Schema pigSchema = thriftToPig.toSchema();
  LOG.info("{}", pigSchema);
  MessageType schema = new PigSchemaConverter().convert(pigSchema);
  LOG.info("{}", schema);
  TupleWriteSupport tupleWriteSupport = new TupleWriteSupport(pigSchema);
  tupleWriteSupport.init(null);
  tupleWriteSupport.prepareForWrite(recordConsumer);
  final Tuple pigTuple = thriftToPig.getPigTuple(a);
  LOG.info("{}", pigTuple);
  tupleWriteSupport.write(pigTuple);
  return schema;
}
 
Example 4
Source File: ThriftAgentConnection.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSupportCommand(TBase command) {
    for (TCommandType supportCommand : supportCommandList) {
        if (supportCommand.getClazz() == command.getClass()) {
            return true;
        }
    }

    TCommandTypeVersion commandVersion = TCommandTypeVersion.getVersion(agentInfo.getVersion());
    if (commandVersion.isSupportCommand(command)) {
        return true;
    }

    return false;
}
 
Example 5
Source File: GrpcAgentConnection.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSupportCommand(TBase command) {
    for (TCommandType supportCommand : supportCommandList) {
        if (supportCommand.getClazz() == command.getClass()) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: AgentEventHandlingFilter.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private Class<?> readPayload(byte[] payload) {
    if (payload == null) {
        return Void.class;
    }

    try {
        final Message<TBase<?, ?>> deserialize = SerializationUtils.deserialize(payload, commandDeserializerFactory);
        final TBase tBase = deserialize.getData();
        return tBase.getClass();
    } catch (TException e) {
        logger.warn("Error deserializing ResponseEvent payload", e);
    }
    return null;
}
 
Example 7
Source File: ThriftMessageToResultConverter.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public ResultResponse toMessage(Object object) {
    if (object instanceof ResponseMessage) {
        final ResponseMessage responseMessage = (ResponseMessage) object;
        final byte[] byteMessage = responseMessage.getMessage();
        final Message<TBase<?, ?>> message = SerializationUtils.deserialize(byteMessage, HeaderTBaseDeserializerFactory.DEFAULT_FACTORY, null);
        if (message == null) {
            throw new IllegalArgumentException("message is null. response message=" + responseMessage);
        }

        final TBase<?, ?> tbase = message.getData();
        if (!(tbase instanceof TResult)) {
            throw new IllegalArgumentException("invalid message data. response message=" + responseMessage + ", data=" + tbase.getClass());
        }

        final TResult result = (TResult) tbase;
        return new ResultResponse() {
            @Override
            public boolean isSuccess() {
                return result.isSuccess();
            }

            @Override
            public String getMessage() {
                return result.getMessage();
            }
        };
    }
    return null;
}
 
Example 8
Source File: ThriftConverter.java    From luxun with Apache License 2.0 5 votes vote down vote up
public static byte[] toBytes(TBase event) {
	TSerializer tSerializer = tSerializerLocal.get();
	byte[] data;
	try {
		data = tSerializer.serialize(event);
	} catch (TException e) {
		throw new RuntimeException("fail to serialize thrift object of type " + event.getClass());
	}
	return data;
}
 
Example 9
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private ThriftFunction(
        Class<?> serviceType, String name, Object func, Type type,
        TFieldIdEnum[] argFields, @Nullable TBase<?, ?> result,
        Class<?>[] declaredExceptions, @Nullable Object implementation) throws Exception {

    this.func = func;
    this.type = type;
    this.serviceType = serviceType;
    this.name = name;
    this.argFields = argFields;
    this.result = result;
    this.declaredExceptions = declaredExceptions;
    this.implementation = implementation;

    // Determine the success and exception fields of the function.
    final ImmutableMap.Builder<Class<Throwable>, TFieldIdEnum> exceptionFieldsBuilder =
            ImmutableMap.builder();
    TFieldIdEnum successField = null;

    if (result != null) { // if not oneway
        @SuppressWarnings("unchecked")
        final Class<? extends TBase<?, ?>> resultType = (Class<? extends TBase<?, ?>>) result.getClass();
        @SuppressWarnings("unchecked")
        final Map<TFieldIdEnum, FieldMetaData> metaDataMap =
                (Map<TFieldIdEnum, FieldMetaData>) FieldMetaData.getStructMetaDataMap(resultType);

        for (Entry<TFieldIdEnum, FieldMetaData> e : metaDataMap.entrySet()) {
            final TFieldIdEnum key = e.getKey();
            final String fieldName = key.getFieldName();
            if ("success".equals(fieldName)) {
                successField = key;
                continue;
            }

            final Class<?> fieldType = resultType.getDeclaredField(fieldName).getType();
            if (Throwable.class.isAssignableFrom(fieldType)) {
                @SuppressWarnings("unchecked")
                final Class<Throwable> exceptionFieldType = (Class<Throwable>) fieldType;
                exceptionFieldsBuilder.put(exceptionFieldType, key);
            }
        }
    }

    this.successField = successField;
    exceptionFields = exceptionFieldsBuilder.build();
}