TransferStatus status = new TransferStatus();
ApplicationDeploymentDescriptionType app = jobExecutionContext.getApplicationContext()
.getApplicationDeploymentDescription().getType();
try {
Cluster cluster = null;
if (jobExecutionContext.getSecurityContext(GSISecurityContext.GSI_SECURITY_CONTEXT) != null) {
cluster = ((GSISecurityContext) jobExecutionContext.getSecurityContext(GSISecurityContext.GSI_SECURITY_CONTEXT)).getPbsCluster();
} else {
cluster = ((GSISecurityContext) jobExecutionContext.getSecurityContext(GSISecurityContext.GSI_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());
//todo this is a mess we have to fix this
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);
Set<String> strings = output.keySet();
outputArray.clear();
for(String key:strings) {
ActualParameter actualParameter1 = (ActualParameter) output.get(key);
if("URI".equals(actualParameter1.getType().getType().toString())){
String downloadFile = MappingFactory.toString(actualParameter1);
cluster.scpFrom(downloadFile, outputDataDir);
String fileName = downloadFile.substring(downloadFile.lastIndexOf(File.separatorChar)+1, downloadFile.length());
String localFile = outputDataDir + File.separator +fileName;
jobExecutionContext.addOutputFile(localFile);
MappingFactory.fromString(actualParameter1, localFile);
DataObjectType dataObjectType = new DataObjectType();
dataObjectType.setValue(localFile);
dataObjectType.setKey(key);
dataObjectType.setType(DataType.URI);
outputArray.add(dataObjectType);
}
}
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);