1 package fr.in2p3.jsaga.command;
2
3 import org.apache.commons.cli.*;
4 import org.ogf.saga.url.URL;
5 import org.ogf.saga.url.URLFactory;
6 import org.ogf.saga.error.BadParameterException;
7 import org.ogf.saga.session.Session;
8 import org.ogf.saga.session.SessionFactory;
9 import org.ogf.saga.job.*;
10
11 import java.util.regex.Pattern;
12 import java.util.regex.Matcher;
13
14 import fr.in2p3.jsaga.impl.job.instance.JobImpl;
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class JobGetOutput extends AbstractCommand {
29 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
30
31 protected JobGetOutput() {
32 super("jsaga-job-get-output", new String[]{"jobId"}, new String[]{OPT_HELP, LONGOPT_HELP});
33 }
34
35 public static void main(String[] args) throws Exception {
36 JobGetOutput command = new JobGetOutput();
37 CommandLine line = command.parse(args);
38 if (line.hasOption(OPT_HELP))
39 {
40 command.printHelpAndExit(null);
41 }
42 else
43 {
44
45 URL serviceURL;
46 String nativeJobId;
47 Pattern pattern = Pattern.compile("\\[(.*)\\]-\\[(.*)\\]");
48 Matcher matcher = pattern.matcher(command.m_nonOptionValues[0]);
49 if (matcher.find()) {
50 serviceURL = URLFactory.createURL(matcher.group(1));
51 nativeJobId = matcher.group(2);
52 } else {
53 throw new BadParameterException("Job ID does not match regular expression: "+pattern.pattern());
54 }
55
56
57 Session session = SessionFactory.createSession(true);
58 JobService service = JobFactory.createJobService(session, serviceURL);
59 Job job = service.getJob(nativeJobId);
60
61
62 ((JobImpl)job).postStagingAndCleanup();
63 System.out.println("Job output have been retrieved successfully (if it exists)");
64 System.exit(0);
65 }
66 }
67
68 protected Options createOptions() {
69 Options opt = new Options();
70
71
72 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
73 .withLongOpt(LONGOPT_HELP)
74 .create(OPT_HELP));
75
76
77 return opt;
78 }
79 }