This project has retired. For details please refer to its Attic page.
Source code
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 java.io.File;
023import java.io.IOException;
024import java.util.List;
025
026import org.apache.commons.io.FileUtils;
027import org.codehaus.plexus.digest.Digester;
028
029/**
030 * Event to copy a file.
031 *
032 *
033 */
034public 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    @Override
055    public void commit()
056        throws IOException
057    {
058        createBackup( destination );
059
060        mkDirs( destination.getParentFile() );
061
062        FileUtils.copyFile( source, destination );
063
064        createChecksums( destination, true );
065        copyChecksums();
066
067        copyChecksum( "asc" );
068    }
069
070    /**
071     * Copy checksums of source file with all digesters if exist
072     * 
073     * @throws IOException
074     */
075    private void copyChecksums()
076        throws IOException
077    {
078        for ( Digester digester : getDigesters() )
079        {
080            copyChecksum( getDigesterFileExtension( digester ) );
081        }
082    }
083
084    /**
085     * Copy checksum of source file with extension provided if exists
086     * 
087     * @param extension
088     * @return whether the checksum exists or not 
089     * @throws IOException
090     */
091    private boolean copyChecksum( String extension )
092        throws IOException
093    {
094        File checksumSource = new File( source.getAbsolutePath() + "." + extension );
095        if ( checksumSource.exists() )
096        {
097            File checksumDestination = new File( destination.getAbsolutePath() + "." + extension );
098            FileUtils.copyFile( checksumSource, checksumDestination );
099            return true;
100        }
101        return false;
102    }
103
104    @Override
105    public void rollback()
106        throws IOException
107    {
108        destination.delete();
109
110        revertFilesCreated();
111
112        revertMkDirs();
113
114        restoreBackups();
115    }
116}