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 org.apache.archiva.checksum.ChecksumAlgorithm; 023 024import java.io.IOException; 025import java.nio.file.Files; 026import java.nio.file.Path; 027import java.util.List; 028 029/** 030 * Event for creating a file from a string content. 031 * 032 * 033 */ 034public class CreateFileEvent 035 extends AbstractTransactionEvent 036{ 037 private final Path destination; 038 039 private final String content; 040 041 /** 042 * 043 * @param content 044 * @param destination 045 * @param checksumAlgorithms digesters to use for checksumming 046 */ 047 public CreateFileEvent( String content, Path destination, List<ChecksumAlgorithm> checksumAlgorithms ) 048 { 049 super( checksumAlgorithms ); 050 this.content = content; 051 this.destination = destination; 052 } 053 054 @Override 055 public void commit() 056 throws IOException 057 { 058 createBackup( destination ); 059 060 mkDirs( destination.getParent() ); 061 062 if ( !Files.exists(destination)) 063 { 064 Files.createFile(destination); 065 } 066 067 writeStringToFile( destination, content ); 068 069 createChecksums( destination, true ); 070 } 071 072 @Override 073 public void rollback() 074 throws IOException 075 { 076 Files.deleteIfExists(destination); 077 078 revertFilesCreated(); 079 080 revertMkDirs(); 081 082 restoreBackups(); 083 } 084}