Search in sources :

Example 36 with HazelcastSerializationException

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

the class DelegatingSerializationService method serializerFor.

@Override
public SerializerAdapter serializerFor(Object object, boolean includeSchema) {
    Class<?> clazz = object == null ? null : object.getClass();
    SerializerAdapter serializer = null;
    if (clazz != null) {
        serializer = serializersByClass.get(clazz);
    }
    if (serializer == null) {
        try {
            serializer = delegate.serializerFor(object, includeSchema);
        } catch (HazelcastSerializationException hse) {
            throw serializationException(clazz, hse);
        }
    }
    if (serializer == null) {
        throw active ? serializationException(clazz) : new HazelcastInstanceNotActiveException();
    }
    return serializer;
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) SerializerAdapter(com.hazelcast.internal.serialization.impl.SerializerAdapter)

Example 37 with HazelcastSerializationException

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

the class AbstractSerializationService method serializerFor.

@SuppressWarnings("checkstyle:npathcomplexity")
public SerializerAdapter serializerFor(final Object object, boolean includeSchema) {
    if (!active) {
        throw notActiveExceptionSupplier.get();
    }
    // 1-NULL serializer
    if (object == null) {
        return nullSerializerAdapter;
    }
    final Class type = object.getClass();
    // 2-Default serializers, Dataserializable, Compact, Portable, primitives, arrays, String and
    // some helper Java types(BigInteger etc)
    SerializerAdapter serializer = lookupDefaultSerializer(type, includeSchema);
    // 3-Custom registered types by user
    if (serializer == null || allowOverrideDefaultSerializers) {
        SerializerAdapter customSerializer = lookupCustomSerializer(type);
        if (customSerializer != null) {
            serializer = customSerializer;
        }
    }
    // 4-JDK serialization ( Serializable and Externalizable )
    if (serializer == null && !overrideJavaSerialization) {
        serializer = lookupJavaSerializer(type);
    }
    // 5-Global serializer if registered by user
    if (serializer == null) {
        serializer = lookupGlobalSerializer(type);
    }
    // 6-Compact serializer
    if (serializer == null && compactStreamSerializer.isEnabled()) {
        serializer = getCompactSerializer(includeSchema);
    }
    if (serializer == null) {
        throw new HazelcastSerializationException("There is no suitable serializer for " + type);
    }
    return serializer;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) CompactWithSchemaStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactWithSchemaStreamSerializerAdapter) CompactStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactStreamSerializerAdapter)

Example 38 with HazelcastSerializationException

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

the class ByteArraySerializerAdapter method read.

@Override
public Object read(ObjectDataInput in, Class aClass) throws IOException {
    byte[] bytes = in.readByteArray();
    if (bytes == null) {
        return null;
    }
    if (!(serializer instanceof TypedByteArrayDeserializer)) {
        throw new HazelcastSerializationException(serializer + " is not implementing the " + TypedByteArrayDeserializer.class + " interface. Please implement this interface to deserialize for class " + aClass);
    }
    TypedByteArrayDeserializer deserializer = (TypedByteArrayDeserializer) serializer;
    return deserializer.read(bytes, aClass);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) TypedByteArrayDeserializer(com.hazelcast.nio.serialization.TypedByteArrayDeserializer)

Example 39 with HazelcastSerializationException

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

the class DataSerializableSerializer method readInternal.

private DataSerializable readInternal(ObjectDataInput in, Class aClass) throws IOException {
    setInputVersion(in, version);
    DataSerializable ds = null;
    if (null != aClass) {
        try {
            ds = (DataSerializable) aClass.newInstance();
        } catch (Exception e) {
            e = tryClarifyInstantiationException(aClass, e);
            throw new HazelcastSerializationException("Requested class " + aClass + " could not be instantiated.", e);
        }
    }
    final byte header = in.readByte();
    int id = 0;
    int factoryId = 0;
    String className = null;
    try {
        // BasicOperationService::extractOperationCallId
        if (isFlagSet(header, IDS_FLAG)) {
            factoryId = in.readInt();
            final DataSerializableFactory dsf = factories.get(factoryId);
            if (dsf == null) {
                throw new HazelcastSerializationException("No DataSerializerFactory registered for namespace: " + factoryId);
            }
            id = in.readInt();
            if (null == aClass) {
                ds = dsf.create(id);
                if (ds == null) {
                    throw new HazelcastSerializationException(dsf + " is not be able to create an instance for ID: " + id + " on factory ID: " + factoryId);
                }
            }
        } else {
            className = in.readString();
            if (null == aClass) {
                ds = ClassLoaderUtil.newInstance(in.getClassLoader(), className);
            }
        }
        if (isFlagSet(header, EE_FLAG)) {
            in.readByte();
            in.readByte();
        }
        ds.readData(in);
        return ds;
    } catch (Exception e) {
        e = tryClarifyNoSuchMethodException(in.getClassLoader(), className, e);
        throw rethrowReadException(id, factoryId, className, e);
    }
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) DataSerializableFactory(com.hazelcast.nio.serialization.DataSerializableFactory) IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) TypedDataSerializable(com.hazelcast.nio.serialization.TypedDataSerializable) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) IOException(java.io.IOException)

Example 40 with HazelcastSerializationException

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

the class SerializationServiceV1 method registerClassDefinition.

private void registerClassDefinition(ClassDefinition cd, Map<Integer, Map<Integer, ClassDefinition>> factoryMap) {
    Set<String> fieldNames = cd.getFieldNames();
    for (String fieldName : fieldNames) {
        FieldDefinition fd = cd.getField(fieldName);
        if (fd.getType() == FieldType.PORTABLE || fd.getType() == FieldType.PORTABLE_ARRAY) {
            int factoryId = fd.getFactoryId();
            int classId = fd.getClassId();
            Map<Integer, ClassDefinition> classDefinitionMap = factoryMap.get(factoryId);
            if (classDefinitionMap != null) {
                ClassDefinition nestedCd = classDefinitionMap.get(classId);
                if (nestedCd != null) {
                    registerClassDefinition(nestedCd, factoryMap);
                    portableContext.registerClassDefinition(nestedCd);
                    continue;
                }
            }
            if (portableContext.shouldCheckClassDefinitionErrors()) {
                throw new HazelcastSerializationException("Could not find registered ClassDefinition for factory-id : " + factoryId + ", class-id " + classId);
            }
        }
    }
    portableContext.registerClassDefinition(cd);
}
Also used : BigInteger(java.math.BigInteger) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

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