use of com.hazelcast.nio.serialization.IdentifiedDataSerializable in project hazelcast by hazelcast.
the class MultiMapDataSerializerHook method createFactory.
public DataSerializableFactory createFactory() {
ConstructorFunction<Integer, IdentifiedDataSerializable>[] constructors = new ConstructorFunction[ENTRY_SET_RESPONSE + 1];
constructors[CLEAR_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new ClearBackupOperation();
}
};
constructors[CLEAR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new ClearOperation();
}
};
constructors[CONTAINS_ENTRY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new ContainsEntryOperation();
}
};
constructors[COUNT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new CountOperation();
}
};
constructors[ENTRY_SET] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new EntrySetOperation();
}
};
constructors[GET_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new GetAllOperation();
}
};
constructors[KEY_SET] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new KeySetOperation();
}
};
constructors[PUT_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new PutBackupOperation();
}
};
constructors[PUT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new PutOperation();
}
};
constructors[REMOVE_ALL_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new RemoveAllBackupOperation();
}
};
constructors[REMOVE_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new RemoveAllOperation();
}
};
constructors[REMOVE_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new RemoveBackupOperation();
}
};
constructors[REMOVE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new RemoveOperation();
}
};
constructors[SIZE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new SizeOperation();
}
};
constructors[VALUES] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new ValuesOperation();
}
};
constructors[TXN_COMMIT_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnCommitBackupOperation();
}
};
constructors[TXN_COMMIT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnCommitOperation();
}
};
constructors[TXN_GENERATE_RECORD_ID] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnGenerateRecordIdOperation();
}
};
constructors[TXN_LOCK_AND_GET] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnLockAndGetOperation();
}
};
constructors[TXN_PREPARE_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnPrepareBackupOperation();
}
};
constructors[TXN_PREPARE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnPrepareOperation();
}
};
constructors[TXN_PUT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnPutOperation();
}
};
constructors[TXN_PUT_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnPutBackupOperation();
}
};
constructors[TXN_REMOVE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnRemoveOperation();
}
};
constructors[TXN_REMOVE_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnRemoveBackupOperation();
}
};
constructors[TXN_REMOVE_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnRemoveAllOperation();
}
};
constructors[TXN_REMOVE_ALL_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnRemoveAllBackupOperation();
}
};
constructors[TXN_ROLLBACK_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnRollbackBackupOperation();
}
};
constructors[TXN_ROLLBACK] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new TxnRollbackOperation();
}
};
constructors[MULTIMAP_OP_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new MultiMapOperationFactory();
}
};
constructors[MULTIMAP_TRANSACTION_LOG_RECORD] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new MultiMapTransactionLogRecord();
}
};
constructors[MULTIMAP_EVENT_FILTER] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new MultiMapEventFilter();
}
};
constructors[MULTIMAP_RECORD] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new MultiMapRecord();
}
};
constructors[MULTIMAP_MIGRATION_OPERATION] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new MultiMapMigrationOperation();
}
};
constructors[MULTIMAP_RESPONSE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new MultiMapResponse();
}
};
constructors[ENTRY_SET_RESPONSE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new EntrySetResponse();
}
};
return new ArrayDataSerializableFactory(constructors);
}
use of com.hazelcast.nio.serialization.IdentifiedDataSerializable in project hazelcast by hazelcast.
the class PredicateDataSerializerHook method createFactory.
@Override
public DataSerializableFactory createFactory() {
ConstructorFunction<Integer, IdentifiedDataSerializable>[] constructors = new ConstructorFunction[LEN];
constructors[SQL_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new SqlPredicate();
}
};
constructors[AND_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new AndPredicate();
}
};
constructors[BETWEEN_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new BetweenPredicate();
}
};
constructors[EQUAL_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new EqualPredicate();
}
};
constructors[GREATERLESS_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new GreaterLessPredicate();
}
};
constructors[LIKE_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new LikePredicate();
}
};
constructors[ILIKE_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new ILikePredicate();
}
};
constructors[IN_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new InPredicate();
}
};
constructors[INSTANCEOF_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new InstanceOfPredicate();
}
};
constructors[NOTEQUAL_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new NotEqualPredicate();
}
};
constructors[NOT_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new NotPredicate();
}
};
constructors[OR_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new OrPredicate();
}
};
constructors[REGEX_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new RegexPredicate();
}
};
constructors[FALSE_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return FalsePredicate.INSTANCE;
}
};
constructors[TRUE_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return TruePredicate.INSTANCE;
}
};
constructors[PAGING_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new PagingPredicate();
}
};
constructors[PARTITION_PREDICATE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new PartitionPredicate();
}
};
constructors[NULL_OBJECT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {
public IdentifiedDataSerializable createNew(Integer arg) {
return new IndexImpl.NullObject();
}
};
return new ArrayDataSerializableFactory(constructors);
}
use of com.hazelcast.nio.serialization.IdentifiedDataSerializable in project hazelcast by hazelcast.
the class DataSerializableConventionsTest method test_identifiedDataSerializables_haveUniqueFactoryAndTypeId.
/**
* Fails when {@link IdentifiedDataSerializable} classes:
* - do not have a default no-args constructor
* - factoryId/id pairs are not unique per class
*/
@Test
public void test_identifiedDataSerializables_haveUniqueFactoryAndTypeId() throws Exception {
Set<String> classesWithInstantiationProblems = new TreeSet<String>();
Set<String> classesThrowingUnsupportedOperationException = new TreeSet<String>();
Multimap<Integer, Integer> factoryToTypeId = HashMultimap.create();
Set<Class<? extends IdentifiedDataSerializable>> identifiedDataSerializables = getIDSConcreteClasses();
for (Class<? extends IdentifiedDataSerializable> klass : identifiedDataSerializables) {
// exclude classes which are known to be meant for local use only
if (!AbstractLocalOperation.class.isAssignableFrom(klass)) {
// 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.getId();
if (factoryToTypeId.containsEntry(factoryId, typeId)) {
fail("Factory-Type ID pair {" + factoryId + ", " + typeId + "} from " + klass.toString() + " is already" + " registered in another type.");
} else {
factoryToTypeId.put(factoryId, typeId);
}
} catch (UnsupportedOperationException e) {
// expected from local operation classes not meant for serialization
// gather those and print them to system.out for information at end of test
classesThrowingUnsupportedOperationException.add(klass.getName());
} catch (InstantiationException e) {
classesWithInstantiationProblems.add(klass.getName() + " failed with " + e.getMessage());
} catch (NoSuchMethodException e) {
classesWithInstantiationProblems.add(klass.getName() + " failed with " + e.getMessage());
}
}
}
if (!classesThrowingUnsupportedOperationException.isEmpty()) {
System.out.println("INFO: " + classesThrowingUnsupportedOperationException.size() + " classes threw" + " UnsupportedOperationException in getFactoryId/getId invocation:");
for (String className : classesThrowingUnsupportedOperationException) {
System.out.println(className);
}
}
if (!classesWithInstantiationProblems.isEmpty()) {
System.out.println("There are " + classesWithInstantiationProblems.size() + " classes which threw an exception while" + " attempting to invoke a default no-args constructor. See console output for exception details." + " List of problematic classes:");
for (String className : classesWithInstantiationProblems) {
System.out.println(className);
}
fail("There are " + classesWithInstantiationProblems.size() + " classes which threw an exception while" + " attempting to invoke a default no-args constructor. See test output for exception details.");
}
}
use of com.hazelcast.nio.serialization.IdentifiedDataSerializable 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;
}
// 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.getId();
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.IdentifiedDataSerializable in project hazelcast by hazelcast.
the class WanDataSerializerHookTest method testExistingTypes.
@Test
public void testExistingTypes() {
WanDataSerializerHook hook = new WanDataSerializerHook();
IdentifiedDataSerializable wanReplicationEvent = hook.createFactory().create(WanDataSerializerHook.WAN_REPLICATION_EVENT);
assertTrue(wanReplicationEvent instanceof WanReplicationEvent);
IdentifiedDataSerializable mapUpdate = hook.createFactory().create(WanDataSerializerHook.MAP_REPLICATION_UPDATE);
assertTrue(mapUpdate instanceof MapReplicationUpdate);
IdentifiedDataSerializable mapRemove = hook.createFactory().create(WanDataSerializerHook.MAP_REPLICATION_REMOVE);
assertTrue(mapRemove instanceof MapReplicationRemove);
}
Aggregations