ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xav...@apache.org
Subject svn commit: r671566 - in /ant/ivy/core/trunk: src/java/org/apache/ivy/plugins/repository/file/FileRepository.java src/java/org/apache/ivy/plugins/resolver/FileSystemResolver.java test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java
Date Wed, 25 Jun 2008 14:32:22 GMT
Author: xavier
Date: Wed Jun 25 07:32:22 2008
New Revision: 671566

URL: http://svn.apache.org/viewvc?rev=671566&view=rev
Log:
add more test and improve atomic publish support

Modified:
    ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/file/FileRepository.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/FileSystemResolver.java
    ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/file/FileRepository.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/file/FileRepository.java?rev=671566&r1=671565&r2=671566&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/file/FileRepository.java
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/file/FileRepository.java
Wed Jun 25 07:32:22 2008
@@ -57,12 +57,16 @@
         copy(source, getFile(destination), overwrite);
     }
     
-    public void move(File src, File dest) {
-        src.renameTo(dest);
+    public void move(File src, File dest) throws IOException {
+        if (!src.renameTo(dest)) {
+            throw new IOException("impossible to move '" + src + "' to '" + dest + "'");
+        }
     }
 
-    public void delete(File f) {
-        FileUtil.forceDelete(f);
+    public void delete(File f) throws IOException {
+        if (!FileUtil.forceDelete(f)) {
+            throw new IOException("impossible to delete '" + f + "'");
+        }
     }
 
     private void copy(File src, File destination, boolean overwrite) throws IOException {

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/FileSystemResolver.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/FileSystemResolver.java?rev=671566&r1=671565&r2=671566&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/FileSystemResolver.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/FileSystemResolver.java Wed
Jun 25 07:32:22 2008
@@ -66,10 +66,6 @@
     private Map/*<String,String>*/ fullTransactionPatterns = new HashMap();
 
     /**
-     * Is the current transaction open in overwrite mode?
-     */
-    private boolean overwriteTransaction = false;
-    /**
      * Location where files are published during the transaction
      */
     private File transactionTempDir;
@@ -100,7 +96,7 @@
     
 
     protected String getDestination(String pattern, Artifact artifact, ModuleRevisionId mrid)
{
-        if (supportTransaction() && !overwriteTransaction) {
+        if (supportTransaction() && isTransactionStarted()) {
             
             String destPattern = (String) fullTransactionPatterns.get(pattern);
             if (destPattern == null) {
@@ -117,40 +113,69 @@
         }
     }
 
+    private boolean isTransactionStarted() {
+        return transactionTempDir != null;
+    }
+
     public void abortPublishTransaction() throws IOException {
-        if (supportTransaction() && !overwriteTransaction) {
-            if (transactionTempDir == null) {
+        if (supportTransaction()) {
+            if (!isTransactionStarted()) {
                 throw new IllegalStateException("no current transaction!");
             }
-            getFileRepository().delete(transactionTempDir);
-            Message.info("\tpublish aborted: deleted " + transactionTempDir);
-            closeTransaction();
+            try {
+                getFileRepository().delete(transactionTempDir);
+                Message.info("\tpublish aborted: deleted " + transactionTempDir);
+            } finally {
+                closeTransaction();
+            }
         }
     }
 
     public void commitPublishTransaction() throws IOException {
-        if (supportTransaction() && !overwriteTransaction) {
-            if (transactionTempDir == null) {
+        if (supportTransaction()) {
+            if (!isTransactionStarted()) {
                 throw new IllegalStateException("no current transaction!");
             }
-            getFileRepository().move(transactionTempDir, transactionDestDir);
-            Message.info("\tpublish commited: moved " + transactionTempDir 
-                + " \n\t\tto " + transactionDestDir);
-            closeTransaction();
+            try {
+                getFileRepository().move(transactionTempDir, transactionDestDir);
+                
+                Message.info("\tpublish commited: moved " + transactionTempDir 
+                    + " \n\t\tto " + transactionDestDir);
+            } catch (IOException ex) {
+                IOException commitEx;
+                try {
+                    getFileRepository().delete(transactionTempDir);
+                    commitEx = new IOException(
+                        "publish transaction commit error for " + transactionDestDir 
+                        + ": rolled back");
+                } catch (IOException deleteEx) {
+                    commitEx = new IOException(
+                        "publish transaction commit error for " + transactionDestDir 
+                        + ": rollback impossible either, "
+                        + "please remove " + transactionTempDir + " manually");
+                }
+                commitEx.initCause(ex);
+                throw commitEx;
+            } finally {
+                closeTransaction();
+            }
         }
     }
 
     public void beginPublishTransaction(
             ModuleRevisionId module, boolean overwrite) throws IOException {
         if (supportTransaction()) {
-            if (transactionTempDir != null) {
+            if (isTransactionStarted()) {
                 throw new IllegalStateException("a transaction is already started and not
closed!");
             }
-            overwriteTransaction = overwrite;
-            if (overwriteTransaction) {
+            if (overwrite) {
                 unsupportedTransaction("overwrite transaction not supported yet");
             } else {
                 initTransaction(module);
+                Message.verbose(
+                    "\tstarting transaction: publish during transaction will be done in \n\t\t"

+                    + transactionTempDir 
+                    + "\n\t\tand on commit moved to \n\t\t" + transactionDestDir);
             }
         }
     }
@@ -185,6 +210,7 @@
     
     private void checkSupportTransaction() {
         if (supportTransaction == null) {
+            supportTransaction = Boolean.FALSE;
             List ivyPatterns = getIvyPatterns();
             List artifactPatterns = getArtifactPatterns();
             

Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java?rev=671566&r1=671565&r2=671566&view=diff
==============================================================================
--- ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java
(original)
+++ ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java
Wed Jun 25 07:32:22 2008
@@ -20,6 +20,7 @@
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
+import java.io.IOException;
 import java.util.Date;
 import java.util.GregorianCalendar;
 
@@ -594,6 +595,46 @@
         }
     }
 
+    public void testPublishOverwrite() throws Exception {
+        try {
+            FileSystemResolver resolver = new FileSystemResolver();
+            resolver.setName("test");
+            resolver.setSettings(settings);
+            assertEquals("test", resolver.getName());
+
+            resolver.addIvyPattern(
+                "test/repositories/1/[organisation]/[module]/[revision]/[artifact].[ext]");
+            resolver.addArtifactPattern(
+                 "test/repositories/1/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]");
+
+            File ivyFile = new File("test/repositories/1/myorg/mymodule/myrevision/ivy.xml");
+            File artifactFile = new File("test/repositories/1/myorg/mymodule/myrevision/myartifact-myrevision.myext");
+            touch(ivyFile);
+            touch(artifactFile);
+            
+            ModuleRevisionId mrid = ModuleRevisionId.newInstance("myorg", "mymodule", "myrevision");
+            Artifact ivyArtifact = new DefaultArtifact(mrid, new Date(), "ivy", "ivy", "xml");
+            Artifact artifact = new DefaultArtifact(mrid, new Date(), "myartifact", "mytype",
+                    "myext");
+            File src = new File("test/repositories/ivysettings.xml");
+            resolver.beginPublishTransaction(mrid, true);
+            resolver.publish(ivyArtifact, src, true);
+            resolver.publish(artifact, src, true);
+            resolver.commitPublishTransaction();
+
+            long length = src.length();
+            assertEquals(length, ivyFile.length());
+            assertEquals(length, artifactFile.length());
+        } finally {
+            FileUtil.forceDelete(new File("test/repositories/1/myorg"));
+        }
+    }
+
+    private void touch(File file) throws IOException {
+        file.getParentFile().mkdirs();
+        file.createNewFile();
+    }
+
     public void testPublishTransaction() throws Exception {
         try {
             FileSystemResolver resolver = new FileSystemResolver();
@@ -780,7 +821,7 @@
                 resolver.beginPublishTransaction(mrid, true);
                 
                 resolver.publish(artifact, src, true);
-                fail("publishing with transaction=true and overzrite mode should raise an
exception");
+                fail("publishing with transaction=true and overwrite mode should raise an
exception");
             } catch (IllegalStateException ex) {
                 assertTrue(ex.getMessage().indexOf("transactional") != -1);
             }



Mime
View raw message