View Javadoc

1   package fr.in2p3.jsaga.impl.file.stream;
2   
3   import fr.in2p3.jsaga.impl.task.AbstractThreadedTask;
4   import org.ogf.saga.SagaObject;
5   import org.ogf.saga.error.*;
6   import org.ogf.saga.file.FileOutputStream;
7   import org.ogf.saga.session.Session;
8   import org.ogf.saga.task.Task;
9   import org.ogf.saga.task.TaskMode;
10  
11  import java.io.IOException;
12  import java.util.UUID;
13  
14  /* ***************************************************
15  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16  * ***             http://cc.in2p3.fr/             ***
17  * ***************************************************
18  * File:   AbstractAsyncFileOutputStreamImpl
19  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20  * Date:   20 mars 2008
21  * ***************************************************
22  * Description:                                      */
23  /**
24   *
25   */
26  public abstract class AbstractAsyncFileOutputStreamImpl extends FileOutputStream {
27      private Session m_session;
28      private UUID m_uuid;
29  
30      /** constructor */
31      public AbstractAsyncFileOutputStreamImpl(Session session) {
32          m_session = session;
33          m_uuid = UUID.randomUUID();
34      }
35  
36      /** clone */
37      public SagaObject clone() throws CloneNotSupportedException {
38          AbstractAsyncFileOutputStreamImpl clone = (AbstractAsyncFileOutputStreamImpl) super.clone();
39          clone.m_session = m_session;
40          clone.m_uuid = UUID.randomUUID();
41          return clone;
42      }
43  
44      /////////////////////////////////// interface SagaObject ////////////////////////////////////
45  
46      public Session getSession() throws DoesNotExistException {
47          if (m_session != null) {
48              return m_session;
49          } else {
50              throw new DoesNotExistException("This object does not have a session attached", this);
51          }
52      }
53  
54      public String getId() {
55          return m_uuid.toString();
56      }
57  
58      ///////////////////////////////// interface FileInputStream /////////////////////////////////
59  
60      public Task<FileOutputStream, Void> write(TaskMode mode, final int b) throws NotImplementedException {
61          return new AbstractThreadedTask<FileOutputStream,Void>(mode) {
62              public Void invoke() throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
63                  try {
64                      AbstractAsyncFileOutputStreamImpl.this.write(b);
65                      return null;
66                  } catch (IOException e) {
67                      throw new NoSuccessException(e);
68                  }
69              }
70          };
71      }
72  
73      public Task<FileOutputStream, Void> write(TaskMode mode, final byte[] buf, final int off, final int len) throws NotImplementedException {
74          return new AbstractThreadedTask<FileOutputStream,Void>(mode) {
75              public Void invoke() throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
76                  try {
77                      AbstractAsyncFileOutputStreamImpl.this.write(buf, off, len);
78                      return null;
79                  } catch (IOException e) {
80                      throw new NoSuccessException(e);
81                  }
82              }
83          };
84      }
85  
86      public Task<FileOutputStream, Void> flush(TaskMode mode) throws NotImplementedException {
87          return new AbstractThreadedTask<FileOutputStream,Void>(mode) {
88              public Void invoke() throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
89                  try {
90                      AbstractAsyncFileOutputStreamImpl.this.flush();
91                      return null;
92                  } catch (IOException e) {
93                      throw new NoSuccessException(e);
94                  }
95              }
96          };
97      }
98  
99      public Task<FileOutputStream, Void> close(TaskMode mode) throws NotImplementedException {
100         return new AbstractThreadedTask<FileOutputStream,Void>(mode) {
101             public Void invoke() throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
102                 try {
103                     AbstractAsyncFileOutputStreamImpl.this.close();
104                     return null;
105                 } catch (IOException e) {
106                     throw new NoSuccessException(e);
107                 }
108             }
109         };
110     }
111 }