Search in sources :

Example 1 with Serializer

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

the class DefaultSerializationServiceBuilder method build.

@Override
public InternalSerializationService build() {
    initVersions();
    if (config != null) {
        addConfigDataSerializableFactories(dataSerializableFactories, config, classLoader);
        addConfigPortableFactories(portableFactories, config, classLoader);
        classDefinitions.addAll(config.getClassDefinitions());
    }
    InputOutputFactory inputOutputFactory = createInputOutputFactory();
    InternalSerializationService ss = createSerializationService(inputOutputFactory);
    registerSerializerHooks(ss);
    if (config != null) {
        if (config.getGlobalSerializerConfig() != null) {
            GlobalSerializerConfig globalSerializerConfig = config.getGlobalSerializerConfig();
            Serializer serializer = globalSerializerConfig.getImplementation();
            if (serializer == null) {
                try {
                    serializer = ClassLoaderUtil.newInstance(classLoader, globalSerializerConfig.getClassName());
                } catch (Exception e) {
                    throw new HazelcastSerializationException(e);
                }
            }
            if (serializer instanceof HazelcastInstanceAware) {
                ((HazelcastInstanceAware) serializer).setHazelcastInstance(hazelcastInstance);
            }
            ((AbstractSerializationService) ss).registerGlobal(serializer, globalSerializerConfig.isOverrideJavaSerialization());
        }
    }
    return ss;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) InputOutputFactory(com.hazelcast.internal.serialization.InputOutputFactory) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) Serializer(com.hazelcast.nio.serialization.Serializer)

Example 2 with Serializer

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

the class SerializerHookLoader method load.

private void load() {
    try {
        final Iterator<SerializerHook> hooks = ServiceLoader.iterator(SerializerHook.class, FACTORY_ID, classLoader);
        while (hooks.hasNext()) {
            final SerializerHook hook = hooks.next();
            final Class serializationType = hook.getSerializationType();
            if (serializationType != null) {
                serializers.put(serializationType, hook);
            }
        }
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
    if (serializerConfigs != null) {
        for (SerializerConfig serializerConfig : serializerConfigs) {
            Serializer serializer = serializerConfig.getImplementation();
            Class serializationType = serializerConfig.getTypeClass();
            if (serializationType == null) {
                try {
                    serializationType = ClassLoaderUtil.loadClass(classLoader, serializerConfig.getTypeClassName());
                } catch (ClassNotFoundException e) {
                    throw new HazelcastSerializationException(e);
                }
            }
            if (serializer == null) {
                serializer = createSerializerInstance(serializerConfig, serializationType);
            }
            register(serializationType, serializer);
        }
    }
}
Also used : SerializerConfig(com.hazelcast.config.SerializerConfig) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) SerializerHook(com.hazelcast.nio.serialization.SerializerHook) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) Serializer(com.hazelcast.nio.serialization.Serializer)

Example 3 with Serializer

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

the class StreamSerializerAdapterTest method testAdaptor.

@Test
public void testAdaptor() throws Exception {
    int[] testIn = new int[] { 1, 2, 3 };
    ByteArrayObjectDataOutput out = new ByteArrayObjectDataOutput(100, mockSerializationService, ByteOrder.BIG_ENDIAN);
    ByteArrayObjectDataInput in = new ByteArrayObjectDataInput(out.buffer, mockSerializationService, ByteOrder.BIG_ENDIAN);
    adapter.write(out, testIn);
    int[] read = (int[]) adapter.read(in);
    Serializer impl = adapter.getImpl();
    assertArrayEquals(testIn, read);
    assertEquals(serializer, impl);
}
Also used : Serializer(com.hazelcast.nio.serialization.Serializer) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with Serializer

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

the class DefaultSerializationServiceBuilder method registerSerializerHooks.

private void registerSerializerHooks(InternalSerializationService ss) {
    SerializerHookLoader serializerHookLoader = new SerializerHookLoader(config, classLoader);
    Map<Class, Object> serializers = serializerHookLoader.getSerializers();
    for (Map.Entry<Class, Object> entry : serializers.entrySet()) {
        Class serializationType = entry.getKey();
        Object value = entry.getValue();
        Serializer serializer;
        if (value instanceof SerializerHook) {
            serializer = ((SerializerHook) value).createSerializer();
        } else {
            serializer = (Serializer) value;
        }
        if (value instanceof HazelcastInstanceAware) {
            ((HazelcastInstanceAware) value).setHazelcastInstance(hazelcastInstance);
        }
        if (ClassLoaderUtil.isInternalType(value.getClass())) {
            ((AbstractSerializationService) ss).safeRegister(serializationType, serializer);
        } else {
            ((AbstractSerializationService) ss).register(serializationType, serializer);
        }
    }
}
Also used : HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) HashMap(java.util.HashMap) Map(java.util.Map) SerializerHook(com.hazelcast.nio.serialization.SerializerHook) Serializer(com.hazelcast.nio.serialization.Serializer)

Example 5 with Serializer

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

the class ByteArraySerializerAdapterTest method testAdaptor.

@Test
public void testAdaptor() throws Exception {
    byte[] testByteArray = new byte[] { 1, 2, 3 };
    ByteArrayObjectDataOutput out = new ByteArrayObjectDataOutput(10, mockSerializationService, ByteOrder.BIG_ENDIAN);
    ByteArrayObjectDataInput in = new ByteArrayObjectDataInput(out.buffer, mockSerializationService, ByteOrder.BIG_ENDIAN);
    adapter.write(out, testByteArray);
    byte[] read = (byte[]) adapter.read(in);
    Serializer impl = adapter.getImpl();
    assertArrayEquals(testByteArray, read);
    assertEquals(serializer, impl);
    assertEquals(serializer.getTypeId(), adapter.getTypeId());
}
Also used : Serializer(com.hazelcast.nio.serialization.Serializer) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

Serializer (com.hazelcast.nio.serialization.Serializer)5 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)2 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 SerializerHook (com.hazelcast.nio.serialization.SerializerHook)2 ParallelTest (com.hazelcast.test.annotation.ParallelTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 Test (org.junit.Test)2 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)1 SerializerConfig (com.hazelcast.config.SerializerConfig)1 InputOutputFactory (com.hazelcast.internal.serialization.InputOutputFactory)1 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1