View Javadoc

1   /*
2    * $Id: TransactedFileSession.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util.file;
12  
13  import org.mule.util.xa.AbstractXAResourceManager;
14  import org.mule.util.xa.DefaultXASession;
15  
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.FileOutputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.io.RandomAccessFile;
23  
24  import org.apache.commons.io.IOUtils;
25  
26  /**
27   * todo document
28   * 
29   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30   * @version $Revision: 7976 $
31   */
32  public class TransactedFileSession extends DefaultXASession implements FileSession
33  {
34  
35      public TransactedFileSession(AbstractXAResourceManager resourceManager)
36      {
37          super(resourceManager);
38      }
39  
40      /*
41       * (non-Javadoc)
42       * 
43       * @see org.mule.transaction.xa.file.FileSession#openInputStream(java.io.File)
44       */
45      public FileInputStream openInputStream(File f) throws IOException
46      {
47          if (localContext != null)
48          {
49              // TODO
50              return null;
51          }
52          else
53          {
54              return new FileInputStream(f);
55          }
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File,
62       *      boolean)
63       */
64      public FileOutputStream openOutputStream(File f, boolean append) throws IOException
65      {
66          if (localContext != null)
67          {
68              // TODO
69              return null;
70          }
71          else
72          {
73              return new FileOutputStream(f, append);
74          }
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File)
81       */
82      public FileOutputStream openOutputStream(File f) throws IOException
83      {
84          return openOutputStream(f, false);
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see org.mule.transaction.xa.file.FileSession#mkdir(java.io.File)
91       */
92      public boolean mkdir(File f) throws IOException
93      {
94          if (localContext != null)
95          {
96              // TODO
97              return false;
98          }
99          else
100         {
101             return f.mkdir();
102         }
103     }
104 
105     /*
106      * (non-Javadoc)
107      * 
108      * @see org.mule.transaction.xa.file.FileSession#openRandomAccess(java.io.File,
109      *      java.lang.String)
110      */
111     public RandomAccessFile openRandomAccess(File f, String mode) throws IOException
112     {
113         if (localContext != null)
114         {
115             // TODO
116             return null;
117         }
118         else
119         {
120             return new RandomAccessFile(f, mode);
121         }
122     }
123 
124     /*
125      * (non-Javadoc)
126      * 
127      * @see org.mule.transaction.xa.file.FileSession#delete(java.io.File)
128      */
129     public void delete(File f) throws IOException
130     {
131         if (localContext != null)
132         {
133             // TODO
134         }
135         else
136         {
137             if (!f.delete())
138             {
139                 throw new DeleteException(f);
140             }
141         }
142     }
143 
144     /*
145      * (non-Javadoc)
146      * 
147      * @see org.mule.transaction.xa.file.FileSession#copy(java.io.File, java.io.File)
148      */
149     public void copy(File source, File dest) throws IOException
150     {
151         if (dest.exists())
152         {
153             delete(dest);
154         }
155         InputStream is = null;
156         OutputStream os = null;
157         try
158         {
159             is = openInputStream(source);
160             try
161             {
162                 os = openOutputStream(dest);
163                 IOUtils.copy(is, os);
164             }
165             finally
166             {
167                 IOUtils.closeQuietly(os);
168             }
169         }
170         finally
171         {
172             IOUtils.closeQuietly(is);
173         }
174     }
175 
176     /*
177      * (non-Javadoc)
178      * 
179      * @see org.mule.transaction.xa.file.FileSession#rename(java.io.File,
180      *      java.io.File)
181      */
182     public void rename(File source, File dest) throws IOException
183     {
184         copy(source, dest);
185         delete(dest);
186     }
187 
188 }