View Javadoc

1   package fr.in2p3.jsaga.adaptor.data;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.adaptor.base.defaults.Default;
5   import fr.in2p3.jsaga.adaptor.base.usage.Usage;
6   import fr.in2p3.jsaga.adaptor.data.impl.DataEmulatorConnection;
7   import fr.in2p3.jsaga.adaptor.data.impl.DataEmulatorConnectionAbstract;
8   import fr.in2p3.jsaga.adaptor.data.link.LinkAdaptor;
9   import fr.in2p3.jsaga.adaptor.data.link.NotLink;
10  import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
11  import fr.in2p3.jsaga.adaptor.data.read.FileReaderStreamFactory;
12  import fr.in2p3.jsaga.adaptor.data.write.FileWriterStreamFactory;
13  import fr.in2p3.jsaga.adaptor.schema.data.emulator.*;
14  import fr.in2p3.jsaga.adaptor.schema.data.emulator.File;
15  import fr.in2p3.jsaga.adaptor.security.SecurityCredential;
16  import org.ogf.saga.error.*;
17  
18  import java.io.*;
19  import java.util.Map;
20  
21  /* ***************************************************
22  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
23  * ***             http://cc.in2p3.fr/             ***
24  * ***************************************************
25  * File:   EmulatorDataAdaptor
26  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
27  * Date:   26 juin 2007
28  * ***************************************************
29  * Description:                                      */
30  /**
31   *
32   */
33  public class EmulatorDataAdaptor implements FileReaderStreamFactory, FileWriterStreamFactory, LinkAdaptor
34  //        , DataFilteredList
35  {
36      protected DataEmulatorConnectionAbstract m_server;
37  
38      public String getType() {
39          return "test";
40      }
41  
42      public Usage getUsage() {
43          return null;
44      }
45  
46      public Default[] getDefaults(Map attributes) throws IncorrectStateException {
47          return null;
48      }
49  
50      public Class[] getSupportedSecurityCredentialClasses() {
51          return null;    // no context class
52      }
53  
54      public void setSecurityCredential(SecurityCredential credential) {
55          // do nothing
56      }
57  
58      public int getDefaultPort() {
59          return 1234;
60      }
61  
62      public void connect(String userInfo, String host, int port, String basePath, Map attributes) throws AuthenticationFailedException, AuthorizationFailedException, TimeoutException, NoSuccessException {
63          m_server = new DataEmulatorConnection(this.getType(), host, port);
64          if(Base.DEBUG) m_server.commit();
65      }
66  
67      public void disconnect() throws NoSuccessException {
68          m_server.commit();
69      }
70  
71      public boolean exists(String absolutePath, String additionalArgs) throws PermissionDeniedException, TimeoutException, NoSuccessException {
72          try {
73              m_server.getEntry(absolutePath);
74              return true;
75          } catch (DoesNotExistException e) {
76              return false;
77          }
78      }
79  
80      public InputStream getInputStream(String absolutePath, String additionalArgs) throws PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
81          File file = m_server.getFile(absolutePath);
82          String content = file.getContent()!=null ? file.getContent() : "";
83          return new ByteArrayInputStream(content.getBytes());
84      }
85  
86      public OutputStream getOutputStream(String parentAbsolutePath, String fileName, boolean exclusive, boolean append, String additionalArgs) throws PermissionDeniedException, BadParameterException, AlreadyExistsException, ParentDoesNotExist, TimeoutException, NoSuccessException {
87          DirectoryType parent;
88          try {
89              parent = m_server.getDirectory(parentAbsolutePath);
90          } catch (DoesNotExistException e) {
91              throw new ParentDoesNotExist("Parent directory does not exist");
92          }
93          File file;
94          try {
95              file = m_server.getFile(parent, fileName);
96              if (exclusive) {
97                  throw new AlreadyExistsException("File already exists");
98              } else if (append) {
99                  // do nothing
100             } else {
101                 file.setContent(null);  //overwrite
102             }
103         } catch(DoesNotExistException e) {
104             file = m_server.addFile(parent, fileName);
105         }
106         return new EmulatorOutputStream(m_server, file);
107     }
108 
109     public void removeFile(String parentAbsolutePath, String fileName, String additionalArgs) throws PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
110         DirectoryType parent = m_server.getDirectory(parentAbsolutePath);
111         m_server.removeFile(parent, fileName);
112         if(Base.DEBUG) m_server.commit();
113     }
114 
115     public FileAttributes getAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
116         EntryType entry = m_server.getEntry(absolutePath);
117         return new EmulatorFileAttributes(entry);
118     }
119 
120     public FileAttributes[] listAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
121         EntryType[] list = m_server.listEntries(absolutePath);
122         FileAttributes[] ret = new FileAttributes[list.length];
123         for (int i=0; i<list.length; i++) {
124             ret[i] = new EmulatorFileAttributes(list[i]);
125         }
126         return ret;
127     }
128 
129     public void makeDir(String parentAbsolutePath, String directoryName, String additionalArgs) throws PermissionDeniedException, BadParameterException, AlreadyExistsException, ParentDoesNotExist, TimeoutException, NoSuccessException {
130         DirectoryType parent;
131         try {
132             parent = m_server.getDirectory(parentAbsolutePath);
133         } catch (DoesNotExistException e) {
134             throw new ParentDoesNotExist("Parent directory does not exist");
135         }
136         try {
137             m_server.getEntry(parent, directoryName);
138             throw new AlreadyExistsException("Entry already exists");
139         } catch(DoesNotExistException e) {
140             m_server.addDirectory(parent, directoryName);
141         }
142         if(Base.DEBUG) m_server.commit();
143     }
144 
145     public void removeDir(String parentAbsolutePath, String directoryName, String additionalArgs) throws PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
146         DirectoryType parent = m_server.getDirectory(parentAbsolutePath);
147         m_server.removeDirectory(parent, directoryName);
148         if(Base.DEBUG) m_server.commit();
149     }
150 
151     public String readLink(String absolutePath) throws NotLink, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
152         EntryType entry = m_server.getEntry(absolutePath);
153         if (entry instanceof FileType && ((FileType)entry).getLink()!=null) {
154             return ((FileType)entry).getLink();
155         } else {
156             throw new NotLink(absolutePath);
157         }
158     }
159 
160     public void link(String sourceAbsolutePath, String linkAbsolutePath, boolean overwrite) throws PermissionDeniedException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException {
161         File link;
162         try {
163             link = m_server.getFile(linkAbsolutePath);
164             if (! overwrite) {
165                 throw new AlreadyExistsException("Link already exists");
166             }
167         } catch(DoesNotExistException e) {
168             link = m_server.addFile(linkAbsolutePath);
169         }
170         link.setLink(sourceAbsolutePath);
171     }
172 
173     ///////////////////////////////////////// optional interfaces /////////////////////////////////////////
174 
175     public FileAttributes[] listDirectories(String absolutePath) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
176         EntryType[] list = m_server.listDirectories(absolutePath);
177         FileAttributes[] ret = new FileAttributes[list.length];
178         for (int i=0; i<list.length; i++) {
179             ret[i] = new EmulatorFileAttributes(list[i]);
180         }
181         return ret;
182     }
183 }