Coverage Report - org.apache.maven.archiva.transaction.AbstractTransactionEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTransactionEvent
0%
0/65
0%
0/32
0
 
 1  
 package org.apache.maven.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 org.apache.commons.io.FileUtils;
 23  
 import org.codehaus.plexus.digest.Digester;
 24  
 import org.codehaus.plexus.digest.DigesterException;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.IOException;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collections;
 30  
 import java.util.HashMap;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * Abstract class for the TransactionEvents
 37  
  *
 38  
  * @version $Id: AbstractTransactionEvent.java 755305 2009-03-17 16:24:16Z brett $
 39  
  */
 40  
 public abstract class AbstractTransactionEvent
 41  
     implements TransactionEvent
 42  
 {
 43  0
     private Map<File, File> backups = new HashMap<File, File>();
 44  
 
 45  0
     private List<File> createdDirs = new ArrayList<File>();
 46  
 
 47  0
     private List<File> createdFiles = new ArrayList<File>();
 48  
 
 49  
     /**
 50  
      * {@link List}&lt;{@link Digester}>
 51  
      */
 52  
     private List<Digester> digesters;
 53  
 
 54  
     protected AbstractTransactionEvent()
 55  
     {
 56  0
         this( new ArrayList<Digester>( 0 ) );
 57  0
     }
 58  
 
 59  
     protected AbstractTransactionEvent( List<Digester> digesters )
 60  0
     {
 61  0
         this.digesters = digesters;
 62  0
     }
 63  
 
 64  
     protected List<Digester> getDigesters()
 65  
     {
 66  0
         return digesters;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Method that creates a directory as well as all the parent directories needed
 71  
      *
 72  
      * @param dir The File directory to be created
 73  
      * @throws IOException when an unrecoverable error occurred
 74  
      */
 75  
     protected void mkDirs( File dir )
 76  
         throws IOException
 77  
     {
 78  0
         List<File> createDirs = new ArrayList<File>();
 79  
 
 80  0
         File parent = dir;
 81  0
         while ( !parent.exists() || !parent.isDirectory() )
 82  
         {
 83  0
             createDirs.add( parent );
 84  
 
 85  0
             parent = parent.getParentFile();
 86  
         }
 87  
 
 88  0
         while ( !createDirs.isEmpty() )
 89  
         {
 90  0
             File directory = (File) createDirs.remove( createDirs.size() - 1 );
 91  
 
 92  0
             if ( directory.mkdir() )
 93  
             {
 94  0
                 createdDirs.add( directory );
 95  
             }
 96  
             else
 97  
             {
 98  0
                 throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
 99  
             }
 100  0
         }
 101  0
     }
 102  
 
 103  
     protected void revertMkDirs()
 104  
         throws IOException
 105  
     {
 106  0
         if ( createdDirs != null )
 107  
         {
 108  0
             Collections.reverse( createdDirs );
 109  
 
 110  0
             while ( !createdDirs.isEmpty() )
 111  
             {
 112  0
                 File dir = (File) createdDirs.remove( 0 );
 113  
 
 114  0
                 if ( dir.isDirectory() && dir.list().length == 0 )
 115  
                 {
 116  0
                     FileUtils.deleteDirectory( dir );
 117  
                 }
 118  
                 else
 119  
                 {
 120  
                     //cannot rollback created directory if it still contains files
 121  
                     break;
 122  
                 }
 123  0
             }
 124  
         }
 125  0
     }
 126  
 
 127  
     protected void revertFilesCreated()
 128  
         throws IOException
 129  
     {
 130  0
         Iterator<File> it = createdFiles.iterator();
 131  0
         while ( it.hasNext() )
 132  
         {
 133  0
             File file = (File) it.next();
 134  0
             file.delete();
 135  0
             it.remove();
 136  0
         }
 137  0
     }
 138  
 
 139  
     protected void createBackup( File file )
 140  
         throws IOException
 141  
     {
 142  0
         if ( file.exists() && file.isFile() )
 143  
         {
 144  0
             File backup = File.createTempFile( "temp-", ".backup" );
 145  
 
 146  0
             FileUtils.copyFile( file, backup );
 147  
 
 148  0
             backup.deleteOnExit();
 149  
 
 150  0
             backups.put( file, backup );
 151  
         }
 152  0
     }
 153  
 
 154  
     protected void restoreBackups()
 155  
         throws IOException
 156  
     {
 157  0
         for ( Map.Entry<File, File> entry : backups.entrySet() )
 158  
         {
 159  0
             FileUtils.copyFile( entry.getValue(), entry.getKey() );
 160  
         }
 161  0
     }
 162  
 
 163  
     protected void restoreBackup( File file )
 164  
         throws IOException
 165  
     {
 166  0
         File backup = (File) backups.get( file );
 167  0
         if ( backup != null )
 168  
         {
 169  0
             FileUtils.copyFile( backup, file );
 170  
         }
 171  0
     }
 172  
 
 173  
     /**
 174  
      * Create checksums of file using all digesters defined at construction time.
 175  
      *
 176  
      * @param file
 177  
      * @param force whether existing checksums should be overwritten or not
 178  
      * @throws IOException
 179  
      */
 180  
     protected void createChecksums( File file, boolean force )
 181  
         throws IOException
 182  
     {
 183  0
         for ( Digester digester : getDigesters() )
 184  
         {
 185  0
             File checksumFile = new File( file.getAbsolutePath() + "." + getDigesterFileExtension( digester ) );
 186  0
             if ( checksumFile.exists() )
 187  
             {
 188  0
                 if ( !force )
 189  
                 {
 190  0
                     continue;
 191  
                 }
 192  0
                 createBackup( checksumFile );
 193  
             }
 194  
             else
 195  
             {
 196  0
                 createdFiles.add( checksumFile );
 197  
             }
 198  
 
 199  
             try
 200  
             {
 201  0
                 writeStringToFile( checksumFile, digester.calc( file ) );
 202  
             }
 203  0
             catch ( DigesterException e )
 204  
             {
 205  0
                 throw (IOException) e.getCause();
 206  0
             }
 207  0
         }
 208  0
     }
 209  
 
 210  
     /**
 211  
      * TODO: Remove in favor of using FileUtils directly.
 212  
      */
 213  
     protected void writeStringToFile( File file, String content )
 214  
         throws IOException
 215  
     {
 216  0
         FileUtils.writeStringToFile( file, content );
 217  0
     }
 218  
 
 219  
     /**
 220  
      * File extension for checksums
 221  
      * TODO should be moved to plexus-digester ?
 222  
      */
 223  
     protected String getDigesterFileExtension( Digester digester )
 224  
     {
 225  0
         return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" );
 226  
     }
 227  
 
 228  
 }