This project has retired. For details please refer to its Attic page.
CopyFileEvent xref
View Javadoc
1   package org.apache.archiva.transaction;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.codehaus.plexus.digest.Digester;
28  
29  /**
30   * Event to copy a file.
31   *
32   *
33   */
34  public class CopyFileEvent
35      extends AbstractTransactionEvent
36  {
37      private final File source;
38  
39      private final File destination;
40  
41      /**
42       * 
43       * @param source
44       * @param destination
45       * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming 
46       */
47      public CopyFileEvent( File source, File destination, List<? extends Digester> digesters )
48      {
49          super( digesters );
50          this.source = source;
51          this.destination = destination;
52      }
53  
54      @Override
55      public void commit()
56          throws IOException
57      {
58          createBackup( destination );
59  
60          mkDirs( destination.getParentFile() );
61  
62          FileUtils.copyFile( source, destination );
63  
64          createChecksums( destination, true );
65          copyChecksums();
66  
67          copyChecksum( "asc" );
68      }
69  
70      /**
71       * Copy checksums of source file with all digesters if exist
72       * 
73       * @throws IOException
74       */
75      private void copyChecksums()
76          throws IOException
77      {
78          for ( Digester digester : getDigesters() )
79          {
80              copyChecksum( getDigesterFileExtension( digester ) );
81          }
82      }
83  
84      /**
85       * Copy checksum of source file with extension provided if exists
86       * 
87       * @param extension
88       * @return whether the checksum exists or not 
89       * @throws IOException
90       */
91      private boolean copyChecksum( String extension )
92          throws IOException
93      {
94          File checksumSource = new File( source.getAbsolutePath() + "." + extension );
95          if ( checksumSource.exists() )
96          {
97              File checksumDestination = new File( destination.getAbsolutePath() + "." + extension );
98              FileUtils.copyFile( checksumSource, checksumDestination );
99              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 }