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.List; 025 026import org.codehaus.plexus.digest.Digester; 027 028/** 029 * Event for creating a file from a string content. 030 * 031 * 032 */ 033public class CreateFileEvent 034 extends AbstractTransactionEvent 035{ 036 private final File destination; 037 038 private final String content; 039 040 /** 041 * 042 * @param content 043 * @param destination 044 * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming 045 */ 046 public CreateFileEvent( String content, File destination, List<? extends Digester> digesters ) 047 { 048 super( digesters ); 049 this.content = content; 050 this.destination = destination; 051 } 052 053 @Override 054 public void commit() 055 throws IOException 056 { 057 createBackup( destination ); 058 059 mkDirs( destination.getParentFile() ); 060 061 if ( !destination.exists() && !destination.createNewFile() ) 062 { 063 throw new IOException( "Unable to create new file" ); 064 } 065 066 writeStringToFile( destination, content ); 067 068 createChecksums( destination, true ); 069 } 070 071 @Override 072 public void rollback() 073 throws IOException 074 { 075 destination.delete(); 076 077 revertFilesCreated(); 078 079 revertMkDirs(); 080 081 restoreBackups(); 082 } 083}