KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > file > TransactedFileSession


1 /*
2  * $Id: TransactedFileSession.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.apache.commons.io.IOUtils;
14 import org.mule.util.xa.AbstractXAResourceManager;
15 import org.mule.util.xa.DefaultXASession;
16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.RandomAccessFile JavaDoc;
24
25 /**
26  * todo document
27  *
28  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
29  * @version $Revision: 3798 $
30  */

31 public class TransactedFileSession extends DefaultXASession implements FileSession
32 {
33
34     public TransactedFileSession(AbstractXAResourceManager resourceManager)
35     {
36         super(resourceManager);
37     }
38
39     /*
40      * (non-Javadoc)
41      *
42      * @see org.mule.transaction.xa.file.FileSession#openInputStream(java.io.File)
43      */

44     public FileInputStream JavaDoc openInputStream(File JavaDoc f) throws IOException JavaDoc
45     {
46         if (localContext != null)
47         {
48             // TODO
49
return null;
50         }
51         else
52         {
53             return new FileInputStream JavaDoc(f);
54         }
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File,
61      * boolean)
62      */

63     public FileOutputStream JavaDoc openOutputStream(File JavaDoc f, boolean append) throws IOException JavaDoc
64     {
65         if (localContext != null)
66         {
67             // TODO
68
return null;
69         }
70         else
71         {
72             return new FileOutputStream JavaDoc(f, append);
73         }
74     }
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File)
80      */

81     public FileOutputStream JavaDoc openOutputStream(File JavaDoc f) throws IOException JavaDoc
82     {
83         return openOutputStream(f, false);
84     }
85
86     /*
87      * (non-Javadoc)
88      *
89      * @see org.mule.transaction.xa.file.FileSession#mkdir(java.io.File)
90      */

91     public boolean mkdir(File JavaDoc f) throws IOException JavaDoc
92     {
93         if (localContext != null)
94         {
95             // TODO
96
return false;
97         }
98         else
99         {
100             return f.mkdir();
101         }
102     }
103
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.mule.transaction.xa.file.FileSession#openRandomAccess(java.io.File,
108      * java.lang.String)
109      */

110     public RandomAccessFile JavaDoc openRandomAccess(File JavaDoc f, String JavaDoc mode) throws IOException JavaDoc
111     {
112         if (localContext != null)
113         {
114             // TODO
115
return null;
116         }
117         else
118         {
119             return new RandomAccessFile JavaDoc(f, mode);
120         }
121     }
122
123     /*
124      * (non-Javadoc)
125      *
126      * @see org.mule.transaction.xa.file.FileSession#delete(java.io.File)
127      */

128     public void delete(File JavaDoc f) throws IOException JavaDoc
129     {
130         if (localContext != null)
131         {
132             // TODO
133
}
134         else
135         {
136             if (!f.delete())
137             {
138                 throw new DeleteException(f);
139             }
140         }
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.mule.transaction.xa.file.FileSession#copy(java.io.File, java.io.File)
147      */

148     public void copy(File JavaDoc source, File JavaDoc dest) throws IOException JavaDoc
149     {
150         if (dest.exists())
151         {
152             delete(dest);
153         }
154         InputStream JavaDoc is = null;
155         OutputStream JavaDoc os = null;
156         try
157         {
158             is = openInputStream(source);
159             try
160             {
161                 os = openOutputStream(dest);
162                 IOUtils.copy(is, os);
163             }
164             finally
165             {
166                 IOUtils.closeQuietly(os);
167             }
168         }
169         finally
170         {
171             IOUtils.closeQuietly(is);
172         }
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see org.mule.transaction.xa.file.FileSession#rename(java.io.File,
179      * java.io.File)
180      */

181     public void rename(File JavaDoc source, File JavaDoc dest) throws IOException JavaDoc
182     {
183         copy(source, dest);
184         delete(dest);
185     }
186
187 }
188
Popular Tags