001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.release.plugin;
018
019import java.io.File;
020import java.io.IOException;
021
022import org.apache.maven.plugin.MojoExecutionException;
023import org.apache.maven.plugin.logging.Log;
024import org.codehaus.plexus.util.FileUtils;
025
026/**
027 * Shared static functions for all of our Mojos.
028 *
029 * @author chtompki
030 * @since 1.0
031 */
032public final class SharedFunctions {
033
034    /**
035     * I want a buffer that is an array with 1024 elements of bytes. We declare
036     * the constant here for the sake of making the code more readable.
037     */
038    public static final int BUFFER_BYTE_SIZE = 1024;
039
040    /**
041     * Making the constructor private because the class only contains static methods.
042     */
043    private SharedFunctions() {
044        // Utility Class
045    }
046
047    /**
048     * Cleans and then initializes an empty directory that is given by the <code>workingDirectory</code>
049     * parameter.
050     *
051     * @param log is the Maven log for output logging, particularly in regards to error management.
052     * @param workingDirectory is a {@link File} that represents the directory to first attempt to delete then create.
053     * @throws MojoExecutionException when an {@link IOException} or {@link NullPointerException} is caught for the
054     *      purpose of bubbling the exception up to Maven properly.
055     */
056    public static void initDirectory(Log log, File workingDirectory) throws MojoExecutionException {
057        if (workingDirectory.exists()) {
058            try {
059                FileUtils.deleteDirectory(workingDirectory);
060            } catch (IOException | NullPointerException e) {
061                final String message = String.format("Unable to remove directory %s: %s", workingDirectory,
062                        e.getMessage());
063                log.error(message);
064                throw new MojoExecutionException(message, e);
065            }
066        }
067        if (!workingDirectory.exists()) {
068            workingDirectory.mkdirs();
069        }
070    }
071
072    /**
073     * Copies a {@link File} from the <code>fromFile</code> to the <code>toFile</code> and logs the failure
074     * using the Maven {@link Log}.
075     *
076     * @param log the {@link Log}, the maven logger.
077     * @param fromFile the {@link File} from which to copy.
078     * @param toFile the {@link File} to which to copy into.
079     * @throws MojoExecutionException if an {@link IOException} or {@link NullPointerException} is caught.
080     */
081    public static void copyFile(Log log, File fromFile, File toFile) throws MojoExecutionException {
082        try {
083            FileUtils.copyFile(fromFile, toFile);
084        } catch (IOException | NullPointerException e) {
085            final String message = String.format("Unable to copy file %s tp %s: %s", fromFile, toFile, e.getMessage());
086            log.error(message);
087            throw new MojoExecutionException(message, e);
088        }
089    }
090}