View Javadoc

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   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
18   * ***             http://cc.in2p3.fr/             ***
19   * ***************************************************
20   * File:   InputDataStagingToWorker
21   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
22   * Date:   20 mai 2009
23   * ***************************************************
24   * Description:                                      */
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          // set variables
35          String WORKER_PATH = m_workerPath;
36          String FUNCTION = "input_"+position;
37  
38          // read fully
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          // generate script
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      /** must not be used on Windows platform */
88  //    private static void encode(byte[] decoded, PrintStream stdin) throws NoSuccessException {
89  //        try {
90  //            new BASE64Encoder().encodeBuffer(decoded, stdin);
91  //        } catch (IOException e) {
92  //            throw new NoSuccessException(e);
93  //        }
94  //    }
95  }