commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ozeigerm...@apache.org
Subject svn commit: r560766 - in /commons/proper/transaction/branches/TRANSACTION_2/src: java/org/apache/commons/transaction/file/ java/org/apache/commons/transaction/util/ test/org/apache/commons/transaction/file/
Date Sun, 29 Jul 2007 17:23:49 GMT
Author: ozeigermann
Date: Sun Jul 29 10:23:48 2007
New Revision: 560766

URL: http://svn.apache.org/viewvc?view=rev&rev=560766
Log:
First complete version of tx file manager including test and undomanager (yet untested)

Added:
    commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/TxFileResourceManagerTest.java
Modified:
    commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
    commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
    commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
    commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java

Modified: commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
URL: http://svn.apache.org/viewvc/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java?view=diff&rev=560766&r1=560765&r2=560766
==============================================================================
--- commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
(original)
+++ commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/FileResourceUndoManager.java
Sun Jul 29 10:23:48 2007
@@ -5,7 +5,7 @@
 public interface FileResourceUndoManager {
 
     public enum Code {
-        DELETED_DIRECTORY, CONTENT_CHANGED, CREATED_DIRECTORY, CREATED_FILE
+        DELETED_DIRECTORY, UPDATED_CONTENT, CREATED
     }
     
     public void startRecord();
@@ -14,11 +14,10 @@
 
     public void forgetRecord();
 
-    public void recordCreateAsDirectory(File directory);
-    public void recordCreateAsFile(File file);
+    public void recordCreate(File file);
 
     public void recordDelete(File file);
 
-    public void recordChangeContent(File file);
+    public void recordUpdate(File file);
 
 }

Modified: commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
URL: http://svn.apache.org/viewvc/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java?view=diff&rev=560766&r1=560765&r2=560766
==============================================================================
--- commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
(original)
+++ commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/MemoryUndoManager.java
Sun Jul 29 10:23:48 2007
@@ -1,9 +1,7 @@
 package org.apache.commons.transaction.file;
 
-import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -16,45 +14,15 @@
 public class MemoryUndoManager implements FileResourceUndoManager {
 
     private Log logger = LogFactory.getLog(getClass());
+    
     protected ThreadLocal<List<UndoRecord>> localRecords = new ThreadLocal<List<UndoRecord>>();
 
     
-    public void recordChangeContent(File file) {
-        UndoRecord record = new UndoRecord();
-        record.code = Code.CONTENT_CHANGED;
-        record.file = file;
-        try {
-            record.oldConent = new ByteArrayInputStream(FileHelper.readInto(file));
-        } catch (IOException e) {
-            logger.fatal("Could not store changed content for "+file);
-            // FIXME: This really should cause an error
-        }
-        storeRecord(record);
-    }
+    private final File logDirectory;
 
-    public void recordCreateAsDirectory(File directory) {
-        UndoRecord record = new UndoRecord();
-        record.code = Code.CREATED_DIRECTORY;
-        record.file = directory;
-        storeRecord(record);
-    }
-
-    public void recordCreateAsFile(File file) {
-        UndoRecord record = new UndoRecord();
-        record.code = Code.CREATED_FILE;
-        record.file = file;
-        storeRecord(record);
-    }
-
-    public void recordDelete(File file) {
-        if (file.isFile()) {
-            recordChangeContent(file);
-        } else {
-            UndoRecord record = new UndoRecord();
-            record.code = Code.DELETED_DIRECTORY;
-            record.file = file;
-            storeRecord(record);
-        }
+    public MemoryUndoManager(String logDir) throws IOException {
+        logDirectory = new File(logDir);
+        logDirectory.mkdirs();
     }
 
     public void startRecord() {
@@ -78,23 +46,71 @@
         records.add(record);
     }
 
-    protected static class UndoRecord {
+    public void recordUpdate(File file) {
+        recordUpdate(file, false);
+    }
+
+    protected void recordUpdate(File file, boolean moveAllowed) {
+        try {
+            new UndoRecord(Code.UPDATED_CONTENT, file, FileHelper.makeBackup(file, logDirectory,
+                    moveAllowed));
+        } catch (IOException e) {
+            // FIXME: This really is fatal: How to signal?
+            logger.fatal("Can not record content update", e);
+        }
+    }
+
+    public void recordCreate(File file) {
+        new UndoRecord(Code.CREATED, file);
+    }
+
+    public void recordDelete(File file) {
+        if (file.isFile()) {
+            recordUpdate(file, true);
+        } else {
+            new UndoRecord(Code.DELETED_DIRECTORY, file);
+        }
+    }
+
+    protected class UndoRecord {
         Code code;
 
         File file;
 
-        File to;
+        File updatedFile;
+
+        public UndoRecord(Code code, File file) {
+            this(code, file, null);
+        }
+
+        public UndoRecord(Code code, File file, File updatedFile) {
+            this.code = code;
+            this.file = file;
+            this.updatedFile = updatedFile;
+            save();
+        }
 
-        InputStream oldConent;
+        protected void save() {
+        }
 
-        // FIXME: Needs implementation (not that hard)
-        // ugly c-style - who cares?
         public void undo() {
-            // TODO
             switch (code) {
-            
+            case DELETED_DIRECTORY:
+                file.mkdirs();
+                break;
+            case CREATED:
+                file.delete();
+                break;
+            case UPDATED_CONTENT:
+                try {
+                        FileHelper.move(updatedFile, file);
+                    } catch (IOException e) {
+                        // FIXME: This really is fatal: How to signal?
+                        logger.fatal("Can not undo content update", e);
+                    }
+                break;
             }
-            
+
         }
     }
 

Modified: commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
URL: http://svn.apache.org/viewvc/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java?view=diff&rev=560766&r1=560765&r2=560766
==============================================================================
--- commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
(original)
+++ commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java
Sun Jul 29 10:23:48 2007
@@ -45,8 +45,6 @@
 
     private Log logger = LogFactory.getLog(getClass());
 
-    protected String contextFileName = "transaction.log";
-
     protected String rootPath;
 
     protected FileResourceManager wrapped;
@@ -61,10 +59,6 @@
         wrapped = new FileResourceManager(rootPath);
     }
 
-    public void setContextFileName(String contextFile) {
-        this.contextFileName = contextFile;
-    }
-
     @Override
     protected FileTxContext createContext() {
         return new FileTxContext();
@@ -140,13 +134,13 @@
 
             public void createAsDirectory() throws ResourceException {
                 writeLock();
-                getUndoManager().recordCreateAsDirectory(getFile());
+                getUndoManager().recordCreate(getFile());
                 super.createAsDirectory();
             }
 
             public void createAsFile() throws ResourceException {
                 writeLock();
-                getUndoManager().recordCreateAsFile(getFile());
+                getUndoManager().recordCreate(getFile());
                 super.createAsFile();
             }
 
@@ -213,7 +207,7 @@
             protected void copyNonRecursive(TxFileResource target) throws ResourceException
{
                 readLock();
                 target.writeLock();
-                getUndoManager().recordCreateAsFile(target.getFile());
+                getUndoManager().recordCreate(target.getFile());
                 try {
                     FileHelper.copy(getFile(), target.getFile());
                 } catch (IOException e) {
@@ -225,7 +219,7 @@
                 writeLock();
                 target.writeLock();
                 getUndoManager().recordDelete(getFile());
-                getUndoManager().recordCreateAsFile(target.getFile());
+                getUndoManager().recordCreate(target.getFile());
                 try {
                     FileHelper.move(getFile(), target.getFile());
                 } catch (IOException e) {
@@ -318,7 +312,7 @@
 
             public OutputStream writeStream(boolean append) throws ResourceException {
                 writeLock();
-                getUndoManager().recordChangeContent(getFile());
+                getUndoManager().recordUpdate(getFile());
                 OutputStream os = super.writeStream(append);
                 registerStream(os);
                 return os;
@@ -365,7 +359,7 @@
         return rootPath;
     }
 
-    public FileResourceUndoManager getUndoManager() {
+    protected FileResourceUndoManager getUndoManager() {
         return undoManager;
     }
 

Modified: commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java?view=diff&rev=560766&r1=560765&r2=560766
==============================================================================
--- commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
(original)
+++ commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/FileHelper.java
Sun Jul 29 10:23:48 2007
@@ -32,7 +32,7 @@
 public final class FileHelper {
 
     public static File makeBackup(File file, File backupDirectory, boolean moveAllowed) throws
IOException {
-        File copy = File.createTempFile("ctx2", ".backup", backupDirectory);
+        File copy = File.createTempFile(file.getName(), ".backup", backupDirectory);
         boolean success = false;
         if (moveAllowed) {
             success = file.renameTo(copy);

Added: commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/TxFileResourceManagerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/TxFileResourceManagerTest.java?view=auto&rev=560766
==============================================================================
--- commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/TxFileResourceManagerTest.java
(added)
+++ commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/TxFileResourceManagerTest.java
Sun Jul 29 10:23:48 2007
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.transaction.file;
+
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.commons.transaction.file.FileResourceManager.FileResource;
+import org.apache.commons.transaction.locking.LockManager;
+import org.apache.commons.transaction.locking.RWLockManager;
+import org.junit.Test;
+
+public class TxFileResourceManagerTest {
+    public static junit.framework.Test suite() {
+        return new JUnit4TestAdapter(TxFileResourceManagerTest.class);
+    }
+
+    @Test
+    public void basic() {
+        TxFileResourceManager manager = new TxFileResourceManager("TxFileManager", "d:/tmp/content");
+        LockManager lm = new RWLockManager<String, String>();
+        FileResourceUndoManager um;
+        try {
+            um = new MemoryUndoManager("d:/tmp/txlogs");
+            manager.setLm(lm);
+            manager.setUndoManager(um);
+            manager.startTransaction(60, TimeUnit.SECONDS);
+            FileResource file = manager.getResource("d:/tmp/content/aha");
+            file.createAsFile();
+            OutputStream os = file.writeStream(false);
+            PrintStream ps = new PrintStream(os);
+            ps.print("Huhu");
+            manager.commitTransaction();
+        } catch (Throwable throwable) {
+            manager.rollbackTransaction();
+        }
+
+    }
+
+    public static void main(String[] args) {
+        new TxFileResourceManagerTest().basic();
+    }
+}



Mime
View raw message