Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.maven.archiva.transaction;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;

Abstract class for the TransactionEvents

Author(s):
Edwin Punzalan
Carlos Sanchez
Version:
$Id: AbstractTransactionEvent.java 548354 2007-06-18 13:28:59Z brett $
 
 public abstract class AbstractTransactionEvent
     extends AbstractLogEnabled
     implements TransactionEvent
 {
     private Map backups = new HashMap();
 
     private List createdDirs = new ArrayList();
 
     private List createdFiles = new ArrayList();

    
 
     private List digesters;
 
     protected AbstractTransactionEvent()
     {
         thisnew ArrayList( 0 ) );
     }
 
     protected AbstractTransactionEventList digesters )
     {
         this. = digesters;
     }
 
     protected List getDigesters()
     {
         return ;
     }

    
Method that creates a directory as well as all the parent directories needed

Parameters:
dir The File directory to be created
Throws:
java.io.IOException when an unrecoverable error occurred
 
     protected void mkDirsFile dir )
         throws IOException
     {
         List createDirs = new ArrayList();
 
         File parent = dir;
         while ( !parent.exists() || !parent.isDirectory() )
         {
             createDirs.addparent );
 
             parent = parent.getParentFile();
         }
 
         while ( !createDirs.isEmpty() )
         {
             File directory = (FilecreateDirs.removecreateDirs.size() - 1 );
 
             if ( directory.mkdir() )
             {
                .adddirectory );
            }
            else
            {
                throw new IOException"Failed to create directory: " + directory.getAbsolutePath() );
            }
        }
    }
    protected void revertMkDirs()
        throws IOException
    {
        if (  != null )
        {
            Collections.reverse );
            while ( !.isEmpty() )
            {
                File dir = (File.remove( 0 );
                if ( dir.isDirectory() && dir.list().length == 0 )
                {
                    FileUtils.deleteDirectorydir );
                }
                else
                {
                    //cannot rollback created directory if it still contains files
                    break;
                }
            }
        }
    }
    protected void revertFilesCreated()
        throws IOException
    {
        Iterator it = .iterator();
        while ( it.hasNext() )
        {
            File file = (Fileit.next();
            file.delete();
            it.remove();
        }
    }
    protected void createBackupFile file )
        throws IOException
    {
        if ( file.exists() && file.isFile() )
        {
            File backup = File.createTempFile"temp-"".backup" );
            FileUtils.copyFilefilebackup );
            backup.deleteOnExit();
            .putfilebackup );
        }
    }
    protected void restoreBackups()
        throws IOException
    {
        Iterator it = .entrySet().iterator();
        while ( it.hasNext() )
        {
            Map.Entry entry = (Map.Entryit.next();
            FileUtils.copyFile( (Fileentry.getValue(), (Fileentry.getKey() );
        }
    }
    protected void restoreBackupFile file )
        throws IOException
    {
        File backup = (File.getfile );
        if ( backup != null )
        {
            FileUtils.copyFilebackupfile );
        }
    }

    
Create checksums of file using all digesters defined at construction time.

Parameters:
file
force whether existing checksums should be overwritten or not
Throws:
java.io.IOException
    protected void createChecksumsFile fileboolean force )
        throws IOException
    {
        Iterator it = getDigesters().iterator();
        while ( it.hasNext() )
        {
            Digester digester = (Digesterit.next();
            File checksumFile = new Filefile.getAbsolutePath() + "." + getDigesterFileExtensiondigester ) );
            if ( checksumFile.exists() )
            {
                if ( !force )
                {
                    continue;
                }
                createBackupchecksumFile );
            }
            else
            {
                .addchecksumFile );
            }
            try
            {
                writeStringToFilechecksumFiledigester.calcfile ) );
            }
            catch ( DigesterException e )
            {
                throw (IOExceptione.getCause();
            }
        }
    }
    protected void writeStringToFileFile fileString content )
        throws IOException
    {
        FileOutputStream out = null;
        try
        {
            out = new FileOutputStreamfile );
            IOUtil.copycontentout );
        }
        finally
        {
            IOUtil.closeout );
        }
    }

    
File extension for checksums TODO should be moved to plexus-digester ?
    protected String getDigesterFileExtensionDigester digester )
    {
        return digester.getAlgorithm().toLowerCase().replaceAll"-""" );
    }
New to GrepCode? Check out our FAQ X