com.alipay.remoting.exception.CodecException Java Examples

The following examples show how to use com.alipay.remoting.exception.CodecException. 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: ProtostuffSerializer.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T deserialize(final byte[] data, final String classOfT) throws CodecException {
    Class<?> clazz = classCache.get(classOfT);
    if (clazz == null) {
        try {
            final Class<?> newClazz = Class.forName(classOfT);
            clazz = classCache.putIfAbsent(classOfT, newClazz);
            if (clazz == null) {
                clazz = newClazz;
            }
        } catch (final Exception e) {
            ThrowUtil.throwException(e);
        }
    }
    return (T) this.delegate.readObject(data, clazz);
}
 
Example #2
Source File: ProtocolCodeBasedDecoder.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    in.markReaderIndex();
    ProtocolCode protocolCode = decodeProtocolCode(in);
    if (null != protocolCode) {
        byte protocolVersion = decodeProtocolVersion(in);
        if (ctx.channel().attr(Connection.PROTOCOL).get() == null) {
            ctx.channel().attr(Connection.PROTOCOL).set(protocolCode);
            if (DEFAULT_ILLEGAL_PROTOCOL_VERSION_LENGTH != protocolVersion) {
                ctx.channel().attr(Connection.VERSION).set(protocolVersion);
            }
        }
        Protocol protocol = ProtocolManager.getProtocol(protocolCode);
        if (null != protocol) {
            in.resetReaderIndex();
            protocol.getDecoder().decode(ctx, in, out);
        } else {
            throw new CodecException("Unknown protocol code: [" + protocolCode
                                     + "] while decode in ProtocolDecoder.");
        }
    }
}
 
Example #3
Source File: CounterServiceImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
private void applyOperation(final CounterOperation op, final CounterClosure closure) {
    if (!isLeader()) {
        handlerNotLeaderError(closure);
        return;
    }

    try {
        closure.setCounterOperation(op);
        final Task task = new Task();
        task.setData(ByteBuffer.wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(op)));
        task.setDone(closure);
        this.counterServer.getNode().apply(task);
    } catch (CodecException e) {
        String errorMsg = "Fail to encode CounterOperation";
        LOG.error(errorMsg, e);
        closure.failure(errorMsg, StringUtils.EMPTY);
        closure.run(new Status(RaftError.EINTERNAL, errorMsg));
    }
}
 
Example #4
Source File: CommandCodec.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Decode the command object from byte array.
 * @param content
 * @param clazz
 * @return
 */
public static <T> T decodeCommand(byte[] content, Class<T> clazz) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(content, clazz.getName());
    } catch (final CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #5
Source File: CommandCodec.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * encode the command,returns the byte array.
 * @param obj
 * @return
 */
public static byte[] encodeCommand(Object obj) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(obj);
    } catch (final CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #6
Source File: CommandCodec.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * encode the command,returns the byte array.
 * @param obj
 * @return
 */
public static byte[] encodeCommand(Object obj) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(obj);
    } catch (CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #7
Source File: CommandCodec.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Decode the command object from byte array.
 * @param content
 * @param clazz
 * @return
 */
public static <T> T decodeCommand(byte[] content, Class<T> clazz) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(content,
            clazz.getName());
    } catch (CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #8
Source File: DmetaSnapshotFile.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Map<String, String> load() throws IOException, CodecException {
  final String s = FileUtils.readFileToString(new File(path));
  JSONObject jsonObject = JSONObject.parseObject(s);
  Set<String> set = jsonObject.keySet();
  Map<String, String> map = new TreeMap<>();
  for (String key : set) {
    map.put(key,(String) jsonObject.get(key));
  }
  return map;
}
 
Example #9
Source File: DBRequestProcessor.java    From KitDB with Apache License 2.0 5 votes vote down vote up
public void handle(DBCommandChunk dbCommandChunk) throws CodecException, InterruptedException, KitDBException {
    final DBClosure closure = new DBClosure();
    closure.setChunk(dbCommandChunk);
    final Task task = new Task();
    task.setDone(closure);
    task.setData(ByteBuffer
            .wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(dbCommandChunk)));
    kitRaft.getNode().apply(task);
    synchronized (closure) {
        closure.wait();
    }
    if (closure.getCode() != 0) {
        throw new KitDBException(ErrorType.STROE_ERROR, closure.getMsg());
    }
}
 
Example #10
Source File: DBRequestProcessor.java    From KitDB with Apache License 2.0 5 votes vote down vote up
@Override
public void call(DBCommandChunk dbCommandChunk) throws KitDBException {
    try {
        handle(dbCommandChunk);
    } catch (CodecException | InterruptedException e) {
        throw new KitDBException(ErrorType.STROE_ERROR, e);
    }
}
 
Example #11
Source File: HessianSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/** 
 * @see com.alipay.remoting.serialization.Serializer#serialize(java.lang.Object)
 */
@Override
public byte[] serialize(Object obj) throws CodecException {
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    Hessian2Output output = new Hessian2Output(byteArray);
    output.setSerializerFactory(serializerFactory);
    try {
        output.writeObject(obj);
        output.close();
    } catch (IOException e) {
        throw new CodecException("IOException occurred when Hessian serializer encode!", e);
    }

    return byteArray.toByteArray();
}
 
Example #12
Source File: HessianSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @see com.alipay.remoting.serialization.Serializer#deserialize(byte[], java.lang.String)
 */
@SuppressWarnings("unchecked")
@Override
public <T> T deserialize(byte[] data, String classOfT) throws CodecException {
    Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(data));
    input.setSerializerFactory(serializerFactory);
    Object resultObject;
    try {
        resultObject = input.readObject();
        input.close();
    } catch (IOException e) {
        throw new CodecException("IOException occurred when Hessian serializer decode!", e);
    }
    return (T) resultObject;
}
 
Example #13
Source File: RpcResponseResolver.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * Convert remoting response command to throwable if it is a throwable, otherwise return null.
 */
private static Throwable toThrowable(ResponseCommand responseCommand) throws CodecException {
    RpcResponseCommand resp = (RpcResponseCommand) responseCommand;
    resp.deserialize();
    Object ex = resp.getResponseObject();
    if (ex instanceof Throwable) {
        return (Throwable) ex;
    }
    return null;
}
 
Example #14
Source File: CounterStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        long current = 0;
        CounterOperation counterOperation = null;

        CounterClosure closure = null;
        if (iter.done() != null) {
            // This task is applied by this node, get value from closure to avoid additional parsing.
            closure = (CounterClosure) iter.done();
            counterOperation = closure.getCounterOperation();
        } else {
            // Have to parse FetchAddRequest from this user log.
            final ByteBuffer data = iter.getData();
            try {
                counterOperation = SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(
                    data.array(), CounterOperation.class.getName());
            } catch (final CodecException e) {
                LOG.error("Fail to decode IncrementAndGetRequest", e);
            }
        }
        if (counterOperation != null) {
            switch (counterOperation.getOp()) {
                case GET:
                    current = this.value.get();
                    LOG.info("Get value={} at logIndex={}", current, iter.getIndex());
                    break;
                case INCREMENT:
                    final long delta = counterOperation.getDelta();
                    final long prev = this.value.get();
                    current = this.value.addAndGet(delta);
                    LOG.info("Added value={} by delta={} at logIndex={}", prev, delta, iter.getIndex());
                    break;
            }

            if (closure != null) {
                closure.success(current);
                closure.run(Status.OK());
            }
        }
        iter.next();
    }
}
 
Example #15
Source File: RpcResponseResolver.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
/**
 * Convert remoting response command to application response object.
 */
private static Object toResponseObject(ResponseCommand responseCommand) throws CodecException {
    RpcResponseCommand response = (RpcResponseCommand) responseCommand;
    response.deserialize();
    return response.getResponseObject();
}
 
Example #16
Source File: ProtostuffSerializer.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] serialize(final Object obj) throws CodecException {
    return this.delegate.writeObject(obj);
}
 
Example #17
Source File: Serializer.java    From sofa-bolt with Apache License 2.0 2 votes vote down vote up
/**
 * Encode object into bytes.
 * 
 * @param obj target object
 * @return serialized result
 */
byte[] serialize(final Object obj) throws CodecException;
 
Example #18
Source File: Serializer.java    From sofa-bolt with Apache License 2.0 2 votes vote down vote up
/**
 * Decode bytes into Object.
 * 
 * @param data serialized data
 * @param classOfT class of original data
 */
<T> T deserialize(final byte[] data, String classOfT) throws CodecException;