Author: ozeigermann
Date: Sat Jul 28 17:10:37 2007
New Revision: 560650
URL: http://svn.apache.org/viewvc?view=rev&rev=560650
Log:
Recursive bulk operations (copy, move, delete) are now properly logged in undo
and lock manager.
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceManager.java
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/ResourceException.java
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/StreamableResource.java
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
Modified: jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
--- jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml
(original)
+++ jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml Sat
Jul 28 17:10:37 2007
@@ -136,6 +136,18 @@
<name>John Rousseau</name>
<email>[EMAIL PROTECTED]</email>
</contributor>
+ <contributor>
+ <name>Peter Fassev</name>
+ <email>fassev at gmx dot de</email>
+ </contributor>
+ <contributor>
+ <name>Dennis Thrysøe</name>
+ <email>dth at conscius dot com</email>
+ </contributor>
+ <contributor>
+ <name>Holger Hoffstätte</name>
+ <email>holger dot hoffstaette at googlemail dot com</email>
+ </contributor>
</contributors>
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceManager.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceManager.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceManager.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceManager.java
Sat Jul 28 17:10:37 2007
@@ -55,6 +55,8 @@
private final File file;
private final String canonicalPath;
+
+ private final String name;
protected static File getFileForResource(StreamableResource resource)
throws ResourceException {
@@ -67,12 +69,7 @@
}
public FileResource(String path) throws ResourceException {
- this.file = new File(path.trim());
- try {
- this.canonicalPath = file.getCanonicalPath();
- } catch (IOException e) {
- throw new ResourceException(e);
- }
+ this(new File(path.trim()));
}
public FileResource(File file) throws ResourceException {
@@ -82,10 +79,11 @@
} catch (IOException e) {
throw new ResourceException(e);
}
+ this.name = file.getName();
}
public void createAsDirectory() throws ResourceException {
- if (!file.mkdirs()) {
+ if (!file.exists() && !file.mkdirs()) {
throw new
ResourceException(ResourceException.Code.COULD_NOT_CREATE,
"Could not create directory");
}
@@ -123,7 +121,7 @@
List<FileResource> result = new ArrayList<FileResource>();
File[] files = file.listFiles();
for (File file : files) {
- result.add(new FileResource(file));
+ result.add(create(file));
}
return result;
}
@@ -135,7 +133,8 @@
* if (getPath().equals(getRootPath())) return null;
*/
File parent = file.getParentFile();
- return new FileResource(parent);
+ if (parent == null) return null;
+ return create(parent);
}
public String getPath() {
@@ -151,7 +150,8 @@
}
public void move(StreamableResource destination) throws
ResourceException {
- moveorCopySaneCheck(destination);
+ if (!prepareMoveorCopy(destination))
+ moveOrCopySaneCheck(destination);
try {
if (isFile()) {
FileHelper.move(file, getFileForResource(destination));
@@ -164,7 +164,8 @@
}
public void copy(StreamableResource destination) throws
ResourceException {
- moveorCopySaneCheck(destination);
+ if (!prepareMoveorCopy(destination))
+ moveOrCopySaneCheck(destination);
try {
if (isFile()) {
FileHelper.copy(file, getFileForResource(destination));
@@ -189,9 +190,7 @@
return false;
}
- protected void moveorCopySaneCheck(StreamableResource destination)
throws ResourceException {
- if (prepareMoveorCopy(destination))
- return;
+ protected void moveOrCopySaneCheck(StreamableResource destination)
throws ResourceException {
File from = getFile();
File to = getFileForResource(destination);
@@ -257,6 +256,19 @@
protected File getFile() {
return file;
+ }
+
+ protected FileResource create(File file) throws ResourceException {
+ return new FileResource(file);
+ }
+
+ public FileResource getChild(String name) throws ResourceException {
+ File child = new File(file, name);
+ return create(child);
+ }
+
+ public String getName() {
+ return name;
}
}
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
Sat Jul 28 17:10:37 2007
@@ -5,7 +5,7 @@
public interface FileResourceUndoManager {
public enum Code {
- CREATED_DIRECTORY, CREATED_FILE, DELETED_DIRECTORY, MOVED, COPIED,
CONTENT_CHANGED, PROPERTY_CHANGED
+ DELETED_DIRECTORY, CONTENT_CHANGED, CREATED_DIRECTORY, CREATED_FILE
}
public void startRecord();
@@ -14,17 +14,10 @@
public void forgetRecord();
- public void recordCopy(File from, File to);
-
public void recordCreateAsDirectory(File directory);
-
public void recordCreateAsFile(File file);
public void recordDelete(File file);
-
- public void recordMove(File from, File to);
-
- public void recordChangeProperty(File file, String name, Object oldValue);
public void recordChangeContent(File file);
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
Sat Jul 28 17:10:37 2007
@@ -32,17 +32,6 @@
storeRecord(record);
}
- public void recordCopy(File from, File to) {
- if (to.exists()) {
- recordChangeContent(to);
- }
- UndoRecord record = new UndoRecord();
- record.code = Code.COPIED;
- record.file = from;
- record.to = to;
- storeRecord(record);
- }
-
public void recordCreateAsDirectory(File directory) {
UndoRecord record = new UndoRecord();
record.code = Code.CREATED_DIRECTORY;
@@ -68,26 +57,6 @@
}
}
- public void recordMove(File from, File to) {
- if (to.exists()) {
- recordChangeContent(to);
- }
- UndoRecord record = new UndoRecord();
- record.code = Code.MOVED;
- record.file = from;
- record.to = to;
- storeRecord(record);
- }
-
- public void recordChangeProperty(File file, String name, Object oldValue) {
- UndoRecord record = new UndoRecord();
- record.code = Code.PROPERTY_CHANGED;
- record.file = file;
- record.propertyName = name;
- record.oldValue = oldValue;
- storeRecord(record);
- }
-
public void startRecord() {
localRecords.set(new ArrayList<UndoRecord>());
}
@@ -115,10 +84,6 @@
File file;
File to;
-
- String propertyName;
-
- Object oldValue;
InputStream oldConent;
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
Sat Jul 28 17:10:37 2007
@@ -37,6 +37,7 @@
import org.apache.commons.transaction.resource.ResourceException;
import org.apache.commons.transaction.resource.ResourceManager;
import org.apache.commons.transaction.resource.StreamableResource;
+import org.apache.commons.transaction.util.FileHelper;
public class TxFileResourceManager extends
AbstractTransactionalResourceManager<TxFileResourceManager.FileTxContext>
implements
@@ -149,10 +150,99 @@
super.createAsFile();
}
- public void delete() throws ResourceException {
+ protected TxFileResource create(File file) throws
ResourceException {
+ return new TxFileResource(file);
+ }
+
+ protected void mkdirs() throws ResourceException {
+ if (exists())
+ return;
+ TxFileResource parent = getParent();
+ if (parent != null) {
+ parent.mkdirs();
+ }
+ createAsDirectory();
+ }
+
+ protected void moveOrcopyRecursive(TxFileResource target, boolean
move)
+ throws ResourceException {
+ moveOrCopySaneCheck(target);
+
+ if (isDirectory()) {
+ target.mkdirs();
+ for (TxFileResource child : getChildren()) {
+ TxFileResource targetChild =
target.getChild(child.getName());
+ if (child.isDirectory()) {
+ targetChild.mkdirs();
+ child.moveOrcopyRecursive(targetChild, move);
+ targetChild.createAsDirectory();
+ } else {
+ if (targetChild.exists()) {
+ targetChild.removeNonRecursive();
+ }
+ moveOrcopyRecursive(targetChild, move);
+ }
+ }
+ } else {
+ // isFile()!
+ if (!target.exists()) {
+ target.getParent().mkdirs();
+ target.createAsFile();
+ }
+ if (!move) {
+ copyNonRecursive(target);
+ } else {
+ moveNonRecursive(target);
+ }
+ }
+ }
+
+ // recursively delete, depth first
+ protected void removeRecursive() throws ResourceException {
+ if (exists()) {
+ if (isDirectory()) {
+ List<? extends TxFileResource> children =
getChildren();
+ for (TxFileResource resource : children) {
+ resource.delete();
+ }
+ }
+ removeNonRecursive();
+ }
+ }
+
+ protected void copyNonRecursive(TxFileResource target) throws
ResourceException {
+ readLock();
+ target.writeLock();
+ getUndoManager().recordCreateAsFile(target.getFile());
+ try {
+ FileHelper.copy(getFile(), target.getFile());
+ } catch (IOException e) {
+ throw new
ResourceException(ResourceException.Code.CANT_MOVE_OR_COPY, e);
+ }
+ }
+
+ protected void moveNonRecursive(TxFileResource target) throws
ResourceException {
writeLock();
+ target.writeLock();
getUndoManager().recordDelete(getFile());
- super.delete();
+ getUndoManager().recordCreateAsFile(target.getFile());
+ try {
+ FileHelper.move(getFile(), target.getFile());
+ } catch (IOException e) {
+ throw new
ResourceException(ResourceException.Code.CANT_MOVE_OR_COPY, e);
+ }
+ }
+
+ protected void removeNonRecursive() throws ResourceException {
+ writeLock();
+ getUndoManager().recordDelete(getFile());
+ if (!getFile().delete()) {
+ throw new
ResourceException(ResourceException.Code.COULD_NOT_DELETE);
+ }
+ }
+
+ public void delete() throws ResourceException {
+ removeRecursive();
}
public boolean exists() {
@@ -160,6 +250,12 @@
return super.exists();
}
+ public TxFileResource getChild(String name) throws
ResourceException {
+ readLock();
+ File child = new File(getFile(), name);
+ return create(child);
+ }
+
public List<? extends TxFileResource> getChildren() throws
ResourceException {
readLock();
@@ -178,6 +274,8 @@
public TxFileResource getParent() throws ResourceException {
readLock();
FileResource parent = super.getParent();
+ if (parent == null)
+ return null;
TxFileResource txFileParent = new
TxFileResource(parent.getFile());
// if we acquire the parent, we want to be sure it really exist
txFileParent.readLock();
@@ -203,20 +301,12 @@
return super.isFile();
}
- public void copy(FileResource destination) throws
ResourceException {
- super.moveorCopySaneCheck(destination);
- readLock();
- new TxFileResource(destination.getFile()).writeLock();
- getUndoManager().recordCopy(getFile(),
getFileForResource(destination));
- super.copy(destination);
+ public void copy(TxFileResource destination) throws
ResourceException {
+ moveOrcopyRecursive(destination, false);
}
- public void move(FileResource destination) throws
ResourceException {
- super.moveorCopySaneCheck(destination);
- writeLock();
- new TxFileResource(destination.getFile()).writeLock();
- getUndoManager().recordMove(getFile(),
getFileForResource(destination));
- super.move(destination);
+ public void move(TxFileResource destination) throws
ResourceException {
+ moveOrcopyRecursive(destination, true);
}
public InputStream readStream() throws ResourceException {
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/ResourceException.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/ResourceException.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/ResourceException.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/ResourceException.java
Sat Jul 28 17:10:37 2007
@@ -16,6 +16,7 @@
*/
package org.apache.commons.transaction.resource;
+import java.io.IOException;
public class ResourceException extends Exception {
@@ -44,6 +45,15 @@
public ResourceException(Code code, String message) {
super(message);
+ this.code = code;
+ }
+
+ public ResourceException(Code code) {
+ this.code = code;
+ }
+
+ public ResourceException(Code code, Throwable cause) {
+ super(cause);
this.code = code;
}
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/StreamableResource.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/StreamableResource.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/StreamableResource.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/resource/StreamableResource.java
Sat Jul 28 17:10:37 2007
@@ -22,6 +22,7 @@
public interface StreamableResource {
String getPath();
+ String getName();
boolean isDirectory();
@@ -30,6 +31,7 @@
List<? extends StreamableResource> getChildren() throws ResourceException;
StreamableResource getParent() throws ResourceException;
+ StreamableResource getChild(String name) throws ResourceException;
InputStream readStream() throws ResourceException;
Modified:
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java?view=diff&rev=560650&r1=560649&r2=560650
==============================================================================
---
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
(original)
+++
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
Sat Jul 28 17:10:37 2007
@@ -31,6 +31,18 @@
*/
public final class FileHelper {
+ public static File makeBackup(File file, File backupDirectory, boolean
moveAllowed) throws IOException {
+ File copy = File.createTempFile("ctx2", ".backup", backupDirectory);
+ boolean success = false;
+ if (moveAllowed) {
+ success = file.renameTo(copy);
+ }
+ if (!success) {
+ copy(file, copy);
+ }
+ return copy;
+ }
+
public static byte[] readInto(File file) throws IOException {
long length = file.length();
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]