Example usage for org.apache.commons.io FileUtils copyFileToDirectory

List of usage examples for org.apache.commons.io FileUtils copyFileToDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyFileToDirectory.

Prototype

public static void copyFileToDirectory(File srcFile, File destDir) throws IOException 

Source Link

Document

Copies a file to a directory preserving the file date.

Usage

From source file:org.apache.kylin.tool.AbstractInfoExtractor.java

private void dumpBasicDiagInfo() throws IOException {
    try {/*from ww w. j  a va2  s .  co m*/
        for (String commitSHA1File : COMMIT_SHA1_FILES) {
            File commitFile = new File(KylinConfig.getKylinHome(), commitSHA1File);
            if (commitFile.exists()) {
                FileUtils.copyFileToDirectory(commitFile, exportDir);
            }
        }
    } catch (IOException e) {
        logger.warn("Failed to copy commit_SHA1 file.", e);
    }

    String output = KylinVersion.getKylinClientInformation() + "\n";
    FileUtils.writeStringToFile(new File(exportDir, "kylin_env"), output, Charset.defaultCharset());

    StringBuilder basicSb = new StringBuilder();
    basicSb.append("MetaStoreID: ").append(ToolUtil.getMetaStoreId()).append("\n");
    basicSb.append("PackageType: ").append(packageType.toUpperCase()).append("\n");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    basicSb.append("PackageTimestamp: ").append(format.format(new Date())).append("\n");
    basicSb.append("Host: ").append(ToolUtil.getHostName()).append("\n");
    FileUtils.writeStringToFile(new File(exportDir, "info"), basicSb.toString(), Charset.defaultCharset());
}

From source file:org.apache.kylin.tool.DiagnosisInfoCLI.java

@Override
protected void executeExtract(final OptionsHelper optionsHelper, final File exportDir) throws IOException {
    final String projectInput = optionsHelper.getOptionValue(options.getOption("project"));
    final boolean includeConf = optionsHelper.hasOption(OPTION_INCLUDE_CONF)
            ? Boolean.valueOf(optionsHelper.getOptionValue(OPTION_INCLUDE_CONF))
            : true;//from   w  w w .  j  a v a2  s.c o m
    final boolean includeHBase = optionsHelper.hasOption(OPTION_INCLUDE_HBASE)
            ? Boolean.valueOf(optionsHelper.getOptionValue(OPTION_INCLUDE_HBASE))
            : true;
    final boolean includeClient = optionsHelper.hasOption(OPTION_INCLUDE_CLIENT)
            ? Boolean.valueOf(optionsHelper.getOptionValue(OPTION_INCLUDE_CLIENT))
            : true;
    final boolean includeJob = optionsHelper.hasOption(OPTION_INCLUDE_JOB)
            ? Boolean.valueOf(optionsHelper.getOptionValue(OPTION_INCLUDE_JOB))
            : true;
    final int threadsNum = optionsHelper.hasOption(OPTION_THREADS)
            ? Integer.valueOf(optionsHelper.getOptionValue(OPTION_THREADS))
            : DEFAULT_PARALLEL_SIZE;
    final String projectNames = StringUtils.join(getProjects(projectInput), ",");
    final int period = optionsHelper.hasOption(OPTION_PERIOD)
            ? Integer.valueOf(optionsHelper.getOptionValue(OPTION_PERIOD))
            : DEFAULT_PERIOD;

    logger.info("Start diagnosis info extraction in {} threads.", threadsNum);
    executorService = Executors.newFixedThreadPool(threadsNum);

    // export cube metadata
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            logger.info("Start to extract metadata.");
            try {
                String[] cubeMetaArgs = { "-destDir", new File(exportDir, "metadata").getAbsolutePath(),
                        "-project", projectNames, "-compress", "false", "-includeJobs", "false", "-submodule",
                        "true" };
                CubeMetaExtractor cubeMetaExtractor = new CubeMetaExtractor();
                logger.info("CubeMetaExtractor args: " + Arrays.toString(cubeMetaArgs));
                cubeMetaExtractor.execute(cubeMetaArgs);
            } catch (Exception e) {
                logger.error("Error in export metadata.", e);
            }
        }
    });

    // extract all job instances
    if (includeJob) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                logger.info("Start to extract jobs.");
                try {
                    String[] jobArgs = { "-destDir", new File(exportDir, "jobs").getAbsolutePath(), "-period",
                            Integer.toString(period), "-compress", "false", "-submodule", "true" };
                    JobInstanceExtractor jobInstanceExtractor = new JobInstanceExtractor();
                    jobInstanceExtractor.execute(jobArgs);
                } catch (Exception e) {
                    logger.error("Error in export jobs.", e);
                }
            }
        });
    }

    // export HBase
    if (includeHBase) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                logger.info("Start to extract HBase usage.");
                try {
                    // use reflection to isolate NoClassDef errors when HBase is not available
                    String[] hbaseArgs = { "-destDir", new File(exportDir, "hbase").getAbsolutePath(),
                            "-project", projectNames, "-compress", "false", "-submodule", "true" };
                    logger.info("HBaseUsageExtractor args: " + Arrays.toString(hbaseArgs));
                    Object extractor = ClassUtil.newInstance("org.apache.kylin.tool.HBaseUsageExtractor");
                    Method execute = extractor.getClass().getDeclaredMethod("execute", String[].class);
                    execute.invoke(extractor, (Object) hbaseArgs);
                } catch (Throwable e) {
                    logger.error("Error in export HBase usage.", e);
                }
            }
        });
    }

    // export conf
    if (includeConf) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                logger.info("Start to extract kylin conf files.");
                try {
                    File destConfDir = new File(exportDir, "conf");
                    FileUtils.forceMkdir(destConfDir);
                    File srcConfDir = new File(ToolUtil.getConfFolder());
                    Preconditions.checkState(srcConfDir.exists(),
                            "Cannot find config dir: " + srcConfDir.getAbsolutePath());
                    File[] confFiles = srcConfDir.listFiles();
                    if (confFiles != null) {
                        for (File confFile : confFiles) {
                            FileUtils.copyFileToDirectory(confFile, destConfDir);
                        }
                    }
                } catch (Exception e) {
                    logger.error("Error in export conf.", e);
                }
            }
        });
    }

    // export client
    if (includeClient) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String[] clientArgs = { "-destDir", new File(exportDir, "client").getAbsolutePath(),
                            "-compress", "false", "-submodule", "true" };
                    ClientEnvExtractor clientEnvExtractor = new ClientEnvExtractor();
                    logger.info("ClientEnvExtractor args: " + Arrays.toString(clientArgs));
                    clientEnvExtractor.execute(clientArgs);
                } catch (Exception e) {
                    logger.error("Error in export client info.", e);
                }
            }
        });
    }

    // export logs
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            logger.info("Start to extract logs.");
            try {
                String[] logsArgs = { "-destDir", new File(exportDir, "logs").getAbsolutePath(), "-logPeriod",
                        Integer.toString(period), "-compress", "false", "-submodule", "true" };
                KylinLogExtractor logExtractor = new KylinLogExtractor();
                logger.info("KylinLogExtractor args: " + Arrays.toString(logsArgs));
                logExtractor.execute(logsArgs);
            } catch (Exception e) {
                logger.error("Error in export logs.", e);
            }
        }
    });

    executorService.shutdown();
    try {
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Diagnosis info dump interrupted.", e);
    }
}

From source file:org.apache.maven.plugin.dependency.GetMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    if (isSkip()) {
        getLog().info("Skipping plugin execution");
        return;//from  w  w w .  j  a v  a2  s  . c  om
    }

    if (artifactId == null && artifact == null) {
        throw new MojoFailureException("You must specify an artifact, "
                + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0");
    }
    if (artifact != null) {
        String[] tokens = StringUtils.split(artifact, ":");
        if (tokens.length < 3 || tokens.length > 5) {
            throw new MojoFailureException(
                    "Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] "
                            + artifact);
        }
        groupId = tokens[0];
        artifactId = tokens[1];
        version = tokens[2];
        if (tokens.length >= 4) {
            packaging = tokens[3];
        }
        if (tokens.length == 5) {
            classifier = tokens[4];
        } else {
            classifier = null;
        }
    }

    Artifact toDownload = classifier == null
            ? artifactFactory.createBuildArtifact(groupId, artifactId, version, packaging)
            : artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, packaging, classifier);
    Artifact dummyOriginatingArtifact = artifactFactory.createBuildArtifact("org.apache.maven.plugins",
            "maven-downloader-plugin", "1.0", "jar");

    ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(true,
            ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);

    List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();

    if (pomRemoteRepositories != null) {
        repoList.addAll(pomRemoteRepositories);
    }

    if (remoteRepositories != null) {
        // Use the same format as in the deploy plugin id::layout::url
        List<String> repos = Arrays.asList(StringUtils.split(remoteRepositories, ","));
        for (String repo : repos) {
            repoList.add(parseRepository(repo, always));
        }
    }

    if (repositoryUrl != null) {
        getLog().warn("repositoryUrl parameter is deprecated. Use remoteRepositories instead");
        ArtifactRepository remoteRepo = artifactRepositoryFactory.createArtifactRepository(repositoryId,
                repositoryUrl, getLayout("default"), always, always);
        repoList.add(remoteRepo);
    }

    try {
        if (transitive) {
            getLog().info("Resolving " + toDownload + " with transitive dependencies");
            artifactResolver.resolveTransitively(Collections.singleton(toDownload), dummyOriginatingArtifact,
                    repoList, localRepository, source);
        } else {
            getLog().info("Resolving " + toDownload);
            artifactResolver.resolve(toDownload, repoList, localRepository);
        }
    } catch (AbstractArtifactResolutionException e) {
        throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
    }

    if (destination != null) {
        getLog().warn("destination/dest parameter is deprecated: it will disappear in future version.");

        File src = toDownload.getFile();
        File dest = new File(destination);

        getLog().info("Copying " + src.getAbsolutePath() + " to " + dest.getAbsolutePath());
        if (transitive) {
            getLog().warn("Notice transitive dependencies won't be copied.");
        }

        try {
            if (dest.isDirectory()) {
                FileUtils.copyFileToDirectory(src, dest);
            } else {
                FileUtils.copyFile(src, dest);
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Couldn't copy downloaded artifact from " + src.getAbsolutePath()
                    + " to " + dest.getAbsolutePath() + " : " + e.getMessage(), e);
        }
    }
}

From source file:org.apache.myfaces.trinidadbuild.plugin.javascript.ReduceJavascriptMojo.java

static private void _copyJavascript(File sourceDir, File targetDir) throws IOException {
    File[] sourceFiles = sourceDir.listFiles();
    if (sourceFiles != null) {
        for (File sourceFile : sourceFiles) {
            if (sourceFile.isDirectory()) {
                File targetSubdir = new File(targetDir, sourceFile.getName());
                if (!targetSubdir.exists())
                    targetSubdir.mkdir();
                _copyJavascript(sourceFile, targetSubdir);
            } else if (sourceFile.getName().endsWith(".js")) {
                FileUtils.copyFileToDirectory(sourceFile, targetDir);
            }/*from  w w  w. jav a 2 s. c  o m*/
        }
    }
}

From source file:org.apache.oodt.cas.crawl.action.MoveFile.java

public boolean performAction(File product, Metadata productMetadata) throws CrawlerActionException {
    String mvFile = file;//  ww  w.  j  av  a 2s. c o  m
    try {
        if (mvFile == null) {
            mvFile = product.getAbsolutePath();
            if (this.fileExtension != null) {
                mvFile += "." + this.fileExtension;
            }
        }
        File srcFile = new File(mvFile);
        File toFile = new File(toDir + "/" + srcFile.getName());
        if (createToDir) {
            toFile.getParentFile().mkdirs();
        }
        LOG.log(Level.INFO, "Moving file " + srcFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
        if (!srcFile.renameTo(toFile)) {//If the file failed to copy
            LOG.log(Level.INFO, "Moving failed, possibly because ingest dir is nfs mounted. Retrying to move "
                    + srcFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
            FileUtils.copyFileToDirectory(srcFile, toFile.getParentFile());
            FileUtils.forceDelete(srcFile); //Need to delete the old file
            return true; //File copied on second attempt
        } else {
            return true; //File copied
        }
    } catch (Exception e) {
        throw new CrawlerActionException(
                "Failed to move file from " + mvFile + " to " + this.toDir + " : " + e.getMessage(), e);
    }
}

From source file:org.apache.oodt.cas.filemgr.tools.ProductTypeDocTool.java

public void doProductTypeDoc(String productTypeXmlFilePath, String elementXmlFilePath)
        throws IOException, TransformerException {
    // copy element xml to current path
    FileUtils.copyFileToDirectory(new File(elementXmlFilePath), new File("."));
    // copy product type xsl to current path
    FileUtils.copyFileToDirectory(new File(xslFilePath), new File("."));

    String xslLocalFilePath = new File(".").getAbsolutePath();
    if (!xslLocalFilePath.endsWith("/")) {
        xslLocalFilePath += "/";
    }/*w ww  .  j  a  v  a2 s .  c  o  m*/
    xslLocalFilePath += new File(xslFilePath).getName();

    String elementLocalFilePath = new File(".").getAbsolutePath();
    if (!elementLocalFilePath.endsWith("/")) {
        elementLocalFilePath += "/";
    }

    elementLocalFilePath += new File(elementXmlFilePath).getName();

    Transformer xformer = TransformerFactory.newInstance()
            .newTransformer(new StreamSource(new File(xslLocalFilePath)));

    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    String productTypeFileName = new File(productTypeXmlFilePath).getName();
    String outputProductTypeDocFileName = productTypeFileName.replaceAll("xml", "html");

    Result result = new StreamResult(new File(outputDirPath + outputProductTypeDocFileName));

    xformer.transform(new StreamSource(new File(productTypeXmlFilePath)), result);

    // now cleanup
    new File(xslLocalFilePath).delete();
    new File(elementLocalFilePath).delete();
}

From source file:org.apache.oodt.cas.filemgr.validation.TestXMLValidationLayer.java

@Override
protected void setUp() throws Exception {
    try {/*w ww . j  a  v a  2s .c  o m*/
        File tempDir = File.createTempFile("ignore", "txt").getParentFile();
        // copy the val layer policy into the temp dir
        URL url = this.getClass().getResource("/vallayer");
        for (File f : new File(url.getFile()).listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                return pathname.isFile();
            }
        })) {
            FileUtils.copyFileToDirectory(f, tempDir);
        }

        testDirUris.add(tempDir.toURI().toString());
        validationLayer = new XMLValidationLayer(testDirUris);

    } catch (IOException e) {
        fail(e.getMessage());
    }
}

From source file:org.apache.oodt.cas.install.CASInstallDistMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {

    if (casDistributionFile == null || (!casDistributionFile.exists())) {
        throw new MojoExecutionException("the CAS distribution: [" + casDistributionFile + "] does not exist!");
    }//from   w w  w.  j av a  2s  .  c  o m

    // remove cas installation libs directory and its contents if custom libs specified.
    // this is to prevent legacy jars from polluting the cas installation libs directory
    if (customLibs != null && customLibs.length > 0) {

        File libDir = null;

        // get the lib dir
        try {
            libDir = new File(casInstallationDir.getCanonicalPath() + File.separator + LIB_DIR_NAME);

        } catch (IOException e) {
            getLog().warn("Unable to detect lib dir: IO exception: " + e.getMessage());
        }

        // delete the lib dir
        if (libDir != null) {

            getLog().warn("removing pre-existing CAS libraries directory [" + libDir.getAbsolutePath()
                    + "] since custom CAS libraries have been specified");

            try {
                FileUtils.deleteDirectory(libDir);
            } catch (IOException e) {
                getLog().warn("Unable to delete lib dir [" + libDir.getAbsolutePath() + "]: " + e.getMessage());
            }
        }
    }

    getLog().info("unpackaging distro: [" + casDistributionFile + "] to: [" + casInstallationDir + "]");
    try {
        AntDecorator.untarFile(casDistributionFile, casInstallationDir);
    } catch (IOException e) {
        throw new MojoExecutionException(
                "an IO exception occured while untarring the CAS distribution: Message: " + e.getMessage());
    }

    if (customPolicyDirs != null && customPolicyDirs.length > 0) {
        getLog().info("installing [" + customPolicyDirs.length + "] custom policy dirs");

        // remove the default policy
        File policyDir = null;
        try {
            policyDir = new File(casInstallationDir.getCanonicalPath() + File.separator + POLICY_DIR_NAME);
            AntDecorator.deleteAllFilesAndDir(policyDir);
        } catch (IOException e) {
            getLog().warn("IO exception when removing default policy from null policy dir: " + "Message: "
                    + e.getMessage());
        }

        for (File customPolicyDir : customPolicyDirs) {
            getLog().info("Installing: [" + customPolicyDir + "] to: [" + policyDir + "]");

            if (customPolicyDir.exists()) {
                try {
                    FileUtils.copyDirectoryToDirectory(customPolicyDir, policyDir);
                } catch (IOException e) {
                    getLog().warn("error copying custom policy dir: [" + customPolicyDir + "] to policy dir: ["
                            + policyDir + "]");
                }
            }

        }

    }

    if (customConfigurationDir != null && customConfigurationDir.exists()) {

        try {
            File configDir = new File(casInstallationDir.getCanonicalPath() + File.separator + CONFIG_DIR_NAME);

            // remove default config
            AntDecorator.deleteAllFilesAndDir(configDir);

            configDir.mkdir();

            // install custom config
            FileUtils.copyDirectory(customConfigurationDir, configDir, true);

        } catch (IOException e) {
            getLog().warn("Unable to detect configuration dir: IO exception: " + e.getMessage());
        }

    }

    if (customLibs != null && customLibs.length > 0) {
        getLog().info("installing [" + customLibs.length + "] custom CAS libraries");

        File libDir = null;
        // get the lib dir
        try {
            libDir = new File(casInstallationDir.getCanonicalPath() + File.separator + LIB_DIR_NAME);

        } catch (IOException e) {
            getLog().warn("Unable to detect lib dir: IO exception: " + e.getMessage());
        }

        for (File customLib : customLibs) {
            getLog().info("installing [" + customLib + "] to " + libDir.getAbsolutePath() + "]");
            try {
                FileUtils.copyFileToDirectory(customLib, libDir);
            } catch (IOException e) {
                getLog().warn("IOException installing [" + customLib + "] to " + libDir.getAbsolutePath()
                        + "]: Message: " + e.getMessage());
            }
        }
    }

    if (envVarReplaceFiles != null && envVarReplaceFiles.length > 0) {
        getLog().info("Replacing env vars on [" + envVarReplaceFiles.length + "] files");

        for (EnvReplacer envVarReplaceFile : envVarReplaceFiles) {
            try {
                envVarReplaceFile.doEnvReplace();
            } catch (IOException e) {
                getLog().warn("IOException while doing env replacement on: [" + envVarReplaceFile.getFilepath()
                        + "]: Message: " + e.getMessage());
            }
        }
    }

    if (customBinScripts != null && customBinScripts.length > 0) {
        getLog().info("installing [" + customBinScripts.length + "] custom bin scripts");

        File binDir = null;

        try {
            binDir = new File(casInstallationDir.getCanonicalPath() + File.separator + BIN_DIR_NAME);
        } catch (IOException e) {
            getLog().warn("Unable to detect bin dir: IO exception: Message: " + e.getMessage());
        }

        for (File customBinScript : customBinScripts) {
            getLog().info("installing [" + customBinScript + "] to [" + binDir + "]");
            try {
                FileUtils.copyFileToDirectory(customBinScript, binDir);
                // now chmod it with exec perms
                String custBinScriptFullPath = binDir + File.separator + customBinScript.getName();

                getLog().info("fixing perms on [" + custBinScriptFullPath + "]");
                AntDecorator.chmodFile(new File(custBinScriptFullPath), "ugo+rx");
            } catch (IOException e) {
                getLog().warn("unable to install [" + customBinScript + "] to [" + binDir
                        + "]: IO exception: Message: " + e.getMessage());
            }
        }
    }

}

From source file:org.apache.oodt.cas.resource.system.TestAvroRpcResourceManager.java

private void generateTestConfiguration() throws IOException {
    Properties config = new Properties();

    String propertiesFile = "." + File.separator + "src" + File.separator + "test" + File.separator
            + "resources" + File.separator + "test.resource.properties";
    System.getProperties().load(new FileInputStream(new File("./src/test/resources/test.resource.properties")));

    // stage policy
    File tmpPolicyDir = null;//  w  w w . jav  a 2  s  .  com
    try {
        tmpPolicyDir = File.createTempFile("test", "ignore").getParentFile();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    for (File policyFile : new File("./src/test/resources/policy").listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isFile() && pathname.getName().endsWith(".xml");
        }
    })) {
        try {
            FileUtils.copyFileToDirectory(policyFile, tmpPolicyDir);
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }

    config.setProperty("org.apache.oodt.cas.resource.nodes.dirs", tmpPolicyDir.toURI().toString());
    config.setProperty("org.apache.oodt.cas.resource.nodetoqueues.dirs", tmpPolicyDir.toURI().toString());

    System.getProperties().putAll(config);
    this.tmpPolicyDir = tmpPolicyDir;
}

From source file:org.apache.oodt.cas.resource.system.TestAvroRpcResourceManagerClient.java

private static void generateTestConfiguration() throws IOException {
    Properties config = new Properties();

    String propertiesFile = "." + File.separator + "src" + File.separator + "test" + File.separator
            + "resources" + File.separator + "test.resource.properties";
    System.getProperties().load(new FileInputStream(new File(propertiesFile)));

    // stage policy
    File tmpPolicyDir = null;//w w w .ja  v  a 2  s. c  o m
    try {
        tmpPolicyDir = File.createTempFile("test", "ignore").getParentFile();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    for (File policyFile : new File("./src/test/resources/policy").listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isFile() && pathname.getName().endsWith(".xml");
        }
    })) {
        try {
            FileUtils.copyFileToDirectory(policyFile, tmpPolicyDir);
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }

    config.setProperty("org.apache.oodt.cas.resource.nodes.dirs", tmpPolicyDir.toURI().toString());
    config.setProperty("org.apache.oodt.cas.resource.nodetoqueues.dirs", tmpPolicyDir.toURI().toString());

    System.getProperties().putAll(config);

}