use of com.hazelcast.nio.serialization.DataSerializableFactory in project hazelcast by hazelcast.
the class DefaultSerializationServiceBuilder method addConfigDataSerializableFactories.
private void addConfigDataSerializableFactories(Map<Integer, DataSerializableFactory> dataSerializableFactories, SerializationConfig config, ClassLoader cl) {
registerDataSerializableFactories(dataSerializableFactories, config);
buildDataSerializableFactories(dataSerializableFactories, config, cl);
for (DataSerializableFactory f : dataSerializableFactories.values()) {
if (f instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) f).setHazelcastInstance(hazelcastInstance);
}
}
}
use of com.hazelcast.nio.serialization.DataSerializableFactory in project hazelcast by hazelcast.
the class TestFullApplicationContext method testSerializationConfig.
@Test
public void testSerializationConfig() {
SerializationConfig serializationConfig = config.getSerializationConfig();
assertTrue(serializationConfig.isAllowOverrideDefaultSerializers());
assertEquals(ByteOrder.BIG_ENDIAN, serializationConfig.getByteOrder());
assertFalse(serializationConfig.isCheckClassDefErrors());
assertEquals(13, serializationConfig.getPortableVersion());
Map<Integer, String> dataSerializableFactoryClasses = serializationConfig.getDataSerializableFactoryClasses();
assertFalse(dataSerializableFactoryClasses.isEmpty());
assertEquals(DummyDataSerializableFactory.class.getName(), dataSerializableFactoryClasses.get(1));
Map<Integer, DataSerializableFactory> dataSerializableFactories = serializationConfig.getDataSerializableFactories();
assertFalse(dataSerializableFactories.isEmpty());
assertEquals(DummyDataSerializableFactory.class, dataSerializableFactories.get(2).getClass());
Map<Integer, String> portableFactoryClasses = serializationConfig.getPortableFactoryClasses();
assertFalse(portableFactoryClasses.isEmpty());
assertEquals(DummyPortableFactory.class.getName(), portableFactoryClasses.get(1));
Map<Integer, PortableFactory> portableFactories = serializationConfig.getPortableFactories();
assertFalse(portableFactories.isEmpty());
assertEquals(DummyPortableFactory.class, portableFactories.get(2).getClass());
Collection<SerializerConfig> serializerConfigs = serializationConfig.getSerializerConfigs();
assertFalse(serializerConfigs.isEmpty());
GlobalSerializerConfig globalSerializerConfig = serializationConfig.getGlobalSerializerConfig();
assertNotNull(globalSerializerConfig);
assertEquals(dummySerializer, globalSerializerConfig.getImplementation());
}
use of com.hazelcast.nio.serialization.DataSerializableFactory 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);
}
}
use of com.hazelcast.nio.serialization.DataSerializableFactory in project hazelcast by hazelcast.
the class DataSerializableConventionsTest method test_identifiedDataSerializables_areInstancesOfSameClass_whenConstructedFromFactory.
/**
* Locates {@link IdentifiedDataSerializable} classes via reflection, iterates over them and asserts an instance created by
* a factory is of the same classes as an instance created via reflection.
*/
@Test
public void test_identifiedDataSerializables_areInstancesOfSameClass_whenConstructedFromFactory() throws Exception {
Set<Class<? extends DataSerializerHook>> dsHooks = REFLECTIONS.getSubTypesOf(DataSerializerHook.class);
Map<Integer, DataSerializableFactory> factories = new HashMap<Integer, DataSerializableFactory>();
for (Class<? extends DataSerializerHook> hookClass : dsHooks) {
DataSerializerHook dsHook = hookClass.newInstance();
DataSerializableFactory factory = dsHook.createFactory();
factories.put(dsHook.getFactoryId(), factory);
}
Set<Class<? extends IdentifiedDataSerializable>> identifiedDataSerializables = getIDSConcreteClasses();
for (Class<? extends IdentifiedDataSerializable> klass : identifiedDataSerializables) {
if (AbstractLocalOperation.class.isAssignableFrom(klass)) {
continue;
}
if (isReadOnlyConfig(klass)) {
continue;
}
// wrap all of this in try-catch, as it is legitimate for some classes to throw UnsupportedOperationException
try {
Constructor<? extends IdentifiedDataSerializable> ctor = klass.getDeclaredConstructor();
ctor.setAccessible(true);
IdentifiedDataSerializable instance = ctor.newInstance();
int factoryId = instance.getFactoryId();
int typeId = instance.getClassId();
if (!factories.containsKey(factoryId)) {
fail("Factory with ID " + factoryId + " declared in " + klass + " not found." + " Is such a factory ID registered?");
}
IdentifiedDataSerializable instanceFromFactory = factories.get(factoryId).create(typeId);
assertNotNull("Factory with ID " + factoryId + " returned null for type with ID " + typeId, instanceFromFactory);
assertTrue("Factory with ID " + factoryId + " instantiated an object of " + instanceFromFactory.getClass() + " while expected type was " + instance.getClass(), instanceFromFactory.getClass().equals(instance.getClass()));
} catch (UnsupportedOperationException ignored) {
// expected from local operation classes not meant for serialization
}
}
}
use of com.hazelcast.nio.serialization.DataSerializableFactory in project hazelcast by hazelcast.
the class ReplicatedMapReorderedReplicationTest method updateFactory.
private void updateFactory() throws Exception {
// Get Field to manipulate and save it's old value to replicatedMapDataSerializableFactory
field = ReplicatedMapDataSerializerHook.class.getDeclaredField("FACTORY");
field.setAccessible(true);
final DataSerializableFactory factory = (DataSerializableFactory) field.get(null);
replicatedMapDataSerializableFactory = factory;
updateFactoryField(new TestReplicatedMapDataSerializerFactory(factory));
}
Aggregations