Example usage for java.io File setExecutable

List of usage examples for java.io File setExecutable

Introduction

In this page you can find the example usage for java.io File setExecutable.

Prototype

public boolean setExecutable(boolean executable) 

Source Link

Document

A convenience method to set the owner's execute permission for this abstract pathname.

Usage

From source file:io.snappydata.hydra.cluster.SnappyTest.java

/**
 * Task(ENDTASK) for cleaning up snappy processes, because they are not stopped by Hydra in case of Test failure.
 *//* w w  w .  j  a  v  a2  s . c  o m*/
public static void HydraTask_cleanUpSnappyProcessesOnFailure() {
    Process pr = null;
    ProcessBuilder pb = null;
    File logFile = null, log = null, nukeRunOutput = null;
    try {
        List<String> pidList = new ArrayList();
        HostDescription hd = TestConfig.getInstance().getMasterDescription().getVmDescription()
                .getHostDescription();
        pidList = snappyTest.getPidList();
        log = new File(".");
        String nukerun = log.getCanonicalPath() + File.separator + "snappyNukeRun.sh";
        logFile = new File(nukerun);
        String nukeRunOutputString = log.getCanonicalPath() + File.separator + "nukeRunOutput.log";
        nukeRunOutput = new File(nukeRunOutputString);
        FileWriter fw = new FileWriter(logFile.getAbsoluteFile(), true);
        BufferedWriter bw = new BufferedWriter(fw);
        for (String pidString : pidList) {
            int pid = Integer.parseInt(pidString);
            bw.write("/bin/kill -KILL " + pid);
            bw.newLine();
            try {
                RemoteTestModule.Master.removePID(hd, pid);
            } catch (RemoteException e) {
                String s = "Failed to remove PID from nukerun script: " + pid;
                throw new HydraRuntimeException(s, e);
            }
        }
        bw.close();
        fw.close();
        logFile.setExecutable(true);
        pb = new ProcessBuilder(nukerun);
        pb.redirectErrorStream(true);
        pb.redirectOutput(ProcessBuilder.Redirect.appendTo(nukeRunOutput));
        pr = pb.start();
        pr.waitFor();
    } catch (IOException e) {
        throw new TestException("IOException occurred while retriving logFile path " + log + "\nError Message:"
                + e.getMessage());
    } catch (InterruptedException e) {
        String s = "Exception occurred while waiting for the process execution : " + pr;
        throw new TestException(s, e);
    }
}

From source file:hd3gtv.mydmam.useraction.fileoperation.CopyMove.java

private void copyFile(File source_file, File destination_file) throws IOException {
    if (destination_file.exists()) {
        Log2Dump dump = new Log2Dump();
        dump.add("source_file", source_file);
        dump.add("destination_file", destination_file);
        dump.add("delete_after_copy", delete_after_copy);

        if (fileexistspolicy == FileExistsPolicy.IGNORE) {
            Log2.log.debug("Destination file exists, ignore copy/move", dump);
            return;
        } else if (fileexistspolicy == FileExistsPolicy.OVERWRITE) {
            Log2.log.debug("Destination file exists, overwrite it", dump);
            FileUtils.forceDelete(destination_file);
        } else if (fileexistspolicy == FileExistsPolicy.RENAME) {
            // destination_file
            int cursor = 1;
            int dot_pos;
            StringBuilder sb;/*from   w ww . j  av  a 2  s  .co  m*/
            while (destination_file.exists()) {
                sb = new StringBuilder();
                sb.append(destination_file.getParent());
                sb.append(File.separator);
                dot_pos = destination_file.getName().lastIndexOf(".");
                if (dot_pos > 0) {
                    sb.append(destination_file.getName().substring(0, dot_pos));
                    sb.append(" (");
                    sb.append(cursor);
                    sb.append(")");
                    sb.append(
                            destination_file.getName().substring(dot_pos, destination_file.getName().length()));
                } else {
                    sb.append(destination_file.getName());
                    sb.append(" (");
                    sb.append(cursor);
                    sb.append(")");
                }
                destination_file = new File(sb.toString());
                cursor++;
            }
            dump.add("new destination file name", destination_file);
            Log2.log.debug("Destination file exists, change destionation name", dump);
        }
    }
    /**
     * Imported from org.apache.commons.io.FileUtils
     * Licensed to the Apache Software Foundation,
     * http://www.apache.org/licenses/LICENSE-2.0
     */
    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        fis = new FileInputStream(source_file);
        fos = new FileOutputStream(destination_file);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
            pos += output.transferFrom(input, pos, count);

            if (progression != null) {
                actual_progress_value = (int) ((pos + progress_copy) / (1024 * 1024));
                if (actual_progress_value > last_progress_value) {
                    last_progress_value = actual_progress_value;
                    progression.updateProgress(actual_progress_value, progress_size);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fis);
    }

    if (source_file.length() != destination_file.length()) {
        throw new IOException(
                "Failed to copy full contents from '" + source_file + "' to '" + destination_file + "'");
    }
    if (destination_file.setExecutable(source_file.canExecute()) == false) {
        Log2.log.error("Can't set Executable status to dest file", new IOException(destination_file.getPath()));
    }
    if (destination_file.setLastModified(source_file.lastModified()) == false) {
        Log2.log.error("Can't set LastModified status to dest file",
                new IOException(destination_file.getPath()));
    }
}