String[] hostgridFTP = host.getGridFTPEndPointArray();
if (hostgridFTP == null || hostgridFTP.length == 0) {
hostgridFTP = new String[] { host.getHostAddress() };
}
ProviderException pe = null;
for (String endpoint : host.getGridFTPEndPointArray()) {
try {
/*
* Read Stdout and Stderror
*/
URI stdoutURI = GfacUtils.createGsiftpURI(endpoint, app.getStandardOutput());
URI stderrURI = GfacUtils.createGsiftpURI(endpoint, app.getStandardError());
log.info("STDOUT:" + stdoutURI.toString());
log.info("STDERR:" + stderrURI.toString());
File logDir = new File("./service_logs");
if (!logDir.exists()) {
logDir.mkdir();
}
String timeStampedServiceName = GfacUtils.createUniqueNameForService(invocationContext
.getServiceName());
File localStdOutFile = File.createTempFile(timeStampedServiceName, "stdout");
localStdErrFile = File.createTempFile(timeStampedServiceName, "stderr");
String stdout = ftp.readRemoteFile(stdoutURI, gssCred, localStdOutFile);
String stderr = ftp.readRemoteFile(stderrURI, gssCred, localStdErrFile);
Map<String,ActualParameter> stringMap = null;
MessageContext<Object> output = invocationContext.getOutput();
for (Iterator<String> iterator = output.getNames(); iterator.hasNext(); ) {
String paramName = iterator.next();
ActualParameter actualParameter = (ActualParameter) output.getValue(paramName);
if ("URIArray".equals(actualParameter.getType().getType().toString())) {
URI outputURI = GfacUtils.createGsiftpURI(endpoint,app.getOutputDataDirectory());
List<String> outputList = ftp.listDir(outputURI,gssCred);
String[] valueList = outputList.toArray(new String[outputList.size()]);
((URIArrayType) actualParameter.getType()).setValueArray(valueList);
stringMap = new HashMap<String, ActualParameter>();
stringMap.put(paramName, actualParameter);
invocationContext.getExecutionContext().getNotifier().output(invocationContext, actualParameter.toString());
}
if ("StringArray".equals(actualParameter.getType().getType().toString())) {
String[] valueList = OutputUtils.parseStdoutArray(stdout, paramName);
((StringArrayType) actualParameter.getType()).setValueArray(valueList);
stringMap = new HashMap<String, ActualParameter>();
stringMap.put(paramName, actualParameter);
invocationContext.getExecutionContext().getNotifier().output(invocationContext, actualParameter.toString());
}
else{
// This is to handle exception during the output parsing.
stringMap = OutputUtils.fillOutputFromStdout(invocationContext.<ActualParameter>getOutput(), stdout,stderr);
String paramValue = output.getStringValue(paramName);
if(paramValue == null || paramValue.isEmpty()){
int errCode = listener.getError();
String errorMsg = "Job " + job.getID() + " on host " + host.getHostAddress();
JobSubmissionFault error = new JobSubmissionFault(this, new Exception(errorMsg), "GFAC HOST",
gateKeeper, job.getRSL(), invocationContext);
errorReason(errCode, error);
invocationContext.getExecutionContext().getNotifier().executionFail(invocationContext,error,
readLastLinesofStdOut(localStdErrFile.getPath(), 20));
throw error;
}
}
}
if(stringMap == null || stringMap.isEmpty()){
ProviderException exception = new ProviderException("Gram provider: Error creating job output", invocationContext);
invocationContext.getExecutionContext().getNotifier().executionFail(invocationContext,exception,exception.getLocalizedMessage());
throw exception;
}
// If users has given an output DAta poth we download the output files in to that directory, this will be apath in the machine where GFac is installed
if(WorkflowContextHeaderBuilder.getCurrentContextHeader() != null &&
WorkflowContextHeaderBuilder.getCurrentContextHeader().getWorkflowOutputDataHandling() != null){
WorkflowOutputDataHandlingDocument.WorkflowOutputDataHandling workflowOutputDataHandling =
WorkflowContextHeaderBuilder.getCurrentContextHeader().getWorkflowOutputDataHandling();
if(workflowOutputDataHandling.getApplicationOutputDataHandlingArray().length != 0){
String outputDataDirectory = workflowOutputDataHandling.getApplicationOutputDataHandlingArray()[0].getOutputDataDirectory();
if(outputDataDirectory != null && !"".equals(outputDataDirectory)){
stageOutputFiles(invocationContext,outputDataDirectory);
}
}
}
return stringMap;
}catch (XmlException e) {
throw new ProviderException(e.getMessage(), e,invocationContext,readLastLinesofStdOut(localStdErrFile.getPath(), 20));
}
catch (ToolsException e) {
throw new ProviderException(e.getMessage(), e,invocationContext,readLastLinesofStdOut(localStdErrFile.getPath(), 20));
} catch (URISyntaxException e) {
throw new ProviderException("URI is malformatted:" + e.getMessage(), e, invocationContext,readLastLinesofStdOut(localStdErrFile.getPath(), 20));
}catch (NullPointerException e) {
throw new ProviderException("Output is not produced in stdout:" + e.getMessage(), e, invocationContext, readLastLinesofStdOut(localStdErrFile.getPath(), 20));
}
}
/*
* If the execution reach here, all GridFTP Endpoint is failed.
*/
throw pe;
} catch (Exception e) {
// invocationContext.getExecutionContext().getNotifier().executionFail(invocationContext,e,readLastLinesofStdOut(localStdErrFile.getPath(), 20));
throw new ProviderException(e.getMessage(), e, invocationContext, readLastLinesofStdOut(localStdErrFile.getPath(), 20));
}
}