List of usage examples for java.io File setExecutable
public boolean setExecutable(boolean executable)
From source file:edu.isi.wings.execution.engine.api.impl.distributed.DistributedExecutionEngine.java
@Override public ProcessStatus call() throws Exception { File tempdir = File.createTempFile(planName + "-", "-" + exeName); if (!tempdir.delete() || !tempdir.mkdirs()) throw new Exception("Cannot create temp directory"); ProcessStatus status = new ProcessStatus(); try {//w w w. j ava2s . c o m File codef = new File(this.codeBinary); codef.setExecutable(true); PrintWriter fout = null; if (outfilepath != null) { File f = new File(outfilepath); f.getParentFile().mkdirs(); fout = new PrintWriter(f); } ProcessBuilder pb = new ProcessBuilder(args); pb.directory(tempdir); pb.redirectErrorStream(true); // Set environment variables for (String var : this.environment.keySet()) pb.environment().put(var, this.environment.get(var)); this.process = pb.start(); // Read output stream StreamGobbler outputGobbler = new StreamGobbler(this.process.getInputStream(), fout); outputGobbler.start(); // Wait for the process to exit this.process.waitFor(); status.setExitValue(this.process.exitValue()); status.setLog(outputGobbler.getLog()); } catch (InterruptedException e) { if (this.process != null) { //System.out.println("Stopping remote process"); this.process.destroy(); } status.setLog("!! Stopping Remotely !! .. " + exeName); status.setExitValue(-1); } catch (Exception e) { status.setLog(e.getMessage()); status.setExitValue(-1); } // Delete temp directory FileUtils.deleteDirectory(tempdir); return status; }
From source file:com.esminis.server.library.service.server.installpackage.InstallerPackage.java
private void install(File targetDirectory, String filename, InputStream input) throws Throwable { final File file = new File(targetDirectory, filename); FileOutputStream output = null; try {/*from w ww . j a v a2s. c o m*/ output = new FileOutputStream(file); IOUtils.copy(input, output); if (!file.isFile() || (!file.canExecute() && !file.setExecutable(true))) { throw new Exception("Cannot set file permissions: " + file.getAbsolutePath()); } } finally { if (output != null) { try { output.close(); } catch (IOException ignored) { } } } }
From source file:de.flapdoodle.embed.process.extract.ArchiveIsFileExtractor.java
@Override public void extract(IDownloadConfig runtime, File source, File destination, Pattern file) throws IOException { IProgressListener progressListener = runtime.getProgressListener(); String progressLabel = "Extract (not really) " + source; progressListener.start(progressLabel); FileInputStream fin = new FileInputStream(source); BufferedInputStream in = new BufferedInputStream(fin); try {//from w w w .j a v a 2 s . c o m Files.write(in, destination); destination.setExecutable(true); progressListener.done(progressLabel); } finally { fin.close(); } }
From source file:com.axelor.studio.service.ModuleRecorderService.java
private String getRestartScriptPath() throws IOException, FileNotFoundException { String ext = "sh"; if (SystemUtils.IS_OS_WINDOWS) { ext = "bat"; }/*w w w .ja va 2 s .com*/ InputStream stream = this.getClass().getResourceAsStream("/script/RestartServer." + ext); File script = File.createTempFile("RestartServer", "." + ext); script.setExecutable(true); FileOutputStream out = new FileOutputStream(script); IOUtils.copy(stream, out); out.close(); return script.getAbsolutePath(); }
From source file:com.iontorrent.utils.settings.Config.java
public Config(File userFile, URL initialConfigFile) { if (userFile == null) { warn("Got no user file:" + userFile); //userConfigFile = initialConfigFile }/*from w w w .j av a 2 s . c o m*/ if (!userFile.exists() || userFile.length() < 10) { p("Copy source to target first"); File parent = userFile.getParentFile(); if (!parent.exists()) { p("Parent " + parent + " does not exist, creating folder"); boolean ok = parent.mkdirs(); parent.setExecutable(true); parent.setWritable(true); if (!parent.exists()) { err("Parent folder still does not exist"); userFile = new File(parent.getParentFile() + "/" + userFile.getName()); p("Trying " + userFile); } } boolean ok = FileTools.copyUrl(initialConfigFile, userFile); if (!ok) { err("Was uable to copy " + initialConfigFile + " to " + userFile); } } else { p("Already found user file " + userFile); } try { this.url = userFile.toURI().toURL(); } catch (MalformedURLException ex) { Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex); } p("Reading XML file from URL:" + url); this.setXMLConfig(url); init(); }
From source file:com.thoughtworks.go.util.command.CommandLineTest.java
@Test @DisabledOnOs(OS.WINDOWS)/* w w w .jav a 2 s.c o m*/ void shouldBeAbleToRunCommandsFromRelativeDirectories() throws IOException { File shellScript = temporaryFolder.newFile("hello-world.sh"); FileUtils.writeStringToFile(shellScript, "echo ${PWD}", UTF_8); assertThat(shellScript.setExecutable(true), is(true)); CommandLine line = CommandLine.createCommandLine("../hello-world.sh").withWorkingDir(subFolder) .withEncoding("utf-8"); InMemoryStreamConsumer out = new InMemoryStreamConsumer(); line.execute(out, new EnvironmentVariableContext(), null).waitForExit(); assertThat(out.getAllOutput().trim(), endsWith("subFolder")); }
From source file:com.facebook.buck.util.ProjectFilesystemTest.java
@Test public void testCreateZipPreservesExecutablePermissions() throws IOException { // Create a empty executable file. File exe = tmp.newFile("test.exe"); exe.setExecutable(true); // Archive it into a zipfile using `ProjectFileSystem.createZip`. File zipFile = new File(tmp.getRoot().toPath().toString() + "/test.zip"); filesystem.createZip(ImmutableList.of(exe.toPath()), zipFile); // Now unpack the archive (using apache's common-compress, as it preserves // executable permissions) and verify that the archive entry has executable // permissions. try (ZipFile zip = new ZipFile(zipFile)) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); assertTrue(entries.hasMoreElements()); ZipArchiveEntry entry = entries.nextElement(); Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE)); assertFalse(entries.hasMoreElements()); }/*from ww w . j a v a 2s . c o m*/ }
From source file:org.dbgl.util.PlatformUtils.java
public static void createShortcut(final Profile profile, final List<DosboxVersion> dbversionsList) throws IOException { DosboxVersion dbversion = DosboxVersion.findById(dbversionsList, profile.getDbversionId()); String strictFilename = profile.getTitle().replaceAll("[\\/:*?\"<>|]", " ").trim(); StringBuffer params = new StringBuffer(128); if (dbversion.isMultiConfig()) { params.append("-conf \"\"").append(dbversion.getCanonicalConfFile()).append("\"\" "); }/*from w w w .ja va2s . c o m*/ params.append("-conf \"\"").append(profile.getCanonicalConfFile()).append("\"\""); if (Settings.getInstance().getSettings().getBooleanValue("dosbox", "hideconsole")) { params.append(" -noconsole"); } if (IS_WINDOWS) { File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory(); File lnkFile = new File(desktopDir, strictFilename + ".lnk"); File vbsFile = FileUtils.canonicalToData("shortcut.vbs"); BufferedWriter vbsWriter = new BufferedWriter(new FileWriter(vbsFile)); vbsWriter.write("Set oWS = WScript.CreateObject(\"WScript.Shell\")" + EOLN); vbsWriter.write("Set oLink = oWS.CreateShortcut(\"" + lnkFile.getCanonicalPath() + "\")" + EOLN); vbsWriter.write("oLink.TargetPath = \"" + dbversion.getCanonicalExecutable().getPath() + "\"" + EOLN); vbsWriter.write("oLink.Arguments = \"" + params.toString() + "\"" + EOLN); vbsWriter.write("oLink.Description = \"" + Settings.getInstance().msg("general.shortcut.title", new Object[] { strictFilename }) + "\"" + EOLN); vbsWriter.write("oLink.WorkingDirectory = \"" + FileUtils.getDosRoot() + "\"" + EOLN); vbsWriter.write("oLink.Save" + EOLN); vbsWriter.close(); Process proc = Runtime.getRuntime().exec(new String[] { "CSCRIPT", vbsFile.getCanonicalPath() }, null, vbsFile.getParentFile()); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "CSCRIPT stderr"); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "CSCRIPT stdout"); outputGobbler.start(); errorGobbler.start(); try { proc.waitFor(); } catch (InterruptedException e) { } FileUtils.removeFile(vbsFile); } else if (IS_LINUX) { File desktopDir = new File(System.getProperty("user.home"), "/Desktop"); File desktopFile = new File(desktopDir, strictFilename + ".desktop"); BufferedWriter desktopWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(desktopFile), "UTF-8")); desktopWriter.write("[Desktop Entry]" + EOLN); desktopWriter.write("Version=1.0" + EOLN); desktopWriter.write("Type=Application" + EOLN); desktopWriter.write("Name=" + strictFilename + EOLN); desktopWriter.write("Comment=" + Settings.getInstance().msg("general.shortcut.title", new Object[] { strictFilename }) + EOLN); desktopWriter.write("Icon=" + new File(dbversion.getCanonicalExecutable().getParent(), "dosbox.ico").getPath() + EOLN); desktopWriter.write("TryExec=" + dbversion.getCanonicalExecutable().getPath() + EOLN); desktopWriter.write("Exec=" + dbversion.getCanonicalExecutable().getPath() + " " + StringUtils.replace(params.toString(), "\"\"", "\"") + EOLN); desktopWriter.write("Path=" + FileUtils.getDosRoot() + EOLN); desktopWriter.close(); desktopFile.setExecutable(true); } }
From source file:org.springframework.cloud.stream.app.plugin.SpringCloudStreamAppMojo.java
private String moveProjectWithMavenModelsUpdated(String key, File project, File generatedProjectHome, boolean testIgnored, String generatedProjectVersion) throws IOException, XmlPullParserException { Model model = isNewDir(generatedProjectHome) ? MavenModelUtils.populateModel(generatedProjectHome.getName(), getApplicationGroupId(applicationType), generatedProjectVersion) : MavenModelUtils.getModelFromContainerPom(generatedProjectHome, getApplicationGroupId(applicationType), generatedProjectVersion); if (model != null && MavenModelUtils.addModuleIntoModel(model, key)) { MavenModelUtils.writeModelToFile(model, new FileOutputStream(new File(generatedProjectHome, "pom.xml"))); }/*from www . j a v a 2 s.co m*/ try { File generatedAppHome = new File(generatedProjectHome, key); removeExistingContent(generatedAppHome.toPath()); FileUtils.copyDirectory(new File(project, key), generatedAppHome); File mvnw = new File(generatedAppHome, "mvnw"); if (mvnw.exists()) { mvnw.setExecutable(true); } if (testIgnored) { SpringCloudStreamPluginUtils .ignoreUnitTestGeneratedByInitializer(generatedAppHome.getAbsolutePath()); } //MavenModelUtils.addModuleInfoToContainerPom(generatedProjectHome); return generatedAppHome.getAbsolutePath(); } catch (IOException e) { getLog().error("Error during plugin execution", e); throw new IllegalStateException(e); } }
From source file:samplecode.tools.PropertiesBasedScriptGenerator.java
private File getFile() { String filename = createFilename(className, directory); File script = new File(filename); try {/*from w w w . jav a 2 s .com*/ script.createNewFile(); } catch (IOException ioException) { getLogger().error("Unable to create a new file from " + script + " " + ioException); return null; } if (!script.setExecutable(true)) { String msg = String.format("The attempt to set the permissions of %s failed. This " + "means the script will not be executable and must be invoked with a" + " shell command.", script); getLogger().error(msg); return null; } try { Writer writer = new FileWriter(script); writer.write(generateContents()); writer.close(); } catch (IOException e) { getLogger().error("Unable to write script " + script); return null; } return script; }