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