Search in sources :

Example 1 with JsonMapper

use of rpc.turbo.serialization.JsonMapper in project turbo-rpc by hank-whu.

the class ServerConfig method parse.

private static ServerConfig parse(Config config) {
    String group = getStringOrElse(config, "group", TurboService.DEFAULT_GROUP);
    String app = getStringOrElse(config, "app", TurboService.DEFAULT_GROUP);
    String ownerName = getStringOrElse(config, "owner.name", "");
    String ownerPhone = getStringOrElse(config, "owner.phone", "");
    String serializerClass = getStringOrElse(config, "serializer.class", ProtostuffSerializer.class.getName());
    Serializer serializer;
    try {
        serializer = (Serializer) // 
        Class.forName(// 
        serializerClass).getDeclaredConstructor().newInstance();
    } catch (Throwable t) {
        throw new ConfigException(t);
    }
    String jsonMapperClass = getStringOrElse(config, "jsonMapper.class", JacksonMapper.class.getName());
    JsonMapper jsonMapper;
    try {
        jsonMapper = (JsonMapper) // 
        Class.forName(// 
        jsonMapperClass).getDeclaredConstructor().newInstance();
    } catch (Throwable t) {
        throw new ConfigException(t);
    }
    List<? extends Config> registerConfigList = config.getConfigList("registers");
    List<RegisterConfig> registers = // 
    registerConfigList.stream().map(registerConfig -> {
        try {
            return RegisterConfig.parse(registerConfig);
        } catch (Throwable e) {
            throw new ConfigException(e);
        }
    }).collect(Collectors.toList());
    Map<Integer, RegisterConfig> portMap = new HashMap<>();
    for (RegisterConfig registerConfig : registers) {
        HostPort serverAddress = registerConfig.getServerAddress();
        RegisterConfig existed = portMap.get(serverAddress.port);
        if (existed == null) {
            portMap.put(serverAddress.port, registerConfig);
            continue;
        }
        if (existed.getProtocol() == registerConfig.getProtocol() && existed.getServerAddress().equals(serverAddress)) {
            continue;
        }
        throw new ConfigException("存在端口冲突: " + registerConfig + " vs " + existed);
    }
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setGroup(group);
    serverConfig.setApp(app);
    serverConfig.setOwnerName(ownerName);
    serverConfig.setOwnerPhone(ownerPhone);
    serverConfig.setSerializer(serializer);
    serverConfig.setJsonMapper(jsonMapper);
    serverConfig.setRegisters(registers);
    return serverConfig;
}
Also used : Serializer(rpc.turbo.serialization.Serializer) Config(com.typesafe.config.Config) TurboService(rpc.turbo.annotation.TurboService) ConfigUtils.getStringOrElse(rpc.turbo.config.ConfigUtils.getStringOrElse) HashMap(java.util.HashMap) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer) ConfigException(rpc.turbo.config.ConfigException) Collectors(java.util.stream.Collectors) JacksonMapper(rpc.turbo.serialization.jackson.JacksonMapper) List(java.util.List) HostPort(rpc.turbo.config.HostPort) JsonMapper(rpc.turbo.serialization.JsonMapper) Map(java.util.Map) ConfigFactory(com.typesafe.config.ConfigFactory) JacksonMapper(rpc.turbo.serialization.jackson.JacksonMapper) HashMap(java.util.HashMap) HostPort(rpc.turbo.config.HostPort) ConfigException(rpc.turbo.config.ConfigException) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer) JsonMapper(rpc.turbo.serialization.JsonMapper) Serializer(rpc.turbo.serialization.Serializer) ProtostuffSerializer(rpc.turbo.serialization.protostuff.ProtostuffSerializer)

Aggregations

Config (com.typesafe.config.Config)1 ConfigFactory (com.typesafe.config.ConfigFactory)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 TurboService (rpc.turbo.annotation.TurboService)1 ConfigException (rpc.turbo.config.ConfigException)1 ConfigUtils.getStringOrElse (rpc.turbo.config.ConfigUtils.getStringOrElse)1 HostPort (rpc.turbo.config.HostPort)1 JsonMapper (rpc.turbo.serialization.JsonMapper)1 Serializer (rpc.turbo.serialization.Serializer)1 JacksonMapper (rpc.turbo.serialization.jackson.JacksonMapper)1 ProtostuffSerializer (rpc.turbo.serialization.protostuff.ProtostuffSerializer)1