This project has retired. For details please refer to its
Attic page.
AbstractTransactionEvent xref
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 org.apache.archiva.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksummedFile;
24 import org.apache.commons.io.FileUtils;
25
26 import java.io.IOException;
27 import java.nio.charset.Charset;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.stream.Stream;
38
39
40
41
42
43
44 public abstract class AbstractTransactionEvent
45 implements TransactionEvent
46 {
47 private Map<Path, Path> backups = new HashMap<>();
48
49 private List<Path> createdDirs = new ArrayList<>();
50
51 private List<Path> createdFiles = new ArrayList<>();
52
53 private List<ChecksumAlgorithm> checksumAlgorithms;
54
55 protected AbstractTransactionEvent()
56 {
57 this( new ArrayList<ChecksumAlgorithm>( 0 ) );
58 }
59
60 protected AbstractTransactionEvent( List<ChecksumAlgorithm> checksumAlgorithms)
61 {
62 this.checksumAlgorithms = checksumAlgorithms;
63 }
64
65 protected List<ChecksumAlgorithm> getChecksumAlgorithms()
66 {
67 return checksumAlgorithms;
68 }
69
70
71
72
73
74
75
76 protected void mkDirs( Path dir )
77 throws IOException
78 {
79 List<Path> createDirs = new ArrayList<>();
80
81 Path parent = dir;
82 while ( !Files.exists(parent) || !Files.isDirectory(parent) )
83 {
84 createDirs.add( parent );
85
86 parent = parent.getParent();
87 }
88
89 while ( !createDirs.isEmpty() )
90 {
91 Path directory = createDirs.remove( createDirs.size() - 1 );
92 Files.createDirectories(directory);
93 createdDirs.add( directory );
94 }
95 }
96
97 protected void revertMkDirs()
98 throws IOException
99 {
100 if ( createdDirs != null )
101 {
102 Collections.reverse( createdDirs );
103
104 while ( !createdDirs.isEmpty() )
105 {
106 Path dir = createdDirs.remove( 0 );
107
108 if ( Files.isDirectory(dir))
109 {
110 try(Stream<Path> str = Files.list(dir)) {
111 if (str.count()==0) {
112 org.apache.archiva.common.utils.FileUtils.deleteDirectory(dir);
113 }
114 }
115 }
116 else
117 {
118
119 break;
120 }
121 }
122 }
123 }
124
125 protected void revertFilesCreated()
126 throws IOException
127 {
128 Iterator<Path> it = createdFiles.iterator();
129 while ( it.hasNext() )
130 {
131 Path file = it.next();
132 Files.deleteIfExists(file);
133 it.remove();
134 }
135 }
136
137 protected void createBackup( Path file )
138 throws IOException
139 {
140 if ( Files.exists(file) && Files.isRegularFile(file) )
141 {
142 Path backup = Files.createTempFile( "temp-", ".backup" );
143
144 FileUtils.copyFile( file.toFile(), backup.toFile() );
145
146 backup.toFile().deleteOnExit();
147
148 backups.put( file, backup );
149 }
150 }
151
152 protected void restoreBackups()
153 throws IOException
154 {
155 for ( Map.Entry<Path, Path> entry : backups.entrySet() )
156 {
157 FileUtils.copyFile( entry.getValue().toFile(), entry.getKey().toFile() );
158 }
159 }
160
161 protected void restoreBackup( Path file )
162 throws IOException
163 {
164 Path backup = backups.get( file );
165 if ( backup != null )
166 {
167 FileUtils.copyFile( backup.toFile(), file.toFile() );
168 }
169 }
170
171
172
173
174
175
176
177
178 protected void createChecksums( Path file, boolean force )
179 throws IOException
180 {
181 for ( ChecksumAlgorithm checksumAlgorithm : getChecksumAlgorithms() )
182 {
183 Path checksumFile = Paths.get( file.toAbsolutePath( ) + "." + getChecksumFileExtension( checksumAlgorithm ) );
184 if ( Files.exists( checksumFile ) )
185 {
186 if ( !force )
187 {
188 continue;
189 }
190 createBackup( checksumFile );
191 }
192 else
193 {
194 createdFiles.add( checksumFile );
195 }
196 }
197 ChecksummedFile csFile = new ChecksummedFile( file );
198 csFile.fixChecksums( getChecksumAlgorithms() );
199 }
200
201
202
203
204 protected void writeStringToFile( Path file, String content )
205 throws IOException
206 {
207 org.apache.archiva.common.utils.FileUtils.writeStringToFile( file, Charset.defaultCharset(), content );
208 }
209
210
211
212
213
214 protected String getChecksumFileExtension( ChecksumAlgorithm algorithm )
215 {
216 return algorithm.getExt().get(0);
217 }
218
219 }