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;
021import java.util.Optional;
022
023import org.apache.maven.plugin.MojoExecutionException;
024import org.apache.maven.plugin.logging.Log;
025import org.apache.maven.scm.provider.ScmProviderRepository;
026import org.apache.maven.settings.Server;
027import org.apache.maven.settings.Settings;
028import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
029import org.apache.maven.settings.crypto.SettingsDecrypter;
030import org.apache.maven.settings.crypto.SettingsDecryptionResult;
031import org.codehaus.plexus.util.FileUtils;
032
033/**
034 * Shared static functions for all of our Mojos.
035 *
036 * @author chtompki
037 * @since 1.0
038 */
039public final class SharedFunctions {
040
041    /**
042     * I want a buffer that is an array with 1024 elements of bytes. We declare
043     * the constant here for the sake of making the code more readable.
044     */
045    public static final int BUFFER_BYTE_SIZE = 1024;
046
047    /**
048     * Making the constructor private because the class only contains static methods.
049     */
050    private SharedFunctions() {
051        // Utility Class
052    }
053
054    /**
055     * Cleans and then initializes an empty directory that is given by the <code>workingDirectory</code>
056     * parameter.
057     *
058     * @param log is the Maven log for output logging, particularly in regards to error management.
059     * @param workingDirectory is a {@link File} that represents the directory to first attempt to delete then create.
060     * @throws MojoExecutionException when an {@link IOException} or {@link NullPointerException} is caught for the
061     *      purpose of bubbling the exception up to Maven properly.
062     */
063    public static void initDirectory(Log log, File workingDirectory) throws MojoExecutionException {
064        if (workingDirectory.exists()) {
065            try {
066                FileUtils.deleteDirectory(workingDirectory);
067            } catch (IOException | NullPointerException e) {
068                final String message = String.format("Unable to remove directory %s: %s", workingDirectory,
069                        e.getMessage());
070                log.error(message);
071                throw new MojoExecutionException(message, e);
072            }
073        }
074        if (!workingDirectory.exists()) {
075            workingDirectory.mkdirs();
076        }
077    }
078
079    /**
080     * Copies a {@link File} from the <code>fromFile</code> to the <code>toFile</code> and logs the failure
081     * using the Maven {@link Log}.
082     *
083     * @param log the {@link Log}, the maven logger.
084     * @param fromFile the {@link File} from which to copy.
085     * @param toFile the {@link File} to which to copy into.
086     * @throws MojoExecutionException if an {@link IOException} or {@link NullPointerException} is caught.
087     */
088    public static void copyFile(Log log, File fromFile, File toFile) throws MojoExecutionException {
089        try {
090            FileUtils.copyFile(fromFile, toFile);
091        } catch (IOException | NullPointerException e) {
092            final String message = String.format("Unable to copy file %s tp %s: %s", fromFile, toFile, e.getMessage());
093            log.error(message);
094            throw new MojoExecutionException(message, e);
095        }
096    }
097
098    /**
099     * Set authentication information on the specified {@link ScmProviderRepository}.
100     * @param providerRepository target.
101     * @param distServer temp.
102     * @param settings temp.
103     * @param settingsDecrypter temp.
104     * @param username temp.
105     * @param password temp.
106     */
107    public static void setAuthentication(ScmProviderRepository providerRepository,
108                                   String distServer,
109                                   Settings settings,
110                                   SettingsDecrypter settingsDecrypter,
111                                   String username,
112                                   String password) {
113        Optional<Server> server =
114                Optional.ofNullable(distServer).map(settings::getServer).map(DefaultSettingsDecryptionRequest::new)
115                        .map(settingsDecrypter::decrypt).map(SettingsDecryptionResult::getServer);
116
117        providerRepository.setUser(server.map(Server::getUsername).orElse(username));
118        providerRepository.setPassword(server.map(Server::getPassword).orElse(password));
119    }
120}