com.arangodb.shaded.vertx.core.eventbus.impl.codecs.ClusterSerializableCodec Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package com.arangodb.shaded.vertx.core.eventbus.impl.codecs;
import com.arangodb.shaded.netty.util.CharsetUtil;
import com.arangodb.shaded.vertx.core.buffer.Buffer;
import com.arangodb.shaded.vertx.core.eventbus.MessageCodec;
import com.arangodb.shaded.vertx.core.eventbus.impl.CodecManager;
import com.arangodb.shaded.vertx.core.shareddata.impl.ClusterSerializable;
import static com.arangodb.shaded.vertx.core.impl.ClusterSerializableUtils.copy;
public class ClusterSerializableCodec implements MessageCodec {
private final CodecManager codecManager;
public ClusterSerializableCodec(CodecManager codecManager) {
this.codecManager = codecManager;
}
@Override
public void encodeToWire(Buffer buffer, ClusterSerializable obj) {
byte[] classNameBytes = obj.getClass().getName().getBytes(CharsetUtil.UTF_8);
buffer.appendInt(classNameBytes.length).appendBytes(classNameBytes);
obj.writeToBuffer(buffer);
}
@Override
public ClusterSerializable decodeFromWire(int pos, Buffer buffer) {
int len = buffer.getInt(pos);
pos += 4;
byte[] classNameBytes = buffer.getBytes(pos, pos + len);
String className = new String(classNameBytes, CharsetUtil.UTF_8);
if (!codecManager.acceptClusterSerializable(className)) {
throw new RuntimeException("Class not allowed: " + className);
}
pos += len;
ClusterSerializable clusterSerializable;
try {
Class clazz = getClassLoader().loadClass(className);
clusterSerializable = (ClusterSerializable) clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
clusterSerializable.readFromBuffer(pos, buffer);
return clusterSerializable;
}
private static ClassLoader getClassLoader() {
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
return tccl != null ? tccl : ClusterSerializableCodec.class.getClassLoader();
}
@Override
public ClusterSerializable transform(ClusterSerializable obj) {
return copy(obj);
}
@Override
public String name() {
return "clusterserializable";
}
@Override
public byte systemCodecID() {
return 16;
}
}