1 package org.apache.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.apache.commons.io.FileUtils;
23 import org.codehaus.plexus.digest.Digester;
24 import org.codehaus.plexus.digest.DigesterException;
26 import java.io.IOException;
27 import java.nio.charset.Charset;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.stream.Stream;
40 * Abstract class for the TransactionEvents
44 public abstract class AbstractTransactionEvent
45 implements TransactionEvent
47 private Map<Path, Path> backups = new HashMap<>();
49 private List<Path> createdDirs = new ArrayList<>();
51 private List<Path> createdFiles = new ArrayList<>();
54 * {@link List}<{@link Digester}>
56 private List<? extends Digester> digesters;
58 protected AbstractTransactionEvent()
60 this( new ArrayList<Digester>( 0 ) );
63 protected AbstractTransactionEvent( List<? extends Digester> digesters )
65 this.digesters = digesters;
68 protected List<? extends Digester> 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( Path dir )
82 List<Path> createDirs = new ArrayList<>();
85 while ( !Files.exists(parent) || !Files.isDirectory(parent) )
87 createDirs.add( parent );
89 parent = parent.getParent();
92 while ( !createDirs.isEmpty() )
94 Path directory = createDirs.remove( createDirs.size() - 1 );
95 Files.createDirectories(directory);
96 createdDirs.add( directory );
100 protected void revertMkDirs()
103 if ( createdDirs != null )
105 Collections.reverse( createdDirs );
107 while ( !createdDirs.isEmpty() )
109 Path dir = createdDirs.remove( 0 );
111 if ( Files.isDirectory(dir))
113 try(Stream<Path> str = Files.list(dir)) {
114 if (str.count()==0) {
115 org.apache.archiva.common.utils.FileUtils.deleteDirectory(dir);
121 //cannot rollback created directory if it still contains files
128 protected void revertFilesCreated()
131 Iterator<Path> it = createdFiles.iterator();
132 while ( it.hasNext() )
134 Path file = it.next();
135 Files.deleteIfExists(file);
140 protected void createBackup( Path file )
143 if ( Files.exists(file) && Files.isRegularFile(file) )
145 Path backup = Files.createTempFile( "temp-", ".backup" );
147 FileUtils.copyFile( file.toFile(), backup.toFile() );
149 backup.toFile().deleteOnExit();
151 backups.put( file, backup );
155 protected void restoreBackups()
158 for ( Map.Entry<Path, Path> entry : backups.entrySet() )
160 FileUtils.copyFile( entry.getValue().toFile(), entry.getKey().toFile() );
164 protected void restoreBackup( Path file )
167 Path backup = backups.get( file );
168 if ( backup != null )
170 FileUtils.copyFile( backup.toFile(), file.toFile() );
175 * Create checksums of file using all digesters defined at construction time.
178 * @param force whether existing checksums should be overwritten or not
179 * @throws IOException
181 protected void createChecksums( Path file, boolean force )
184 for ( Digester digester : getDigesters() )
186 Path checksumFile = Paths.get(file.toAbsolutePath() + "." + getDigesterFileExtension( digester ) );
187 if ( Files.exists(checksumFile) )
193 createBackup( checksumFile );
197 createdFiles.add( checksumFile );
202 writeStringToFile( checksumFile, digester.calc( file.toFile() ) );
204 catch ( DigesterException e )
206 throw (IOException) e.getCause();
212 * TODO: Remove in favor of using FileUtils directly.
214 protected void writeStringToFile( Path file, String content )
217 org.apache.archiva.common.utils.FileUtils.writeStringToFile( file, Charset.defaultCharset(), content );
221 * File extension for checksums
222 * TODO should be moved to plexus-digester ?
224 protected String getDigesterFileExtension( Digester digester )
226 return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" );