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.ParentDoesNotExist;
6   import fr.in2p3.jsaga.adaptor.data.optimise.DataCopy;
7   import fr.in2p3.jsaga.adaptor.data.optimise.DataCopyDelegated;
8   import fr.in2p3.jsaga.adaptor.data.read.LogicalReader;
9   import fr.in2p3.jsaga.adaptor.data.write.LogicalWriter;
10  import fr.in2p3.jsaga.engine.descriptors.AdaptorDescriptors;
11  import fr.in2p3.jsaga.engine.factories.DataAdaptorFactory;
12  import fr.in2p3.jsaga.impl.logicalfile.AbstractSyncLogicalFileImpl;
13  import fr.in2p3.jsaga.impl.namespace.FlagsHelper;
14  import fr.in2p3.jsaga.impl.namespace.JSAGAFlags;
15  import org.ogf.saga.error.*;
16  import org.ogf.saga.logicalfile.LogicalFile;
17  import org.ogf.saga.logicalfile.LogicalFileFactory;
18  import org.ogf.saga.namespace.*;
19  import org.ogf.saga.session.Session;
20  import org.ogf.saga.url.URL;
21  import org.ogf.saga.url.URLFactory;
22  
23  import java.util.List;
24  
25  /* ***************************************************
26  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
27  * ***             http://cc.in2p3.fr/             ***
28  * ***************************************************
29  * File:   LogicalFileCopy
30  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
31  * Date:   9 juil. 2008
32  * ***************************************************
33  * Description:                                      */
34  /**
35   *
36   */
37  public class LogicalFileCopy {
38      private static final String JSAGA_FACTORY = Base.getSagaFactory();
39  
40      private Session m_session;
41      private AbstractSyncLogicalFileImpl m_sourceFile;
42      private DataAdaptor m_adaptor;
43  
44      /** constructor */
45      public LogicalFileCopy(Session session, AbstractSyncLogicalFileImpl sourceFile, DataAdaptor adaptor) throws NotImplementedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
46          m_session = session;
47          m_sourceFile = sourceFile;
48          m_adaptor = adaptor;
49      }
50  
51      public void copy(URL effectiveTarget, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
52          boolean overwrite = Flags.OVERWRITE.isSet(flags);
53          URL source = m_sourceFile.getURL();
54          if (m_adaptor instanceof DataCopyDelegated && source.getScheme().equals(effectiveTarget.getScheme())) {
55              try {
56                  ((DataCopyDelegated)m_adaptor).requestTransfer(
57                          source,
58                          effectiveTarget,
59                          overwrite, source.getQuery());
60              } catch (DoesNotExistException doesNotExist) {
61                  throw new IncorrectStateException("Logical file does not exist: "+source, doesNotExist);
62              } catch (AlreadyExistsException alreadyExists) {
63                  throw new AlreadyExistsException("Target entry already exists: "+effectiveTarget, alreadyExists.getCause());
64              }
65          } else if (m_adaptor instanceof DataCopy && source.getScheme().equals(effectiveTarget.getScheme())) {
66              try {
67                  ((DataCopy)m_adaptor).copy(
68                          source.getPath(),
69                          effectiveTarget.getHost(), effectiveTarget.getPort(), effectiveTarget.getPath(),
70                          overwrite, source.getQuery(), null);
71              } catch (ParentDoesNotExist parentDoesNotExist) {
72                  throw new DoesNotExistException("Target parent directory does not exist: "+effectiveTarget.resolve(URLFactory.createURL(JSAGA_FACTORY, ".")), parentDoesNotExist);
73              } catch (DoesNotExistException doesNotExist) {
74                  throw new IncorrectStateException("Logical file does not exist: "+source, doesNotExist);
75              } catch (AlreadyExistsException alreadyExists) {
76                  throw new AlreadyExistsException("Target entry already exists: "+effectiveTarget, alreadyExists.getCause());
77              }
78          } else if (m_adaptor instanceof LogicalReader) {
79              DataAdaptor targetAdaptor = new DataAdaptorFactory(AdaptorDescriptors.getInstance()).getDataAdaptor(effectiveTarget, m_session);
80              if (targetAdaptor instanceof LogicalWriter) {
81                  this.putToLogicalFile(effectiveTarget, flags);
82              } else {
83                  this.putToPhysicalFile(effectiveTarget, flags);
84              }
85          } else {
86              throw new NotImplementedException("Not supported for this protocol: "+source.getScheme());
87          }
88      }
89  
90      private void putToPhysicalFile(URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
91          // get location of source entry (may be logical or physical
92          List<URL> sourceLocations = m_sourceFile.listLocationsSync();
93          if (sourceLocations!=null && sourceLocations.size()>0) {
94              // open source entry
95              URL source = sourceLocations.get(0);
96              NSEntry sourceEntry = this.createSourceNSEntry(source);
97  
98              // copy
99              try {
100     			sourceEntry.copy(target, flags);
101     		} finally {
102                 // close source entry (but not the source logical file)
103                 sourceEntry.close();
104     		}
105         } else {
106             throw new NoSuccessException("No location found for logical file: "+m_sourceFile.getURL());
107         }
108     }
109 
110     private void putToLogicalFile(URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
111         // get location of source physical file
112         List<URL> sourceLocations = m_sourceFile.listLocationsSync();
113         if (sourceLocations!=null && sourceLocations.size()>0) {
114             // open target logical file
115             LogicalFile targetLogicalFile = this.createTargetLogicalFile(target, flags);
116             try {
117                 // copy
118                 if (Flags.OVERWRITE.isSet(flags)) {
119                     // remove all target locations
120                     try {
121                         List<URL> targetLocations = targetLogicalFile.listLocations();
122                         for (int i=0; targetLocations !=null && i< targetLocations.size(); i++) {
123                             targetLogicalFile.removeLocation(targetLocations.get(i));
124                         }
125                     } catch(IncorrectStateException e) {
126                         // ignore if target logical file does not exist
127                     }
128                 }
129                 // add all source locations
130                 for (int i=0; sourceLocations!=null && i<sourceLocations.size(); i++) {
131                     targetLogicalFile.addLocation(sourceLocations.get(i));
132                 }
133             } catch (DoesNotExistException e) {
134                 throw new NoSuccessException("Unexpected exception", e);
135             } finally {
136                 // close target
137                 targetLogicalFile.close();
138             }
139         }
140     }
141 
142     private NSEntry createSourceNSEntry(URL source) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
143         try {
144             return NSFactory.createNSEntry(JSAGA_FACTORY, m_session, source, Flags.NONE.getValue());
145         } catch (AlreadyExistsException e) {
146             throw new NoSuccessException("Unexpected exception", e);
147         } catch (DoesNotExistException doesNotExist) {
148             throw new IncorrectStateException("Source physical file does not exist: "+source, doesNotExist);
149         }
150     }
151 
152     private LogicalFile createTargetLogicalFile(URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
153         int correctedFlags = flags;
154         correctedFlags = new FlagsHelper(correctedFlags).add(Flags.WRITE, Flags.CREATE);
155         correctedFlags = new FlagsHelper(correctedFlags).remove(JSAGAFlags.PRESERVETIMES);
156         if (Flags.OVERWRITE.isSet(correctedFlags)) {
157             correctedFlags = correctedFlags - Flags.OVERWRITE.getValue();
158         } else {
159             correctedFlags = correctedFlags + Flags.EXCL.getValue();
160         }
161         try {
162             return LogicalFileFactory.createLogicalFile(JSAGA_FACTORY, m_session, target, correctedFlags);
163         } catch (DoesNotExistException e) {
164             throw new NoSuccessException("Unexpected exception", e);
165         } catch (AlreadyExistsException alreadyExists) {
166             throw new AlreadyExistsException("Target entry already exists: "+target, alreadyExists.getCause());
167         }
168     }
169 }