1 package fr.in2p3.jsaga.impl.job.instance.stream;
2
3 import fr.in2p3.jsaga.adaptor.job.control.interactive.*;
4 import fr.in2p3.jsaga.impl.job.instance.AbstractSyncJobImpl;
5 import org.ogf.saga.error.*;
6
7 import java.io.*;
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public class JobStderrInputStream extends Stdout {
22 protected AbstractSyncJobImpl m_job;
23 private JobIOHandler m_ioHandler;
24
25 public JobStderrInputStream(AbstractSyncJobImpl job, JobIOHandler ioHandler) throws NotImplementedException, DoesNotExistException, TimeoutException, NoSuccessException {
26 m_job = job;
27 m_ioHandler = ioHandler;
28 switch(m_job.getJobState()) {
29 case DONE:
30 case CANCELED:
31 case FAILED:
32 case RUNNING:
33
34 break;
35 default:
36 throw new DoesNotExistException("Stderr is not available because job is neither finished nor running: "+m_job.getState());
37 }
38 }
39
40
41 protected JobStderrInputStream(AbstractSyncJobImpl job) {
42 m_job = job;
43 }
44
45 public void closeJobIOHandler() throws PermissionDeniedException, TimeoutException, NoSuccessException {
46
47 if (m_stream == null) {
48 if (m_ioHandler instanceof JobIOGetter) {
49 m_stream = ((JobIOGetter)m_ioHandler).getStderr();
50 } else if (m_ioHandler instanceof JobIOSetter) {
51 m_stream = new PipedStderr((JobIOSetter) m_ioHandler);
52 }
53 }
54
55
56 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
57 try {
58 int len;
59 byte[] bytes = new byte[1024];
60 while ( (len=m_stream.read(bytes)) > -1 ) {
61 buffer.write(bytes, 0, len);
62 }
63 } catch (IOException e) {
64 throw new NoSuccessException(e);
65 }
66 m_buffer = new ByteArrayInputStream(buffer.toByteArray());
67 }
68
69
70
71 public int read() throws IOException {return this.getStream().read();}
72 public int read(byte[] b) throws IOException {return this.getStream().read(b);}
73 public int read(byte[] b, int off, int len) throws IOException {return this.getStream().read(b, off, len);}
74 public long skip(long n) throws IOException {return this.getStream().skip(n);}
75 public void close() throws IOException {this.getStream().close();}
76
77
78
79 private ByteArrayInputStream m_buffer;
80 private InputStream m_stream;
81 private InputStream getStream() throws IOException {
82 try {
83 switch(m_job.getState()) {
84 case DONE:
85 case CANCELED:
86 case FAILED:
87 if (m_buffer == null) {
88 throw new NoSuccessException("INTERNAL ERROR: JobIOHandler has not been closed");
89 }
90 return m_buffer;
91 case RUNNING:
92 if (m_stream == null) {
93 if (m_ioHandler instanceof JobIOGetter) {
94 m_stream = ((JobIOGetter)m_ioHandler).getStderr();
95 } else if (m_ioHandler instanceof JobIOSetter) {
96 m_stream = new PipedStderr((JobIOSetter) m_ioHandler);
97 } else {
98 throw new NoSuccessException("Can not read from stderr because job is running and adaptor does not support job interactivity");
99 }
100 }
101 return m_stream;
102 default:
103 throw new DoesNotExistException("Stderr is not available because job is neither finished nor running");
104 }
105 } catch (Exception e) {
106 throw new IOException(e.getMessage());
107 }
108 }
109 }