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:org.fuin.esmp.EventStoreDownloadMojo.java

private void applyFileMode(final File file, final FileMode fileMode) throws MojoExecutionException {

    if (OS.isFamilyUnix() || OS.isFamilyMac()) {
        final String smode = fileMode.toChmodStringFull();
        final CommandLine cmdLine = new CommandLine("chmod");
        cmdLine.addArgument(smode);//from   w w  w .  jav  a 2s. c o m
        cmdLine.addArgument(file.getAbsolutePath());
        final Executor executor = new DefaultExecutor();
        try {
            final int result = executor.execute(cmdLine);
            if (result != 0) {
                throw new MojoExecutionException("Error # " + result + " while trying to set mode \"" + smode
                        + "\" for file: " + file.getAbsolutePath());
            }
        } catch (final IOException ex) {
            throw new MojoExecutionException(
                    "Error while trying to set mode \"" + smode + "\" for file: " + file.getAbsolutePath(), ex);
        }
    } else {
        file.setReadable(fileMode.isUr() || fileMode.isGr() || fileMode.isOr());
        file.setWritable(fileMode.isUw() || fileMode.isGw() || fileMode.isOw());
        file.setExecutable(fileMode.isUx() || fileMode.isGx() || fileMode.isOx());
    }
}

From source file:brut.androlib.res.AndrolibResources.java

/**
 * Using a prebuilt aapt and forcing its use, allows us to prevent bugs from older aapt's
 * along with having a finer control over the build procedure.
 *
 * Aapt can still be overridden via --aapt/-a on build, but specific features will be disabled
 *
 * @url https://github.com/iBotPeaches/platform_frameworks_base
 * @throws AndrolibException//from   w ww . ja va 2s. c o m
 */
public File getAaptBinaryFile() throws AndrolibException {
    File aaptBinary;

    try {
        if (OSDetection.isMacOSX()) {
            aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/macosx/aapt");
        } else if (OSDetection.isUnix()) {
            aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/linux/aapt");
        } else if (OSDetection.isWindows()) {
            aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/windows/aapt.exe");
        } else {
            LOGGER.warning("Unknown Operating System: " + OSDetection.returnOS());
            return null;
        }
    } catch (BrutException ex) {
        throw new AndrolibException(ex);
    }
    if (aaptBinary.setExecutable(true)) {
        return aaptBinary;
    }

    System.err.println("Can't set aapt binary as executable");
    throw new AndrolibException("Can't set aapt binary as executable");
}

From source file:it.drwolf.ridire.index.cwb.CWBPatternSearcher.java

private void executeCQPQuery(File queryFile) throws ExecuteException, IOException {
    Executor executor = new DefaultExecutor();
    File tempSh = File.createTempFile("ridireSH", ".sh");
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("export LC_ALL=C\n");
    stringBuffer.append(this.cqpExecutable + " -f " + queryFile.getAbsolutePath() + " -D " + this.cqpCorpusName
            + " -r " + this.cqpRegistry + "\n");
    FileUtils.writeStringToFile(tempSh, stringBuffer.toString());
    tempSh.setExecutable(true);
    CommandLine commandLine = new CommandLine(tempSh.getAbsolutePath());
    executor.execute(commandLine);//from   w  w w.ja va  2s.com
    FileUtils.deleteQuietly(tempSh);
}

From source file:com.android.builder.core.AtlasBuilder.java

private File getAapt() {

    String osName = "mac";
    String fileName = "aapt";

    if (StringUtils.containsIgnoreCase(System.getProperty("os.name"), "Mac")) {
        osName = "mac";
    } else if (StringUtils.containsIgnoreCase(System.getProperty("os.name"), "Linux")) {
        osName = "linux";
    } else if (StringUtils.containsIgnoreCase(System.getProperty("os.name"), "windows")) {
        osName = "win";
        fileName = "aapt.exe";
    }//from  ww  w . j a v a2 s  . c o m
    String aaptPath = "aapt/" + osName + "/" + fileName;
    File aaptFile = new File(AtlasBuilder.class.getClassLoader().getResource(aaptPath).getFile());
    if (aaptFile.isFile()) {
        return aaptFile;
    }

    String path = AtlasBuilder.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    File jarFile = new File(path);
    File jarFolder = new File(jarFile.getParentFile(), FilenameUtils.getBaseName(jarFile.getName()));
    jarFolder.mkdirs();
    aaptFile = new File(jarFolder, fileName);

    if (!aaptFile.exists()) {
        aaptFile = ZipUtils.extractZipFileToFolder(jarFile, aaptPath, aaptFile.getParentFile());
        aaptFile.setExecutable(true);
    }
    return aaptFile;
}

From source file:com.photon.maven.plugins.android.AbstractEmulatorMojo.java

/**
 * Writes the script to start the emulator in the background for windows based environments.
 *
 * @return absolute path name of start script
 * @throws IOException//from  w w w .ja  v  a  2 s . c  o m
 * @throws MojoExecutionException
 */
private String writeEmulatorStartScriptWindows() throws MojoExecutionException {

    String filename = SCRIPTFOLDER + "\\android-maven-plugin-emulator-start.vbs";

    File file = new File(filename);
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(new FileWriter(file));

        // command needs to be assembled before unique window title since it parses settings and sets up parsedAvd
        // and others.
        String command = assembleStartCommandLine();
        String uniqueWindowTitle = "AndroidMavenPlugin-AVD" + parsedAvd;
        writer.println("Dim oShell");
        writer.println("Set oShell = WScript.CreateObject(\"WScript.shell\")");
        String cmdPath = System.getenv("COMSPEC");
        if (cmdPath == null) {
            cmdPath = "cmd.exe";
        }
        String cmd = cmdPath + " /X /C START /SEPARATE \"\"" + uniqueWindowTitle + "\"\"  " + command.trim();
        writer.println("oShell.run \"" + cmd + "\"");
    } catch (IOException e) {
        getLog().error("Failure writing file " + filename);
    } finally {
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    }
    file.setExecutable(true);
    return filename;
}

From source file:it.drwolf.ridire.index.cwb.CWBPatternSearcher.java

private Integer getCQPQueryResultsSize(File queryFile, String cqpSizeQuery)
        throws ExecuteException, IOException {
    EnvironmentUtils.addVariableToEnvironment(EnvironmentUtils.getProcEnvironment(), "LC_ALL=C");
    Executor executor = new DefaultExecutor();
    File tempSize = File.createTempFile("ridireSZ", ".size");
    File tempSh = File.createTempFile("ridireSH", ".sh");
    CommandLine commandLine = new CommandLine(this.cqpExecutable);
    commandLine.addArgument("-f").addArgument(queryFile.getAbsolutePath()).addArgument("-D")
            .addArgument(this.cqpCorpusName).addArgument("-r").addArgument(this.cqpRegistry);
    String commLineString = commandLine.toString() + " > " + tempSize.getAbsolutePath();
    FileUtils.writeStringToFile(tempSh, commLineString);
    tempSh.setExecutable(true);
    executor = new DefaultExecutor();
    executor.setExitValue(0);/*from w  ww .j a v a  2s .c o  m*/
    ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBPatternSearcher.TIMEOUT);
    executor.setWatchdog(watchdog);
    commandLine = new CommandLine(tempSh.getAbsolutePath());
    executor.execute(commandLine);
    Integer size = 0;
    List<String> lines = FileUtils.readLines(tempSize);
    if (lines.size() > 0) {
        size = Integer.parseInt(lines.get(0).trim());
    }
    FileUtils.deleteQuietly(tempSh);
    FileUtils.deleteQuietly(tempSize);
    return size;
}

From source file:it.drwolf.ridire.index.cwb.CWBConcordancer.java

private void executeCQPQuery(File queryFile, boolean inverse) throws ExecuteException, IOException {
    Executor executor = new DefaultExecutor();
    File tempSh = File.createTempFile("ridireSH", ".sh");
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("LC_ALL=C && ");
    String corpusName = this.cqpCorpusName;
    if (inverse) {
        corpusName += "INV";
    }//from www .j  a v a2 s  .  c o m
    stringBuffer.append(this.cqpExecutable + " -f " + queryFile.getAbsolutePath() + " -D " + corpusName + " -r "
            + this.cqpRegistry + "\n");
    FileUtils.writeStringToFile(tempSh, stringBuffer.toString());
    tempSh.setExecutable(true);
    CommandLine commandLine = new CommandLine(tempSh.getAbsolutePath());
    executor.execute(commandLine);
    FileUtils.deleteQuietly(tempSh);
}

From source file:it.drwolf.ridire.index.cwb.CWBConcordancer.java

private Integer getCQPQueryResultsSize(File queryFile) throws ExecuteException, IOException {
    Executor executor = new DefaultExecutor();
    File tempSh = File.createTempFile("ridireSH", ".sh");
    File tempSize = File.createTempFile("ridireSZ", ".size");
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("export LC_ALL=C\n");
    stringBuffer.append(this.cqpExecutable + " -f " + queryFile.getAbsolutePath() + " -D " + this.cqpCorpusName
            + " -r " + this.cqpRegistry + " > " + tempSize.getAbsolutePath() + "\n");
    FileUtils.writeStringToFile(tempSh, stringBuffer.toString());
    tempSh.setExecutable(true);
    CommandLine commandLine = new CommandLine(tempSh.getAbsolutePath());
    executor.execute(commandLine);/*from   www.  ja  va  2  s  . co  m*/
    Integer size = 0;
    List<String> lines = FileUtils.readLines(tempSize);
    if (lines.size() > 0) {
        size = Integer.parseInt(lines.get(0).trim());
    }
    FileUtils.deleteQuietly(tempSh);
    FileUtils.deleteQuietly(tempSize);
    return size;
}

From source file:org.kepler.ssh.LocalExec.java

/**
 * Copies src file to dst file. If the dst file does not exist, it is
 * created/*from  w  ww  . ja v a 2  s . co  m*/
 */
private void copyFile(File src, File dst) throws IOException {
    // see if source and destination are the same
    if (src.equals(dst)) {
        // do not copy
        return;
    }

    //System.out.println("copying " + src + " to " + dst);

    FileChannel srcChannel = new FileInputStream(src).getChannel();
    FileChannel dstChannel = new FileOutputStream(dst).getChannel();
    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    srcChannel.close();
    dstChannel.close();

    /* hacking for non-windows */
    // set the permission of the target file the same as the source file
    if (_commandArr[0] == "/bin/sh") {

        String osName = StringUtilities.getProperty("os.name");
        if (osName.startsWith("Mac OS X")) {

            // chmod --reference does not exist on mac, so do the best
            // we can using the java file api
            // WARNING: this relies on the umask to set the group, world
            // permissions.
            dst.setExecutable(src.canExecute());
            dst.setWritable(src.canWrite());

        } else {

            String cmd = "chmod --reference=" + src.getAbsolutePath() + " " + dst.getAbsolutePath();
            try {
                ByteArrayOutputStream streamOut = new ByteArrayOutputStream();
                ByteArrayOutputStream streamErr = new ByteArrayOutputStream();
                executeCmd(cmd, streamOut, streamErr);
            } catch (ExecException e) {
                log.warn("Tried to set the target file permissions the same as "
                        + "the source but the command failed: " + cmd + "\n" + e);
            }
        }
    }
}

From source file:com.comcast.magicwand.spells.web.chrome.ChromePhoenixDriver.java

/**
 * {@inheritDoc}/*w  w  w  .  j a v a  2s .  c o  m*/
 */
public boolean verify(PhoenixDriverIngredients i) {
    Map<String, Object> driverConfigs = i.getDriverConfigs();

    ChromePlatformSpecifics cps = createChromePlatformSpecifics(
            (String) driverConfigs.get(CHROME_DRIVER_VERSION));

    LOG.debug("Using cps[{}, {}, {}]", cps.getSuffix(), cps.getExtension(), cps.getVersion());

    String version = cps.getVersion();
    String osName = cps.getSuffix();
    String pwd = System.getProperty("user.dir");

    //determine the driver name
    String driverName = String.format("chromedriver%s-%s", osName, version);
    String driverTargetDir = Paths.get(pwd, "target", "drivers").toString();
    File driver = Paths.get(driverTargetDir, driverName + cps.getExtension()).toFile();

    if (!driver.exists()) {
        LOG.debug("No cached chromedriver driver found");

        /* Download chromedriver zip */
        File zipDriver = Paths.get(driverTargetDir, driverName + ".zip").toFile();
        if (!zipDriver.exists()) {
            String driverURL = String.format(DRIVER_URL_FORMAT, version, osName);
            try {
                URL driverZipURL = new URL(driverURL);
                LOG.debug("Will download driver package [{}]", driverURL);

                FileUtils.copyURLToFile(driverZipURL, zipDriver);
            } catch (IOException e) {
                LOG.error("Error downloading [{}]: {}", driverURL, e);
                return false;
            }
        }

        /* Exctract chromedriver zip */
        try {
            extractZip(zipDriver, driverTargetDir);
        } catch (IOException e) {
            LOG.error("Error extracting [{}]: {}", zipDriver, driverTargetDir, e);
            return false;
        }

        /* For caching purposes, rename chromedriver to keep os and version info */
        File genericDriver = Paths.get(driverTargetDir, "chromedriver" + cps.getExtension()).toFile();
        try {
            FileUtils.moveFile(genericDriver, driver);
        } catch (IOException e) {
            LOG.error("Error moving [{}] to [{}]: {}", genericDriver, driver, e);
            return false;
        }

        driver.setExecutable(true);
    }

    LOG.debug("Will use driver at [{}]", driver);
    systemSetProperty("webdriver.chrome.driver", driver.toString());

    this.webDriver = this.createDriver(i.getDriverCapabilities());

    return true;
}