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.hops.hopsworks.common.util.HopsUtils.java

/**
 * Utility method that copies project user certificates from the Database, to
 * either hdfs to be passed as LocalResources to the YarnJob or to used
 * by another method.//from   w w w.ja  va  2 s.  com
 *
 * @param project
 * @param username
 * @param localTmpDir
 * @param remoteTmpDir
 * @param jobType
 * @param dfso
 * @param projectLocalResources
 * @param jobSystemProperties
 * @param flinkCertsDir
 * @param applicationId
 */
public static void copyProjectUserCerts(Project project, String username, String localTmpDir,
        String remoteTmpDir, JobType jobType, DistributedFileSystemOps dfso,
        List<LocalResourceDTO> projectLocalResources, Map<String, String> jobSystemProperties,
        String flinkCertsDir, String applicationId, CertificateMaterializer certMat, boolean isRpcTlsEnabled) {

    // Let the Certificate Materializer handle the certificates
    UserCerts userCert = new UserCerts(project.getName(), username);
    try {
        certMat.materializeCertificatesLocal(username, project.getName());
        CertificateMaterializer.CryptoMaterial material = certMat.getUserMaterial(username, project.getName());
        userCert.setUserKey(material.getKeyStore().array());
        userCert.setUserCert(material.getTrustStore().array());
        userCert.setUserKeyPwd(new String(material.getPassword()));
    } catch (IOException | CryptoPasswordNotFoundException ex) {
        throw new RuntimeException("Could not materialize user certificates", ex);
    }

    //Check if the user certificate was actually retrieved
    if (userCert.getUserCert() != null && userCert.getUserCert().length > 0 && userCert.getUserKey() != null
            && userCert.getUserKey().length > 0) {

        Map<String, byte[]> certFiles = new HashMap<>();
        certFiles.put(Settings.T_CERTIFICATE, userCert.getUserCert());
        certFiles.put(Settings.K_CERTIFICATE, userCert.getUserKey());

        try {
            String kCertName = HopsUtils.getProjectKeystoreName(project.getName(), username);
            String tCertName = HopsUtils.getProjectTruststoreName(project.getName(), username);
            String passName = getProjectMaterialPasswordName(project.getName(), username);

            try {
                if (jobType != null) {
                    switch (jobType) {
                    case FLINK:
                        File appDir = Paths.get(flinkCertsDir, applicationId).toFile();
                        if (!appDir.exists()) {
                            appDir.mkdir();
                        }

                        File f_k_cert = new File(appDir.toString() + File.separator + kCertName);
                        f_k_cert.setExecutable(false);
                        f_k_cert.setReadable(true, true);
                        f_k_cert.setWritable(false);

                        File t_k_cert = new File(appDir.toString() + File.separator + tCertName);
                        t_k_cert.setExecutable(false);
                        t_k_cert.setReadable(true, true);
                        t_k_cert.setWritable(false);

                        if (!f_k_cert.exists()) {
                            Files.write(certFiles.get(Settings.K_CERTIFICATE), f_k_cert);
                            Files.write(certFiles.get(Settings.T_CERTIFICATE), t_k_cert);
                        }

                        File certPass = new File(appDir.toString() + File.separator + passName);
                        certPass.setExecutable(false);
                        certPass.setReadable(true, true);
                        certPass.setWritable(false);
                        FileUtils.writeStringToFile(certPass, userCert.getUserKeyPwd(), false);
                        jobSystemProperties.put(Settings.CRYPTO_MATERIAL_PASSWORD, certPass.toString());
                        jobSystemProperties.put(Settings.K_CERTIFICATE, f_k_cert.toString());
                        jobSystemProperties.put(Settings.T_CERTIFICATE, t_k_cert.toString());
                        break;
                    case PYSPARK:
                    case SPARK:
                        Map<String, File> certs = new HashMap<>();
                        certs.put(Settings.K_CERTIFICATE, new File(localTmpDir + File.separator + kCertName));
                        certs.put(Settings.T_CERTIFICATE, new File(localTmpDir + File.separator + tCertName));
                        certs.put(Settings.CRYPTO_MATERIAL_PASSWORD,
                                new File(localTmpDir + File.separator + passName));

                        for (Map.Entry<String, File> entry : certs.entrySet()) {
                            //Write the actual file(cert) to localFS
                            //Create HDFS certificate directory. This is done
                            //So that the certificates can be used as LocalResources
                            //by the YarnJob
                            if (!dfso.exists(remoteTmpDir)) {
                                dfso.mkdir(new Path(remoteTmpDir),
                                        new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL));
                            }
                            //Put project certificates in its own dir
                            String certUser = project.getName() + "__" + username;
                            String remoteTmpProjDir = remoteTmpDir + File.separator + certUser;
                            if (!dfso.exists(remoteTmpProjDir)) {
                                dfso.mkdir(new Path(remoteTmpProjDir),
                                        new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE));
                                dfso.setOwner(new Path(remoteTmpProjDir), certUser, certUser);
                            }

                            String remoteProjAppDir = remoteTmpProjDir + File.separator + applicationId;
                            Path remoteProjAppPath = new Path(remoteProjAppDir);
                            if (!dfso.exists(remoteProjAppDir)) {
                                dfso.mkdir(remoteProjAppPath,
                                        new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE));
                                dfso.setOwner(remoteProjAppPath, certUser, certUser);
                            }

                            dfso.copyToHDFSFromLocal(false, entry.getValue().getAbsolutePath(),
                                    remoteProjAppDir + File.separator + entry.getValue().getName());

                            dfso.setPermission(
                                    new Path(remoteProjAppDir + File.separator + entry.getValue().getName()),
                                    new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.NONE));
                            dfso.setOwner(
                                    new Path(remoteProjAppDir + File.separator + entry.getValue().getName()),
                                    certUser, certUser);

                            projectLocalResources.add(new LocalResourceDTO(entry.getKey(),
                                    "hdfs://" + remoteProjAppDir + File.separator + entry.getValue().getName(),
                                    LocalResourceVisibility.APPLICATION.toString(),
                                    LocalResourceType.FILE.toString(), null));
                        }
                        break;
                    default:
                        break;
                    }
                }
            } catch (IOException ex) {
                LOG.log(Level.SEVERE, "Error writing project user certificates to local fs", ex);
            }

        } finally {
            if (jobType != null) {
                certMat.removeCertificatesLocal(username, project.getName());
            }
        }
    }
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

public static String unzipFile(File f, String todirname, String toDirectory) {
    File todir = new File(toDirectory);
    if (!todir.exists())
        todir.mkdirs();/* w w w  . ja v  a2 s  . c o m*/

    try {
        // Check if the zip file contains only one directory
        ZipFile zfile = new ZipFile(f);
        String topDir = null;
        boolean isOneDir = true;
        for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements();) {
            ZipEntry ze = e.nextElement();
            String name = ze.getName().replaceAll("/.+$", "");
            name = name.replaceAll("/$", "");
            // OSX Zips carry an extra __MACOSX directory. Ignore it
            if (name.equals("__MACOSX"))
                continue;

            if (topDir == null)
                topDir = name;
            else if (!topDir.equals(name)) {
                isOneDir = false;
                break;
            }
        }
        zfile.close();

        // Delete existing directory (if any)
        FileUtils.deleteDirectory(new File(toDirectory + File.separator + todirname));

        // Unzip file(s) into toDirectory/todirname
        ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            // OSX Zips carry an extra __MACOSX directory. Ignore it
            if (fileName.startsWith("__MACOSX")) {
                ze = zis.getNextEntry();
                continue;
            }

            // Get relative file path translated to 'todirname'
            if (isOneDir)
                fileName = fileName.replaceFirst(topDir, todirname);
            else
                fileName = todirname + File.separator + fileName;

            // Create directories
            File newFile = new File(toDirectory + File.separator + fileName);
            if (ze.isDirectory())
                newFile.mkdirs();
            else
                newFile.getParentFile().mkdirs();

            try {
                // Copy file
                FileOutputStream fos = new FileOutputStream(newFile);
                IOUtils.copy(zis, fos);
                fos.close();

                String mime = new Tika().detect(newFile);
                if (mime.equals("application/x-sh") || mime.startsWith("text/"))
                    FileUtils.writeLines(newFile, FileUtils.readLines(newFile));

                // Set all files as executable for now
                newFile.setExecutable(true);
            } catch (FileNotFoundException fe) {
                // Silently ignore
                //fe.printStackTrace();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();

        return toDirectory + File.separator + todirname;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:name.martingeisse.ecobuild.moduletool.c.DevHostApplicationTool.java

@Override
protected void onModuleOutdated(IModuleToolContext context) throws IOException {

    File moduleSourceFolder = context.getModuleSourceFolder();
    File moduleBuildFolder = context.getModuleBuildFolder();
    AbstractLogger logger = context.getLogger();

    String executableName = getOutputFileName(context);
    String sourceFiles = StringUtils.join(moduleSourceFolder.list(new SuffixFileFilter(".c")), ' ');
    String outputFileName = moduleBuildFolder.getCanonicalPath() + "/" + executableName;
    String command = "$" + Constants.GCC_PATH + "$ -Wall -m32 -o $" + outputFileName + "$ " + sourceFiles;
    new GccInvocation(context, moduleSourceFolder, command, logger).invoke();
    File outputFile = new File(moduleBuildFolder, executableName);
    if (!outputFile.setExecutable(true)) {
        context.getLogger().logError("could not make DevApplication target executable: " + outputFile);
    }/* ww  w. jav a2 s.  c  om*/
}

From source file:org.phoenicis.tools.system.terminal.MacOSTerminalOpener.java

@Override
public void openTerminal(String workingDirectory, Map<String, String> environmentVariables) {
    try {/*w  w  w  .j  a  v  a2 s.  c  o m*/
        final File temporaryScript = File.createTempFile("terminal", "sh");
        if (temporaryScript.setExecutable(true)) {
            temporaryScript.deleteOnExit();
            try (FileOutputStream fileOutputStream = new FileOutputStream(temporaryScript)) {
                IOUtils.write(createScript(workingDirectory, environmentVariables), fileOutputStream, "UTF-8");
                fileOutputStream.flush();
            }

            final ProcessBuilder processBuilder = new ProcessBuilder().command("/usr/bin/open", "-b",
                    "com.apple.terminal", temporaryScript.getAbsolutePath());

            processBuilder.start();
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java

/**
 * Copy a file from one place to another, providing progress along the way.
 *//* www .j  a va 2s.c o m*/
public static void copyFile(File fromFile, File toFile, IProgressMonitor monitor) throws IOException {

    byte[] data = new byte[4096];

    InputStream in = new FileInputStream(fromFile);

    toFile.delete();

    OutputStream out = new FileOutputStream(toFile);

    int count = in.read(data);

    while (count != -1) {
        out.write(data, 0, count);
        count = in.read(data);
    }

    in.close();
    out.close();

    toFile.setLastModified(fromFile.lastModified());
    if (fromFile.canExecute()) {
        toFile.setExecutable(true);
    }

    monitor.worked(1);

}

From source file:org.phoenicis.tools.system.terminal.LinuxTerminalOpener.java

@Override
public void openTerminal(String workingDirectory, Map<String, String> environmentVariables) {
    try {//from  ww  w  .j av a 2s. co  m
        final File temporaryScript = File.createTempFile("terminal", "sh");
        if (temporaryScript.setExecutable(true)) {
            temporaryScript.deleteOnExit();
            try (FileOutputStream fileOutputStream = new FileOutputStream(temporaryScript)) {
                IOUtils.write(createScript(workingDirectory, environmentVariables), fileOutputStream, "UTF-8");
                fileOutputStream.flush();
            }

            final ProcessBuilder processBuilder = new ProcessBuilder().command(terminalCommand.orElse("xterm"),
                    "-e", "bash -c '" + temporaryScript.getAbsolutePath() + "'");

            processBuilder.start();
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.codice.git.hook.GitHooks.java

/**
 * Copied/expanded from org.apache.commons.io.IOUtils
 * Takes the name of an input file and the name of an output file and copies the input file to the
 * output file.//from  ww  w.  j av a  2  s .c o m
 *
 * @param fromName       the name of the input file to read from
 * @param output         the name of the output file to write to
 * @param substitutor    the optional string substitutor to use when processing files (can be <code>null</code>)
 * @param makeExecutable boolean indicating whether the output file should be made executable when done copying
 * @return true indicates the file was successfully copied, false if an error occurs
 * @throws java.lang.NullPointerException if the inputu or output is null
 * @throws IOException                    if an I/O error occurs
 * @throws java.lang.ArithmeticException  if the byte count is too large
 */
private static boolean copyFromJar(String fromName, File output, StrSubstitutor substitutor,
        boolean makeExecutable) throws IOException {
    boolean successful = true;
    InputStream is = null;

    try {
        LOGGER.log(Level.FINE, "Copying file {0} to {1}", new Object[] { fromName, output });
        is = GitHooks.class.getResourceAsStream(fromName);
        if (is == null) {
            LOGGER.log(Level.WARNING, "Unable to locate file {0} for copying.", fromName);
            throw new IOException("Unable to locate file " + fromName + " for copying");
        }
        final PrintWriter pw = new PrintWriter(new FileWriter(output));

        for (final String line : IOUtils.readLines(is, Charsets.UTF_8)) {
            if (substitutor != null) {
                pw.println(substitutor.replace(line));
            } else {
                pw.println(line);
            }
        }
        pw.close();
        if (makeExecutable) {
            output.setExecutable(true);
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Exception copying to file " + output + " - file may be incompletely copied.",
                e);
        successful = false;
    } finally {
        IOUtils.closeQuietly(is);
    }
    return successful;
}

From source file:com.twosigma.beaker.cpp.utils.TempCppFiles.java

private void copyFile(String fileName) {
    InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);

    OutputStream os = null;//from w w  w .ja  v  a  2 s.c o m
    try {
        os = new FileOutputStream(tempDirectory.toString() + "/" + fileName);
        byte[] buffer = new byte[4096];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    File file = new File(tempDirectory.toString() + "/" + fileName);
    file.setExecutable(true);
}

From source file:alluxio.network.TieredIdentityFactoryTest.java

private String setupScript(String tieredIdentityString) throws Exception {
    File script = mFolder.newFile();
    script.setExecutable(true);
    String content = "#!/bin/bash\n" + "echo \"" + tieredIdentityString + "\"\n";
    FileUtils.writeStringToFile(script, content);
    return script.getAbsolutePath();
}

From source file:edu.wisc.doit.tcrypt.TokenEncryptDecryptIT.java

@Test
public void testOpenSSLEncJavaDec() throws Exception {
    //Encrypt with openssl
    final File encryptFileScript = setupTempFile("encryptToken.sh");
    encryptFileScript.setExecutable(true);

    final File publicKey = setupTempFile("my.wisc.edu-public.pem");

    final String expected = "foobar";
    final ProcessBuilder pb = new ProcessBuilder(encryptFileScript.getAbsolutePath(),
            publicKey.getAbsolutePath(), expected);

    final Process p = pb.start();
    final int ret = p.waitFor();
    if (ret != 0) {
        final String pOut = IOUtils.toString(p.getInputStream(), TokenEncrypter.CHARSET).trim();
        System.out.println(pOut);
        final String pErr = IOUtils.toString(p.getErrorStream(), TokenEncrypter.CHARSET).trim();
        System.out.println(pErr);
    }//from   w ww .  j av a2  s  .  co  m
    assertEquals(0, ret);

    final String encrypted = IOUtils.toString(p.getInputStream(), TokenEncrypter.CHARSET).trim();

    //Decrypt with java
    final String actual = this.tokenDecrypter.decrypt(encrypted);

    //Verify
    assertEquals(expected, actual);
}