This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.transaction;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.codehaus.plexus.digest.Digester;
028
029/**
030 * Implement commit/rollback semantics for a set of files.
031 *
032 */
033public class FileTransaction
034{
035    private List<AbstractTransactionEvent> events = new ArrayList<>();
036
037    public void commit()
038        throws TransactionException
039    {
040        List<TransactionEvent> toRollback = new ArrayList<TransactionEvent>( events.size() );
041
042        for ( TransactionEvent event : events )
043        {
044            try
045            {
046                event.commit();
047
048                toRollback.add( event );
049            }
050            catch ( IOException e )
051            {
052                try
053                {
054                    rollback( toRollback );
055
056                    throw new TransactionException( "Unable to commit file transaction", e );
057                }
058                catch ( IOException ioe )
059                {
060                    throw new TransactionException(
061                        "Unable to commit file transaction, and rollback failed with error: '" + ioe.getMessage() + "'",
062                        e );
063                }
064            }
065        }
066    }
067
068    private void rollback( List<TransactionEvent> toRollback )
069        throws IOException
070    {
071        for ( TransactionEvent event : toRollback )
072        {
073            event.rollback();
074        }
075    }
076
077    /**
078     * @param source
079     * @param destination
080     * @param digesters   {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; digesters to use for checksumming
081     */
082    public void copyFile( File source, File destination, List<? extends Digester> digesters )
083    {
084        events.add( new CopyFileEvent( source, destination, digesters ) );
085    }
086
087    /**
088     * @param content
089     * @param destination
090     * @param digesters   {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; digesters to use for checksumming
091     */
092    public void createFile( String content, File destination, List<? extends Digester> digesters )
093    {
094        events.add( new CreateFileEvent( content, destination, digesters ) );
095    }
096}