1 package fr.in2p3.jsaga.impl.job.staging;
2
3 import org.apache.commons.codec.binary.Base64;
4 import org.ogf.saga.buffer.Buffer;
5 import org.ogf.saga.buffer.BufferFactory;
6 import org.ogf.saga.error.*;
7 import org.ogf.saga.file.File;
8 import org.ogf.saga.file.FileFactory;
9 import org.ogf.saga.job.JobDescription;
10 import org.ogf.saga.namespace.Flags;
11 import org.ogf.saga.session.Session;
12 import org.ogf.saga.url.URL;
13
14 import java.io.PrintStream;
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class InputDataStagingToWorker extends AbstractDataStagingWorker {
29 protected InputDataStagingToWorker(URL localURL, String workerPath, boolean append) {
30 super(localURL, workerPath, append);
31 }
32
33 public void preStaging(Session session, PrintStream stdin, int position, String executable) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
34
35 String WORKER_PATH = m_workerPath;
36 String FUNCTION = "input_"+position;
37
38
39 Buffer buffer;
40 try {
41 File localFile = FileFactory.createFile(JSAGA_FACTORY, session, m_localURL, Flags.READ.getValue());
42 long size = localFile.getSize();
43 if (size > Integer.MAX_VALUE) {
44 throw new NotImplementedException("Attribute "+ JobDescription.FILETRANSFER+" is not supported for large files: "+size);
45 }
46 buffer = BufferFactory.createBuffer(JSAGA_FACTORY, (int) size);
47 localFile.read(buffer, (int) size);
48 localFile.close();
49 } catch (SagaIOException e) {
50 throw new NoSuccessException(e);
51 } catch (IncorrectURLException e) {
52 throw new NoSuccessException(e);
53 } catch (AlreadyExistsException e) {
54 throw new NoSuccessException(e);
55 }
56
57
58 stdin.println("function "+FUNCTION+" () {");
59 stdin.println("/bin/cat << ====");
60 stdin.println(encode(buffer.getData()));
61 stdin.println("====");
62 stdin.println("}");
63 if (m_append) {
64 stdin.println("decode_append "+WORKER_PATH+" "+FUNCTION);
65 } else {
66 stdin.println("decode "+WORKER_PATH+" "+FUNCTION);
67 }
68 if (WORKER_PATH.equals(executable)) {
69 stdin.println("chmod u+x "+WORKER_PATH);
70 }
71 stdin.println();
72 }
73
74 public void cleanup(PrintStream stdin) {
75 stdin.println("rm -f "+m_workerPath);
76 }
77
78 public boolean isInput() {
79 return INPUT;
80 }
81
82 private static String encode(byte[] decoded) throws NoSuccessException {
83 String encoded = Base64.encodeBase64String(decoded);
84 return encoded.replaceAll("\r\n", "\n");
85 }
86
87
88
89
90
91
92
93
94
95 }