1
2
3
4
5
6
7
8
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
28
29
30
31
32 public class TransactedFileSession extends DefaultXASession implements FileSession
33 {
34
35 public TransactedFileSession(AbstractXAResourceManager resourceManager)
36 {
37 super(resourceManager);
38 }
39
40
41
42
43
44
45 public FileInputStream openInputStream(File f) throws IOException
46 {
47 if (localContext != null)
48 {
49
50 return null;
51 }
52 else
53 {
54 return new FileInputStream(f);
55 }
56 }
57
58
59
60
61
62
63
64 public FileOutputStream openOutputStream(File f, boolean append) throws IOException
65 {
66 if (localContext != null)
67 {
68
69 return null;
70 }
71 else
72 {
73 return new FileOutputStream(f, append);
74 }
75 }
76
77
78
79
80
81
82 public FileOutputStream openOutputStream(File f) throws IOException
83 {
84 return openOutputStream(f, false);
85 }
86
87
88
89
90
91
92 public boolean mkdir(File f) throws IOException
93 {
94 if (localContext != null)
95 {
96
97 return false;
98 }
99 else
100 {
101 return f.mkdir();
102 }
103 }
104
105
106
107
108
109
110
111 public RandomAccessFile openRandomAccess(File f, String mode) throws IOException
112 {
113 if (localContext != null)
114 {
115
116 return null;
117 }
118 else
119 {
120 return new RandomAccessFile(f, mode);
121 }
122 }
123
124
125
126
127
128
129 public void delete(File f) throws IOException
130 {
131 if (localContext != null)
132 {
133
134 }
135 else
136 {
137 if (!f.delete())
138 {
139 throw new DeleteException(f);
140 }
141 }
142 }
143
144
145
146
147
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
178
179
180
181
182 public void rename(File source, File dest) throws IOException
183 {
184 copy(source, dest);
185 delete(dest);
186 }
187
188 }