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

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

Introduction

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

Prototype

public static void cleanDirectory(File directory) throws IOException 

Source Link

Document

Cleans a directory without deleting it.

Usage

From source file:com.github.ipaas.ideploy.agent.util.SVNUtil.java

/**
 * /*  ww w .ja  v a2s.  c  o m*/
 * ?path2path1?
 * @param path1   
 * @param revision1  path1?
 * @param path2
 * @param revision2  path2?
 * @param localPath   ??
 * @param deployPath    ??
 * @throws Exception
 */
public static void getDelta(final String path1, final long revision1, final String path2, final long revision2,
        String localPath) throws Exception {
    DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
    ISVNAuthenticationManager authManager = SVNWCUtil
            .createDefaultAuthenticationManager(Constants.CRS_REPOS_USER, Constants.CRS_REPOS_PWD);
    SVNClientManager svnClientManager = null;
    try {
        //
        File localDict = new File(localPath);
        if (!localDict.exists()) {
            localDict.mkdirs();
        } else {
            FileUtils.cleanDirectory(localDict);
        }

        svnClientManager = SVNClientManager.newInstance(options, authManager);

        SVNRepository repository = svnClientManager
                .createRepository(SVNURL.parseURIEncoded(Constants.CRS_REPOS), true);

        Collection<SVNLog> logs = null;

        boolean update4All = false;

        //???
        if (path1 == null || path1.equals("") || path1.equals(path2)) {
            update4All = true;
        }

        if (update4All) {
            //
            export(svnClientManager, path2, revision2, localPath + Constants.UPDPKG_CODEDIR);
            logs = new LinkedList<SVNLog>();
            logs.add(new SVNLog4All());
        } else {
            //???
            logs = diff(svnClientManager, path1, revision1, path2, revision2);

            if (logs != null && !logs.isEmpty()) {
                // ???
                getDelta4Add(repository, path2, logs, localPath + Constants.UPDPKG_CODEDIR);
            } else {
                //? ?
                export(svnClientManager, path2, revision2, localPath + Constants.UPDPKG_CODEDIR);
                logs = new LinkedList<SVNLog>();
                logs.add(new SVNLog4All());
            }
        }

        //
        writeLogToFile(logs, new File(localPath + Constants.UPDPKG_UPDTXT));
    } finally {
        if (svnClientManager != null) {
            svnClientManager.dispose();
        }
    }
}

From source file:com.phoenixnap.oss.ramlapisync.generation.RamlGenerator.java

/**
 * Create and clean the directories specified in the file path if requested
 *
 * @param file file to be generated//  ww w .  j  a  v a 2  s  .  co  m
 * @param createPathIfMissing should missing directories be created
 * @param removeOldOutput remove any existing contents from destination
  */
private void prepareDirectories(File file, Boolean createPathIfMissing, Boolean removeOldOutput) {
    File outputDirectory;

    if (file.isDirectory()) {
        outputDirectory = file;
    } else {
        outputDirectory = file.getParentFile();
    }

    if (!outputDirectory.exists() && createPathIfMissing) {
        if (!outputDirectory.mkdirs()) {
            logger.info("Failed to create directory: " + outputDirectory);
        }
    }

    if (removeOldOutput) {
        try {
            FileUtils.cleanDirectory(outputDirectory);
        } catch (IOException ioe) {
            logger.error("Failed to clean directory: " + outputDirectory, ioe);
        }
    }
}

From source file:massbank.svn.SVNClientWrapper.java

private void cleanDirectory() {
    logger.info("[SVNService] cleanDirectory");
    try {//  w ww  .  j  av a  2  s .  co  m
        FileUtils.cleanDirectory(this.workCopy);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.hortonworks.streamline.streams.actions.topology.service.TopologyActionsService.java

private void makeEmptyDir(Path path) throws IOException {
    if (path.toFile().exists()) {
        if (path.toFile().isDirectory()) {
            FileUtils.cleanDirectory(path.toFile());
        } else {//from   w  w  w. ja v  a  2  s . c  o  m
            final String errorMessage = String.format("Location '%s' must be a directory.", path);
            LOG.error(errorMessage);
            throw new IOException(errorMessage);
        }
    } else if (!path.toFile().mkdirs()) {
        LOG.error("Could not create dir {}", path);
        throw new IOException("Could not create dir: " + path);
    }
}

From source file:com.hp.test.framework.model.testcasegen.TestCaseGenerator.java

void purgeDirectory(File dir) {
    //    for (File file: dir.listFiles()) {
    //        if (file.isDirectory()) purgeDirectory(file);
    //        file.delete();
    try {/*from  w  w w  .  ja  va  2s  . com*/
        FileUtils.cleanDirectory(dir);
        log.info("Cleared the template directory");
    } catch (IOException e) {
        log.error("Clean directory issue" + e.fillInStackTrace());
    }
}

From source file:it.infn.ct.SimpleToscaInterface.java

/**
 * Process JSON object containing information stored in file:
 * <action_info>/<task_id>.json and submit using tosca adaptor.
 * @return TOSCA orchestrator qUUID/* w w w  .  jav a2s  .com*/
 */
public final int submitTosca() {
    int simpleToscaId = 0;
    JSONObject jsonJobDesc = null;
    LOG.debug("Entering submitSimpleTosca");
    String jobDescFileName = toscaCommand.getActionInfo() + FS + toscaCommand.getTaskId() + ".json";
    LOG.debug("JSON filename: '" + jobDescFileName + "'");
    try {
        // Prepare jobOutput dir for output sandbox
        String outputSandbox = toscaCommand.getActionInfo() + FS + JO;
        LOG.debug("Creating job output directory: '" + outputSandbox + "'");
        File outputSandboxDir = new File(outputSandbox);
        if (!outputSandboxDir.exists()) {
            LOG.debug("Creating job output directory");
            outputSandboxDir.mkdir();
            LOG.debug("Job output successfully created");
        } else {
            // Directory altready exists; clean all its content
            LOG.debug("Cleaning job output directory");
            FileUtils.cleanDirectory(outputSandboxDir);
            LOG.debug("Successfully cleaned job output directory");
        }
        // Now read values from JSON and prepare the submission accordingly
        InputStream is = new FileInputStream(jobDescFileName);
        String jsonTxt = IOUtils.toString(is);
        jsonJobDesc = (JSONObject) new JSONObject(jsonTxt);
        LOG.debug("Loaded APIServer JobDesc:\n" + LS + jsonJobDesc);
        // Username (unused yet but later used for accounting)
        String user = String.format("%s", jsonJobDesc.getString("user"));
        LOG.debug("User: '" + user + "'");
        // Get app Info and Parameters
        JSONObject appInfo = new JSONObject();
        appInfo = jsonJobDesc.getJSONObject("application");
        JSONArray appParams = new JSONArray();
        appParams = appInfo.getJSONArray("parameters");
        // Application parameters
        String executable = "";
        String output = "";
        String error = "";
        String arguments = "";
        for (int i = 0; i < appParams.length(); i++) {
            JSONObject appParameter = appParams.getJSONObject(i);
            // Get parameter name and value
            String paramName = appParameter.getString("param_name");
            String paramValue = appParameter.getString("param_value");
            switch (paramName) {
            case "target_executor":
                LOG.debug("target_executor: '" + paramValue + "'");
                break;
            case "jobdesc_executable":
                executable = paramValue;
                LOG.debug("executable: '" + executable + "'");
                break;
            case "jobdesc_output":
                output = paramValue;
                LOG.debug("output: '" + output + "'");
                break;
            case "jobdesc_error":
                error = paramValue;
                LOG.debug("error: '" + error + "'");
                break;
            case "jobdesc_arguments":
                arguments = paramValue;
                LOG.debug("arguments: '" + arguments + "'");
                break;
            default:
                LOG.warn("Unsupported application parameter name: '" + paramName + "' with value: '"
                        + paramValue + "'");
            }
        }
        // Arguments
        String jobArgs = arguments;
        JSONArray jobArguments = jsonJobDesc.getJSONArray("arguments");
        for (int j = 0; j < jobArguments.length(); j++) {
            jobArgs += ((jobArgs.length() > 0) ? "," : "") + jobArguments.getString(j);
        }
        String[] args = jobArgs.split(",");
        for (int k = 0; k < args.length; k++) {
            LOG.debug("args[" + k + "]: '" + args[k] + "'");
        }
        // Infrastructures
        // Select one of the possible infrastructures among the enabled
        // ones. A random strategy is currently implemented; this could be
        //changed later
        JSONArray jobInfrastructures = appInfo.getJSONArray("infrastructures");
        JSONArray enabledInfras = new JSONArray();
        for (int v = 0, w = 0; w < jobInfrastructures.length(); w++) {
            JSONObject infra = jobInfrastructures.getJSONObject(w);
            if (infra.getString("status").equals("enabled")) {
                enabledInfras.put(v++, infra);
            }
        }
        int selInfraIdx = 0;
        Random rndGen = new Random();
        if (enabledInfras.length() > 1) {
            selInfraIdx = rndGen.nextInt(enabledInfras.length());
        }
        JSONObject selInfra = new JSONObject();
        selInfra = enabledInfras.getJSONObject(selInfraIdx);
        LOG.debug("Selected infra: '" + LS + selInfra.toString() + "'");
        // Infrastructure parameters
        String toscaEndPoint = "";
        String toscaParameters = "";
        String toscaTemplate = "";
        String token = "";
        JSONArray infraParams = selInfra.getJSONArray("parameters");
        for (int h = 0; h < infraParams.length(); h++) {
            JSONObject infraParameter = infraParams.getJSONObject(h);
            String paramName = infraParameter.getString("name");
            String paramValue = infraParameter.getString("value");
            switch (paramName) {
            case "tosca_endpoint":
                toscaEndPoint = paramValue;
                LOG.debug("tosca_endpoint: '" + toscaEndPoint + "'");
                break;
            case "tosca_token":
                token = paramValue;
                LOG.debug("tosca_token: '" + token + "'");
                break;
            case "tosca_template":
                toscaTemplate = toscaCommand.getActionInfo() + "/" + paramValue;
                LOG.debug("tosca_template: '" + toscaTemplate + "'");
                break;
            case "tosca_parameters":
                toscaParameters = "&" + paramValue;
                LOG.debug("tosca_parameters: '" + toscaParameters + "'");
                break;
            default:
                LOG.warn("Unsupported infrastructure parameter name: '" + paramName + "' with value: '"
                        + paramValue + "'");
            }
        }
        // Prepare JSAGA IO file list
        String ioFiles = "";
        JSONArray inputFiles = jsonJobDesc.getJSONArray("input_files");
        for (int i = 0; i < inputFiles.length(); i++) {
            JSONObject fileEntry = inputFiles.getJSONObject(i);
            String fileName = fileEntry.getString("name");
            ioFiles += ((ioFiles.length() > 0) ? "," : "") + toscaCommand.getActionInfo() + FS
                    + fileEntry.getString("name") + ">" + fileEntry.getString("name");
        }
        JSONArray outputFiles = jsonJobDesc.getJSONArray("output_files");
        for (int j = 0; j < outputFiles.length(); j++) {
            JSONObject fileEntry = outputFiles.getJSONObject(j);
            String fileName = fileEntry.getString("name");
            ioFiles += ((ioFiles.length() > 0) ? "," : "") + toscaCommand.getActionInfo() + FS + JO + FS
                    + fileEntry.getString("name") + "<" + fileEntry.getString("name");
        }
        LOG.debug("IOFiles: '" + ioFiles + "'");
        String[] files = ioFiles.split(",");
        for (int i = 0; i < files.length; i++) {
            LOG.debug("IO Files[" + i + "]: '" + files[i] + "'");
        }
        // Add info file name to toscaParameters
        String infoFile = ((toscaParameters.length() > 0) ? "&" : "?") + "info=" + toscaCommand.getActionInfo()
                + FS + toscaCommand.getTaskId() + "_simpleTosca.json";
        // Finally submit the job
        String toscaId = submitJob(token, toscaEndPoint, toscaTemplate, toscaParameters, executable, output,
                error, args, files);
        LOG.info("tosca_id: '" + toscaId + "'");
        // Register JobId, if targetId exists it is a submission retry
        SimpleToscaInterfaceDB stiDB = null;
        String submitStatus = "SUBMITTED";
        try {
            stiDB = new SimpleToscaInterfaceDB(apiServerConnURL);
            int toscaTargetId = toscaCommand.getTargetId();
            if (toscaTargetId > 0) {
                // update toscaId if successful
                if ((toscaId != null) && (toscaId.length() > 0)) {
                    stiDB.updateToscaId(toscaTargetId, toscaId);
                    // Save job related info in runtime_data
                    saveResourceData();
                } else {
                    submitStatus = "ABORTED";
                }
                toscaCommand.setTargetStatus(submitStatus);
                stiDB.updateToscaStatus(toscaTargetId, submitStatus);
                LOG.debug("Updated existing entry in simple_tosca " + "table at id: '" + toscaTargetId + "'"
                        + "' - status: '" + submitStatus + "'");
            } else {
                LOG.debug("Creating a new entry in simple_tosca table " + "for submission: '" + toscaId + "'");
                if (toscaId.length() == 0) {
                    submitStatus = "ABORTED";
                }
                toscaCommand.setTargetStatus(submitStatus);
                simpleToscaId = stiDB.registerToscaId(toscaCommand, toscaId, submitStatus);
                // Save job related info in runtime_data
                saveResourceData();
                LOG.debug("Registered in simple_tosca with id: '" + simpleToscaId + "' - status: '"
                        + submitStatus + "'");
            }
        } catch (Exception e) {
            LOG.fatal("Unable to register tosca_id: '" + toscaId + "'");
        }
    } catch (SecurityException se) {
        LOG.error("Unable to create job output folder in: '" + toscaCommand.getActionInfo() + "' directory");
    } catch (Exception ex) {
        LOG.error("Caught exception: '" + ex.toString() + "'");
    }
    return simpleToscaId;
}

From source file:com.google.dart.Dart2JsMojo.java

private void clearOutputDirectory() throws MojoExecutionException {
    try {//w  w w  .j a  v a  2s .c o  m
        if (outputDirectory.exists()) {
            FileUtils.cleanDirectory(outputDirectory);
            getLog().info("Cleared all compiled dart-files.");
        }
    } catch (IOException e) {
        getLog().debug("Unable to clear directory '" + outputDirectory.getAbsolutePath() + "'.", e);
        throw new MojoExecutionException(
                "Unable to clear directory '" + outputDirectory.getAbsolutePath() + "'.", e);
    }
}

From source file:com.wavemaker.commons.util.IOUtils.java

public static void cleanDirectorySilently(File dir) {
    try {//from  w ww.j  av a2 s  .  c  o  m
        FileUtils.cleanDirectory(dir);
    } catch (IllegalArgumentException | IOException e) {
        // ignore.
    }
}

From source file:eu.planets_project.services.utils.DigitalObjectUtils.java

public static boolean cleanDigObUtilsTmp() {
    try {//from w  w  w  .  j av a  2 s. co m
        FileUtils.cleanDirectory(utils_tmp);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:ddf.catalog.cache.ResourceCacheImplSizeLimitTest.java

private void cleanProductCacheDirectory() throws IOException {
    FileUtils.cleanDirectory(new File(productCacheDir));
}