Java Path Copy nio copyFiles()

Here you can find the source of copyFiles()

Description

Copying resource files to the carbon home location.

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

private static void copyFiles() throws Exception 

Method Source Code

//package com.java2s;
/*//from  w ww. j  a  va2 s . co  m
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class Main {
    /**
     * Copying resource files to the carbon home location.
     *
     * @throws IOException
     */
    private static void copyFiles() throws Exception {
        //Replace the existing carbon.yml file with populated carbon.yml file.
        copy(Paths.get("src", "test", "resources", "conf", "carbon.yml"),
                Paths.get(System.getProperty("carbon.home"), "conf",
                        "carbon.yml"));

        //Replace the existing log4j2.xml file with populated log4j2.xml file.
        copy(Paths.get("src", "test", "resources", "conf", "log4j2.xml"),
                Paths.get("conf", "log4j2.xml"));

        //Replace the existing launch.properties file with populated launch.properties file.
        copy(Paths.get("src", "test", "resources", "conf", "osgi",
                "launch.properties"), Paths.get("conf", "osgi",
                "launch.properties"));

        //Copy deployment.yaml file
        copy(Paths
                .get("src", "test", "resources", "conf", "deployment.yml"),
                Paths.get("conf", "deployment.yml"));

        //Replace the existing "README.txt file with populated "README.txt file.
        copy(Paths.get("src", "test", "resources", "deployment", "uufapps",
                "README.txt"), Paths.get("deployment", "uufapps",
                "README.txt"));
    }

    /**
     * Copy files.
     *
     * @param sourcePath      Path for source
     * @param destinationPath Path for destination
     * @throws IOException
     */
    private static void copy(Path sourcePath, Path destinationPath)
            throws Exception {
        String basedir = System.getProperty("basedir");
        if (basedir == null) {
            basedir = Paths.get(".").toString();
        }

        sourcePath = Paths.get(basedir).resolve(sourcePath);
        destinationPath = getCarbonHome().resolve(destinationPath);

        createOutputFolderStructure(destinationPath);
        try {
            Files.copy(sourcePath, destinationPath,
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            throw new Exception("Error occurred while copying '"
                    + sourcePath + "' file to " + destinationPath + ".", e);
        }
    }

    /**
     * Return carbon home path
     *
     * @return Path for carbon home
     */
    private static Path getCarbonHome() {
        return Paths.get(System.getProperty("carbon.home"));
    }

    /**
     * Create the directory structure.
     *
     * @param destinationPath Path to file copying destination
     * @throws IOException
     */
    private static void createOutputFolderStructure(Path destinationPath)
            throws Exception {
        Path parentPath = destinationPath.getParent();
        try {
            Files.createDirectories(parentPath);
        } catch (IOException e) {
            throw new Exception(
                    "Error occurred while creating the directory '"
                            + parentPath + "'.", e);
        }
    }
}

Related

  1. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  2. copyFile(Path srcFile, Path srcDir, Path dstDir)
  3. copyFile(String srcPath, String destPath)
  4. copyFile(String urlPath, String outFile)
  5. copyFileFromResourcesToServer(String resourceFile, Path targetDirectory, boolean override)
  6. copyFiles(String srcPath, String destPath)
  7. copyFiles(String urlDir, String outPath)
  8. copyFilesAndApplyPermissions(Path sourceDir, Path targetDir, List filenames)
  9. copyFilesRecursively(final Path from, final Path to)