RegionConfig getValidRegionConfig(String regionPath, CacheConfig cacheConfig) { // Check to see if the region path contains an alias e.g "/region1 r1" // Then the first string will be the regionPath String[] regionPathTokens = regionPath.trim().split(" "); regionPath = regionPathTokens[0]; // check to see if the region path is in the form of "--region=region.entrySet() z" RegionConfig regionConfig = cacheConfig.findRegionConfiguration(regionPath); while (regionPath.contains(".") && (regionConfig) == null) { regionPath = regionPath.substring(0, regionPath.lastIndexOf(".")); regionConfig = cacheConfig.findRegionConfiguration(regionPath); } return regionConfig; } }
String getValidRegionName(String regionPath, CacheConfig cacheConfig) { // Check to see if the region path contains an alias e.g "/region1 r1" // Then the first string will be the regionPath String[] regionPathTokens = regionPath.trim().split(" "); regionPath = regionPathTokens[0]; // check to see if the region path is in the form of "--region=region.entrySet() z" while (regionPath.contains(".") && cacheConfig.findRegionConfiguration(regionPath) == null) { regionPath = regionPath.substring(0, regionPath.lastIndexOf(".")); } return regionPath; }
public <T extends CacheElement> List<T> findCustomRegionElements(String regionPath, Class<T> classT) { List<T> newList = new ArrayList<>(); RegionConfig regionConfig = findRegionConfiguration(regionPath); if (regionConfig == null) { return newList; } // streaming won't work here, because it's trying to cast element into CacheElement for (Object element : regionConfig.getCustomRegionElements()) { if (classT.isInstance(element)) { newList.add(classT.cast(element)); } } return newList; }
@Override public boolean updateConfigForGroup(String group, CacheConfig config, Object element) { RegionConfig.Index index = (RegionConfig.Index) element; String regionPath = getValidRegionName(index.getFromClause(), config); RegionConfig regionConfig = config.findRegionConfiguration(regionPath); if (regionConfig == null) { throw new EntityNotFoundException("Region " + index.getFromClause() + " not found."); } regionConfig.getIndexes().add(index); return true; } }
@Test public void getValidRegionName() { CacheConfig cacheConfig = mock(CacheConfig.class); RegionConfig region = new RegionConfig("regionA.regionB", "REPLICATE"); when(cacheConfig.findRegionConfiguration("/regionA.regionB")).thenReturn(region); assertThat(command.getValidRegionName("regionB", cacheConfig)).isEqualTo("regionB"); assertThat(command.getValidRegionName("/regionB", cacheConfig)).isEqualTo("/regionB"); assertThat(command.getValidRegionName("/regionB b", cacheConfig)).isEqualTo("/regionB"); assertThat(command.getValidRegionName("/regionB.entrySet()", cacheConfig)) .isEqualTo("/regionB"); assertThat(command.getValidRegionName("/regionA.regionB.entrySet() A", cacheConfig)) .isEqualTo("/regionA.regionB"); assertThat(command.getValidRegionName("/regionB.regionA.entrySet() B", cacheConfig)) .isEqualTo("/regionB"); } }
@Override public boolean updateConfigForGroup(String group, CacheConfig config, Object element) { RegionConfig.Index indexFromCommand = (RegionConfig.Index) element; String indexName = indexFromCommand.getName(); String regionName = indexFromCommand.getFromClause(); if (regionName != null) { RegionConfig regionConfig = config.findRegionConfiguration(regionName); if (regionConfig == null) { String errorMessage = "Region " + regionName + " not found"; if (!ConfigurationPersistenceService.CLUSTER_CONFIG.equals(group)) { errorMessage += " in group " + group; } throw new EntityNotFoundException(errorMessage); } if (indexName.isEmpty()) { regionConfig.getIndexes().clear(); } else { CacheElement.removeElement(regionConfig.getIndexes(), indexName); } } else { // Need to search for the index name as region was not specified for (RegionConfig r : config.getRegions()) { CacheElement.removeElement(r.getIndexes(), indexName); } } return true; }