View Javadoc

1   package fr.in2p3.jsaga.impl.logicalfile.copy;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
5   import fr.in2p3.jsaga.adaptor.data.optimise.DataCopy;
6   import fr.in2p3.jsaga.adaptor.data.optimise.DataCopyDelegated;
7   import fr.in2p3.jsaga.adaptor.data.write.LogicalWriter;
8   import fr.in2p3.jsaga.impl.logicalfile.AbstractSyncLogicalFileImpl;
9   import fr.in2p3.jsaga.impl.namespace.FlagsHelper;
10  import fr.in2p3.jsaga.impl.namespace.JSAGAFlags;
11  import org.ogf.saga.error.*;
12  import org.ogf.saga.logicalfile.LogicalFile;
13  import org.ogf.saga.logicalfile.LogicalFileFactory;
14  import org.ogf.saga.namespace.Flags;
15  import org.ogf.saga.session.Session;
16  import org.ogf.saga.url.URL;
17  
18  import java.util.List;
19  
20  /* ***************************************************
21  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
22  * ***             http://cc.in2p3.fr/             ***
23  * ***************************************************
24  * File:   LogicalFileCopyFrom
25  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
26  * Date:   9 juil. 2008
27  * ***************************************************
28  * Description:                                      */
29  /**
30   *
31   */
32  public class LogicalFileCopyFrom {
33      private static final String JSAGA_FACTORY = Base.getSagaFactory();
34  
35      private Session m_session;
36      private AbstractSyncLogicalFileImpl m_targetFile;
37      private DataAdaptor m_adaptor;
38  
39      /** constructor */
40      public LogicalFileCopyFrom(Session session, AbstractSyncLogicalFileImpl targetFile, DataAdaptor adaptor) throws NotImplementedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
41          m_session = session;
42          m_targetFile = targetFile;
43          m_adaptor = adaptor;
44      }
45  
46      public void copyFrom(URL effectiveSource, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException, IncorrectURLException {
47          boolean overwrite = Flags.OVERWRITE.isSet(flags);
48          URL target = m_targetFile.getURL();
49          if (m_adaptor instanceof DataCopyDelegated && target.getScheme().equals(effectiveSource.getScheme())) {
50              try {
51                  ((DataCopyDelegated)m_adaptor).requestTransfer(
52                          effectiveSource,
53                          target,
54                          overwrite, target.getQuery());
55              } catch (DoesNotExistException doesNotExist) {
56                  throw new DoesNotExistException("Logical file does not exist: "+effectiveSource, doesNotExist.getCause());
57              } catch (AlreadyExistsException alreadyExists) {
58                  throw new IncorrectStateException("Target entry already exists: "+target, alreadyExists);
59              }
60          } else if (m_adaptor instanceof DataCopy && target.getScheme().equals(effectiveSource.getScheme())) {
61              try {
62                  ((DataCopy)m_adaptor).copyFrom(
63                          effectiveSource.getHost(), effectiveSource.getPort(), effectiveSource.getPath(),
64                          target.getPath(),
65                          overwrite, target.getQuery());
66              } catch (DoesNotExistException doesNotExist) {
67                  throw new DoesNotExistException("Logical file does not exist: "+effectiveSource, doesNotExist.getCause());
68              } catch (AlreadyExistsException alreadyExists) {
69                  throw new IncorrectStateException("Target entry already exists: "+target, alreadyExists);
70              }
71          } else if (m_adaptor instanceof LogicalWriter) {
72              // todo: check that source is not a physical entry
73              this.getFromLogicalFile(effectiveSource, flags);
74          } else {
75              throw new NotImplementedException("Not supported for this protocol: "+target.getScheme());
76          }
77      }
78  
79  
80      private void getFromLogicalFile(URL source, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException, IncorrectURLException {
81          // get location of source physical file
82          LogicalFile sourceLogicalFile = this.createSourceLogicalFile(source, flags);
83          try {
84              List<URL> sourceLocations = sourceLogicalFile.listLocations();
85              if (sourceLocations!=null && sourceLocations.size()>0) {
86                  // remove all target locations
87                  try {
88                      List<URL> targetLocations = m_targetFile.listLocationsSync();
89                      for (int i=0; targetLocations!=null && i<targetLocations.size(); i++) {
90                          m_targetFile.removeLocationSync(targetLocations.get(i));
91                      }
92                  } catch(IncorrectStateException e) {
93                      // ignore if target logical file does not exist
94                  }
95                  // add all source locations
96                  for (int i=0; sourceLocations!=null && i<sourceLocations.size(); i++) {
97                      m_targetFile.addLocationSync(sourceLocations.get(i));
98                  }
99              }
100         } finally {
101             sourceLogicalFile.close();
102         }
103     }
104 
105     private LogicalFile createSourceLogicalFile(URL source, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException, IncorrectURLException {
106         int correctedFlags = flags;
107         correctedFlags = new FlagsHelper(correctedFlags).remove(JSAGAFlags.PRESERVETIMES, Flags.OVERWRITE);
108         try {
109             return LogicalFileFactory.createLogicalFile(JSAGA_FACTORY, m_session, source, correctedFlags);
110         } catch (AlreadyExistsException e) {
111             throw new NoSuccessException("Unexpected exception", e);
112         }
113     }
114 }