/** * Sets class names of binary objects explicitly. * * @param clsNames Class names. * @return {@code this} for chaining. */ public BinaryConfiguration setClassNames(Collection<String> clsNames) { if (typeCfgs == null) typeCfgs = new ArrayList<>(clsNames.size()); for (String clsName : clsNames) typeCfgs.add(new BinaryTypeConfiguration(clsName)); return this; }
c.setCompactFooter(compactFooter); BinaryTypeConfiguration btc = new BinaryTypeConfiguration("org.MyClass"); btc.setIdMapper(BinaryContext.defaultIdMapper()); btc.setEnum(false); btc.setSerializer(new BinarySerializer() { @Override public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); cfg.setMarshaller(new BinaryMarshaller()); BinaryConfiguration binCfg = new BinaryConfiguration(); BinaryTypeConfiguration binTypCfg1 = new BinaryTypeConfiguration(); BinaryTypeConfiguration binTypCfg2 = new BinaryTypeConfiguration(); binTypCfg1.setTypeName(InnerClass.class.getName()); binTypCfg2.setTypeName(InnerClassBinarylizable.class.getName()); List<BinaryTypeConfiguration> binTypCfgs = new ArrayList<>(); binTypCfgs.add(binTypCfg1); binTypCfgs.add(binTypCfg2); binCfg.setTypeConfigurations(binTypCfgs); cfg.setBinaryConfiguration(binCfg); return cfg; }
/** * Create data transfer object for binary type configuration. * * @param src Binary type configuration. */ public VisorBinaryTypeConfiguration(BinaryTypeConfiguration src) { typeName = src.getTypeName(); idMapper = compactClass(src.getIdMapper()); nameMapper = compactClass(src.getNameMapper()); serializer = compactClass(src.getSerializer()); isEnum = src.isEnum(); }
String typeCfgName = typeCfg.getTypeName(); if (typeCfg.getIdMapper() != null) idMapper = typeCfg.getIdMapper(); if (typeCfg.getNameMapper() != null) nameMapper = typeCfg.getNameMapper();
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration("org.gridgain.Class1"); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 100; BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.Class2"); customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 100;
BinaryTypeConfiguration btcfg1 = new BinaryTypeConfiguration(); btcfg1.setTypeName("org.apache.ignite.tests.p2p.CacheDeploymentTestValue"); btcfg1.setSerializer(bs); BinaryTypeConfiguration btcfg2 = new BinaryTypeConfiguration(); btcfg2.setTypeName("org.apache.ignite.internal.processors.cache.binary." + "GridCacheBinaryObjectUserClassloaderSelfTest$TestValue1"); btcfg2.setSerializer(bs);
/** * @throws Exception If failed. */ @Test public void testCustomSerializer() throws Exception { BinaryTypeConfiguration type = new BinaryTypeConfiguration(CustomSerializedObject1.class.getName()); type.setSerializer(new CustomSerializer1()); BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(type)); CustomSerializedObject1 obj1 = new CustomSerializedObject1(10); BinaryObject po1 = marshal(obj1, marsh); assertEquals(20, po1.<CustomSerializedObject1>deserialize().val); }
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); BinaryTypeConfiguration customTypeCfg = new BinaryTypeConfiguration(); customTypeCfg.setTypeName(CustomIdMapper.class.getName()); customTypeCfg.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return ~BinaryContext.defaultIdMapper().typeId(clsName); } @Override public int fieldId(int typeId, String fieldName) { return typeId + ~BinaryContext.defaultIdMapper().fieldId(typeId, fieldName); } }); BinaryConfiguration bCfg = new BinaryConfiguration(); bCfg.setCompactFooter(compactFooter()); bCfg.setTypeConfigurations(Arrays.asList( new BinaryTypeConfiguration(Key.class.getName()), new BinaryTypeConfiguration(Value.class.getName()), new BinaryTypeConfiguration("org.gridgain.grid.internal.util.binary.mutabletest.*"), customTypeCfg)); bCfg.setIdMapper(new BinaryBasicIdMapper(false)); bCfg.setNameMapper(new BinaryBasicNameMapper(false)); cfg.setBinaryConfiguration(bCfg); cfg.setMarshaller(new BinaryMarshaller()); this.cfg = cfg; return cfg; }
/** * * @param nameMapper Name mapper. * @param mapper Mapper. * @throws IgniteCheckedException If failed. */ private void checkOverrideNameMapper(BinaryNameMapper nameMapper, BinaryIdMapper mapper) throws IgniteCheckedException { BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration(); typeCfg.setTypeName(CLASS2_FULL_NAME); typeCfg.setNameMapper(new BinaryNameMapper() { @Override public String typeName(String clsName) { return "type2"; } @Override public String fieldName(String fieldName) { return "field2"; } }); BinaryMarshaller marsh = binaryMarshaller(nameMapper, mapper, Arrays.asList( new BinaryTypeConfiguration("org.apache.ignite.internal.binary.test.*"), typeCfg)); BinaryContext ctx = binaryContext(marsh); ConcurrentMap<Integer, BinaryInternalMapper> types = U.field(ctx, "typeId2Mapper"); assertEquals(3, types.size()); assertTrue(types.containsKey(typeId(CLASS1_FULL_NAME, nameMapper, mapper))); assertTrue(types.containsKey(typeId(INNER_CLASS_FULL_NAME, nameMapper, mapper))); assertTrue(types.containsKey("type2".hashCode())); Map<String, org.apache.ignite.internal.binary.BinaryInternalMapper> typeMappers = U.field(ctx, "cls2Mappers"); assertEquals("type2", typeMappers.get(CLASS2_FULL_NAME).nameMapper().typeName(CLASS2_FULL_NAME)); }
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); if (register) { BinaryConfiguration bCfg = new BinaryConfiguration(); BinaryTypeConfiguration enumCfg = new BinaryTypeConfiguration(EnumType.class.getName()); enumCfg.setEnum(true); if (igniteInstanceName.equals(WRONG_CONF_NODE_NAME)) enumCfg.setEnumValues(F.asMap(EnumType.ONE.name(), EnumType.ONE.ordinal(), EnumType.TWO.name(), EnumType.ONE.ordinal())); else enumCfg.setEnumValues(F.asMap(EnumType.ONE.name(), EnumType.ONE.ordinal(), EnumType.TWO.name(), EnumType.TWO.ordinal())); bCfg.setTypeConfigurations(Arrays.asList(enumCfg, new BinaryTypeConfiguration(EnumHolder.class.getName()))); cfg.setBinaryConfiguration(bCfg); } cfg.setMarshaller(new BinaryMarshaller()); CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME); ccfg.setName(CACHE_NAME); ccfg.setCacheMode(CacheMode.PARTITIONED); cfg.setCacheConfiguration(ccfg); return cfg; }
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.NonExistentClass1"); customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400; BinaryTypeConfiguration customType3 = new BinaryTypeConfiguration("NonExistentClass2"); customType3.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 500; BinaryTypeConfiguration customType4 = new BinaryTypeConfiguration("NonExistentClass0"); customType4.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 0; BinaryTypeConfiguration customType5 = new BinaryTypeConfiguration(DateClass1.class.getName()); customType5.setNameMapper(new BinaryBasicNameMapper(false)); customType5.setIdMapper(new BinaryBasicIdMapper(false));
String clsName = typeCfg.getTypeName(); BinaryIdMapper idMapper = U.firstNotNull(typeCfg.getIdMapper(), globalIdMapper); BinaryNameMapper nameMapper = U.firstNotNull(typeCfg.getNameMapper(), globalNameMapper); BinarySerializer serializer = U.firstNotNull(typeCfg.getSerializer(), globalSerializer); BinaryIdentityResolver identity = BinaryArrayIdentityResolver.instance(); typeCfg.isEnum(), typeCfg.getEnumValues(), true); typeCfg.isEnum(), typeCfg.getEnumValues(), false);
/** * @throws Exception If failed. */ @Test public void testDefaultMapping() throws Exception { BinaryTypeConfiguration customMappingType = new BinaryTypeConfiguration(TestBinary.class.getName()); customMappingType.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { String typeName = BinaryContext.SIMPLE_NAME_LOWER_CASE_MAPPER.typeName(clsName); return typeName.toLowerCase().hashCode(); } @Override public int fieldId(int typeId, String fieldName) { return fieldName.toLowerCase().hashCode(); } }); BinaryMarshaller marsh1 = binaryMarshaller(Arrays.asList( new BinaryTypeConfiguration(SimpleObject.class.getName()), customMappingType )); TestBinary obj = binaryObject(); BinaryObjectImpl po = marshal(obj, marsh1); BinaryMarshaller marsh2 = binaryMarshaller(Arrays.asList( new BinaryTypeConfiguration(SimpleObject.class.getName()), new BinaryTypeConfiguration(TestBinary.class.getName()) )); po = marshal(obj, marsh2); assertEquals(obj, po.deserialize()); }
/** * @throws Exception If failed. */ @Test public void testCustomSerializerWithGlobal() throws Exception { BinaryTypeConfiguration type1 = new BinaryTypeConfiguration(CustomSerializedObject1.class.getName()); BinaryTypeConfiguration type2 = new BinaryTypeConfiguration(CustomSerializedObject2.class.getName()); type2.setSerializer(new CustomSerializer2()); BinaryMarshaller marsh = binaryMarshaller(new CustomSerializer1(), Arrays.asList(type1, type2)); CustomSerializedObject1 obj1 = new CustomSerializedObject1(10); BinaryObject po1 = marshal(obj1, marsh); assertEquals(20, po1.<CustomSerializedObject1>deserialize().val); CustomSerializedObject2 obj2 = new CustomSerializedObject2(10); BinaryObject po2 = marshal(obj2, marsh); assertEquals(30, po2.<CustomSerializedObject2>deserialize().val); }
/** * * @param nameMapper Name mapper. * @param mapper Mapper. * @throws IgniteCheckedException If failed. */ private void checkOverrideIdMapper(BinaryNameMapper nameMapper, BinaryIdMapper mapper) throws IgniteCheckedException { BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration(); typeCfg.setTypeName(CLASS2_FULL_NAME); typeCfg.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 100; } @Override public int fieldId(int typeId, String fieldName) { return 0; } }); BinaryMarshaller marsh = binaryMarshaller(nameMapper, mapper, Arrays.asList( new BinaryTypeConfiguration("org.apache.ignite.internal.binary.test.*"), typeCfg)); BinaryContext ctx = binaryContext(marsh); Map<Integer, Class> typeIds = U.field(ctx, "userTypes"); assertEquals(3, typeIds.size()); assertTrue(typeIds.containsKey(typeId(CLASS1_FULL_NAME, nameMapper, mapper))); assertTrue(typeIds.containsKey(typeId(INNER_CLASS_FULL_NAME, nameMapper, mapper))); assertTrue(typeIds.containsKey(100)); Map<String, org.apache.ignite.internal.binary.BinaryInternalMapper> typeMappers = U.field(ctx, "cls2Mappers"); assertEquals(100, typeMappers.get(CLASS2_FULL_NAME).idMapper().typeId(CLASS2_FULL_NAME)); }
BinaryTypeConfiguration binaryEnumCfg = new BinaryTypeConfiguration(EnumType.class.getName()); binaryEnumCfg.setEnum(true); binaryEnumCfg.setEnumValues(F.asMap(EnumType.ENUM_VAL_0.name(), EnumType.ENUM_VAL_0.ordinal(), EnumType.ENUM_VAL_1.name(),
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.NonExistentClass1"); customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400; BinaryTypeConfiguration customType3 = new BinaryTypeConfiguration("NonExistentClass2"); customType3.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 500; BinaryTypeConfiguration customType4 = new BinaryTypeConfiguration("NonExistentClass0"); customType4.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 0; BinaryTypeConfiguration customType5 = new BinaryTypeConfiguration(DateClass1.class.getName()); customType5.setIdMapper(new BinaryBasicIdMapper(false)); BinaryTypeConfiguration customType6 = new BinaryTypeConfiguration(MyTestClass.class.getName());
String typeCfgName = typeCfg.getTypeName(); if (typeCfg.getIdMapper() != null) idMapper = typeCfg.getIdMapper(); if (typeCfg.getNameMapper() != null) nameMapper = typeCfg.getNameMapper();
/** * @throws Exception If failed. */ @Test public void testWriteReplaceInheritable() throws Exception { ImmutableList<String> obj = ImmutableList.of("This is a test"); BinaryMarshaller marsh = binaryMarshaller(Collections.singleton( new BinaryTypeConfiguration(obj.getClass().getName()) )); BinaryObject po = marshal(obj, marsh); Object des = po.deserialize(); assertEquals(obj, des); }