Search in sources :

Example 6 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class ClassDefinitionWriter method writePortable.

@Override
public void writePortable(String fieldName, Portable portable) throws IOException {
    if (portable == null) {
        throw new HazelcastSerializationException("Cannot write null portable without explicitly " + "registering class definition!");
    }
    int version = SerializationUtil.getPortableVersion(portable, context.getVersion());
    ClassDefinition nestedClassDef = createNestedClassDef(portable, new ClassDefinitionBuilder(portable.getFactoryId(), portable.getClassId(), version));
    builder.addPortableField(fieldName, nestedClassDef);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 7 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class PortableContextImpl method readClassDefinition.

ClassDefinition readClassDefinition(BufferObjectDataInput in, int factoryId, int classId, int version) throws IOException {
    boolean register = true;
    ClassDefinitionBuilder builder = new ClassDefinitionBuilder(factoryId, classId, version);
    // final position after portable is read
    in.readInt();
    // field count
    int fieldCount = in.readInt();
    int offset = in.position();
    for (int i = 0; i < fieldCount; i++) {
        int pos = in.readInt(offset + i * Bits.INT_SIZE_IN_BYTES);
        in.position(pos);
        short len = in.readShort();
        char[] chars = new char[len];
        for (int k = 0; k < len; k++) {
            chars[k] = (char) in.readUnsignedByte();
        }
        FieldType type = FieldType.get(in.readByte());
        String name = new String(chars);
        int fieldFactoryId = 0;
        int fieldClassId = 0;
        int fieldVersion = 0;
        if (type == FieldType.PORTABLE) {
            // is null
            if (in.readBoolean()) {
                register = false;
            }
            fieldFactoryId = in.readInt();
            fieldClassId = in.readInt();
            // TODO: what there's a null inner Portable field
            if (register) {
                fieldVersion = in.readInt();
                readClassDefinition(in, fieldFactoryId, fieldClassId, fieldVersion);
            }
        } else if (type == FieldType.PORTABLE_ARRAY) {
            int k = in.readInt();
            fieldFactoryId = in.readInt();
            fieldClassId = in.readInt();
            // TODO: what there's a null inner Portable field
            if (k > 0) {
                int p = in.readInt();
                in.position(p);
                fieldVersion = in.readInt();
                readClassDefinition(in, fieldFactoryId, fieldClassId, fieldVersion);
            } else {
                register = false;
            }
        }
        builder.addField(new FieldDefinitionImpl(i, name, type, fieldFactoryId, fieldClassId, fieldVersion));
    }
    ClassDefinition classDefinition = builder.build();
    if (register) {
        classDefinition = registerClassDefinition(classDefinition);
    }
    return classDefinition;
}
Also used : ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) FieldType(com.hazelcast.nio.serialization.FieldType)

Example 8 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class BinaryCompatibilityFileGenerator method createSerializationService.

private static SerializationService createSerializationService(ByteOrder byteOrder) {
    SerializationConfig config = new SerializationConfig();
    {
        SerializerConfig serializerConfig = new SerializerConfig();
        serializerConfig.setImplementation(new CustomByteArraySerializer()).setTypeClass(CustomByteArraySerializable.class);
        config.addSerializerConfig(serializerConfig);
    }
    {
        SerializerConfig serializerConfig = new SerializerConfig();
        serializerConfig.setImplementation(new CustomStreamSerializer()).setTypeClass(CustomStreamSerializable.class);
        config.addSerializerConfig(serializerConfig);
    }
    config.setByteOrder(byteOrder);
    ClassDefinition classDefinition = new ClassDefinitionBuilder(ReferenceObjects.PORTABLE_FACTORY_ID, ReferenceObjects.INNER_PORTABLE_CLASS_ID).addIntField("i").addFloatField("f").build();
    return new DefaultSerializationServiceBuilder().setConfig(config).setVersion(VERSION).addPortableFactory(ReferenceObjects.PORTABLE_FACTORY_ID, new APortableFactory()).addDataSerializableFactory(ReferenceObjects.IDENTIFIED_DATA_SERIALIZABLE_FACTORY_ID, new ADataSerializableFactory()).addClassDefinition(classDefinition).build();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Aggregations

ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)8 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)6 SerializationConfig (com.hazelcast.config.SerializationConfig)2 SerializerConfig (com.hazelcast.config.SerializerConfig)2 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)2 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 Portable (com.hazelcast.nio.serialization.Portable)2 FieldType (com.hazelcast.nio.serialization.FieldType)1 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 Before (org.junit.Before)1 Test (org.junit.Test)1