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