All Downloads are FREE. Search and download functionalities are using the official Maven repository.

as.leap.vertx.rpc.impl.RPCBase Maven / Gradle / Ivy

There is a newer version: 3.3.8
Show newest version
package as.leap.vertx.rpc.impl;

import as.leap.vertx.rpc.IgnoreWrapCheck;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * Created by stream.
 */
abstract class RPCBase {
  static final String CALLBACK_TYPE = "callbackType";

  void checkBusAddress(String address) {
    Objects.requireNonNull(address, "service's event bus address can not be null.");
  }

  boolean isWrapType(Class clazz) {
    if (clazz.getAnnotation(IgnoreWrapCheck.class) != null) {
      return false;
    } else {
      return clazz.isPrimitive() || clazz.isArray() || clazz.isEnum()
        || Collection.class.isAssignableFrom(clazz)
        || Map.class.isAssignableFrom(clazz)
        || Modifier.isAbstract(clazz.getModifiers())
        || clazz.isInterface();
    }
  }

  private  byte[] toBytes(Schema schema, T object) throws Exception {
    LinkedBuffer buffer = LinkedBuffer.allocate();
    byte[] bytes = new byte[0];
    try {
      bytes = ProtobufIOUtil.toByteArray(object, schema, buffer);
    } finally {
      buffer.clear();
    }
    return bytes;
  }

   byte[] getWrapTypeBytes(Object object, Class clazz) throws Exception {
    WrapperType wrapperType = new WrapperType(object, clazz);
    Schema schema = RuntimeSchema.getSchema(WrapperType.class);
    return toBytes(schema, wrapperType);
  }

   byte[] asBytes(T object) throws Exception {
    return asBytes(object, object.getClass());
  }

  byte[] asBytes(Object object, Class clazz) throws Exception {
    if (isWrapType(clazz)) {
      return getWrapTypeBytes(object, clazz);
    } else {
      Schema schema = RuntimeSchema.getSchema((Class) clazz);
      return toBytes(schema, object);
    }
  }

   T asObject(byte[] bytes, Class clazz) throws IOException {
    Schema schema = RuntimeSchema.getSchema(clazz);
    T object = schema.newMessage();
    ProtobufIOUtil.mergeFrom(bytes, object, schema);
    return object;
  }

  protected enum CallbackType {
    ASYNC_HANDLER, FUTURE, REACTIVE, COMPLETABLE_FUTURE
  }
}