1 package org.apache.maven.archiva.converter.transaction;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
31 * Abstract class for the TransactionEvents
33 * @author Edwin Punzalan
35 public abstract class AbstractTransactionEvent
36 implements TransactionEvent
40 private List createdDirs;
43 * Method that creates a directory as well as all the parent directories needed
45 * @param dir The File directory to be created
46 * @throws IOException when an unrecoverable error occurred
48 protected void mkDirs( File dir )
51 List createDirs = new ArrayList();
54 while ( !parent.exists() || !parent.isDirectory() )
56 createDirs.add( parent );
58 parent = parent.getParentFile();
61 createdDirs = new ArrayList();
63 while ( !createDirs.isEmpty() )
65 File directory = (File) createDirs.remove( createDirs.size() - 1 );
67 if ( directory.mkdir() )
69 createdDirs.add( directory );
73 throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
78 protected void revertMkDirs()
81 if ( createdDirs != null )
83 Collections.reverse( createdDirs );
85 while ( !createdDirs.isEmpty() )
87 File dir = (File) createdDirs.remove( 0 );
89 if ( dir.isDirectory() && dir.list().length == 0 )
91 FileUtils.deleteDirectory( dir );
95 //cannot rollback created directory if it still contains files
102 protected void createBackup( File file )
105 if ( file.exists() && file.isFile() )
107 backup = File.createTempFile( "temp-", ".backup" );
109 FileUtils.copyFile( file, backup );
111 backup.deleteOnExit();
115 protected void restoreBackup( File file )
118 if ( backup != null )
120 FileUtils.copyFile( backup, file );