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;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.List;
32 * Event to copy a file.
36 public class CopyFileEvent
37 extends AbstractTransactionEvent
39 private final Path source;
41 private final Path destination;
47 * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming
49 public CopyFileEvent( Path source, Path destination, List<? extends Digester> digesters )
53 this.destination = destination;
60 createBackup( destination );
62 mkDirs( destination.getParent() );
64 FileUtils.copyFile( source.toFile(), destination.toFile() );
66 createChecksums( destination, true );
69 copyChecksum( "asc" );
73 * Copy checksums of source file with all digesters if exist
77 private void copyChecksums()
80 for ( Digester digester : getDigesters() )
82 copyChecksum( getDigesterFileExtension( digester ) );
87 * Copy checksum of source file with extension provided if exists
90 * @return whether the checksum exists or not
93 private boolean copyChecksum( String extension )
96 Path checksumSource = Paths.get( source.toAbsolutePath() + "." + extension );
97 if ( Files.exists(checksumSource) )
99 Path checksumDestination = Paths.get( destination.toAbsolutePath() + "." + extension );
100 FileUtils.copyFile( checksumSource.toFile(), checksumDestination.toFile() );
107 public void rollback()
110 Files.deleteIfExists(destination);
112 revertFilesCreated();