1 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 ; 18 import java.io.FileInputStream ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.RandomAccessFile ; 24 25 31 public class TransactedFileSession extends DefaultXASession implements FileSession 32 { 33 34 public TransactedFileSession(AbstractXAResourceManager resourceManager) 35 { 36 super(resourceManager); 37 } 38 39 44 public FileInputStream openInputStream(File f) throws IOException 45 { 46 if (localContext != null) 47 { 48 return null; 50 } 51 else 52 { 53 return new FileInputStream (f); 54 } 55 } 56 57 63 public FileOutputStream openOutputStream(File f, boolean append) throws IOException 64 { 65 if (localContext != null) 66 { 67 return null; 69 } 70 else 71 { 72 return new FileOutputStream (f, append); 73 } 74 } 75 76 81 public FileOutputStream openOutputStream(File f) throws IOException 82 { 83 return openOutputStream(f, false); 84 } 85 86 91 public boolean mkdir(File f) throws IOException 92 { 93 if (localContext != null) 94 { 95 return false; 97 } 98 else 99 { 100 return f.mkdir(); 101 } 102 } 103 104 110 public RandomAccessFile openRandomAccess(File f, String mode) throws IOException 111 { 112 if (localContext != null) 113 { 114 return null; 116 } 117 else 118 { 119 return new RandomAccessFile (f, mode); 120 } 121 } 122 123 128 public void delete(File f) throws IOException 129 { 130 if (localContext != null) 131 { 132 } 134 else 135 { 136 if (!f.delete()) 137 { 138 throw new DeleteException(f); 139 } 140 } 141 } 142 143 148 public void copy(File source, File dest) throws IOException 149 { 150 if (dest.exists()) 151 { 152 delete(dest); 153 } 154 InputStream is = null; 155 OutputStream 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 181 public void rename(File source, File dest) throws IOException 182 { 183 copy(source, dest); 184 delete(dest); 185 } 186 187 } 188 | Popular Tags |