View Javadoc

1   package fr.in2p3.jsaga.impl.file;
2   
3   import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
4   import fr.in2p3.jsaga.impl.namespace.*;
5   import fr.in2p3.jsaga.impl.url.AbstractURLImpl;
6   import fr.in2p3.jsaga.impl.url.URLHelper;
7   import fr.in2p3.jsaga.sync.file.SyncDirectory;
8   import org.ogf.saga.SagaObject;
9   import org.ogf.saga.error.*;
10  import org.ogf.saga.file.*;
11  import org.ogf.saga.namespace.*;
12  import org.ogf.saga.session.Session;
13  import org.ogf.saga.url.URL;
14  
15  import java.util.List;
16  
17  /* ***************************************************
18   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
19   * ***             http://cc.in2p3.fr/             ***
20   * ***************************************************
21   * File:   AbstractSyncDirectoryImpl
22   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
23   * Date:   29 mai 2009
24   * ***************************************************
25   * Description:                                      */
26  /**
27   *
28   */
29  public class AbstractSyncDirectoryImpl extends AbstractNSDirectoryImpl implements SyncDirectory {
30      /** constructor for factory */
31      public AbstractSyncDirectoryImpl(Session session, URL url, DataAdaptor adaptor, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
32          super(session, url, adaptor, new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
33      }
34  
35      /** constructor for NSDirectory.open() */
36      public AbstractSyncDirectoryImpl(AbstractNSDirectoryImpl dir, URL relativeUrl, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
37          super(dir, relativeUrl, new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
38      }
39  
40      /** constructor for NSEntry.openAbsolute() */
41      public AbstractSyncDirectoryImpl(AbstractNSEntryImpl entry, String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
42          super(entry, absolutePath, new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
43      }
44  
45      /** clone */
46      public SagaObject clone() throws CloneNotSupportedException {
47          return super.clone();
48      }
49  
50      /////////////////////////////// class AbstractNSEntryImpl ///////////////////////////////
51  
52      /** timeout not supported */
53      public NSDirectory openAbsoluteDir(String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
54          return new DirectoryImpl(this, absolutePath, flags);
55      }
56  
57      /** timeout not supported */
58      public NSEntry openAbsolute(String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
59          if (URLHelper.isDirectory(absolutePath)) {
60              return new DirectoryImpl(this, absolutePath, flags);
61          } else {
62              return new FileImpl(this, absolutePath, flags);
63          }
64      }
65  
66      /////////////////////////////////// interface NSEntry ///////////////////////////////////
67  
68      /** timeout not supported */
69      public NSDirectory openDir(URL name, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
70          return this.openDirectory(name, flags);
71      }
72      /** timeout not supported */
73      public NSDirectory openDir(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
74          return this.openDirectory(name);
75      }
76  
77      /** timeout not supported */
78      public NSEntry open(URL name, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
79          if (URLHelper.isDirectory(name)) {
80              return this.openDirectory(name, flags);
81          } else {
82              return this.openFile(name, flags);
83          }
84      }
85      /** timeout not supported */
86      public NSEntry open(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
87          if (URLHelper.isDirectory(name)) {
88              return this.openDirectory(name);
89          } else {
90              return this.openFile(name);
91          }
92      }
93  
94      /////////////////////////////////// interface Directory ///////////////////////////////////
95  
96      // <extra specs>
97      public long getSizeSync(int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
98  		int total_size = 0;
99  		try {
100 			List list = this.list(flags);
101 	    	for (int i=0; i<list.size(); i++) {
102 				AbstractURLImpl url = (AbstractURLImpl)list.get(i);
103 				if (url.getPath().endsWith("/")) {
104 					total_size += ((DirectoryImpl)this.openDir(url, flags)).getSize();
105 				} else {
106 					if (url.hasCache()) {
107 						total_size += url.getCache().getSize();
108 					} else {
109 						total_size += ((FileImpl)this.openFile(url, flags)).getSize();
110 					}
111 				}
112 	    	}
113 			return total_size;
114 		} catch (IncorrectURLException e) {
115 			throw new NoSuccessException(e);
116 		} catch (DoesNotExistException e) {
117 			throw new NoSuccessException(e);
118 		} catch (AlreadyExistsException e) {
119 			throw new NoSuccessException(e);
120 		}
121     }
122     
123     public long getSizeSync() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, TimeoutException, NoSuccessException {
124         return this.getSizeSync(Flags.NONE.getValue());
125     }
126     // </extra specs>
127     
128     public long getSizeSync(URL name, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
129         new FlagsHelper(flags).allowed(Flags.NONE, Flags.DEREFERENCE);
130         try {
131             File entry = this.openFile(name, flags);
132             try {
133                 return entry.getSize();
134             } finally {
135                 entry.close();
136             }
137         } catch (BadParameterException bpe) {
138             // This is extra-spec: catch BadParameterException thrown by URLHelper in case URL is a directory (eg ".")
139         	if (bpe.getStackTrace()[0].getClassName().equals(fr.in2p3.jsaga.impl.url.URLHelper.class.getName())) {
140         		if (name.normalize().getPath().equals(m_url.getPath())) {
141         			return this.getSizeSync(flags);
142         		} else {
143         			try {
144 						return ((DirectoryImpl)this.openDir(name, flags)).getSize();
145 					} catch (AlreadyExistsException aee) {
146 			        	// This should never happen since only DEREFERENCE flag is allowed
147 			            throw new NoSuccessException("Wrong exception thrown: "+ name, aee);
148 					}
149         		}
150         	} else {
151         		throw bpe;
152         	}
153         } catch (AlreadyExistsException aee) {
154         	// This should never happen since only DEREFERENCE flag is allowed
155             throw new NoSuccessException("Wrong exception thrown: "+ name, aee);
156         }
157     }
158     
159     public long getSizeSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
160         return this.getSizeSync(name, Flags.NONE.getValue());
161     }
162 
163     public boolean isFileSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
164         return super.isEntrySync(name);
165     }
166 
167     /** timeout not supported */
168     public Directory openDirectory(URL relativeUrl, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
169         return new DirectoryImpl(this, relativeUrl, flags);
170     }
171     /** timeout not supported */
172     public Directory openDirectory(URL relativeUrl) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
173         return new DirectoryImpl(this, relativeUrl, Flags.READ.getValue());
174     }
175 
176     /** timeout not supported */
177     public File openFile(URL relativeUrl, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
178         return new FileImpl(this, relativeUrl, flags);
179     }
180     /** timeout not supported */
181     public File openFile(URL relativeUrl) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
182         return new FileImpl(this, relativeUrl, Flags.READ.getValue());
183     }
184 
185     public FileInputStream openFileInputStreamSync(URL relativeUrl) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
186         URL url = _resolveRelativeUrl(m_url, relativeUrl);
187         return FileFactoryImpl.openFileInputStream(m_session, url, m_adaptor);
188     }
189 
190     public FileOutputStream openFileOutputStreamSync(URL relativeUrl) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
191         URL url = _resolveRelativeUrl(m_url, relativeUrl);
192         boolean exclusive = false;
193         boolean append = false;
194         return FileFactoryImpl.openFileOutputStream(m_session, url, m_adaptor, append, exclusive);
195     }
196     public FileOutputStream openFileOutputStreamSync(URL relativeUrl, boolean append) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
197         URL url = _resolveRelativeUrl(m_url, relativeUrl);
198         boolean exclusive = false;
199         return FileFactoryImpl.openFileOutputStream(m_session, url, m_adaptor, append, exclusive);
200     }
201 }