private static final Logger log = LoggerFactory.getLogger(GridFTPOutputHandler.class);
public void invoke(JobExecutionContext jobExecutionContext) throws GFacHandlerException {
log.info("Invoking GridFTPOutputHandler ...");
ApplicationDeploymentDescriptionType app = jobExecutionContext.getApplicationContext().getApplicationDeploymentDescription().getType();
HostDescriptionType hostType = jobExecutionContext.getApplicationContext().getHostDescription().getType();
String[] gridFTPEndpointArray = null;
String hostName = null;
if(jobExecutionContext.getApplicationContext().getHostDescription().getType() instanceof GlobusHostType){
gridFTPEndpointArray = ((GlobusHostType) hostType).getGridFTPEndPointArray();
hostName = ((GlobusHostType) hostType).getHostName();
}
else if (jobExecutionContext.getApplicationContext().getHostDescription().getType() instanceof UnicoreHostType){
gridFTPEndpointArray = ((UnicoreHostType) hostType).getGridFTPEndPointArray();
hostName = ((UnicoreHostType) hostType).getHostName();
}
else {
//TODO
}
GridFtp ftp = new GridFtp();
File localStdErrFile = null;
Map<String, ActualParameter> stringMap = new HashMap<String, ActualParameter>();
try {
GSSCredential gssCred = ((GSISecurityContext)jobExecutionContext.getSecurityContext(GSISecurityContext.GSI_SECURITY_CONTEXT)).getGssCredentials();
String[] hostgridFTP = gridFTPEndpointArray;
if (hostgridFTP == null || hostgridFTP.length == 0) {
hostgridFTP = new String[]{hostName};
}
for (String endpoint : gridFTPEndpointArray) {
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(jobExecutionContext
.getServiceName());
File localStdOutFile = File.createTempFile(timeStampedServiceName, "stdout");
localStdErrFile = File.createTempFile(timeStampedServiceName, "stderr");
String stdout = null;
String stderr = null;
// TODO: what if job is failed
// and this handler is not able to find std* files?
try {
stdout = ftp.readRemoteFile(stdoutURI, gssCred, localStdOutFile);
stderr = ftp.readRemoteFile(stderrURI, gssCred, localStdErrFile);
//TODO: do we also need to set them as output parameters for another job
ApplicationDescription application = jobExecutionContext.getApplicationContext().getApplicationDeploymentDescription();
ApplicationDeploymentDescriptionType appDesc = application.getType();
appDesc.setStandardOutput(stdout);
appDesc.setStandardError(stderr);
jobExecutionContext.getApplicationContext().setApplicationDeploymentDescription(application);
}
catch(ToolsException e) {
log.error("Cannot download stdout/err files. One reason could be the job is not successfully finished: "+e.getMessage());
}