use of org.apache.geode.management.internal.cli.exceptions.CreateSubregionException in project geode by apache.
the class RegionCreateFunction method createRegion.
public static <K, V> Region<?, ?> createRegion(Cache cache, RegionFunctionArgs regionCreateArgs) {
Region<K, V> createdRegion = null;
final String regionPath = regionCreateArgs.getRegionPath();
final RegionShortcut regionShortcut = regionCreateArgs.getRegionShortcut();
final String useAttributesFrom = regionCreateArgs.getUseAttributesFrom();
// If a region path indicates a sub-region, check whether the parent region exists
RegionPath regionPathData = new RegionPath(regionPath);
String parentRegionPath = regionPathData.getParent();
Region<?, ?> parentRegion = null;
if (parentRegionPath != null && !Region.SEPARATOR.equals(parentRegionPath)) {
parentRegion = cache.getRegion(parentRegionPath);
if (parentRegion == null) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__PARENT_REGION_FOR_0_DOESNOT_EXIST, new Object[] { regionPath }));
}
if (parentRegion.getAttributes().getPartitionAttributes() != null) {
// For a PR, sub-regions are not supported.
throw new CreateSubregionException(CliStrings.format(CliStrings.CREATE_REGION__MSG__0_IS_A_PR_CANNOT_HAVE_SUBREGIONS, parentRegion.getFullPath()));
}
}
// One of Region Shortcut OR Use Attributes From has to be given
if (regionShortcut == null && useAttributesFrom == null) {
throw new IllegalArgumentException(CliStrings.CREATE_REGION__MSG__ONE_OF_REGIONSHORTCUT_AND_USEATTRIBUESFROM_IS_REQUIRED);
}
boolean isPartitioned = false;
RegionFactory<K, V> factory = null;
RegionAttributes<K, V> regionAttributes = null;
if (regionShortcut != null) {
regionAttributes = cache.getRegionAttributes(regionShortcut.toString());
if (logger.isDebugEnabled()) {
logger.debug("Using shortcut {} for {} region attributes : {}", regionShortcut, regionPath, regionAttributes);
}
if (regionAttributes == null) {
if (logger.isDebugEnabled()) {
logger.debug("Shortcut {} doesn't have attributes in {}", regionShortcut, cache.listRegionAttributes());
}
throw new IllegalStateException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COULDNOT_LOAD_REGION_ATTRIBUTES_FOR_SHORTCUT_0, regionShortcut));
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Using Manager's region attributes for {}", regionPath);
}
regionAttributes = regionCreateArgs.getRegionAttributes();
if (logger.isDebugEnabled()) {
logger.debug("Using Attributes : {}", regionAttributes);
}
}
isPartitioned = regionAttributes.getPartitionAttributes() != null;
factory = cache.createRegionFactory(regionAttributes);
if (!isPartitioned && regionCreateArgs.hasPartitionAttributes()) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__OPTION_0_CAN_BE_USED_ONLY_FOR_PARTITIONEDREGION, regionCreateArgs.getPartitionArgs().getUserSpecifiedPartitionAttributes()));
}
if (isPartitioned) {
PartitionAttributes<K, V> partitionAttributes = extractPartitionAttributes(cache, regionAttributes, regionCreateArgs);
DataPolicy originalDataPolicy = regionAttributes.getDataPolicy();
factory.setPartitionAttributes(partitionAttributes);
// We have to do this because AttributesFactory.setPartitionAttributes()
// checks RegionAttributes.hasDataPolicy() which is set only when the data
// policy is set explicitly
factory.setDataPolicy(originalDataPolicy);
}
// Set Constraints
final String keyConstraint = regionCreateArgs.getKeyConstraint();
final String valueConstraint = regionCreateArgs.getValueConstraint();
if (keyConstraint != null && !keyConstraint.isEmpty()) {
Class<K> keyConstraintClass = CliUtil.forName(keyConstraint, CliStrings.CREATE_REGION__KEYCONSTRAINT);
factory.setKeyConstraint(keyConstraintClass);
}
if (valueConstraint != null && !valueConstraint.isEmpty()) {
Class<V> valueConstraintClass = CliUtil.forName(valueConstraint, CliStrings.CREATE_REGION__VALUECONSTRAINT);
factory.setValueConstraint(valueConstraintClass);
}
// Expiration attributes
final RegionFunctionArgs.ExpirationAttrs entryExpirationIdleTime = regionCreateArgs.getEntryExpirationIdleTime();
if (entryExpirationIdleTime != null) {
factory.setEntryIdleTimeout(entryExpirationIdleTime.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs entryExpirationTTL = regionCreateArgs.getEntryExpirationTTL();
if (entryExpirationTTL != null) {
factory.setEntryTimeToLive(entryExpirationTTL.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs regionExpirationIdleTime = regionCreateArgs.getRegionExpirationIdleTime();
if (regionExpirationIdleTime != null) {
factory.setEntryIdleTimeout(regionExpirationIdleTime.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs regionExpirationTTL = regionCreateArgs.getRegionExpirationTTL();
if (regionExpirationTTL != null) {
factory.setEntryTimeToLive(regionExpirationTTL.convertToExpirationAttributes());
}
// Associate a Disk Store
final String diskStore = regionCreateArgs.getDiskStore();
if (diskStore != null && !diskStore.isEmpty()) {
factory.setDiskStoreName(diskStore);
}
if (regionCreateArgs.isSetDiskSynchronous()) {
factory.setDiskSynchronous(regionCreateArgs.isDiskSynchronous());
}
if (regionCreateArgs.isSetOffHeap()) {
factory.setOffHeap(regionCreateArgs.isOffHeap());
}
// Set stats enabled
if (regionCreateArgs.isSetStatisticsEnabled()) {
factory.setStatisticsEnabled(regionCreateArgs.isStatisticsEnabled());
}
// Set conflation
if (regionCreateArgs.isSetEnableAsyncConflation()) {
factory.setEnableAsyncConflation(regionCreateArgs.isEnableAsyncConflation());
}
if (regionCreateArgs.isSetEnableSubscriptionConflation()) {
factory.setEnableSubscriptionConflation(regionCreateArgs.isEnableSubscriptionConflation());
}
// Gateway Sender Ids
final Set<String> gatewaySenderIds = regionCreateArgs.getGatewaySenderIds();
if (gatewaySenderIds != null && !gatewaySenderIds.isEmpty()) {
for (String gatewaySenderId : gatewaySenderIds) {
factory.addGatewaySenderId(gatewaySenderId);
}
}
// Async Queue Ids
final Set<String> asyncEventQueueIds = regionCreateArgs.getAsyncEventQueueIds();
if (asyncEventQueueIds != null && !asyncEventQueueIds.isEmpty()) {
for (String asyncEventQueueId : asyncEventQueueIds) {
factory.addAsyncEventQueueId(asyncEventQueueId);
}
}
// concurrency check enabled & concurrency level
if (regionCreateArgs.isSetConcurrencyChecksEnabled()) {
factory.setConcurrencyChecksEnabled(regionCreateArgs.isConcurrencyChecksEnabled());
}
if (regionCreateArgs.isSetConcurrencyLevel()) {
factory.setConcurrencyLevel(regionCreateArgs.getConcurrencyLevel());
}
// cloning enabled for delta
if (regionCreateArgs.isSetCloningEnabled()) {
factory.setCloningEnabled(regionCreateArgs.isCloningEnabled());
}
// multicast enabled for replication
if (regionCreateArgs.isSetMcastEnabled()) {
factory.setMulticastEnabled(regionCreateArgs.isMcastEnabled());
}
// Set plugins
final Set<String> cacheListeners = regionCreateArgs.getCacheListeners();
if (cacheListeners != null && !cacheListeners.isEmpty()) {
for (String cacheListener : cacheListeners) {
Class<CacheListener<K, V>> cacheListenerKlass = CliUtil.forName(cacheListener, CliStrings.CREATE_REGION__CACHELISTENER);
factory.addCacheListener(CliUtil.newInstance(cacheListenerKlass, CliStrings.CREATE_REGION__CACHELISTENER));
}
}
// Compression provider
if (regionCreateArgs.isSetCompressor()) {
Class<Compressor> compressorKlass = CliUtil.forName(regionCreateArgs.getCompressor(), CliStrings.CREATE_REGION__COMPRESSOR);
factory.setCompressor(CliUtil.newInstance(compressorKlass, CliStrings.CREATE_REGION__COMPRESSOR));
}
final String cacheLoader = regionCreateArgs.getCacheLoader();
if (cacheLoader != null) {
Class<CacheLoader<K, V>> cacheLoaderKlass = CliUtil.forName(cacheLoader, CliStrings.CREATE_REGION__CACHELOADER);
factory.setCacheLoader(CliUtil.newInstance(cacheLoaderKlass, CliStrings.CREATE_REGION__CACHELOADER));
}
final String cacheWriter = regionCreateArgs.getCacheWriter();
if (cacheWriter != null) {
Class<CacheWriter<K, V>> cacheWriterKlass = CliUtil.forName(cacheWriter, CliStrings.CREATE_REGION__CACHEWRITER);
factory.setCacheWriter(CliUtil.newInstance(cacheWriterKlass, CliStrings.CREATE_REGION__CACHEWRITER));
}
String regionName = regionPathData.getName();
if (parentRegion != null) {
createdRegion = factory.createSubregion(parentRegion, regionName);
} else {
createdRegion = factory.create(regionName);
}
return createdRegion;
}
use of org.apache.geode.management.internal.cli.exceptions.CreateSubregionException in project geode by apache.
the class RegionCreateFunction method execute.
@Override
public void execute(FunctionContext context) {
ResultSender<Object> resultSender = context.getResultSender();
Cache cache = CacheFactory.getAnyInstance();
String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
RegionFunctionArgs regionCreateArgs = (RegionFunctionArgs) context.getArguments();
if (regionCreateArgs.isSkipIfExists()) {
Region<Object, Object> region = cache.getRegion(regionCreateArgs.getRegionPath());
if (region != null) {
resultSender.lastResult(new CliFunctionResult(memberNameOrId, true, CliStrings.format(CliStrings.CREATE_REGION__MSG__SKIPPING_0_REGION_PATH_1_ALREADY_EXISTS, new Object[] { memberNameOrId, regionCreateArgs.getRegionPath() })));
return;
}
}
try {
Region<?, ?> createdRegion = createRegion(cache, regionCreateArgs);
XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", createdRegion.getName());
resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format(CliStrings.CREATE_REGION__MSG__REGION_0_CREATED_ON_1, new Object[] { createdRegion.getFullPath(), memberNameOrId })));
} catch (IllegalStateException e) {
String exceptionMsg = e.getMessage();
String localizedString = LocalizedStrings.DiskStore_IS_USED_IN_NONPERSISTENT_REGION.toLocalizedString();
if (localizedString.equals(e.getMessage())) {
exceptionMsg = exceptionMsg + " " + CliStrings.format(CliStrings.CREATE_REGION__MSG__USE_ONE_OF_THESE_SHORTCUTS_0, new Object[] { String.valueOf(CreateAlterDestroyRegionCommands.PERSISTENT_OVERFLOW_SHORTCUTS) });
}
resultSender.lastResult(handleException(memberNameOrId, exceptionMsg, null));
} catch (IllegalArgumentException e) {
resultSender.lastResult(handleException(memberNameOrId, e.getMessage(), e));
} catch (RegionExistsException e) {
String exceptionMsg = CliStrings.format(CliStrings.CREATE_REGION__MSG__REGION_PATH_0_ALREADY_EXISTS_ON_1, new Object[] { regionCreateArgs.getRegionPath(), memberNameOrId });
resultSender.lastResult(handleException(memberNameOrId, exceptionMsg, e));
} catch (CreateSubregionException e) {
resultSender.lastResult(handleException(memberNameOrId, e.getMessage(), e));
} catch (Exception e) {
String exceptionMsg = e.getMessage();
if (exceptionMsg == null) {
exceptionMsg = CliUtil.stackTraceAsString(e);
}
resultSender.lastResult(handleException(memberNameOrId, exceptionMsg, e));
}
}
Aggregations