001 package 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 022 import java.io.File; 023 import java.io.IOException; 024 import java.util.List; 025 026 import org.apache.commons.io.FileUtils; 027 import org.codehaus.plexus.digest.Digester; 028 029 /** 030 * Event to copy a file. 031 * 032 * 033 */ 034 public class CopyFileEvent 035 extends AbstractTransactionEvent 036 { 037 private final File source; 038 039 private final File destination; 040 041 /** 042 * 043 * @param source 044 * @param destination 045 * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming 046 */ 047 public CopyFileEvent( File source, File destination, List<? extends Digester> digesters ) 048 { 049 super( digesters ); 050 this.source = source; 051 this.destination = destination; 052 } 053 054 public void commit() 055 throws IOException 056 { 057 createBackup( destination ); 058 059 mkDirs( destination.getParentFile() ); 060 061 FileUtils.copyFile( source, destination ); 062 063 createChecksums( destination, true ); 064 copyChecksums(); 065 066 copyChecksum( "asc" ); 067 } 068 069 /** 070 * Copy checksums of source file with all digesters if exist 071 * 072 * @throws IOException 073 */ 074 private void copyChecksums() 075 throws IOException 076 { 077 for ( Digester digester : getDigesters() ) 078 { 079 copyChecksum( getDigesterFileExtension( digester ) ); 080 } 081 } 082 083 /** 084 * Copy checksum of source file with extension provided if exists 085 * 086 * @param extension 087 * @return whether the checksum exists or not 088 * @throws IOException 089 */ 090 private boolean copyChecksum( String extension ) 091 throws IOException 092 { 093 File checksumSource = new File( source.getAbsolutePath() + "." + extension ); 094 if ( checksumSource.exists() ) 095 { 096 File checksumDestination = new File( destination.getAbsolutePath() + "." + extension ); 097 FileUtils.copyFile( checksumSource, checksumDestination ); 098 return true; 099 } 100 return false; 101 } 102 103 public void rollback() 104 throws IOException 105 { 106 destination.delete(); 107 108 revertFilesCreated(); 109 110 revertMkDirs(); 111 112 restoreBackups(); 113 } 114 }