1 package org.apache.archiva.transaction;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
44
45
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 public void commit()
55 throws IOException
56 {
57 createBackup( destination );
58
59 mkDirs( destination.getParentFile() );
60
61 FileUtils.copyFile( source, destination );
62
63 createChecksums( destination, true );
64 copyChecksums();
65
66 copyChecksum( "asc" );
67 }
68
69
70
71
72
73
74 private void copyChecksums()
75 throws IOException
76 {
77 for ( Digester digester : getDigesters() )
78 {
79 copyChecksum( getDigesterFileExtension( digester ) );
80 }
81 }
82
83
84
85
86
87
88
89
90 private boolean copyChecksum( String extension )
91 throws IOException
92 {
93 File checksumSource = new File( source.getAbsolutePath() + "." + extension );
94 if ( checksumSource.exists() )
95 {
96 File checksumDestination = new File( destination.getAbsolutePath() + "." + extension );
97 FileUtils.copyFile( checksumSource, checksumDestination );
98 return true;
99 }
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 }