1 package fr.in2p3.jsaga.impl.file;
2
3 import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
4 import fr.in2p3.jsaga.adaptor.data.read.*;
5 import fr.in2p3.jsaga.adaptor.data.write.FileWriter;
6 import fr.in2p3.jsaga.impl.file.copy.*;
7 import fr.in2p3.jsaga.impl.namespace.*;
8 import fr.in2p3.jsaga.impl.url.URLHelper;
9 import fr.in2p3.jsaga.impl.url.AbstractURLImpl;
10 import fr.in2p3.jsaga.sync.file.SyncFile;
11 import org.ogf.saga.SagaObject;
12 import org.ogf.saga.buffer.Buffer;
13 import org.ogf.saga.error.*;
14 import org.ogf.saga.file.*;
15 import org.ogf.saga.namespace.*;
16 import org.ogf.saga.session.Session;
17 import org.ogf.saga.url.URL;
18
19 import java.io.IOException;
20 import java.util.List;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public abstract class AbstractSyncFileImpl extends AbstractNSEntryImplWithStream implements SyncFile {
35 protected FileInputStream m_inStream;
36
37
38 protected AbstractSyncFileImpl(Session session, URL url, DataAdaptor adaptor, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
39 super(session, URLHelper.toFileURL(url), adaptor, new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
40 boolean initOK = false;
41 try {
42 this.init(flags);
43 initOK = true;
44 } finally {
45 if (!initOK) this.close();
46 }
47 }
48
49
50 protected AbstractSyncFileImpl(AbstractNSDirectoryImpl dir, URL relativeUrl, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
51 super(dir, URLHelper.toFileURL(relativeUrl), new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
52 boolean initOK = false;
53 try {
54 this.init(flags);
55 initOK = true;
56 } finally {
57 if (!initOK) this.close();
58 }
59 }
60
61
62 protected AbstractSyncFileImpl(AbstractNSEntryImpl entry, String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
63 super(entry, URLHelper.toFilePath(absolutePath), new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
64 boolean initOK = false;
65 try {
66 this.init(flags);
67 initOK = true;
68 } finally {
69 if (!initOK) this.close();
70 }
71 }
72
73 private void init(int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
74 if(Flags.CREATEPARENTS.isSet(flags)) flags=Flags.CREATE.or(flags);
75 if(Flags.CREATE.isSet(flags)) flags=Flags.WRITE.or(flags);
76 new FlagsHelper(flags).allowed(JSAGAFlags.BYPASSEXIST, Flags.ALLFILEFLAGS);
77 if (Flags.WRITE.isSet(flags)) {
78 if (!Flags.CREATE.isSet(flags)) {
79 if (m_adaptor instanceof DataReaderAdaptor && !((DataReaderAdaptor)m_adaptor).exists(m_url.getPath(), m_url.getQuery())) {
80 throw new DoesNotExistException("File does not exist: "+ m_url);
81 }
82 }
83 try {
84 this.tryToOpen(flags);
85 } catch (DoesNotExistException e) {
86 if (Flags.CREATEPARENTS.isSet(flags)) {
87
88 this._makeParentDirs();
89
90 this.tryToOpen(flags);
91 } else {
92 throw e;
93 }
94 } catch (AlreadyExistsException e) {
95 if (Flags.EXCL.isSet(flags)) {
96 throw e;
97 } else {
98 if (!Flags.APPEND.isSet(flags)) {
99
100 try {
101 this.remove();
102 } catch (IncorrectStateException e1) {
103 throw e;
104 }
105 } else {
106 throw new NoSuccessException("Adaptor should not throw AlreadExistsException with flag APPEND");
107 }
108
109 this.tryToOpen(flags);
110 }
111 }
112 }
113 if (Flags.READ.isSet(flags)) {
114 m_inStream = FileFactoryImpl.openFileInputStream(m_session, m_url, m_adaptor);
115 }
116 if (Flags.READ.isSet(flags) || Flags.WRITE.isSet(flags)) {
117
118 } else if (!JSAGAFlags.BYPASSEXIST.isSet(flags) && !((AbstractURLImpl)m_url).hasCache() && m_adaptor instanceof DataReaderAdaptor) {
119 boolean exists = ((DataReaderAdaptor)m_adaptor).exists(m_url.getPath(), m_url.getQuery());
120 if (! exists) {
121 throw new DoesNotExistException("File does not exist: "+ m_url);
122 }
123 }
124 }
125 private void tryToOpen(int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
126 boolean append = Flags.APPEND.isSet(flags);
127 boolean exclusive = Flags.EXCL.isSet(flags);
128 try {
129 m_outStream = FileFactoryImpl.openFileOutputStream(m_session, m_url, m_adaptor, append, exclusive);
130 } catch (DoesNotExistException e) {
131 throw new DoesNotExistException("Failed to create parent directory", e.getCause());
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public SagaObject clone() throws CloneNotSupportedException {
149 FileImpl clone = (FileImpl) super.clone();
150 clone.m_inStream = m_inStream;
151 return clone;
152 }
153
154
155
156
157 public synchronized void close() throws NotImplementedException, NoSuccessException {
158 try {
159 if (m_outStream != null) {
160 m_outStream.close();
161 m_outStream = null;
162 }
163 if (m_inStream != null) {
164 m_inStream.close();
165 m_inStream = null;
166 }
167 } catch (IOException e) {
168 throw new NoSuccessException(e);
169 } finally {
170 super.close();
171 }
172 }
173
174
175 public void removeSync(int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
176 if (m_outStream != null) {
177 try {m_outStream.close();} catch (IOException e) {
178 }
179 if (m_inStream != null) {
180 try {m_inStream.close();} catch (IOException e) {
181 }
182 super.removeSync(flags);
183 }
184
185
186 public void copySync(URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
187 this._copyAndMonitor(target, flags, null);
188 }
189 public void _copyAndMonitor(URL target, int flags, AbstractCopyTask progressMonitor) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
190 new FlagsHelper(flags).allowed(JSAGAFlags.PRESERVETIMES, Flags.DEREFERENCE, Flags.OVERWRITE, Flags.CREATEPARENTS);
191 if (Flags.DEREFERENCE.isSet(flags)) {
192 AbstractSyncNSEntryImpl entry = this._dereferenceEntry();
193 try {
194 entry.copySync(target, flags - Flags.DEREFERENCE.getValue());
195 } finally {
196 entry.close();
197 }
198 return;
199 }
200 URL effectiveTarget = this._getEffectiveURL(target);
201 if (m_outStream != null) {
202 try {m_outStream.close();} catch (IOException e) {
203 }
204 if (JSAGAFlags.PRESERVETIMES.isSet(flags)) {
205
206 long sourceTimes = this.getMTime();
207 AbstractNSEntryImpl targetEntry = super._getTargetEntry_checkPreserveTimes(effectiveTarget);
208
209
210 new FileCopy(m_session, this, m_adaptor).copy(effectiveTarget, flags, progressMonitor);
211
212
213 targetEntry.setMTime(sourceTimes);
214 } else {
215
216 new FileCopy(m_session, this, m_adaptor).copy(effectiveTarget, flags, progressMonitor);
217 }
218 }
219
220
221 public void copyFromSync(URL source, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException, IncorrectURLException {
222 this._copyFromAndMonitor(source, flags, null);
223 }
224 public void _copyFromAndMonitor(URL source, int flags, AbstractCopyFromTask progressMonitor) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException, IncorrectURLException {
225 new FlagsHelper(flags).allowed(JSAGAFlags.PRESERVETIMES, Flags.DEREFERENCE, Flags.OVERWRITE);
226 if (Flags.DEREFERENCE.isSet(flags)) {
227 AbstractSyncNSEntryImpl entry = this._dereferenceEntry();
228 try {
229 entry.copyFromSync(source, flags - Flags.DEREFERENCE.getValue());
230 } finally {
231 entry.close();
232 }
233 return;
234 }
235 URL effectiveSource = this._getEffectiveURL(source);
236 if (m_inStream != null) {
237 try {m_inStream.close();} catch (IOException e) {
238 }
239 if (JSAGAFlags.PRESERVETIMES.isSet(flags)) {
240
241 long sourceTimes = super._getSourceTimes_checkPreserveTimes(effectiveSource);
242
243
244 new FileCopyFrom(m_session, this, m_adaptor).copyFrom(effectiveSource, flags, progressMonitor);
245
246
247 this.setMTime(sourceTimes);
248 } else {
249
250 new FileCopyFrom(m_session, this, m_adaptor).copyFrom(effectiveSource, flags, progressMonitor);
251 }
252 }
253
254
255
256
257 public NSDirectory openAbsoluteDir(String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
258 return new DirectoryImpl(this, absolutePath, flags);
259 }
260
261
262 public NSEntry openAbsolute(String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
263 if (URLHelper.isDirectory(absolutePath)) {
264 return new DirectoryImpl(this, absolutePath, flags);
265 } else {
266 return new FileImpl(this, absolutePath, flags);
267 }
268 }
269
270
271
272 public long getSizeSync() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
273 if (m_adaptor instanceof FileReader) {
274 if (m_outStream != null) {
275 try {m_outStream.close();} catch (IOException e) {
276 }
277 }
278 FileAttributes attrs = this._getFileAttributes();
279 long size = attrs.getSize();
280 if (size > -1) {
281 return size;
282 }
283 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
284 }
285
286 public int readSync(Buffer buffer, int offset, int len) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
287 final int EOF = -1;
288 if (m_inStream == null) {
289 throw new IncorrectStateException("Reading file requires READ or READWRITE flags", this);
290 }
291 if (len < 0) {
292 throw new BadParameterException("Read length must have a positive value: "+len, this);
293 }
294 if (m_adaptor instanceof FileReader) {
295 int readlen;
296 try {
297 byte[] bytes = buffer.getData();
298 readlen = m_inStream.read(bytes, offset, len);
299 } catch (DoesNotExistException e) {
300 try {m_inStream.close();} catch (IOException e1) {
301 throw new IncorrectStateException(e);
302 } catch (IOException e) {
303 try {m_inStream.close();} catch (IOException e1) {
304 throw new SagaIOException(e);
305 }
306 return readlen;
307 } else {
308 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
309 }
310 }
311 public int readSync(Buffer buffer, int len) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
312 return this.readSync(buffer, 0, len);
313 }
314 public int readSync(Buffer buffer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
315 return this.readSync(buffer, 0, buffer.getSize());
316 }
317
318 public int writeSync(Buffer buffer, int offset, int len) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
319 if (m_outStream == null) {
320 throw new IncorrectStateException("Writing file requires WRITE or READWRITE flags", this);
321 }
322 if (len < 0) {
323 throw new BadParameterException("Write length must have a positive value: "+len, this);
324 }
325 if (m_adaptor instanceof FileWriter) {
326 try {
327 byte[] bytes = buffer.getData();
328 m_outStream.write(bytes, offset, len);
329 } catch (DoesNotExistException e) {
330 try {m_outStream.close();} catch (IOException e1) {
331 throw new NoSuccessException(e);
332 } catch (IOException e) {
333 try {m_outStream.close();} catch (IOException e1) {
334 throw new SagaIOException(e);
335 }
336 return len;
337 } else {
338 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
339 }
340 }
341 public int writeSync(Buffer buffer, int len) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
342 return this.writeSync(buffer, 0, len);
343 }
344 public int writeSync(Buffer buffer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
345 return this.writeSync(buffer, 0, buffer.getSize());
346 }
347
348 public long seekSync(long offset, SeekMode whence) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
349 throw new NotImplementedException("Not implemented by the SAGA engine", this);
350 }
351
352 public void readVSync(IOVec[] iovecs) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
353 throw new NotImplementedException("Not implemented by the SAGA engine", this);
354 }
355
356 public void writeVSync(IOVec[] iovecs) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
357 throw new NotImplementedException("Not implemented by the SAGA engine", this);
358 }
359
360 public int sizePSync(String pattern) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, IncorrectStateException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
361 throw new NotImplementedException("Not implemented by the SAGA engine", this);
362 }
363
364 public int readPSync(String pattern, Buffer buffer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
365 throw new NotImplementedException("Not implemented by the SAGA engine", this);
366 }
367
368 public int writePSync(String pattern, Buffer buffer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
369 throw new NotImplementedException("Not implemented by the SAGA engine", this);
370 }
371
372 public List<String> modesESync() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
373 throw new NotImplementedException("Not implemented by the SAGA engine", this);
374 }
375
376 public int sizeESync(String emode, String spec) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, IncorrectStateException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
377 throw new NotImplementedException("Not implemented by the SAGA engine", this);
378 }
379
380 public int readESync(String emode, String spec, Buffer buffer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
381 throw new NotImplementedException("Not implemented by the SAGA engine", this);
382 }
383
384 public int writeESync(String emode, String spec, Buffer buffer) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, SagaIOException {
385 throw new NotImplementedException("Not implemented by the SAGA engine", this);
386 }
387
388
389
390 public FileInputStream getFileInputStream() {
391 return m_inStream;
392 }
393
394 public FileOutputStream getFileOutputStream() {
395 return m_outStream;
396 }
397
398 public FileInputStream newFileInputStream() throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
399 return FileFactoryImpl.openFileInputStream(m_session, m_url, m_adaptor);
400 }
401
402 public FileOutputStream newFileOutputStream(boolean overwrite) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
403 boolean append = false;
404 boolean exclusive = !overwrite;
405 return FileFactoryImpl.openFileOutputStream(m_session, m_url, m_adaptor, append, exclusive);
406 }
407 }