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:edu.stolaf.cs.wmrserver.streaming.PipeMapRed.java

public void configure(JobConf job) {
    try {//from  www . j a  v  a2  s .  c  o  m
        String argv = getPipeCommand(job);

        joinDelay_ = job.getLong("stream.joindelay.milli", 0);

        job_ = job;
        fs_ = FileSystem.get(job_);

        nonZeroExitIsFailure_ = job_.getBoolean("stream.non.zero.exit.is.failure", true);

        doPipe_ = getDoPipe();
        if (!doPipe_)
            return;

        setStreamJobDetails(job);

        String[] argvSplit = splitArgs(argv);
        String prog = argvSplit[0];
        File currentDir = new File(".").getAbsoluteFile();
        if (new File(prog).isAbsolute()) {
            // we don't own it. Hope it is executable
        } else {
            // Try to find executable in unpacked job JAR and make absolute if
            // present. Otherwise, leave it as relative to be resolved against PATH
            File jarDir = new File(job.getJar()).getParentFile();
            File progFile = new File(jarDir, argvSplit[0]);
            if (progFile.isFile()) {
                progFile.setExecutable(true);
                argvSplit[0] = progFile.getAbsolutePath();
            }
        }

        logprintln("PipeMapRed exec " + Arrays.asList(argvSplit));
        Hashtable<String, String> childEnv = new Hashtable();
        addJobConfToEnvironment(job_, childEnv);
        addEnvironment(childEnv, job_.get("stream.addenvironment"));
        // add TMPDIR environment variable with the value of java.io.tmpdir
        envPut(childEnv, "TMPDIR", System.getProperty("java.io.tmpdir"));

        // Start the process
        ProcessBuilder builder = new ProcessBuilder(argvSplit);
        // The process' environment initially inherits all vars from the parent --
        // only setting those we add/override
        builder.environment().putAll(childEnv);
        // Set the working directory to the job jars directory
        // This is a bad idea... fix this.
        builder.directory(new File(job.getJar()).getParentFile());
        sim = builder.start();

        clientOut_ = new DataOutputStream(new BufferedOutputStream(sim.getOutputStream(), BUFFER_SIZE));
        clientIn_ = new DataInputStream(new BufferedInputStream(sim.getInputStream(), BUFFER_SIZE));
        clientErr_ = new DataInputStream(new BufferedInputStream(sim.getErrorStream()));
        startTime_ = System.currentTimeMillis();

        errThread_ = new MRErrorThread();
        errThread_.start();
    } catch (Exception e) {
        logStackTrace(e);
        LOG.error("configuration exception", e);
        throw new RuntimeException("configuration exception", e);
    }
}

From source file:com.netflix.genie.core.services.impl.LocalJobRunner.java

private File createRunScript(final File jobWorkingDir) throws GenieException {
    final long start = System.nanoTime();
    try {/*from  w  w  w  . ja  v  a2 s  . c  om*/
        final File runScript = new File(jobWorkingDir, JobConstants.GENIE_JOB_LAUNCHER_SCRIPT);
        if (!runScript.exists()) {
            try {
                if (!runScript.createNewFile()) {
                    throw new GenieServerException("Unable to create run script file due to unknown reason.");
                }
            } catch (final IOException ioe) {
                throw new GenieServerException("Unable to create run script file due to IOException.", ioe);
            }
        }
        if (!runScript.setExecutable(true)) {
            throw new GenieServerException("Unable to make run script executable");
        }
        log.info("Created run script {}", runScript);
        return runScript;
    } finally {
        this.createRunScriptTimer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    }
}

From source file:com.streamsets.datacollector.cluster.TestShellClusterProvider.java

@Test
public void testCopyDirectory() throws Exception {
    File copyTempDir = new File(tempDir, "copy");
    File srcDir = new File(copyTempDir, "somedir");
    File dstDir = new File(copyTempDir, "dst");
    Assert.assertTrue(srcDir.mkdirs());/*from   ww  w . j a v a  2  s  .  com*/
    Assert.assertTrue(dstDir.mkdirs());
    File link1 = new File(copyTempDir, "link1");
    File link2 = new File(copyTempDir, "link2");
    File dir1 = new File(copyTempDir, "dir1");
    File file1 = new File(dir1, "f1");
    File file2 = new File(dir1, "f2");
    Assert.assertTrue(dir1.mkdirs());
    Assert.assertTrue(file1.createNewFile());
    Assert.assertTrue(file2.createNewFile());
    file2.setReadable(false);
    file2.setWritable(false);
    file2.setExecutable(false);
    Files.createSymbolicLink(link1.toPath(), dir1.toPath());
    Files.createSymbolicLink(link2.toPath(), link1.toPath());
    Files.createSymbolicLink(new File(srcDir, "dir1").toPath(), link2.toPath());
    File clone = ShellClusterProvider.createDirectoryClone(srcDir, srcDir.getName(), dstDir);
    File cloneF1 = new File(new File(clone, "dir1"), "f1");
    Assert.assertTrue(cloneF1.isFile());
}

From source file:net.codestory.simplelenium.driver.Downloader.java

protected void extractExe(String driverName, String url, File installDirectory, File executable) {
    if (executable.exists()) {
        return;/*from  w w w.j  av a2  s.c o m*/
    }

    String zipName = url.substring(url.lastIndexOf('/') + 1);
    File targetZip = new File(installDirectory, zipName);
    downloadZip(driverName, url, targetZip);

    System.out.println("Extracting " + driverName);
    try {
        if (url.endsWith(".zip")) {
            unzip(targetZip, installDirectory);
        } else {
            untarbz2(targetZip, installDirectory);
        }
    } catch (Exception e) {
        throw new IllegalStateException(
                "Unable to uncompress " + driverName + " from " + targetZip.getAbsolutePath(), e);
    }

    executable.setExecutable(true);
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.TestFsDatasetImpl.java

/**
 * Tests stopping all the active DataXceiver thread on volume failure event.
 * @throws Exception/*ww  w .j av a2s. c  o  m*/
 */
@Test
public void testCleanShutdownOfVolume() throws Exception {
    MiniDFSCluster cluster = null;
    try {
        Configuration config = new HdfsConfiguration();
        config.setLong(DFSConfigKeys.DFS_DATANODE_XCEIVER_STOP_TIMEOUT_MILLIS_KEY, 1000);
        config.setInt(DFSConfigKeys.DFS_DATANODE_FAILED_VOLUMES_TOLERATED_KEY, 1);

        cluster = new MiniDFSCluster.Builder(config).numDataNodes(1).build();
        cluster.waitActive();
        FileSystem fs = cluster.getFileSystem();
        DataNode dataNode = cluster.getDataNodes().get(0);
        Path filePath = new Path("test.dat");
        // Create a file and keep the output stream unclosed.
        FSDataOutputStream out = fs.create(filePath, (short) 1);
        out.write(1);
        out.hflush();

        ExtendedBlock block = DFSTestUtil.getFirstBlock(fs, filePath);
        FsVolumeImpl volume = (FsVolumeImpl) dataNode.getFSDataset().getVolume(block);
        File finalizedDir = volume.getFinalizedDir(cluster.getNamesystem().getBlockPoolId());

        if (finalizedDir.exists()) {
            // Remove write and execute access so that checkDiskErrorThread detects
            // this volume is bad.
            finalizedDir.setExecutable(false);
            finalizedDir.setWritable(false);
        }
        Assert.assertTrue("Reference count for the volume should be greater " + "than 0",
                volume.getReferenceCount() > 0);
        // Invoke the synchronous checkDiskError method
        dataNode.getFSDataset().checkDataDir();
        // Sleep for 1 second so that datanode can interrupt and cluster clean up
        Thread.sleep(1000);
        assertEquals("There are active threads still referencing volume: " + volume.getBasePath(), 0,
                volume.getReferenceCount());
        LocatedBlock lb = DFSTestUtil.getAllBlocks(fs, filePath).get(0);
        DatanodeInfo info = lb.getLocations()[0];

        try {
            out.close();
            Assert.fail("This is not a valid code path. " + "out.close should have thrown an exception.");
        } catch (IOException ioe) {
            GenericTestUtils.assertExceptionContains(info.getXferAddr(), ioe);
        }
        finalizedDir.setWritable(true);
        finalizedDir.setExecutable(true);
    } finally {
        cluster.shutdown();
    }
}

From source file:org.apache.drill.yarn.scripts.ScriptUtils.java

/**
 * Copy the standard scripts from source location to the mock distribution
 * directory.//from  w w w.  j ava 2 s.  c  o m
 */

private void copyScripts(File sourceDir) throws IOException {
    File binDir = new File(testDrillHome, "bin");
    for (String script : ScriptUtils.scripts) {
        File source = new File(sourceDir, script);
        File dest = new File(binDir, script);
        copyFile(source, dest);
        dest.setExecutable(true);
    }

    // Create the "magic" wrapper script that simulates the Drillbit and
    // captures the output we need for testing.

    String wrapper = "wrapper.sh";
    File dest = new File(binDir, wrapper);
    try (InputStream is = getClass().getResourceAsStream("/" + wrapper)) {
        Files.copy(is, dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
    dest.setExecutable(true);
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

private static void replaceInJar(File sourceJar, String origFile, File replacementFile) throws IOException {
    String srcJarAbsPath = sourceJar.getAbsolutePath();
    String srcJarSuffix = srcJarAbsPath.substring(srcJarAbsPath.lastIndexOf(File.separator) + 1);
    String srcJarName = srcJarSuffix.split(".jar")[0];

    String destJarName = srcJarName + "-managix";
    String destJarSuffix = destJarName + ".jar";
    File destJar = new File(sourceJar.getParentFile().getAbsolutePath() + File.separator + destJarSuffix);
    //  File destJar = new File(sourceJar.getAbsolutePath() + ".modified");
    JarFile sourceJarFile = new JarFile(sourceJar);
    Enumeration<JarEntry> entries = sourceJarFile.entries();
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(destJar));
    byte[] buffer = new byte[2048];
    int read;//from   w w  w. j  av  a 2  s. c o m
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = entry.getName();
        if (name.equals(origFile)) {
            continue;
        }
        InputStream jarIs = sourceJarFile.getInputStream(entry);
        jos.putNextEntry(entry);
        while ((read = jarIs.read(buffer)) != -1) {
            jos.write(buffer, 0, read);
        }
        jarIs.close();
    }
    sourceJarFile.close();
    JarEntry entry = new JarEntry(origFile);
    jos.putNextEntry(entry);
    FileInputStream fis = new FileInputStream(replacementFile);
    while ((read = fis.read(buffer)) != -1) {
        jos.write(buffer, 0, read);
    }
    fis.close();
    jos.close();
    sourceJar.delete();
    destJar.renameTo(sourceJar);
    destJar.setExecutable(true);
}

From source file:asciidoc.maven.plugin.tools.ZipHelper.java

public void unzipEntry(ZipFile zipfile, ZipEntry zipEntry, File outputDir) throws IOException {
    if (zipEntry.isDirectory()) {
        createDir(new File(outputDir, zipEntry.getName()));
        return;//from  w ww . java  2  s  .  c  o m
    }
    File outputFile = new File(outputDir, zipEntry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    if (this.log.isDebugEnabled())
        this.log.debug("Extracting " + zipEntry);
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(zipEntry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }

    if ((outputFile != null)
            && (!outputFile.isDirectory() && FileHelper.getFileExtension(outputFile).equalsIgnoreCase("py"))) {
        outputFile.setExecutable(true);
    }
}

From source file:es.bsc.servicess.ide.PackagingUtils.java

/** Copy the runtime files required for the Core Elements.
 * @param runtimePath Path to the programming model runtime
 * @param jarFolder Folder to store the jars
 *///w  w w. j  a  v  a2  s .  c  o  m
private static void copyCoreRuntimeFiles(String runtimePath, IFolder jarFolder) {

    File d = new File(runtimePath + "/../worker/");
    File it_jar = new File(runtimePath + File.separator + "lib" + File.separator + "IT.jar");
    File jar_folder_file = jarFolder.getLocation().toFile();
    File jar_lib_folder = jarFolder.getLocation().append("lib").toFile();
    try {
        FileUtils.copyFileToDirectory(it_jar, jar_lib_folder);
    } catch (IOException e) {
        log.error("File " + it_jar.getAbsolutePath() + "could not be copied to "
                + jar_lib_folder.getAbsolutePath());
    }
    if (d.isDirectory()) {
        Iterator<File> fi = FileUtils.iterateFiles(d, new String[] { "sh" }, false);
        while (fi.hasNext()) {
            File f = fi.next();
            if (!isFileInDiscardList(f)) {
                try {
                    // System.out.println("Trying to copy File "+
                    // f.getAbsolutePath());
                    FileUtils.copyFileToDirectory(f, jar_folder_file);
                    File fc = new File(jar_folder_file.getAbsolutePath() + File.separator + f.getName());
                    fc.setExecutable(true);
                    log.debug(" File copied " + f.getAbsolutePath());
                } catch (IOException e) {
                    log.error("File " + f.getAbsolutePath() + "could not be copied to "
                            + jar_folder_file.getAbsolutePath());
                }
            }
        }
    } else
        log.error("File " + d.getAbsolutePath() + "is not a directory");
}

From source file:com.jivesoftware.os.jive.utils.shell.utils.Untar.java

public static List<File> unTar(boolean verbose, final File outputDir, final File inputFile,
        boolean deleteOriginal) throws FileNotFoundException, IOException, ArchiveException {

    if (verbose) {
        System.out.println(String.format("untaring %s to dir %s.", inputFile.getAbsolutePath(),
                outputDir.getAbsolutePath()));
    }//from www  . j  a  va 2 s.c o  m

    final List<File> untaredFiles = new LinkedList<>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        String entryName = entry.getName();
        entryName = entryName.substring(entryName.indexOf("/") + 1);
        final File outputFile = new File(outputDir, entryName);
        if (entry.isDirectory()) {
            if (verbose) {
                System.out.println(String.format("Attempting to write output directory %s.",
                        getRelativePath(outputDir, outputFile)));
            }
            if (!outputFile.exists()) {
                if (verbose) {
                    System.out.println(String.format("Attempting to create output directory %s.",
                            getRelativePath(outputDir, outputFile)));
                }
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("Couldn't create directory %s.",
                            getRelativePath(outputDir, outputFile)));
                }
            }
        } else {
            try {
                if (verbose) {
                    System.out.println(
                            String.format("Creating output file %s.", getRelativePath(outputDir, outputFile)));
                }
                outputFile.getParentFile().mkdirs();
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();

                if (getRelativePath(outputDir, outputFile).contains("bin/")
                        || outputFile.getName().endsWith(".sh")) { // Hack!
                    if (verbose) {
                        System.out.println(
                                String.format("chmod +x file %s.", getRelativePath(outputDir, outputFile)));
                    }
                    outputFile.setExecutable(true);
                }

            } catch (Exception x) {
                System.err.println("failed to untar " + getRelativePath(outputDir, outputFile) + " " + x);
            }
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    if (deleteOriginal) {
        FileUtils.forceDelete(inputFile);
        if (verbose) {
            System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath()));
        }
    }
    return untaredFiles;
}