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 org.apache.archiva.checksum.ChecksumAlgorithm;
23  import org.apache.commons.io.FileUtils;
24  
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;
30  
31  /**
32   * Event to copy a file.
33   *
34   *
35   */
36  public class CopyFileEvent
37      extends AbstractTransactionEvent
38  {
39      private final Path source;
40  
41      private final Path destination;
42  
43      /**
44       * 
45       * @param source
46       * @param destination
47       * @param checksumAlgorithms The checksum algorithms
48       */
49      public CopyFileEvent( Path source, Path destination, List<ChecksumAlgorithm> checksumAlgorithms )
50      {
51          super( checksumAlgorithms );
52          this.source = source;
53          this.destination = destination;
54      }
55  
56      @Override
57      public void commit()
58          throws IOException
59      {
60          createBackup( destination );
61  
62          mkDirs( destination.getParent() );
63  
64          FileUtils.copyFile( source.toFile(), destination.toFile() );
65  
66          createChecksums( destination, true );
67          copyChecksums();
68  
69          copyChecksum( "asc" );
70      }
71  
72      /**
73       * Copy checksums of source file with all digesters if exist
74       * 
75       * @throws IOException
76       */
77      private void copyChecksums()
78          throws IOException
79      {
80          for ( ChecksumAlgorithm checksumAlgorithm : getChecksumAlgorithms() )
81          {
82              copyChecksum( getChecksumFileExtension( checksumAlgorithm ) );
83          }
84      }
85  
86      /**
87       * Copy checksum of source file with extension provided if exists
88       * 
89       * @param extension
90       * @return whether the checksum exists or not 
91       * @throws IOException
92       */
93      private boolean copyChecksum( String extension )
94          throws IOException
95      {
96          Path checksumSource = Paths.get( source.toAbsolutePath() + "." + extension );
97          if ( Files.exists(checksumSource) )
98          {
99              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 }