package org.ngbw.examples; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.ngbw.sdk.Workbench; import org.ngbw.sdk.WorkbenchSession; import org.ngbw.sdk.common.util.FileUtils; import org.ngbw.sdk.common.util.Resource; import org.ngbw.sdk.database.User; import org.ngbw.sdk.jobs.Job; import org.ngbw.sdk.jobs.JobFile; import org.ngbw.sdk.jobs.JobMetadata; import org.ngbw.sdk.jobs.JobOutput; import org.ngbw.sdk.jobs.JobStatus; import org.ngbw.sdk.tool.DisabledResourceException; /* Submits a job for the specified user. InputData is the path to a directory that contains the data for the test. It can be either a path on the file system or a (relative) resource path. The directory must contain the 2 files described below and may also contain the input files referenced in testInput.properties (either with filesystem or resource paths). testParam.properties: Name=value pairs, corresponding to visible params for the tool. The special property toolId comes first in the file and isn't a parameter for a tool but tells us which tool to run. testInput.properties: Name=value pairs, where name is an input file param name and value is path to the file or resource with the data. */ public class TestJob { private static char[] buffer = new char[1024]; private static final String USERNAME = "test1"; private static final String PASSWORD = "test1"; private static final String testSpec1 = "examples/new_tooltest/CLUSTALW"; private static final String testSpec2 = "examples/new_tooltest/READSEQ"; private static final String testSpec3 = "examples/new_tooltest/RAXMLHPC2BB"; private static final String testSpec4 = "examples/new_tooltest/BEAST_TG"; private static final String paramFile = "/testParam.properties"; private static final String inputFile = "/testInput.properties"; //Workbench object instances private static Workbench workbench = Workbench.getInstance(); public static String runJob(User user, String path) throws DisabledResourceException, Exception { Map testParam = Resource.getResource(path + paramFile).getPropertiesAsMap(); Map testInput = Resource.getResource(path + inputFile).getPropertiesAsMap(); String toolId = testParam.get("toolId").trim(); testParam.remove("toolId"); /* Copy data from each testInput item to a tmp file because the submission mechanism may modify the contents of the file (to convert the encoding). Also because the path given in testInput may refer to a resource, not a file. */ HashMap inputFiles = new HashMap(); for (Map.Entry entry : testInput.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); byte[] data; data = Resource.getResource(value).getBytes(); File tmpFile = File.createTempFile("TestJob", null); FileUtils.writeFile(tmpFile, data); inputFiles.put(name, tmpFile); } /* I modified Job.submit to make 4th param Map> so that we could handle pisexml "List" parameters, but haven't really updated this code to handle it. As a temporary workaround I'm just taking our testParam and changing the type of map below. The caveat is that this won't correctly handle List type parameters in pise files. */ Map> toolParameters = new HashMap>(testParam.size()); for (String key : testParam.keySet()) { ArrayList al = new ArrayList(1); al.add(testParam.get(key)); toolParameters.put(key, al); } JobMetadata md = new JobMetadata(); String jh = Job.submit(user, null, md, toolId, toolParameters, inputFiles, false); return jh; } public static void main(String[] args) throws Exception { WorkbenchSession session = null; session = workbench.getSession(USERNAME, PASSWORD); User user = session.getUser(); String jobHandle; JobStatus jobStatus; jobHandle = runJob(user, testSpec1); jobStatus = new JobStatus(jobHandle, user, null); System.out.println("Submitted job " + jobStatus.getJobHandle()); displayStatus(jobStatus); jobHandle = runJob(user, testSpec2); jobStatus = new JobStatus(jobHandle, user, null); System.out.println("Submitted job " + jobStatus.getJobHandle()); displayStatus(jobStatus); jobHandle = runJob(user, testSpec3); jobStatus = new JobStatus(jobHandle, user, null); System.out.println("Submitted job " + jobStatus.getJobHandle()); displayStatus(jobStatus); jobHandle = runJob(user, testSpec4); jobStatus = new JobStatus(jobHandle, user, null); System.out.println("Submitted job " + jobStatus.getJobHandle()); displayStatus(jobStatus); } static void displayStatus(JobStatus status) { System.out.println("\nJOB STATUS " + status.getJobHandle()); System.out.println("Terminal Stage: " + status.isTerminalStage()); System.out.println("Job Stage: " + status.getJobStage()); System.out.println("Date Submitted: " + status.getDateSubmitted().toString()); } static void displayOutput(JobFile file, User user) throws Exception { System.out.println("\nResult File " + file.getFilename()); System.out.println("Parameter: " + file.getParameterName()); System.out.println("OutputDocumentId: " + file.getOutputDocumentId()); System.out.println("Length: " + file.getLength()); InputStream is = null; InputStreamReader reader = null; try { //is = Job.getOutputContent(file.getJobHandle(), file.getOutputDocumentId(), user); JobOutput output = Job.getOutput(file.getJobHandle(), user, null); JobFile jf = output.getDocumentInfo(file.getOutputDocumentId()); is = output.getDocument(jf); reader = new InputStreamReader(is); int len = reader.read(buffer, 0, buffer.length); if (len > -1) { System.out.println(buffer + " ...\n"); } else { System.out.println("EOF trying to read contents."); } } catch(Exception e) { System.out.println("Error reading contents: " + e.getMessage()); } finally { if (reader != null) { reader.close(); } if (is != null) { is.close(); } } } }