001package org.apache.archiva.transaction; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.checksum.ChecksumAlgorithm; 023import org.apache.commons.io.FileUtils; 024 025import java.io.IOException; 026import java.nio.file.Files; 027import java.nio.file.Path; 028import java.nio.file.Paths; 029import java.util.List; 030 031/** 032 * Event to copy a file. 033 * 034 * 035 */ 036public class CopyFileEvent 037 extends AbstractTransactionEvent 038{ 039 private final Path source; 040 041 private final Path destination; 042 043 /** 044 * 045 * @param source 046 * @param destination 047 * @param checksumAlgorithms The checksum algorithms 048 */ 049 public CopyFileEvent( Path source, Path destination, List<ChecksumAlgorithm> checksumAlgorithms ) 050 { 051 super( checksumAlgorithms ); 052 this.source = source; 053 this.destination = destination; 054 } 055 056 @Override 057 public void commit() 058 throws IOException 059 { 060 createBackup( destination ); 061 062 mkDirs( destination.getParent() ); 063 064 FileUtils.copyFile( source.toFile(), destination.toFile() ); 065 066 createChecksums( destination, true ); 067 copyChecksums(); 068 069 copyChecksum( "asc" ); 070 } 071 072 /** 073 * Copy checksums of source file with all digesters if exist 074 * 075 * @throws IOException 076 */ 077 private void copyChecksums() 078 throws IOException 079 { 080 for ( ChecksumAlgorithm checksumAlgorithm : getChecksumAlgorithms() ) 081 { 082 copyChecksum( getChecksumFileExtension( checksumAlgorithm ) ); 083 } 084 } 085 086 /** 087 * Copy checksum of source file with extension provided if exists 088 * 089 * @param extension 090 * @return whether the checksum exists or not 091 * @throws IOException 092 */ 093 private boolean copyChecksum( String extension ) 094 throws IOException 095 { 096 Path checksumSource = Paths.get( source.toAbsolutePath() + "." + extension ); 097 if ( Files.exists(checksumSource) ) 098 { 099 Path checksumDestination = Paths.get( destination.toAbsolutePath() + "." + extension ); 100 FileUtils.copyFile( checksumSource.toFile(), checksumDestination.toFile() ); 101 return true; 102 } 103 return false; 104 } 105 106 @Override 107 public void rollback() 108 throws IOException 109 { 110 Files.deleteIfExists(destination); 111 112 revertFilesCreated(); 113 114 revertMkDirs(); 115 116 restoreBackups(); 117 } 118}