boolean result = true;
if ( file.exists() )
{
- // TODO: utility methods in the model converter
File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
String contents = null;
--- /dev/null
+package org.apache.maven.repository.converter.transaction;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Abstract class for the TransactionEvents
+ *
+ * @author Edwin Punzalan
+ */
+public abstract class AbstractTransactionEvent
+ implements TransactionEvent
+{
+ private File backup;
+
+ private List createdDirs;
+
+ /**
+ * Method that creates a directory as well as all the parent directories needed
+ *
+ * @param dir The File directory to be created
+ * @throws IOException when an unrecoverable error occurred
+ */
+ protected void mkDirs( File dir )
+ throws IOException
+ {
+ List createDirs = new ArrayList();
+
+ File parent = dir;
+ while( !parent.exists() || !parent.isDirectory() )
+ {
+ createDirs.add( parent );
+
+ parent = parent.getParentFile();
+ }
+
+ createdDirs = new ArrayList();
+
+ while ( !createDirs.isEmpty() )
+ {
+ File directory = (File) createDirs.remove( createDirs.size() - 1 );
+
+ if ( directory.mkdir() )
+ {
+ createdDirs.add( directory );
+ }
+ else
+ {
+ throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
+ }
+ }
+ }
+
+ protected void revertMkDirs()
+ throws IOException
+ {
+ if ( createdDirs != null )
+ {
+ Collections.reverse( createdDirs );
+
+ while( !createdDirs.isEmpty() )
+ {
+ File dir = (File) createdDirs.remove( 0 );
+
+ if ( dir.isDirectory() && dir.list().length == 0 )
+ {
+ FileUtils.deleteDirectory( dir.getAbsolutePath() );
+ }
+ else
+ {
+ //cannot rollback created directory if it still contains files
+ break;
+ }
+ }
+ }
+ }
+
+ protected void createBackup( File file )
+ throws IOException
+ {
+ if ( file.exists() && file.isFile() )
+ {
+ backup = File.createTempFile( "temp-", ".backup" );
+
+ FileUtils.copyFile( file, backup );
+
+ backup.deleteOnExit();
+ }
+ }
+
+ protected void restoreBackup( File file )
+ throws IOException
+ {
+ if ( backup != null )
+ {
+ FileUtils.copyFile( backup, file );
+ }
+ }
+}
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class CopyFileEvent
- implements TransactionEvent
+ extends AbstractTransactionEvent
{
private final File source;
public void commit()
throws IOException
{
- destination.getParentFile().mkdirs();
+ createBackup( destination );
+
+ mkDirs( destination.getParentFile() );
FileUtils.copyFile( source, destination );
}
public void rollback()
throws IOException
{
- // TODO: revert to backup/delete if was created
+ FileUtils.fileDelete( destination.getAbsolutePath() );
+
+ revertMkDirs();
+
+ restoreBackup( destination );
}
}
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class CreateFileEvent
- implements TransactionEvent
+ extends AbstractTransactionEvent
{
private final File destination;
public void commit()
throws IOException
{
- destination.getParentFile().mkdirs();
+ createBackup( destination );
+
+ mkDirs( destination.getParentFile() );
+
+ if ( !destination.exists() && !destination.createNewFile() )
+ {
+ throw new IOException( "Unable to create new file" );
+ }
FileUtils.fileWrite( destination.getAbsolutePath(), content );
}
public void rollback()
throws IOException
{
- // TODO: revert to backup/delete if was created
+ FileUtils.fileDelete( destination.getAbsolutePath() );
+
+ revertMkDirs();
+
+ restoreBackup( destination );
}
}
--- /dev/null
+package org.apache.maven.repository.converter.transaction;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+
+/**
+ * @author Edwin Punzalan
+ */
+public class CreateFileEventTest
+ extends PlexusTestCase
+{
+ private File testDir = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/create-file" );
+
+ public void testCreateCommitRollback()
+ throws Exception
+ {
+ File testFile = new File( testDir, "test-file.txt" );
+
+ CreateFileEvent event = new CreateFileEvent( "file contents", testFile );
+
+ assertFalse( "Test file is not yet created", testFile.exists() );
+
+ event.commit();
+
+ assertTrue( "Test file is not yet created", testFile.exists() );
+
+ event.rollback();
+
+ assertFalse( "Test file is has been deleted after rollback", testFile.exists() );
+ assertFalse( "Test file parent directories has been rolledback too", testDir.exists() );
+ assertTrue( "target directory still exists", new File( PlexusTestCase.getBasedir(), "target" ).exists() );
+ }
+
+ public void testCreateCommitRollbackWithBackup()
+ throws Exception
+ {
+ File testFile = new File( testDir, "test-file.txt" );
+
+ testFile.createNewFile();
+
+ FileUtils.fileWrite( testFile.getAbsolutePath(), "original contents" );
+
+ CreateFileEvent event = new CreateFileEvent( "modified contents", testFile );
+
+ String contents = FileUtils.fileRead( testFile.getAbsolutePath() );
+
+ assertEquals( "Test contents have not changed", "original contents", contents );
+
+ event.commit();
+
+ assertTrue( "Test file is not yet created", testFile.exists() );
+
+ event.rollback();
+
+ assertFalse( "Test file is has been deleted after rollback", testFile.exists() );
+ assertFalse( "Test file parent directories has been rolledback too", testDir.exists() );
+ assertTrue( "target directory still exists", new File( PlexusTestCase.getBasedir(), "target" ).exists() );
+ }
+
+ public void testCreateRollbackCommit()
+ throws Exception
+ {
+ File testFile = new File( testDir, "test-file.txt" );
+
+ CreateFileEvent event = new CreateFileEvent( "file contents", testFile );
+
+ assertFalse( "Test file is not yet created", testFile.exists() );
+
+ event.rollback();
+
+ assertFalse( "Test file is not yet created", testFile.exists() );
+
+ event.commit();
+
+ assertTrue( "Test file is not yet created", testFile.exists() );
+ }
+
+ protected void tearDown()
+ throws Exception
+ {
+ super.tearDown();
+
+ FileUtils.deleteDirectory( new File( testDir, "target/transaction-tests" ).getAbsolutePath() );
+ }
+}