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 | 0 | super(resourceManager); |
35 | 0 | } |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public FileInputStream openInputStream(File f) throws IOException |
43 | |
{ |
44 | 0 | if (localContext != null) |
45 | |
{ |
46 | |
|
47 | 0 | return null; |
48 | |
} |
49 | |
else |
50 | |
{ |
51 | 0 | 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 | 0 | if (localContext != null) |
64 | |
{ |
65 | |
|
66 | 0 | return null; |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 0 | return new FileOutputStream(f, append); |
71 | |
} |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public FileOutputStream openOutputStream(File f) throws IOException |
80 | |
{ |
81 | 0 | return openOutputStream(f, false); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public boolean mkdir(File f) throws IOException |
90 | |
{ |
91 | 0 | if (localContext != null) |
92 | |
{ |
93 | |
|
94 | 0 | return false; |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | return f.mkdir(); |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public RandomAccessFile openRandomAccess(File f, String mode) throws IOException |
109 | |
{ |
110 | 0 | if (localContext != null) |
111 | |
{ |
112 | |
|
113 | 0 | return null; |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 0 | return new RandomAccessFile(f, mode); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public void delete(File f) throws IOException |
127 | |
{ |
128 | 0 | if (localContext != null) |
129 | |
{ |
130 | |
|
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | if (!f.delete()) |
135 | |
{ |
136 | 0 | throw new DeleteException(f); |
137 | |
} |
138 | |
} |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public void copy(File source, File dest) throws IOException |
147 | |
{ |
148 | 0 | if (dest.exists()) |
149 | |
{ |
150 | 0 | delete(dest); |
151 | |
} |
152 | 0 | InputStream is = null; |
153 | 0 | OutputStream os = null; |
154 | |
try |
155 | |
{ |
156 | 0 | is = openInputStream(source); |
157 | |
try |
158 | |
{ |
159 | 0 | os = openOutputStream(dest); |
160 | 0 | IOUtils.copy(is, os); |
161 | |
} |
162 | |
finally |
163 | |
{ |
164 | 0 | IOUtils.closeQuietly(os); |
165 | 0 | } |
166 | |
} |
167 | |
finally |
168 | |
{ |
169 | 0 | IOUtils.closeQuietly(is); |
170 | 0 | } |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public void rename(File source, File dest) throws IOException |
180 | |
{ |
181 | 0 | copy(source, dest); |
182 | 0 | delete(dest); |
183 | 0 | } |
184 | |
|
185 | |
} |