Example usage for java.io File canExecute

List of usage examples for java.io File canExecute

Introduction

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

Prototype

public boolean canExecute() 

Source Link

Document

Tests whether the application can execute the file denoted by this abstract pathname.

Usage

From source file:com.blackducksoftware.integration.hub.detect.util.executable.ExecutableFinder.java

private File findExecutableFileFromPath(final String path, final String executableName) {
    final List<String> executables;
    final OperatingSystemType currentOs = detectInfo.getCurrentOs();
    if (currentOs == OperatingSystemType.WINDOWS) {
        executables = Arrays.asList(executableName + ".cmd", executableName + ".bat", executableName + ".exe");
    } else {/*  w w  w.j  a  v  a2 s.  c o m*/
        executables = Arrays.asList(executableName);
    }

    for (final String pathPiece : path.split(File.pathSeparator)) {
        for (final String possibleExecutable : executables) {
            final File foundFile = detectFileFinder.findFile(pathPiece, possibleExecutable);
            if (foundFile != null && foundFile.exists() && foundFile.canExecute()) {
                return foundFile;
            }
        }
    }
    logger.debug(String.format("Could not find the executable: %s while searching through: %s", executableName,
            path));
    return null;
}

From source file:com.flysystem.core.adapter.local.Local.java

public Visibility getVisibility(String path) {
    File file = new File(applyPathPrefix(path));
    if (file.canExecute() && file.canRead() && file.canWrite())
        return Visibility.PUBLIC;
    return Visibility.PRIVATE;
}

From source file:io.github.msdk.io.nativeformats.ThermoRawImportMethod.java

/** {@inheritDoc} */
@SuppressWarnings("null")
@Override/*  w  ww . j a va 2  s  .  c om*/
public RawDataFile execute() throws MSDKException {

    String osName = System.getProperty("os.name").toUpperCase();
    if (!osName.contains("WINDOWS")) {
        throw new MSDKException("Native data format import only works on MS Windows");
    }

    logger.info("Started parsing file " + sourceFile);

    try {

        // Decompress the thermo raw dump executable to a temporary folder
        File tempFolder = Files.createTempDirectory("msdk").toFile();
        tempFolder.deleteOnExit();
        InputStream dumpArchive = this.getClass().getClassLoader().getResourceAsStream("thermorawdump.zip");
        if (dumpArchive == null)
            throw new MSDKException("Failed to load the thermorawdump.zip archive from the MSDK jar");
        ZipUtils.extractStreamToFolder(dumpArchive, tempFolder);

        // Path to the rawdump executable
        File rawDumpPath = new File(tempFolder, "ThermoRawDump.exe");

        if (!rawDumpPath.canExecute())
            throw new MSDKException("Cannot execute program " + rawDumpPath);

        // Create a separate process and execute RAWdump.exe
        final String cmdLine[] = new String[] { rawDumpPath.getPath(), sourceFile.getPath() };
        dumperProcess = Runtime.getRuntime().exec(cmdLine);

        // Get the stdout of RAWdump process as InputStream
        InputStream dumpStream = dumperProcess.getInputStream();

        // Create the new RawDataFile
        String fileName = sourceFile.getName();
        newRawFile = MSDKObjectBuilder.getRawDataFile(fileName, sourceFile, fileType, dataStore);

        // Read the dump data
        parser = new RawDumpParser(newRawFile, dataStore);
        parser.readRAWDump(dumpStream);

        // Cleanup
        dumpStream.close();
        dumperProcess.destroy();

        try {
            FileUtils.deleteDirectory(tempFolder);
        } catch (IOException e) {
            // Ignore errors while deleting the tmp folder
        }

        if (canceled)
            return null;

    } catch (Throwable e) {
        if (dumperProcess != null)
            dumperProcess.destroy();

        throw new MSDKException(e);
    }

    logger.info("Finished parsing " + sourceFile);

    return newRawFile;

}

From source file:io.github.msdk.io.nativeformats.WatersRawImportMethod.java

/** {@inheritDoc} */
@SuppressWarnings("null")
@Override//w  ww.  jav  a 2 s .  c  o m
public RawDataFile execute() throws MSDKException {

    String osName = System.getProperty("os.name").toUpperCase();
    if (!osName.contains("WINDOWS")) {
        throw new MSDKException("Native data format import only works on MS Windows");
    }

    logger.info("Started parsing file " + sourceFile);

    try {

        // Decompress the Waters raw dump executable to a temporary folder
        File tempFolder = Files.createTempDirectory("msdk").toFile();
        tempFolder.deleteOnExit();
        InputStream dumpArchive = this.getClass().getClassLoader().getResourceAsStream("watersrawdump.zip");
        if (dumpArchive == null)
            throw new MSDKException("Failed to load the watersrawdump.zip archive from the MSDK jar");
        ZipUtils.extractStreamToFolder(dumpArchive, tempFolder);

        // Path to the rawdump executable
        File rawDumpPath = new File(tempFolder, "WatersRawDump.exe");

        if (!rawDumpPath.canExecute())
            throw new MSDKException("Cannot execute program " + rawDumpPath);

        // Create a separate process and execute RAWdump.exe
        final String cmdLine[] = new String[] { rawDumpPath.getPath(), sourceFile.getPath() };
        dumperProcess = Runtime.getRuntime().exec(cmdLine);

        // Get the stdout of RAWdump process as InputStream
        InputStream dumpStream = dumperProcess.getInputStream();

        // Create the new RawDataFile
        String fileName = sourceFile.getName();
        newRawFile = MSDKObjectBuilder.getRawDataFile(fileName, sourceFile, fileType, dataStore);

        // Read the dump data
        parser = new RawDumpParser(newRawFile, dataStore);
        parser.readRAWDump(dumpStream);

        // Cleanup
        dumpStream.close();
        dumperProcess.destroy();

        try {
            FileUtils.deleteDirectory(tempFolder);
        } catch (IOException e) {
            // Ignore errors while deleting the tmp folder
        }

        if (canceled)
            return null;

    } catch (Throwable e) {
        if (dumperProcess != null)
            dumperProcess.destroy();

        throw new MSDKException(e);
    }

    logger.info("Finished parsing " + sourceFile);

    return newRawFile;

}

From source file:org.apache.commons.rng.examples.stress.RandomStressTester.java

/**
 * Creates the application.//from ww  w . j  a  v  a  2  s .  co m
 *
 * @param cmd Command line.
 * @param outputPrefix Output prefix for file reports.
 */
private RandomStressTester(List<String> cmd, String outputPrefix) {
    final File exec = new File(cmd.get(0));
    if (!exec.exists() || !exec.canExecute()) {
        throw new IllegalArgumentException("Program is not executable: " + exec);
    }

    cmdLine = new ArrayList<String>(cmd);
    fileOutputPrefix = outputPrefix;

    final File reportDir = new File(fileOutputPrefix).getParentFile();
    if (!reportDir.exists() || !reportDir.isDirectory() || !reportDir.canWrite()) {
        throw new IllegalArgumentException("Invalid output directory: " + reportDir);
    }
}

From source file:com.adaptris.hpcc.DfuplusConnection.java

private File validateCmd(String cmd) throws IOException {
    File dfuPlus = new File(cmd);
    if (dfuPlus.exists() && dfuPlus.isFile() && dfuPlus.canExecute()) {
        return dfuPlus;
    }//  w ww  .j  av a  2  s . c  om
    throw new IOException("Can't execute [" + dfuPlus.getCanonicalPath() + "]");
}

From source file:org.uiautomation.ios.server.simulator.IOSSimulatorManager.java

private void checkPlUtil() {
    File f = new File(PLUTIL);
    if (!f.exists() || !f.canExecute()) {
        throw new IOSAutomationException("Cannot access " + PLUTIL);
    }//from  w ww . j  av  a2  s. c  o  m

}

From source file:org.uiautomation.ios.server.utils.SimulatorSettings.java

private void checkPlUtil() {
    File f = new File(PLUTIL);
    if (!f.exists() || !f.canExecute()) {
        throw new WebDriverException("Cannot access " + PLUTIL);
    }/*  w  w  w .j  a v  a  2 s.  com*/

}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.tgrep.TgrepQuery.java

@Override
public List<EvaluationItem> execute() {
    BufferedReader brInput = null;
    BufferedReader brError = null;
    List<String> output = new ArrayList<String>();
    List<String> error = new ArrayList<String>();

    try {//ww  w.  j a v a  2 s  .c  o  m
        List<String> cmd = new ArrayList<String>();

        File exe = engine.getTgrepExecutable();
        if (!exe.canExecute()) {
            exe.setExecutable(true);
        }

        cmd.add(exe.getAbsolutePath());
        // specify corpus
        cmd.add("-c");
        cmd.add(engine.getCorpusPath(corpus));
        // only one match per sentence
        cmd.add("-f");
        // print options
        cmd.add("-m");
        // comment
        // full sentence
        // match begin token index
        // match end token index
        cmd.add("%c\\n%tw\\n%ym\\n%zm\\n");
        // pattern to search for
        cmd.add(query);
        if (log.isTraceEnabled()) {
            log.trace("Invoking [" + StringUtils.join(cmd, " ") + "]");
        }

        final ProcessBuilder pb = new ProcessBuilder(cmd);
        tgrep = pb.start();

        brInput = new BufferedReader(new InputStreamReader(tgrep.getInputStream(), "UTF-8"));
        brError = new BufferedReader(new InputStreamReader(tgrep.getErrorStream(), "UTF-8"));

        String line;
        while ((line = brInput.readLine()) != null) {
            if (log.isTraceEnabled()) {
                log.trace("<< " + line);
            }
            output.add(line);
        }

        while ((line = brError.readLine()) != null) {
            if (log.isErrorEnabled()) {
                log.error(line);
            }
            error.add(line);
        }

        if (!error.isEmpty()) {
            throw new IOException(StringUtils.join(error, " "));
        }
    } catch (IOException e) {
        throw new DataAccessResourceFailureException("Unable to start Tgrep process.", e);
    } finally {
        IOUtils.closeQuietly(brInput);
        IOUtils.closeQuietly(brError);
    }

    size = output.size() / LINES_PER_MATCH;
    if (maxResults >= 0 && size > maxResults) {
        return parseOutput(output.subList(0, LINES_PER_MATCH * maxResults));
    } else {
        return parseOutput(output);
    }
}

From source file:org.geoserver.importer.transform.AbstractCommandLinePreTransform.java

/**
 * Locates and executable in the system path. On windows it will automatically append .exe to
 * the searched file name//from  w  w  w .  java2s .c om
 * 
 * @param name
 *
 * @throws IOException
 */
protected File getExecutableFromPath(String name) throws IOException {
    if (SystemUtils.IS_OS_WINDOWS) {
        name = name + ".exe";
    }
    String systemPath = System.getenv("PATH");
    if (systemPath == null) {
        systemPath = System.getenv("path");
    }
    if (systemPath == null) {
        throw new IOException("Path is not set, cannot locate " + name);
    }
    String[] paths = systemPath.split(File.pathSeparator);

    for (String pathDir : paths) {
        File file = new File(pathDir, name);
        if (file.exists() && file.isFile() && file.canExecute()) {
            return file;
        }
    }
    throw new IOException(
            "Could not locate executable (or could locate, but does not have execution rights): " + name);
}