1 package fr.in2p3.jsaga.adaptor.job;
2
3 import fr.in2p3.jsaga.adaptor.job.control.advanced.CleanableJobAdaptor;
4 import fr.in2p3.jsaga.adaptor.job.control.description.JobDescriptionTranslator;
5 import fr.in2p3.jsaga.adaptor.job.control.description.JobDescriptionTranslatorXSLT;
6 import fr.in2p3.jsaga.adaptor.job.control.interactive.*;
7 import fr.in2p3.jsaga.adaptor.job.control.staging.StagingJobAdaptorOnePhase;
8 import fr.in2p3.jsaga.adaptor.job.control.staging.StagingTransfer;
9 import fr.in2p3.jsaga.adaptor.job.monitor.JobMonitorAdaptor;
10 import org.ogf.saga.error.*;
11 import org.ogf.saga.job.JobDescription;
12
13 import java.io.*;
14 import java.util.Properties;
15 import java.util.UUID;
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class EmulatorJobControlAdaptor extends EmulatorJobAdaptorAbstract implements StagingJobAdaptorOnePhase, CleanableJobAdaptor, StreamableJobBatch {
30 public int getDefaultPort() {
31 return 1234;
32 }
33
34 public JobMonitorAdaptor getDefaultJobMonitor() {
35 return new EmulatorJobMonitorAdaptor();
36 }
37
38 public JobDescriptionTranslator getJobDescriptionTranslator() throws NoSuccessException {
39 return new JobDescriptionTranslatorXSLT("xsl/job/saga.xsl");
40 }
41
42 public String submit(String jobDesc, boolean checkMatch, String uniqId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
43
44 String nativeJobId = UUID.randomUUID().toString();
45
46
47 Properties prop = new Properties();
48 try {
49 prop.load(new ByteArrayInputStream(jobDesc.getBytes()));
50 } catch (IOException e) {
51 throw new NoSuccessException(e);
52 }
53
54
55 long endTime;
56 try {
57 String totalCPUTime = prop.getProperty(JobDescription.TOTALCPUTIME);
58 long duration = Long.parseLong(totalCPUTime) * 1000;
59 endTime = System.currentTimeMillis() + duration;
60 } catch (NumberFormatException e) {
61 throw new NoSuccessException(e);
62 }
63
64
65 File job = super.getJob(nativeJobId);
66 try {
67 OutputStream out = new FileOutputStream(job);
68 out.write(Long.toString(endTime).getBytes());
69 out.close();
70 } catch (FileNotFoundException e) {
71 throw new PermissionDeniedException(e);
72 } catch (IOException e) {
73 throw new TimeoutException(e);
74 }
75
76
77 return nativeJobId;
78 }
79
80 public JobIOHandler submit(String jobDesc, boolean checkMatch, String uniqId, InputStream stdin) throws PermissionDeniedException, TimeoutException, NoSuccessException {
81 final String nativeJobId = this.submit(jobDesc, checkMatch, uniqId);
82 return new JobIOGetter() {
83 private String m_nativeJobId = nativeJobId;
84 private InputStream m_stdout = new ByteArrayInputStream("output\n".getBytes());
85 private InputStream m_stderr = new ByteArrayInputStream("error\n".getBytes());
86 public String getJobId() {return m_nativeJobId;}
87 public InputStream getStdout() throws PermissionDeniedException, TimeoutException, NoSuccessException {return m_stdout;}
88 public InputStream getStderr() throws PermissionDeniedException, TimeoutException, NoSuccessException {return m_stderr;}
89 };
90 }
91
92 public void cancel(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
93 File job = super.getJob(nativeJobId);
94 try {
95 OutputStream out = new FileOutputStream(job);
96 out.write('0');
97 out.close();
98 } catch (FileNotFoundException e) {
99 throw new PermissionDeniedException(e);
100 } catch (IOException e) {
101 throw new TimeoutException(e);
102 }
103 }
104
105 public void clean(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
106 File job = super.getJob(nativeJobId);
107 if (! job.delete()) {
108 throw new PermissionDeniedException("Failed to cleanup job: "+nativeJobId);
109 }
110 }
111
112 public String getStagingDirectory(String nativeJobDescription, String uniqId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
113 return null;
114 }
115
116 public StagingTransfer[] getInputStagingTransfer(String nativeJobDescription, String uniqId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
117 return new StagingTransfer[]{};
118 }
119
120 public String getStagingDirectory(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
121 return null;
122 }
123
124 public StagingTransfer[] getInputStagingTransfer(String nativeJobId) throws TimeoutException, NoSuccessException {
125 return new StagingTransfer[]{};
126 }
127
128 public StagingTransfer[] getOutputStagingTransfer(String nativeJobId) throws TimeoutException, NoSuccessException {
129 return new StagingTransfer[]{};
130 }
131 }