Search in sources :

Example 1 with DataCommandResult

use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.

the class DataCommandFunctionJUnitTest method testLocateKeyIsString.

@Test
public void testLocateKeyIsString() throws Exception {
    DataCommandFunction dataCmdFn = new DataCommandFunction();
    DataCommandResult result = dataCmdFn.locateEntry("key_2", String.class.getName(), String.class.getName(), PARTITIONED_REGION, false);
    assertNotNull(result);
    result.aggregate(null);
    List<DataCommandResult.KeyInfo> keyInfos = result.getLocateEntryLocations();
    assertEquals(1, keyInfos.size());
}
Also used : DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with DataCommandResult

use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.

the class DataCommands method remove.

@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
@CliCommand(value = { CliStrings.REMOVE }, help = CliStrings.REMOVE__HELP)
public Result remove(@CliOption(key = { CliStrings.REMOVE__KEY }, help = CliStrings.REMOVE__KEY__HELP, specifiedDefaultValue = "") String key, @CliOption(key = { CliStrings.REMOVE__REGION }, mandatory = true, help = CliStrings.REMOVE__REGION__HELP, optionContext = ConverterHint.REGION_PATH) String regionPath, @CliOption(key = CliStrings.REMOVE__ALL, help = CliStrings.REMOVE__ALL__HELP, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false") boolean removeAllKeys, @CliOption(key = { CliStrings.REMOVE__KEYCLASS }, help = CliStrings.REMOVE__KEYCLASS__HELP) String keyClass) {
    InternalCache cache = getCache();
    DataCommandResult dataResult;
    if (StringUtils.isEmpty(regionPath)) {
        return makePresentationResult(DataCommandResult.createRemoveResult(key, null, null, CliStrings.REMOVE__MSG__REGIONNAME_EMPTY, false));
    }
    if (!removeAllKeys && (key == null)) {
        return makePresentationResult(DataCommandResult.createRemoveResult(null, null, null, CliStrings.REMOVE__MSG__KEY_EMPTY, false));
    }
    if (removeAllKeys) {
        this.securityService.authorizeRegionWrite(regionPath);
    } else {
        this.securityService.authorizeRegionWrite(regionPath, key);
    }
    @SuppressWarnings("rawtypes") Region region = cache.getRegion(regionPath);
    DataCommandFunction removefn = new DataCommandFunction();
    if (region == null) {
        Set<DistributedMember> memberList = getRegionAssociatedMembers(regionPath, getCache(), false);
        if (CollectionUtils.isNotEmpty(memberList)) {
            DataCommandRequest request = new DataCommandRequest();
            request.setCommand(CliStrings.REMOVE);
            request.setKey(key);
            request.setKeyClass(keyClass);
            request.setRemoveAllKeys(removeAllKeys ? "ALL" : null);
            request.setRegionName(regionPath);
            dataResult = callFunctionForRegion(request, removefn, memberList);
        } else {
            dataResult = DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false);
        }
    } else {
        dataResult = removefn.remove(key, keyClass, regionPath, removeAllKeys ? "ALL" : null);
    }
    dataResult.setKeyClass(keyClass);
    return makePresentationResult(dataResult);
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) Region(org.apache.geode.cache.Region) DataCommandRequest(org.apache.geode.management.internal.cli.domain.DataCommandRequest) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) DataCommandFunction(org.apache.geode.management.internal.cli.functions.DataCommandFunction) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 3 with DataCommandResult

use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.

the class DataCommands method locateEntry.

@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
@CliCommand(value = { CliStrings.LOCATE_ENTRY }, help = CliStrings.LOCATE_ENTRY__HELP)
public Result locateEntry(@CliOption(key = { CliStrings.LOCATE_ENTRY__KEY }, mandatory = true, help = CliStrings.LOCATE_ENTRY__KEY__HELP) String key, @CliOption(key = { CliStrings.LOCATE_ENTRY__REGIONNAME }, mandatory = true, help = CliStrings.LOCATE_ENTRY__REGIONNAME__HELP, optionContext = ConverterHint.REGION_PATH) String regionPath, @CliOption(key = { CliStrings.LOCATE_ENTRY__KEYCLASS }, help = CliStrings.LOCATE_ENTRY__KEYCLASS__HELP) String keyClass, @CliOption(key = { CliStrings.LOCATE_ENTRY__VALUEKLASS }, help = CliStrings.LOCATE_ENTRY__VALUEKLASS__HELP) String valueClass, @CliOption(key = { CliStrings.LOCATE_ENTRY__RECURSIVE }, help = CliStrings.LOCATE_ENTRY__RECURSIVE__HELP, unspecifiedDefaultValue = "false") boolean recursive) {
    this.securityService.authorizeRegionRead(regionPath, key);
    DataCommandResult dataResult;
    if (StringUtils.isEmpty(regionPath)) {
        return makePresentationResult(DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__REGIONNAME_EMPTY, false));
    }
    if (StringUtils.isEmpty(key)) {
        return makePresentationResult(DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_EMPTY, false));
    }
    DataCommandFunction locateEntry = new DataCommandFunction();
    Set<DistributedMember> memberList = getRegionAssociatedMembers(regionPath, getCache(), true);
    if (CollectionUtils.isNotEmpty(memberList)) {
        DataCommandRequest request = new DataCommandRequest();
        request.setCommand(CliStrings.LOCATE_ENTRY);
        request.setKey(key);
        request.setKeyClass(keyClass);
        request.setRegionName(regionPath);
        request.setValueClass(valueClass);
        request.setRecursive(recursive);
        dataResult = callFunctionForRegion(request, locateEntry, memberList);
    } else {
        dataResult = DataCommandResult.createLocateEntryInfoResult(key, null, null, CliStrings.format(CliStrings.LOCATE_ENTRY__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false);
    }
    dataResult.setKeyClass(keyClass);
    if (valueClass != null) {
        dataResult.setValueClass(valueClass);
    }
    return makePresentationResult(dataResult);
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) DataCommandRequest(org.apache.geode.management.internal.cli.domain.DataCommandRequest) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) DataCommandFunction(org.apache.geode.management.internal.cli.functions.DataCommandFunction) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 4 with DataCommandResult

use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.

the class DataCommands method get.

@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
@CliCommand(value = { CliStrings.GET }, help = CliStrings.GET__HELP)
public Result get(@CliOption(key = { CliStrings.GET__KEY }, mandatory = true, help = CliStrings.GET__KEY__HELP) String key, @CliOption(key = { CliStrings.GET__REGIONNAME }, mandatory = true, help = CliStrings.GET__REGIONNAME__HELP, optionContext = ConverterHint.REGION_PATH) String regionPath, @CliOption(key = { CliStrings.GET__KEYCLASS }, help = CliStrings.GET__KEYCLASS__HELP) String keyClass, @CliOption(key = { CliStrings.GET__VALUEKLASS }, help = CliStrings.GET__VALUEKLASS__HELP) String valueClass, @CliOption(key = CliStrings.GET__LOAD, unspecifiedDefaultValue = "true", specifiedDefaultValue = "true", help = CliStrings.GET__LOAD__HELP) Boolean loadOnCacheMiss) {
    this.securityService.authorizeRegionRead(regionPath, key);
    InternalCache cache = getCache();
    DataCommandResult dataResult;
    if (StringUtils.isEmpty(regionPath)) {
        return makePresentationResult(DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__REGIONNAME_EMPTY, false));
    }
    if (StringUtils.isEmpty(key)) {
        return makePresentationResult(DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__KEY_EMPTY, false));
    }
    @SuppressWarnings("rawtypes") Region region = cache.getRegion(regionPath);
    DataCommandFunction getfn = new DataCommandFunction();
    if (region == null) {
        Set<DistributedMember> memberList = getRegionAssociatedMembers(regionPath, getCache(), false);
        if (CollectionUtils.isNotEmpty(memberList)) {
            DataCommandRequest request = new DataCommandRequest();
            request.setCommand(CliStrings.GET);
            request.setKey(key);
            request.setKeyClass(keyClass);
            request.setRegionName(regionPath);
            request.setValueClass(valueClass);
            request.setLoadOnCacheMiss(loadOnCacheMiss);
            Subject subject = this.securityService.getSubject();
            if (subject != null) {
                request.setPrincipal(subject.getPrincipal());
            }
            dataResult = callFunctionForRegion(request, getfn, memberList);
        } else {
            dataResult = DataCommandResult.createGetInfoResult(key, null, null, CliStrings.format(CliStrings.GET__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false);
        }
    } else {
        dataResult = getfn.get(null, key, keyClass, valueClass, regionPath, loadOnCacheMiss);
    }
    dataResult.setKeyClass(keyClass);
    if (valueClass != null) {
        dataResult.setValueClass(valueClass);
    }
    return makePresentationResult(dataResult);
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) Region(org.apache.geode.cache.Region) DataCommandRequest(org.apache.geode.management.internal.cli.domain.DataCommandRequest) Subject(org.apache.shiro.subject.Subject) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) DataCommandFunction(org.apache.geode.management.internal.cli.functions.DataCommandFunction) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 5 with DataCommandResult

use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.

the class DataCommandFunction method get.

@SuppressWarnings({ "rawtypes" })
public DataCommandResult get(Object principal, String key, String keyClass, String valueClass, String regionName, Boolean loadOnCacheMiss) {
    InternalCache cache = getCache();
    if (StringUtils.isEmpty(regionName)) {
        return DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__REGIONNAME_EMPTY, false);
    }
    if (StringUtils.isEmpty(key)) {
        return DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__KEY_EMPTY, false);
    }
    Region region = cache.getRegion(regionName);
    if (region == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Region Not Found - {}", regionName);
        }
        return DataCommandResult.createGetResult(key, null, null, CliStrings.format(CliStrings.GET__MSG__REGION_NOT_FOUND, regionName), false);
    } else {
        Object keyObject;
        try {
            keyObject = getClassObject(key, keyClass);
        } catch (ClassNotFoundException e) {
            return DataCommandResult.createGetResult(key, null, null, "ClassNotFoundException " + keyClass, false);
        } catch (IllegalArgumentException e) {
            return DataCommandResult.createGetResult(key, null, null, "Error in converting JSON " + e.getMessage(), false);
        }
        // TODO determine whether the following conditional logic (assigned to 'doGet') is safer or
        // necessary
        boolean doGet = Boolean.TRUE.equals(loadOnCacheMiss);
        if (doGet || region.containsKey(keyObject)) {
            Object value = region.get(keyObject);
            // run it through post processor. region.get will return the deserialized object already, so
            // we don't need to
            // deserialize it anymore to pass it to the postProcessor
            value = securityService.postProcess(principal, regionName, keyObject, value, false);
            if (logger.isDebugEnabled()) {
                logger.debug("Get for key {} value {}", key, value);
            }
            // return DataCommandResult.createGetResult(key, value, null, null);
            Object[] array = getJSONForNonPrimitiveObject(value);
            if (value != null) {
                DataCommandResult result = DataCommandResult.createGetResult(key, array[1], null, null, true);
                if (array[0] != null) {
                    result.setValueClass((String) array[0]);
                }
                return result;
            } else {
                return DataCommandResult.createGetResult(key, array[1], null, null, false);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Key is not present in the region {}", regionName);
            }
            return DataCommandResult.createGetInfoResult(key, null, null, CliStrings.GET__MSG__KEY_NOT_FOUND_REGION, false);
        }
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult)

Aggregations

DataCommandResult (org.apache.geode.management.internal.cli.domain.DataCommandResult)13 InternalCache (org.apache.geode.internal.cache.InternalCache)8 Region (org.apache.geode.cache.Region)7 DistributedMember (org.apache.geode.distributed.DistributedMember)6 DataCommandRequest (org.apache.geode.management.internal.cli.domain.DataCommandRequest)6 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)4 CliMetaData (org.apache.geode.management.cli.CliMetaData)4 DataCommandFunction (org.apache.geode.management.internal.cli.functions.DataCommandFunction)4 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)4 CliCommand (org.springframework.shell.core.annotation.CliCommand)4 ArrayList (java.util.ArrayList)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 List (java.util.List)1 DataPolicy (org.apache.geode.cache.DataPolicy)1 ResultCollector (org.apache.geode.cache.execute.ResultCollector)1 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)1 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)1 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)1