001: /*
002: * $Id: TransactedFileSession.java 8077 2007-08-27 20:15:25Z aperepel $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.util.file;
012:
013: import org.mule.util.xa.AbstractXAResourceManager;
014: import org.mule.util.xa.DefaultXASession;
015:
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.io.RandomAccessFile;
023:
024: import org.apache.commons.io.IOUtils;
025:
026: /**
027: * TODO document
028: *
029: */
030: public class TransactedFileSession extends DefaultXASession implements
031: FileSession {
032:
033: public TransactedFileSession(
034: AbstractXAResourceManager resourceManager) {
035: super (resourceManager);
036: }
037:
038: /*
039: * (non-Javadoc)
040: *
041: * @see org.mule.transaction.xa.file.FileSession#openInputStream(java.io.File)
042: */
043: public FileInputStream openInputStream(File f) throws IOException {
044: if (localContext != null) {
045: // TODO
046: return null;
047: } else {
048: return new FileInputStream(f);
049: }
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File,
056: * boolean)
057: */
058: public FileOutputStream openOutputStream(File f, boolean append)
059: throws IOException {
060: if (localContext != null) {
061: // TODO
062: return null;
063: } else {
064: return new FileOutputStream(f, append);
065: }
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.mule.transaction.xa.file.FileSession#openOutputStream(java.io.File)
072: */
073: public FileOutputStream openOutputStream(File f) throws IOException {
074: return openOutputStream(f, false);
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.mule.transaction.xa.file.FileSession#mkdir(java.io.File)
081: */
082: public boolean mkdir(File f) throws IOException {
083: if (localContext != null) {
084: // TODO
085: return false;
086: } else {
087: return f.mkdir();
088: }
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see org.mule.transaction.xa.file.FileSession#openRandomAccess(java.io.File,
095: * java.lang.String)
096: */
097: public RandomAccessFile openRandomAccess(File f, String mode)
098: throws IOException {
099: if (localContext != null) {
100: // TODO
101: return null;
102: } else {
103: return new RandomAccessFile(f, mode);
104: }
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see org.mule.transaction.xa.file.FileSession#delete(java.io.File)
111: */
112: public void delete(File f) throws IOException {
113: if (localContext != null) {
114: // TODO
115: } else {
116: if (!f.delete()) {
117: throw new DeleteException(f);
118: }
119: }
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.mule.transaction.xa.file.FileSession#copy(java.io.File, java.io.File)
126: */
127: public void copy(File source, File dest) throws IOException {
128: if (dest.exists()) {
129: delete(dest);
130: }
131: InputStream is = null;
132: OutputStream os = null;
133: try {
134: is = openInputStream(source);
135: try {
136: os = openOutputStream(dest);
137: IOUtils.copy(is, os);
138: } finally {
139: IOUtils.closeQuietly(os);
140: }
141: } finally {
142: IOUtils.closeQuietly(is);
143: }
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see org.mule.transaction.xa.file.FileSession#rename(java.io.File,
150: * java.io.File)
151: */
152: public void rename(File source, File dest) throws IOException {
153: copy(source, dest);
154: delete(dest);
155: }
156:
157: }
|