View Javadoc

1   /*
2    * $Id: TransactedFileSession.java 7963 2007-08-21 08:53:15Z 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  public class TransactedFileSession extends DefaultXASession implements FileSession
30  {
31  
32      public TransactedFileSession(AbstractXAResourceManager resourceManager)
33      {
34          super(resourceManager);
35      }
36  
37      /*
38       * (non-Javadoc)
39       * 
40       * @see org.mule.transaction.xa.file.FileSession#openInputStream(java.io.File)
41       */
42      public FileInputStream openInputStream(File f) throws IOException
43      {
44          if (localContext != null)
45          {
46              // TODO
47              return null;
48          }
49          else
50          {
51              return new FileInputStream(f);
52          }
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File,
59       *      boolean)
60       */
61      public FileOutputStream openOutputStream(File f, boolean append) throws IOException
62      {
63          if (localContext != null)
64          {
65              // TODO
66              return null;
67          }
68          else
69          {
70              return new FileOutputStream(f, append);
71          }
72      }
73  
74      /*
75       * (non-Javadoc)
76       * 
77       * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File)
78       */
79      public FileOutputStream openOutputStream(File f) throws IOException
80      {
81          return openOutputStream(f, false);
82      }
83  
84      /*
85       * (non-Javadoc)
86       * 
87       * @see org.mule.transaction.xa.file.FileSession#mkdir(java.io.File)
88       */
89      public boolean mkdir(File f) throws IOException
90      {
91          if (localContext != null)
92          {
93              // TODO
94              return false;
95          }
96          else
97          {
98              return f.mkdir();
99          }
100     }
101 
102     /*
103      * (non-Javadoc)
104      * 
105      * @see org.mule.transaction.xa.file.FileSession#openRandomAccess(java.io.File,
106      *      java.lang.String)
107      */
108     public RandomAccessFile openRandomAccess(File f, String mode) throws IOException
109     {
110         if (localContext != null)
111         {
112             // TODO
113             return null;
114         }
115         else
116         {
117             return new RandomAccessFile(f, mode);
118         }
119     }
120 
121     /*
122      * (non-Javadoc)
123      * 
124      * @see org.mule.transaction.xa.file.FileSession#delete(java.io.File)
125      */
126     public void delete(File f) throws IOException
127     {
128         if (localContext != null)
129         {
130             // TODO
131         }
132         else
133         {
134             if (!f.delete())
135             {
136                 throw new DeleteException(f);
137             }
138         }
139     }
140 
141     /*
142      * (non-Javadoc)
143      * 
144      * @see org.mule.transaction.xa.file.FileSession#copy(java.io.File, java.io.File)
145      */
146     public void copy(File source, File dest) throws IOException
147     {
148         if (dest.exists())
149         {
150             delete(dest);
151         }
152         InputStream is = null;
153         OutputStream os = null;
154         try
155         {
156             is = openInputStream(source);
157             try
158             {
159                 os = openOutputStream(dest);
160                 IOUtils.copy(is, os);
161             }
162             finally
163             {
164                 IOUtils.closeQuietly(os);
165             }
166         }
167         finally
168         {
169             IOUtils.closeQuietly(is);
170         }
171     }
172 
173     /*
174      * (non-Javadoc)
175      * 
176      * @see org.mule.transaction.xa.file.FileSession#rename(java.io.File,
177      *      java.io.File)
178      */
179     public void rename(File source, File dest) throws IOException
180     {
181         copy(source, dest);
182         delete(dest);
183     }
184 
185 }