001 package 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 022 import java.io.File; 023 import java.io.IOException; 024 import java.util.List; 025 026 import org.codehaus.plexus.digest.Digester; 027 028 /** 029 * Event for creating a file from a string content. 030 * 031 * 032 */ 033 public 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 public void commit() 054 throws IOException 055 { 056 createBackup( destination ); 057 058 mkDirs( destination.getParentFile() ); 059 060 if ( !destination.exists() && !destination.createNewFile() ) 061 { 062 throw new IOException( "Unable to create new file" ); 063 } 064 065 writeStringToFile( destination, content ); 066 067 createChecksums( destination, true ); 068 } 069 070 public void rollback() 071 throws IOException 072 { 073 destination.delete(); 074 075 revertFilesCreated(); 076 077 revertMkDirs(); 078 079 restoreBackups(); 080 } 081 }