View Javadoc

1   package fr.in2p3.jsaga.impl.job.staging.mgr;
2   
3   import fr.in2p3.jsaga.adaptor.job.control.staging.StagingJobAdaptor;
4   import fr.in2p3.jsaga.adaptor.job.control.staging.StagingTransfer;
5   import fr.in2p3.jsaga.impl.job.instance.AbstractSyncJobImpl;
6   import org.ogf.saga.error.*;
7   import org.ogf.saga.file.*;
8   import org.ogf.saga.job.JobDescription;
9   import org.ogf.saga.namespace.Flags;
10  import org.ogf.saga.session.Session;
11  import org.ogf.saga.url.URL;
12  import org.ogf.saga.url.URLFactory;
13  
14  /* ***************************************************
15   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16   * ***             http://cc.in2p3.fr/             ***
17   * ***************************************************
18   * File:   DataStagingManagerThroughSandbox
19   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20   * Date:   15 mars 2010
21   * ***************************************************
22   * Description:                                      */
23  /**
24   *
25   */
26  public class DataStagingManagerThroughSandbox implements DataStagingManager {
27      protected StagingJobAdaptor m_adaptor;
28  
29      public DataStagingManagerThroughSandbox(StagingJobAdaptor adaptor, String uniqId) throws NotImplementedException, BadParameterException, NoSuccessException {
30          m_adaptor = adaptor;
31      }
32  
33      public JobDescription modifyJobDescription(final JobDescription jobDesc) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
34          return jobDesc;
35      }
36  
37      public void postStaging(AbstractSyncJobImpl job, String nativeJobId) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
38          // for each output file
39          for (StagingTransfer transfer : m_adaptor.getOutputStagingTransfer(nativeJobId)) {
40              transfer(job.getSession(), transfer);
41          }
42      }
43  
44      public Directory cleanup(AbstractSyncJobImpl job, String nativeJobId) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
45          // for each input file
46          for (StagingTransfer transfer : m_adaptor.getInputStagingTransfer(nativeJobId)) {
47              remove(job.getSession(), transfer.getTo());
48          }
49  
50          // for each output file
51          for (StagingTransfer transfer : m_adaptor.getOutputStagingTransfer(nativeJobId)) {
52              remove(job.getSession(), transfer.getFrom());
53          }
54  
55          // remove base directory
56          String stagingDir = m_adaptor.getStagingDirectory(nativeJobId);
57          if (stagingDir != null) {
58              URL url = URLFactory.createURL(JSAGA_FACTORY, stagingDir);
59              Directory dir = null;
60              try {
61                  dir = FileFactory.createDirectory(JSAGA_FACTORY, job.getSession(), url);
62                  return dir;
63              } catch (IncorrectURLException e) {
64                  throw new NoSuccessException(e);
65              } catch (AlreadyExistsException e) {
66                  throw new NoSuccessException(e);
67              }
68          }
69          return null;
70      }
71  
72  	public StagingTransfer[] getOutputStagingTransfer(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
73  		return m_adaptor.getOutputStagingTransfer(nativeJobId);
74  	}
75  
76      protected static void transfer(Session session, StagingTransfer transfer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
77          int append = (transfer.isAppend() ? Flags.APPEND : Flags.NONE).getValue();
78          File file = null;
79          try {
80              URL from = URLFactory.createURL(JSAGA_FACTORY, pathToURL(transfer.getFrom()));
81              URL to = URLFactory.createURL(JSAGA_FACTORY, pathToURL(transfer.getTo()));
82              file = FileFactory.createFile(JSAGA_FACTORY, session, from, Flags.NONE.getValue());
83              file.copy(to, append);
84          } catch (AlreadyExistsException e) {
85              throw new NoSuccessException(e);
86          } catch (IncorrectURLException e) {
87              throw new NoSuccessException(e);
88          }finally{
89          	if(file != null){
90  	        	try{
91  	        		file.close();
92  	        	}catch (Exception e) {
93  					// Ignore it: A problem during the close should not be a problem.
94  				}
95          	}
96          }
97      }
98      private static String pathToURL(String path) {
99          boolean isWindowsAbsolutePath = (path.length()>2
100                 && Character.isLetter(path.charAt(0))
101                 && path.charAt(1)==':'
102                 && (path.charAt(2)=='\\' || path.charAt(2)=='/')
103                 && path.charAt(3)!='/');
104         if (path.contains(":/") && !isWindowsAbsolutePath) {
105             // returns URI
106             return path;
107         } else {
108             // convert path to URI
109             return new java.io.File(path).toURI().toString();
110         }
111     }
112 
113     private static void remove(Session session, String url) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, IncorrectStateException, NoSuccessException {
114     	File file = null;
115     	try {
116             URL urlToRemove = URLFactory.createURL(JSAGA_FACTORY, url);
117             file = FileFactory.createFile(JSAGA_FACTORY, session, urlToRemove, Flags.NONE.getValue());
118             file.remove();
119         } catch (AlreadyExistsException e) {
120             throw new NoSuccessException(e);
121         } catch (IncorrectURLException e) {
122             throw new NoSuccessException(e);
123         }finally{
124         	if(file != null){
125 	        	try{
126 	        		file.close();
127 	        	}catch (Exception e) {
128 					// Ignore it: A problem during the close should not be a problem.
129 				}
130         	}
131         }
132     }
133 }