1package org.apache.archiva.transaction;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.checksum.ChecksumAlgorithm;
2324import java.io.IOException;
25import java.nio.file.Path;
26import java.util.ArrayList;
27import java.util.List;
2829/**30 * Implement commit/rollback semantics for a set of files.31 *32 */33publicclassFileTransaction34 {
35private List<AbstractTransactionEvent> events = new ArrayList<>();
3637publicvoid commit()
38throwsTransactionException39 {
40 List<TransactionEvent> toRollback = new ArrayList<TransactionEvent>( events.size() );
4142for ( TransactionEvent event : events )
43 {
44try45 {
46 event.commit();
4748 toRollback.add( event );
49 }
50catch ( IOException e )
51 {
52try53 {
54 rollback( toRollback );
5556thrownewTransactionException( "Unable to commit file transaction", e );
57 }
58catch ( IOException ioe )
59 {
60thrownewTransactionException(
61"Unable to commit file transaction, and rollback failed with error: '" + ioe.getMessage() + "'",
62 e );
63 }
64 }
65 }
66 }
6768privatevoid rollback( List<TransactionEvent> toRollback )
69throws IOException
70 {
71for ( TransactionEvent event : toRollback )
72 {
73 event.rollback();
74 }
75 }
7677/**78 * @param source79 * @param destination80 * @param checksumAlgorithms The checksum algorithms81 */82publicvoid copyFile(Path source, Path destination, List<ChecksumAlgorithm> checksumAlgorithms )
83 {
84 events.add( newCopyFileEvent( source, destination, checksumAlgorithms ) );
85 }
8687/**88 * @param content89 * @param destination90 * @param checksumAlgorithms Checksum algorithms91 */92publicvoid createFile( String content, Path destination, List<ChecksumAlgorithm> checksumAlgorithms )
93 {
94 events.add( newCreateFileEvent( content, destination, checksumAlgorithms ) );
95 }
96 }