PeerTypeRegistration implements TypeRegistration { @Override public void initialize() { TypeRegistry typeRegistry = cache.getPdxRegistry(); if (typeRegistry != null) { typeRegistry.flushCache(); logger.debug("Flushing TypeRegistry"); } InternalRegionFactory factory = cache.createInternalRegionFactory(); factory.setScope(Scope.DISTRIBUTED_ACK); if (cache.getPdxPersistent()) { if (cache.getCacheConfig().pdxDiskStoreUserSet) { factory.setDiskStoreName(cache.getPdxDiskStore()); } else { factory.setDiskStoreName(cache.getOrCreateDefaultDiskStore().getName()); } factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); } else { factory.setDataPolicy(DataPolicy.REPLICATE); } factory.addCacheListener(new CacheListenerAdapter<Object, Object>() { @Override public void afterCreate(EntryEvent<Object, Object> event) { verifyConfiguration(); Object value = event.getNewValue(); Object key = event.getKey(); if (value != null) { updateLocalAndReverseMaps(key, value); } } }); factory.setCacheWriter(new CacheWriterAdapter<Object, Object>() { @Override public void beforeCreate(EntryEvent<Object, Object> event) throws CacheWriterException { Object newValue = event.getNewValue(); if (newValue instanceof PdxType) { logger.info("Adding new type: {}", ((PdxType) event.getNewValue()).toFormattedString()); } else { logger.info("Adding new type: {} {}", event.getKey(), ((EnumInfo) newValue).toFormattedString()); } } @Override public void beforeUpdate(EntryEvent<Object, Object> event) throws CacheWriterException { if (!event.getRegion().get(event.getKey()).equals(event.getNewValue())) { PdxRegistryMismatchException ex = new PdxRegistryMismatchException( "Trying to add a PDXType with the same id as an existing PDX type. id=" + event.getKey() + ", existing pdx type " + event.getOldValue() + ", new type " + event.getNewValue()); throw new CacheWriterException(ex); } } }); factory.setIsUsedForMetaRegion(true); factory.setMetaRegionWithTransactions(true); try { idToType = factory.create(REGION_NAME); } catch (TimeoutException | RegionExistsException ex) { throw new PdxInitializationException("Could not create pdx registry", ex); } statistics.initialize(); if (!getIdToType().isEmpty()) { verifyConfiguration(); } } PeerTypeRegistration(final InternalCache cache); @Override void initialize(); @Override int defineType(PdxType newType); @Override PdxType getType(int typeId); @Override void addRemoteType(int typeId, PdxType type); @Override void gatewaySenderStarted(GatewaySender gatewaySender); @Override void creatingPersistentRegion(); @Override void creatingPool(); @Override int getEnumId(Enum<?> v); @Override void addRemoteEnum(int id, EnumInfo enumInfo); @Override int defineEnum(final EnumInfo newInfo); @Override EnumInfo getEnumById(int id); @Override Map<Integer, PdxType> types(); @Override Map<Integer, EnumInfo> enums(); @Override PdxType getPdxTypeForField(String fieldName, String className); @Override Set<PdxType> getPdxTypesForClassName(String className); @Override boolean isClient(); @Override void addImportedType(int typeId, PdxType importedType); @Override void addImportedEnum(int id, EnumInfo importedInfo); @Deprecated static int getPdxRegistrySize(); @Override int getLocalSize(); @VisibleForTesting int getTypeToIdSize(); @VisibleForTesting int getEnumToIdSize(); static final String LOCK_SERVICE_NAME; static final String REGION_NAME; static final String REGION_FULL_PATH; @VisibleForTesting
  static final int PLACE_HOLDER_FOR_TYPE_ID; }
@Test public void typeRegistryCacheIsFlushedWhenNotNull() { TypeRegistry typeRegistry = mock(TypeRegistry.class); when(internalCache.getPdxRegistry()).thenReturn(typeRegistry); PeerTypeRegistration peerTypeRegistration = new PeerTypeRegistration(internalCache); peerTypeRegistration.initialize(); verify(typeRegistry).flushCache(); }
@Test public void pdxPersistenceIsSetWithUserDefinedDiskStore() throws NoSuchFieldException, IllegalAccessException { PeerTypeRegistration peerTypeRegistration = spy(new PeerTypeRegistration(internalCache)); doReturn(factory).when(internalCache).createRegionFactory(); when(internalCache.getPdxPersistent()).thenReturn(true); CacheConfig config = mock(CacheConfig.class); when(internalCache.getCacheConfig()).thenReturn(config); Field field = config.getClass().getField("pdxDiskStoreUserSet"); field.setBoolean(config, Boolean.TRUE); final String diskStoreName = "userDiskStore"; when(internalCache.getPdxDiskStore()).thenReturn(diskStoreName); peerTypeRegistration.initialize(); verify(factory).setDiskStoreName(diskStoreName); verify(factory).setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); }
@Test public void pdxPersistenceIsSetWithDefaultDiskStore() { PeerTypeRegistration peerTypeRegistration = spy(new PeerTypeRegistration(internalCache)); doReturn(factory).when(internalCache).createRegionFactory(); when(internalCache.getPdxPersistent()).thenReturn(true); CacheConfig config = mock(CacheConfig.class); when(internalCache.getCacheConfig()).thenReturn(config); DiskStoreImpl defaultDiskStore = mock(DiskStoreImpl.class); when(internalCache.getOrCreateDefaultDiskStore()).thenReturn(defaultDiskStore); final String diskStoreName = "defaultDiskStoreName"; when(defaultDiskStore.getName()).thenReturn(diskStoreName); peerTypeRegistration.initialize(); verify(factory).setDiskStoreName(diskStoreName); verify(factory).setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); }
@Test public void pdxPersistenceIsNotSetWhenPdxPersistenceIsFalse() { PeerTypeRegistration peerTypeRegistration = spy(new PeerTypeRegistration(internalCache)); doReturn(factory).when(internalCache).createRegionFactory(); peerTypeRegistration.initialize(); verify(factory, times(0)).setDiskStoreName(any(String.class)); verify(factory).setDataPolicy(DataPolicy.REPLICATE); }
@Test(expected = PdxInitializationException.class) public void pdxInitializationExceptionIsThrownInInitializeWhenRegionCreationFails() throws IOException, ClassNotFoundException { doThrow(new RegionExistsException(region)).when(factory).create(any()); PeerTypeRegistration peerTypeRegistration = new PeerTypeRegistration(internalCache); peerTypeRegistration.initialize(); }