1package org.apache.archiva.transaction;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.checksum.ChecksumAlgorithm;
2324import java.io.IOException;
25import java.nio.file.Files;
26import java.nio.file.Path;
27import java.util.List;
2829/**30 * Event for creating a file from a string content.31 *32 *33 */34publicclassCreateFileEvent35extendsAbstractTransactionEvent36 {
37privatefinal Path destination;
3839privatefinal String content;
4041/**42 * 43 * @param content44 * @param destination45 * @param checksumAlgorithms digesters to use for checksumming46 */47publicCreateFileEvent( String content, Path destination, List<ChecksumAlgorithm> checksumAlgorithms )
48 {
49super( checksumAlgorithms );
50this.content = content;
51this.destination = destination;
52 }
5354 @Override
55publicvoid commit()
56throws IOException
57 {
58 createBackup( destination );
5960 mkDirs( destination.getParent() );
6162if ( !Files.exists(destination))
63 {
64 Files.createFile(destination);
65 }
6667 writeStringToFile( destination, content );
6869 createChecksums( destination, true );
70 }
7172 @Override
73publicvoid rollback()
74throws IOException
75 {
76 Files.deleteIfExists(destination);
7778 revertFilesCreated();
7980 revertMkDirs();
8182 restoreBackups();
83 }
84 }