Object axisServiceObject = cx.getThreadLocal(MashupConstants.AXIS2_SERVICE);
if (axisServiceObject != null && axisServiceObject instanceof AxisService) {
axisService = (AxisService) axisServiceObject;
} else {
throw new CarbonException("Error obtaining the Service Meta Data: Axis2 Service");
}
ConfigurationContext configurationContext;
// retrieves the ConfigurationContext object from the Rhino Engine
Object configurationContextObject =
cx.getThreadLocal(MashupConstants.AXIS2_CONFIGURATION_CONTEXT);
if (configurationContextObject != null &&
configurationContextObject instanceof ConfigurationContext) {
configurationContext = (ConfigurationContext) configurationContextObject;
} else {
throw new CarbonException(
"Error obtaining the Service Meta Data : Axis2 ConfigurationContext");
}
//Generating UUID + current time for the taskName
String taskName =
system.getFormattedCurrentDateTime() + "-" + UUIDGenerator.getUUID().substring(9);
int argCount = arguments.length;
Object jsFunction = null;
Object[] functionParams = null;
long timeout = 0;
Date currentTime = new Date();
final Map<String, Object> resources = new HashMap<String, Object>();
final TaskDescription taskDescription = new TaskDescription();
final FunctionSchedulingManager functionSchedulingManager;
taskDescription.setGroup(FunctionSchedulingJob.MASHUP_GROUP);
taskDescription.setTaskClass(FunctionExecutionTask.class.getName());
OMElement propElem = FACTORY.createOMElement("property", TASK_OM_NAMESPACE);
OMNamespace nullNS = FACTORY.createOMNamespace("", "");
propElem.addAttribute("name", "axisService", nullNS);
propElem.addAttribute("value", axisService.getName(), nullNS);
taskDescription.addProperty(propElem);
resources.put(MashupConstants.AXIS2_CONFIGURATION_CONTEXT, configurationContext);
switch (argCount) {
case 2://A javascript function and its timeout were passed
//Extracting the javascript function from the arguments
if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) {
jsFunction = arguments[0];
} else {
throw new CarbonException("Invalid parameter. The first parameter must be " +
"a JavaScript function.");
}
//Extracting the frequency from the arguments
if (arguments[1] != null && arguments[1] instanceof Number) {
timeout = ((Number) arguments[1]).longValue();
} else {
throw new CarbonException("Invalid parameter. The second parameter " +
"must be function starting timeout.");
}
//Storing the function meta-data to be used by the job at execution time
resources.put(FunctionSchedulingJob.JAVASCRIPT_FUNCTION, jsFunction);
resources.put(FunctionSchedulingJob.FUNCTION_PARAMETERS, functionParams);
resources.put(FunctionSchedulingJob.AXIS_SERVICE, axisService);
resources.put(FunctionSchedulingJob.TASK_NAME, taskName);
//Creating the trigger. There will be a one-to-one mapping between jobs and triggers in this implementation
taskDescription.setName(taskName);
taskDescription.setCount(1);
taskDescription.setStartTime(new Date(currentTime.getTime() + timeout));
break;
case 3://A javascript function its execution frequency and parameters were passed
//Extracting the javascript function from the arguments=
if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) {
jsFunction = arguments[0];
} else {
throw new CarbonException("Invalid parameter. The first parameter must " +
"be a JavaScript function.");
}
//Extracting the frequency from the arguments
if (arguments[1] != null && arguments[1] instanceof Number) {
timeout = ((Number) arguments[1]).longValue();
} else {
throw new CarbonException(
"Invalid parameter. The second parameter must be the " +
"execution frequency in milliseconds.");
}
//Extracting function parameters from the arguments
if (arguments[2] != null) {
if (arguments[2] instanceof String) {
taskName = (String) arguments[2];
} else {
throw new CarbonException(
"Invalid parameter. The third parameter must be a string " +
"value for the the task name");
}
}
//Storing the function meta-data to be used by the job at execution time
resources.put(FunctionSchedulingJob.JAVASCRIPT_FUNCTION, jsFunction);
resources.put(FunctionSchedulingJob.FUNCTION_PARAMETERS, functionParams);
resources.put(FunctionSchedulingJob.AXIS_SERVICE, axisService);
resources.put(FunctionSchedulingJob.TASK_NAME, taskName);
//Creating the trigger. There will be a one-to-one mapping between jobs and triggers in this implementation
taskDescription.setName(taskName);
taskDescription.setCount(1);
taskDescription.setStartTime(new Date(currentTime.getTime() + timeout));
break;
default:
throw new CarbonException("Invalid number of parameters.");
}
functionSchedulingManager = FunctionSchedulingManager.getInstance();
functionSchedulingManager.scheduleTask(taskDescription, resources, configurationContext);