TaskDetails taskData = null;
try {
taskData = (TaskDetails) registry.get(RegistryModelType.TASK_DETAIL, jobExecutionContext.getTaskData().getTaskID());
} catch (RegistryException e) {
log.error("Error retrieving job details from Registry");
throw new GFacHandlerException("Error retrieving job details from Registry", e);
}
JobDetails jobDetails = taskData.getJobDetailsList().get(0);
String jobDescription = jobDetails.getJobDescription();
if (jobDescription != null) {
JobDescriptor jobDescriptor = null;
try {
jobDescriptor = JobDescriptor.fromXML(jobDescription);
} catch (XmlException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
applicationDeploymentDescription.getType().setScratchWorkingDirectory(
jobDescriptor.getJobDescriptorDocument().getJobDescriptor().getWorkingDirectory());
applicationDeploymentDescription.getType().setInputDataDirectory(jobDescriptor.getInputDirectory());
applicationDeploymentDescription.getType().setOutputDataDirectory(jobDescriptor.getOutputDirectory());
applicationDeploymentDescription.getType().setStandardError(jobDescriptor.getJobDescriptorDocument().getJobDescriptor().getStandardErrorFile());
applicationDeploymentDescription.getType().setStandardOutput(jobDescriptor.getJobDescriptorDocument().getJobDescriptor().getStandardOutFile());
}
}
}
try {
if (jobExecutionContext.getSecurityContext(SSHSecurityContext.SSH_SECURITY_CONTEXT) == null) {
GFACSSHUtils.addSecurityContext(jobExecutionContext);
}
} catch (ApplicationSettingsException e) {
log.error(e.getMessage());
throw new GFacHandlerException("Error while creating SSHSecurityContext", e, e.getLocalizedMessage());
} catch (GFacException e1) {
log.error(e1.getMessage());
throw new GFacHandlerException("Error while creating SSHSecurityContext", e1, e1.getLocalizedMessage());
}
super.invoke(jobExecutionContext);
DataTransferDetails detail = new DataTransferDetails();
TransferStatus status = new TransferStatus();
ApplicationDeploymentDescriptionType app = jobExecutionContext.getApplicationContext()
.getApplicationDeploymentDescription().getType();
try {
Cluster cluster = ((SSHSecurityContext) jobExecutionContext.getSecurityContext(SSHSecurityContext.SSH_SECURITY_CONTEXT)).getPbsCluster();
if (cluster == null) {
throw new GFacProviderException("Security context is not set properly");
} else {
log.info("Successfully retrieved the Security Context");
}
// Get the Stdouts and StdErrs
String timeStampedServiceName = GFacUtils.createUniqueNameForService(jobExecutionContext.getServiceName());
TaskDetails taskData = jobExecutionContext.getTaskData();
String outputDataDir = null;
File localStdOutFile;
File localStdErrFile;
if (taskData.getAdvancedOutputDataHandling() != null) {
outputDataDir = taskData.getAdvancedOutputDataHandling().getOutputDataDir();
}
if (outputDataDir == null) {
outputDataDir = File.separator + "tmp";
}
outputDataDir = outputDataDir + File.separator + jobExecutionContext.getExperimentID() + "-" + jobExecutionContext.getTaskData().getTaskID();
(new File(outputDataDir)).mkdirs();
localStdOutFile = new File(outputDataDir + File.separator + timeStampedServiceName + "stdout");
localStdErrFile = new File(outputDataDir + File.separator + timeStampedServiceName + "stderr");
// cluster.makeDirectory(outputDataDir);
cluster.scpFrom(app.getStandardOutput(), localStdOutFile.getAbsolutePath());
Thread.sleep(1000);
cluster.scpFrom(app.getStandardError(), localStdErrFile.getAbsolutePath());
Thread.sleep(1000);
String stdOutStr = GFacUtils.readFileToString(localStdOutFile.getAbsolutePath());
String stdErrStr = GFacUtils.readFileToString(localStdErrFile.getAbsolutePath());
status.setTransferState(TransferState.COMPLETE);
detail.setTransferStatus(status);
detail.setTransferDescription("STDOUT:" + stdOutStr);
registry.add(ChildDataType.DATA_TRANSFER_DETAIL, detail, jobExecutionContext.getTaskData().getTaskID());
status.setTransferState(TransferState.COMPLETE);
detail.setTransferStatus(status);
detail.setTransferDescription("STDERR:" + stdErrStr);
registry.add(ChildDataType.DATA_TRANSFER_DETAIL, detail, jobExecutionContext.getTaskData().getTaskID());
List<DataObjectType> outputArray = new ArrayList<DataObjectType>();
Map<String, Object> output = jobExecutionContext.getOutMessageContext().getParameters();
Set<String> keys = output.keySet();
for (String paramName : keys) {
ActualParameter actualParameter = (ActualParameter) output.get(paramName);
if ("URI".equals(actualParameter.getType().getType().toString())) {
List<String> outputList = cluster.listDirectory(app.getOutputDataDirectory());
if (outputList.size() == 0 || outputList.get(0).isEmpty()) {
OutputUtils.fillOutputFromStdout(output, stdOutStr, stdErrStr,outputArray);
break;
} else {
String valueList = outputList.get(0);
cluster.scpFrom(app.getOutputDataDirectory() + File.separator + valueList, outputDataDir);
jobExecutionContext.addOutputFile(outputDataDir + File.separator + valueList);
DataObjectType dataObjectType = new DataObjectType();
dataObjectType.setValue(valueList);
dataObjectType.setKey(paramName);
dataObjectType.setType(DataType.URI);
outputArray.add(dataObjectType);
}
} else {
OutputUtils.fillOutputFromStdout(output, stdOutStr, stdErrStr,outputArray);
}
}
if (outputArray == null || outputArray.isEmpty()) {
throw new GFacHandlerException(
"Empty Output returned from the Application, Double check the application"
+ "and ApplicationDescriptor output Parameter Names");
}
app.setStandardError(localStdErrFile.getAbsolutePath());
app.setStandardOutput(localStdOutFile.getAbsolutePath());
app.setOutputDataDirectory(outputDataDir);
status.setTransferState(TransferState.DOWNLOAD);
detail.setTransferStatus(status);
detail.setTransferDescription(outputDataDir);
registry.add(ChildDataType.DATA_TRANSFER_DETAIL, detail, jobExecutionContext.getTaskData().getTaskID());
registry.add(ChildDataType.EXPERIMENT_OUTPUT, outputArray, jobExecutionContext.getExperimentID());
} catch (XmlException e) {
throw new GFacHandlerException("Cannot read output:" + e.getMessage(), e);
} catch (ConnectionException e) {
throw new GFacHandlerException(e.getMessage(), e);
} catch (TransportException e) {
throw new GFacHandlerException(e.getMessage(), e);
} catch (IOException e) {
throw new GFacHandlerException(e.getMessage(), e);
} catch (Exception e) {
try {
status.setTransferState(TransferState.FAILED);
detail.setTransferStatus(status);
registry.add(ChildDataType.DATA_TRANSFER_DETAIL, detail, jobExecutionContext.getTaskData().getTaskID());
GFacUtils.saveErrorDetails(jobExecutionContext, e.getLocalizedMessage(), CorrectiveAction.CONTACT_SUPPORT, ErrorCategory.FILE_SYSTEM_FAILURE);
} catch (Exception e1) {
throw new GFacHandlerException("Error persisting status", e1, e1.getLocalizedMessage());
}
throw new GFacHandlerException("Error in retrieving results", e);
}
}