1 package org.apache.maven.archiva.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.codehaus.plexus.digest.Digester;
23 import org.codehaus.plexus.digest.DigesterException;
24 import org.codehaus.plexus.util.FileUtils;
25 import org.codehaus.plexus.util.IOUtil;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
38 * Abstract class for the TransactionEvents
40 * @author Edwin Punzalan
41 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
44 public abstract class AbstractTransactionEvent
45 implements TransactionEvent
47 private Map backups = new HashMap();
49 private List createdDirs = new ArrayList();
51 private List createdFiles = new ArrayList();
54 * {@link List}<{@link Digester}>
56 private List digesters;
58 protected AbstractTransactionEvent()
60 this( new ArrayList( 0 ) );
63 protected AbstractTransactionEvent( List digesters )
65 this.digesters = digesters;
68 protected List getDigesters()
74 * Method that creates a directory as well as all the parent directories needed
76 * @param dir The File directory to be created
77 * @throws IOException when an unrecoverable error occurred
79 protected void mkDirs( File dir )
82 List createDirs = new ArrayList();
85 while ( !parent.exists() || !parent.isDirectory() )
87 createDirs.add( parent );
89 parent = parent.getParentFile();
92 while ( !createDirs.isEmpty() )
94 File directory = (File) createDirs.remove( createDirs.size() - 1 );
96 if ( directory.mkdir() )
98 createdDirs.add( directory );
102 throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
107 protected void revertMkDirs()
110 if ( createdDirs != null )
112 Collections.reverse( createdDirs );
114 while ( !createdDirs.isEmpty() )
116 File dir = (File) createdDirs.remove( 0 );
118 if ( dir.isDirectory() && dir.list().length == 0 )
120 FileUtils.deleteDirectory( dir );
124 //cannot rollback created directory if it still contains files
131 protected void revertFilesCreated()
134 Iterator it = createdFiles.iterator();
135 while ( it.hasNext() )
137 File file = (File) it.next();
143 protected void createBackup( File file )
146 if ( file.exists() && file.isFile() )
148 File backup = File.createTempFile( "temp-", ".backup" );
150 FileUtils.copyFile( file, backup );
152 backup.deleteOnExit();
154 backups.put( file, backup );
158 protected void restoreBackups()
161 Iterator it = backups.entrySet().iterator();
162 while ( it.hasNext() )
164 Map.Entry entry = (Map.Entry) it.next();
165 FileUtils.copyFile( (File) entry.getValue(), (File) entry.getKey() );
169 protected void restoreBackup( File file )
172 File backup = (File) backups.get( file );
173 if ( backup != null )
175 FileUtils.copyFile( backup, file );
180 * Create checksums of file using all digesters defined at construction time.
183 * @param force whether existing checksums should be overwritten or not
184 * @throws IOException
186 protected void createChecksums( File file, boolean force )
189 Iterator it = getDigesters().iterator();
190 while ( it.hasNext() )
192 Digester digester = (Digester) it.next();
193 File checksumFile = new File( file.getAbsolutePath() + "." + getDigesterFileExtension( digester ) );
194 if ( checksumFile.exists() )
200 createBackup( checksumFile );
204 createdFiles.add( checksumFile );
209 writeStringToFile( checksumFile, digester.calc( file ) );
211 catch ( DigesterException e )
213 throw (IOException) e.getCause();
218 protected void writeStringToFile( File file, String content )
221 FileOutputStream out = null;
224 out = new FileOutputStream( file );
225 IOUtil.copy( content, out );
234 * File extension for checksums
235 * TODO should be moved to plexus-digester ?
237 protected String getDigesterFileExtension( Digester digester )
239 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" );