View Javadoc

1   package fr.in2p3.jsaga.adaptor.data;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.adaptor.data.impl.DataEmulatorConnection;
5   import fr.in2p3.jsaga.adaptor.data.optimise.DataCopy;
6   import fr.in2p3.jsaga.adaptor.data.optimise.DataCopyMonitor;
7   import fr.in2p3.jsaga.adaptor.data.optimise.DataRename;
8   import fr.in2p3.jsaga.adaptor.schema.data.emulator.*;
9   import org.exolab.castor.xml.Marshaller;
10  import org.exolab.castor.xml.Unmarshaller;
11  import org.ogf.saga.error.*;
12  import org.ogf.saga.task.Task;
13  import org.w3c.dom.Document;
14  
15  import javax.xml.parsers.DocumentBuilderFactory;
16  import java.io.Serializable;
17  
18  /* ***************************************************
19  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
20  * ***             http://cc.in2p3.fr/             ***
21  * ***************************************************
22  * File:   OptimizedEmulatorDataAdaptor
23  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
24  * Date:   22 aout 2007
25  * ***************************************************
26  * Description:                                      */
27  /**
28   *
29   */
30  public class OptimizedEmulatorDataAdaptor extends EmulatorDataAdaptor implements DataCopy, DataRename {
31      public String getType() {
32          return "otest";
33      }
34  
35      public void copy(String sourceAbsolutePath, 
36      		String targetHost, 
37      		int targetPort, 
38      		String targetAbsolutePath, 
39      		boolean overwrite, 
40      		String additionalArgs,
41      		DataCopyMonitor progressMonitor) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, ParentDoesNotExist, TimeoutException, NoSuccessException {
42          File file = m_server.getFile(sourceAbsolutePath);
43          DataEmulatorConnection targetServer = new DataEmulatorConnection(this.getType(), targetHost, targetPort);
44  
45          // check if exists
46          try {
47              targetServer.getFile(targetAbsolutePath);
48              if (!overwrite) {
49                  throw new AlreadyExistsException("File already exist");
50              } else {
51                  targetServer.removeFile(targetAbsolutePath);
52              }
53          } catch(DoesNotExistException e) {
54              // do nothing
55          }
56  
57          // clone file
58          File fileClone = (File) clone(file);
59          fileClone.setName(targetServer.getEntryName(targetAbsolutePath));
60  
61          // add clone to target server
62          DirectoryType parent;
63          try {
64              parent = targetServer.getParentDirectory(targetAbsolutePath);
65          } catch (DoesNotExistException e) {
66              throw new ParentDoesNotExist("Parent directory does not exist");
67          }
68          parent.addFile(fileClone);
69          if(Base.DEBUG) targetServer.commit();
70      }
71  
72      public void copyFrom(String sourceHost, int sourcePort, String sourceAbsolutePath, String targetAbsolutePath, boolean overwrite, String additionalArgs) throws AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
73          DataEmulatorConnection sourceServer = new DataEmulatorConnection(this.getType(), sourceHost, sourcePort);
74          File file = sourceServer.getFile(sourceAbsolutePath);
75  
76          // check if exists
77          try {
78              m_server.getFile(targetAbsolutePath);
79              if (!overwrite) {
80                  throw new AlreadyExistsException("File already exist");
81              } else {
82                  m_server.removeFile(targetAbsolutePath);
83              }
84          } catch(DoesNotExistException e) {
85              // do nothing
86          }
87  
88          // clone file
89          File fileClone = (File) clone(file);
90          fileClone.setName(m_server.getEntryName(targetAbsolutePath));
91  
92          // add clone to target server
93          DirectoryType parent = m_server.getParentDirectory(targetAbsolutePath);
94          parent.addFile(fileClone);
95          if(Base.DEBUG) m_server.commit();
96      }
97  
98      public void rename(String sourceAbsolutePath, String targetAbsolutePath, boolean overwrite, String additionalArgs) throws PermissionDeniedException, BadParameterException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException {
99          // get parents and entry names
100         DirectoryType sourceParent = m_server.getParentDirectory(sourceAbsolutePath);
101         DirectoryType targetParent = m_server.getParentDirectory(targetAbsolutePath);
102         String sourceEntryName = m_server.getEntryName(sourceAbsolutePath);
103         String targetEntryName = m_server.getEntryName(targetAbsolutePath);
104 
105         // get entry
106         EntryType entry = m_server.getEntry(sourceParent, sourceEntryName);
107 
108         // rename entry
109         entry.setName(targetEntryName);
110 
111         // move entry
112         if (entry instanceof Directory) {
113             Directory directory = (Directory) entry;
114             targetParent.addDirectory(directory);
115             sourceParent.removeDirectory(directory);
116         } else if (entry instanceof FileType) {
117             File file = (File) entry;
118             targetParent.addFile(file);
119             sourceParent.removeFile(file);
120         } else {
121             throw new NoSuccessException("[ADAPTOR ERROR] Unexpected entry type: "+entry.getClass().getName());
122         }
123         if(Base.DEBUG) m_server.commit();
124     }
125 
126     private static Serializable clone(Serializable bean) throws NoSuccessException {
127         try {
128             Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
129             Marshaller.marshal(bean, doc);
130             return (Serializable) Unmarshaller.unmarshal(bean.getClass(), doc);
131         } catch (Exception e) {
132             throw new NoSuccessException(e);
133         }
134     }
135 }