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.FileFactory;
8   import org.ogf.saga.namespace.Flags;
9   import org.ogf.saga.session.Session;
10  import org.ogf.saga.url.URL;
11  
12  import java.io.*;
13  
14  /* ***************************************************
15   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16   * ***             http://cc.in2p3.fr/             ***
17   * ***************************************************
18   * File:   OutputDataStagingFromWorker
19   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20   * Date:   20 mai 2009
21   * ***************************************************
22   * Description:                                      */
23  /**
24   *
25   */
26  public class OutputDataStagingFromWorker extends AbstractDataStagingWorker {
27      protected OutputDataStagingFromWorker(URL localURL, String workerPath, boolean append) {
28          super(localURL, workerPath, append);
29      }
30  
31      public void preStaging(PrintStream stdin) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
32          String WORKER_PATH = m_workerPath;
33          String LOCAL_PATH = m_localURL.getPath();
34          stdin.println("encode "+WORKER_PATH+" "+LOCAL_PATH);
35          stdin.println();
36      }
37  
38      public void postStaging(Session session, BufferedReader stdout) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
39          // decode
40          byte[] decoded = uudecode(stdout);
41  
42          // write fully
43          int append = (m_append ? Flags.APPEND : Flags.NONE).getValue();
44          Buffer buffer = BufferFactory.createBuffer(JSAGA_FACTORY, decoded);
45          try {
46              org.ogf.saga.file.File localFile = FileFactory.createFile(JSAGA_FACTORY, session, m_localURL, Flags.CREATE.or(append));
47              localFile.write(buffer);
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      public void cleanup(PrintStream stdin) {
59          stdin.println("rm -f "+m_workerPath);
60      }
61  
62      public boolean isInput() {
63          return OUTPUT;
64      }
65  
66      private static byte[] uudecode(BufferedReader reader) throws NoSuccessException {
67          try {
68              for (String line; (line=reader.readLine())!=null && !line.startsWith("begin-base64"); );
69              StringBuffer encoded = new StringBuffer();
70              for (String line; (line=reader.readLine())!=null && !line.equals("===="); ) {
71                  encoded.append(line);
72                  encoded.append('\n');
73              }
74              return Base64.decodeBase64(encoded.toString());
75          } catch (IOException e) {
76              throw new NoSuccessException(e);
77          }
78      }
79  }