Search in sources :

Example 1 with HazelcastSerializationException

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

the class DefaultSerializationServiceBuilder method buildPortableFactories.

private void buildPortableFactories(Map<Integer, PortableFactory> portableFactories, SerializationConfig config, ClassLoader cl) {
    final Map<Integer, String> portableFactoryClasses = config.getPortableFactoryClasses();
    for (Map.Entry<Integer, String> entry : portableFactoryClasses.entrySet()) {
        int factoryId = entry.getKey();
        String factoryClassName = entry.getValue();
        if (factoryId <= 0) {
            throw new IllegalArgumentException("PortableFactory factoryId must be positive! -> " + factoryClassName);
        }
        if (portableFactories.containsKey(factoryId)) {
            throw new IllegalArgumentException("PortableFactory with factoryId '" + factoryId + "' is already registered!");
        }
        PortableFactory factory;
        try {
            factory = ClassLoaderUtil.newInstance(cl, factoryClassName);
        } catch (Exception e) {
            throw new HazelcastSerializationException(e);
        }
        portableFactories.put(factoryId, factory);
    }
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) HashMap(java.util.HashMap) Map(java.util.Map) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException)

Example 2 with HazelcastSerializationException

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

the class ClassDefinitionWriter method writeNullPortable.

@Override
public void writeNullPortable(String fieldName, int factoryId, int classId) throws IOException {
    final ClassDefinition nestedClassDef = context.lookupClassDefinition(factoryId, classId, context.getVersion());
    if (nestedClassDef == null) {
        throw new HazelcastSerializationException("Cannot write null portable without explicitly " + "registering class definition!");
    }
    builder.addPortableField(fieldName, nestedClassDef);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 3 with HazelcastSerializationException

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

the class ClassDefinitionWriter method writePortableArray.

@Override
public void writePortableArray(String fieldName, Portable[] portables) throws IOException {
    if (portables == null || portables.length == 0) {
        throw new HazelcastSerializationException("Cannot write null portable array without explicitly " + "registering class definition!");
    }
    Portable p = portables[0];
    int classId = p.getClassId();
    for (int i = 1; i < portables.length; i++) {
        if (portables[i].getClassId() != classId) {
            throw new IllegalArgumentException("Detected different class-ids in portable array!");
        }
    }
    int version = SerializationUtil.getPortableVersion(p, context.getVersion());
    ClassDefinition nestedClassDef = createNestedClassDef(p, new ClassDefinitionBuilder(p.getFactoryId(), classId, version));
    builder.addPortableArrayField(fieldName, nestedClassDef);
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 4 with HazelcastSerializationException

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

the class DefaultPortableWriter method setPosition.

private FieldDefinition setPosition(String fieldName, FieldType fieldType) throws IOException {
    if (raw) {
        throw new HazelcastSerializationException("Cannot write Portable fields after getRawDataOutput() is called!");
    }
    FieldDefinition fd = cd.getField(fieldName);
    if (fd == null) {
        throw new HazelcastSerializationException("Invalid field name: '" + fieldName + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}");
    }
    if (writtenFields.add(fieldName)) {
        int pos = out.position();
        int index = fd.getIndex();
        out.writeInt(offset + index * INT_SIZE_IN_BYTES, pos);
        out.writeShort(fieldName.length());
        out.writeBytes(fieldName);
        out.writeByte(fieldType.getId());
    } else {
        throw new HazelcastSerializationException("Field '" + fieldName + "' has already been written!");
    }
    return fd;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition)

Example 5 with HazelcastSerializationException

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

the class PortableUtilsTest method unknownFieldException.

@Test
public void unknownFieldException() {
    // GIVEN
    BufferObjectDataInput in = mock(BufferObjectDataInput.class);
    ClassDefinition cd = mock(ClassDefinition.class);
    PortableNavigatorContext ctx = new PortableNavigatorContext(in, cd, null);
    // WHEN
    HazelcastSerializationException ex = PortableUtils.createUnknownFieldException(ctx, "person.brain");
    // THEN
    assertNotNull(ex);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)50 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)10 FieldDefinition (com.hazelcast.nio.serialization.FieldDefinition)10 IOException (java.io.IOException)9 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 Config (com.hazelcast.config.Config)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)5 ClientConfig (com.hazelcast.client.config.ClientConfig)4 JavaSerializationFilterConfig (com.hazelcast.config.JavaSerializationFilterConfig)4 SerializationUtil.createSerializerAdapter (com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter)4 Portable (com.hazelcast.nio.serialization.Portable)4 TestDeserialized (example.serialization.TestDeserialized)4 Map (java.util.Map)4 HazelcastException (com.hazelcast.core.HazelcastException)3 FieldKind (com.hazelcast.nio.serialization.FieldKind)3 HashMap (java.util.HashMap)3 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)2 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)2