com.gemstone.gemfire.management.internal.cli.functions.UserFunctionExecution Maven / Gradle / Ivy
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.management.internal.cli.functions;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CancellationException;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.execute.Execution;
import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;
import com.gemstone.gemfire.cache.execute.FunctionException;
import com.gemstone.gemfire.cache.execute.FunctionService;
import com.gemstone.gemfire.cache.execute.ResultCollector;
import com.gemstone.gemfire.distributed.DistributedMember;
import com.gemstone.gemfire.internal.ClassPathLoader;
import com.gemstone.gemfire.internal.InternalEntity;
import com.gemstone.gemfire.management.internal.cli.GfshParser;
import com.gemstone.gemfire.management.internal.cli.i18n.CliStrings;
/**
* @author Ajay Pande
*
* @since 7.0
*/
public class UserFunctionExecution implements Function, InternalEntity {
public static final String ID = UserFunctionExecution.class.getName();
private static final long serialVersionUID = 1L;
@Override
public void execute(FunctionContext context) {
try {
Cache cache = CacheFactory.getAnyInstance();
DistributedMember member = cache.getDistributedSystem()
.getDistributedMember();
String[] functionArgs = null;
Object[] args = (Object[]) context.getArguments();
if (args != null) {
String functionId = ((String) args[0]);
String filterString = ((String) args[1]);
String resultCollectorName = ((String) args[2]);
String argumentsString = ((String) args[3]);
String onRegion = ((String) args[4]);
try {
if (argumentsString != null && argumentsString.length() > 0) {
functionArgs = argumentsString.split(",");
}
Set filters = new HashSet();
ResultCollector resultCollectorInstance = null;
if (resultCollectorName != null && resultCollectorName.length() > 0) {
resultCollectorInstance = (ResultCollector) ClassPathLoader
.getLatest().forName(resultCollectorName).newInstance();
}
if (filterString != null && filterString.length() > 0) {
filters.add(filterString);
}
Function function = FunctionService.getFunction(functionId);
if (function == null) {
context
.getResultSender()
.lastResult(
CliStrings
.format(
CliStrings.EXECUTE_FUNCTION__MSG__DOES_NOT_HAVE_FUNCTION_0_REGISTERED,
functionId));
} else {
Execution execution = null;
if (onRegion != null && onRegion.length() > 0) {
Region region = cache.getRegion(onRegion);
if (region != null) {
execution = FunctionService.onRegion(region);
}
} else {
execution = FunctionService.onMember(member);
}
if (execution != null) {
if (resultCollectorInstance != null) {
execution = execution.withCollector(resultCollectorInstance);
}
if (functionArgs != null && functionArgs.length > 0) {
execution = execution.withArgs(functionArgs);
}
if (filters != null && filters.size() > 0) {
execution = execution.withFilter(filters);
}
List