Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CopyFileEvent |
|
| 0.0;0 |
1 | package org.apache.maven.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 | * @version $Id: CopyFileEvent.java 755305 2009-03-17 16:24:16Z brett $ | |
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<Digester> digesters ) | |
48 | { | |
49 | 0 | super( digesters ); |
50 | 0 | this.source = source; |
51 | 0 | this.destination = destination; |
52 | 0 | } |
53 | ||
54 | public void commit() | |
55 | throws IOException | |
56 | { | |
57 | 0 | createBackup( destination ); |
58 | ||
59 | 0 | mkDirs( destination.getParentFile() ); |
60 | ||
61 | 0 | FileUtils.copyFile( source, destination ); |
62 | ||
63 | 0 | createChecksums( destination, true ); |
64 | 0 | copyChecksums(); |
65 | ||
66 | 0 | copyChecksum( "asc" ); |
67 | 0 | } |
68 | ||
69 | /** | |
70 | * Copy checksums of source file with all digesters if exist | |
71 | * | |
72 | * @throws IOException | |
73 | */ | |
74 | private void copyChecksums() | |
75 | throws IOException | |
76 | { | |
77 | 0 | for ( Digester digester : getDigesters() ) |
78 | { | |
79 | 0 | copyChecksum( getDigesterFileExtension( digester ) ); |
80 | } | |
81 | 0 | } |
82 | ||
83 | /** | |
84 | * Copy checksum of source file with extension provided if exists | |
85 | * | |
86 | * @param extension | |
87 | * @return whether the checksum exists or not | |
88 | * @throws IOException | |
89 | */ | |
90 | private boolean copyChecksum( String extension ) | |
91 | throws IOException | |
92 | { | |
93 | 0 | File checksumSource = new File( source.getAbsolutePath() + "." + extension ); |
94 | 0 | if ( checksumSource.exists() ) |
95 | { | |
96 | 0 | File checksumDestination = new File( destination.getAbsolutePath() + "." + extension ); |
97 | 0 | FileUtils.copyFile( checksumSource, checksumDestination ); |
98 | 0 | return true; |
99 | } | |
100 | 0 | return false; |
101 | } | |
102 | ||
103 | public void rollback() | |
104 | throws IOException | |
105 | { | |
106 | 0 | destination.delete(); |
107 | ||
108 | 0 | revertFilesCreated(); |
109 | ||
110 | 0 | revertMkDirs(); |
111 | ||
112 | 0 | restoreBackups(); |
113 | 0 | } |
114 | } |