1 package org.apache.maven.archiva.converter.transaction;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.commons.io.FileUtils;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
28 * Abstract class for the TransactionEvents
30 * @author Edwin Punzalan
32 public abstract class AbstractTransactionEvent
33 implements TransactionEvent
37 private List createdDirs;
40 * Method that creates a directory as well as all the parent directories needed
42 * @param dir The File directory to be created
43 * @throws IOException when an unrecoverable error occurred
45 protected void mkDirs( File dir )
48 List createDirs = new ArrayList();
51 while ( !parent.exists() || !parent.isDirectory() )
53 createDirs.add( parent );
55 parent = parent.getParentFile();
58 createdDirs = new ArrayList();
60 while ( !createDirs.isEmpty() )
62 File directory = (File) createDirs.remove( createDirs.size() - 1 );
64 if ( directory.mkdir() )
66 createdDirs.add( directory );
70 throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
75 protected void revertMkDirs()
78 if ( createdDirs != null )
80 Collections.reverse( createdDirs );
82 while ( !createdDirs.isEmpty() )
84 File dir = (File) createdDirs.remove( 0 );
86 if ( dir.isDirectory() && dir.list().length == 0 )
88 FileUtils.deleteDirectory( dir );
92 //cannot rollback created directory if it still contains files
99 protected void createBackup( File file )
102 if ( file.exists() && file.isFile() )
104 backup = File.createTempFile( "temp-", ".backup" );
106 FileUtils.copyFile( file, backup );
108 backup.deleteOnExit();
112 protected void restoreBackup( File file )
115 if ( backup != null )
117 FileUtils.copyFile( backup, file );