This project has retired. For details please refer to its Attic page.
FileTransaction xref
View Javadoc
1   package org.apache.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.ArrayList;
25  import java.util.List;
26  
27  import org.codehaus.plexus.digest.Digester;
28  
29  /**
30   * Implement commit/rollback semantics for a set of files.
31   *
32   */
33  public class FileTransaction
34  {
35      private List<AbstractTransactionEvent> events = new ArrayList<>();
36  
37      public void commit()
38          throws TransactionException
39      {
40          List<TransactionEvent> toRollback = new ArrayList<TransactionEvent>( events.size() );
41  
42          for ( TransactionEvent event : events )
43          {
44              try
45              {
46                  event.commit();
47  
48                  toRollback.add( event );
49              }
50              catch ( IOException e )
51              {
52                  try
53                  {
54                      rollback( toRollback );
55  
56                      throw new TransactionException( "Unable to commit file transaction", e );
57                  }
58                  catch ( IOException ioe )
59                  {
60                      throw new TransactionException(
61                          "Unable to commit file transaction, and rollback failed with error: '" + ioe.getMessage() + "'",
62                          e );
63                  }
64              }
65          }
66      }
67  
68      private void rollback( List<TransactionEvent> toRollback )
69          throws IOException
70      {
71          for ( TransactionEvent event : toRollback )
72          {
73              event.rollback();
74          }
75      }
76  
77      /**
78       * @param source
79       * @param destination
80       * @param digesters   {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; digesters to use for checksumming
81       */
82      public void copyFile( File source, File destination, List<? extends Digester> digesters )
83      {
84          events.add( new CopyFileEvent( source, destination, digesters ) );
85      }
86  
87      /**
88       * @param content
89       * @param destination
90       * @param digesters   {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; digesters to use for checksumming
91       */
92      public void createFile( String content, File destination, List<? extends Digester> digesters )
93      {
94          events.add( new CreateFileEvent( content, destination, digesters ) );
95      }
96  }