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 java.io.File;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.List;
2627import org.codehaus.plexus.digest.Digester;
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 digesters {@link List}<{@link org.codehaus.plexus.digest.Digester}> digesters to use for checksumming81 */82publicvoid copyFile( File source, File destination, List<? extends Digester> digesters )
83 {
84 events.add( new CopyFileEvent( source, destination, digesters ) );
85 }
8687/**88 * @param content89 * @param destination90 * @param digesters {@link List}<{@link org.codehaus.plexus.digest.Digester}> digesters to use for checksumming91 */92publicvoid createFile( String content, File destination, List<? extends Digester> digesters )
93 {
94 events.add( new CreateFileEvent( content, destination, digesters ) );
95 }
96 }