List of usage examples for java.io PrintWriter format
public PrintWriter format(String format, Object... args)
From source file:sf.net.experimaestro.connectors.UnixScriptProcessBuilder.java
private void writeRedirection(PrintWriter writer, Redirect redirect, int stream) throws FileSystemException { if (redirect == null) { writer.format(" %d> /dev/null", stream); } else {// www .j ava 2s.co m switch (redirect.type()) { case INHERIT: break; case APPEND: writer.format(" %d>> %s", stream, protect(connector.resolve(redirect.file()), QUOTED_SPECIAL)); break; case WRITE: writer.format(" %d> %s", stream, protect(connector.resolve(redirect.file()), QUOTED_SPECIAL)); break; default: throw new UnsupportedOperationException("Unsupported output redirection type: " + input.type()); } } }
From source file:sf.net.experimaestro.connectors.UnixScriptProcessBuilder.java
private void printRedirections(CommandContext env, int stream, PrintWriter writer, Redirect outputRedirect, List<FileObject> outputRedirects) throws FileSystemException { if (!outputRedirects.isEmpty()) { writer.format(" %d> >(tee", stream); for (FileObject file : outputRedirects) { writer.format(" \"%s\"", protect(env.resolve(file), QUOTED_SPECIAL)); }//from w w w .ja v a 2 s . c o m writeRedirection(writer, outputRedirect, stream); writer.write(")"); } else { // Finally, write the main redirection writeRedirection(writer, outputRedirect, stream); } }
From source file:de.tudarmstadt.lt.lm.web.servlet.LanguageModelProviderServlet.java
private void show_available(PrintWriter w) throws RemoteException, NotBoundException, UnsupportedEncodingException { scanForLanguageModels();/* w w w . j av a2 s.c o m*/ w.write(_html_header); w.format("<h3>%s</h3>%n", "Available Language Model Provider:"); for (Entry<String, Integer> lmkey2index : _lm_keys.entrySet()) w.format("<p><strong><a href='?lm=%s'>%s</a></strong></p>%n", URLEncoder.encode(lmkey2index.getKey(), "UTF-8"), lmkey2index.getKey()); w.write(_html_footer); }
From source file:sf.net.experimaestro.connectors.UnixScriptProcessBuilder.java
@Override final public XPMProcess start() throws LaunchException, IOException { final FileObject runFile = connector.resolveFile(path); final FileObject basepath = runFile.getParent(); final String baseName = runFile.getName().getBaseName(); try (CommandContext env = new CommandContext.FolderContext(connector, basepath, baseName)) { // Prepare the commands commands().prepare(env);/* www.ja va 2s . com*/ // First generate the run file PrintWriter writer = new PrintWriter(runFile.getContent().getOutputStream()); writer.format("#!%s%n", shPath); writer.format("# Experimaestro generated task: %s%n", path); writer.println(); // A command fails if any of the piped commands fail writer.println("set -o pipefail"); writer.println(); writer.println(); if (environment() != null) { for (Map.Entry<String, String> pair : environment().entrySet()) writer.format("export %s=\"%s\"%n", pair.getKey(), protect(pair.getValue(), QUOTED_SPECIAL)); } if (directory() != null) { writer.format("cd \"%s\"%n", protect(env.resolve(directory()), QUOTED_SPECIAL)); } if (!lockFiles.isEmpty()) { writer.format("%n# Checks that the locks are set%n"); for (String lockFile : lockFiles) { writer.format("test -f %s || exit 017%n", lockFile); } } writer.format( "%n%n# Set traps to cleanup (remove locks and temporary files, kill remaining processes) when exiting%n%n"); writer.format("trap cleanup EXIT SIGINT SIGTERM%n"); writer.format("cleanup() {%n"); for (String file : lockFiles) { writer.format(" rm -f %s;%n", file); } commands().forEachCommand(Streams.propagate(c -> { final CommandContext.NamedPipeRedirections namedRedirections = env.getNamedRedirections(c, false); for (FileObject file : Iterables.concat(namedRedirections.outputRedirections, namedRedirections.errorRedirections)) { writer.format(" rm -f %s;%n", env.resolve(file)); } })); // Kills remaining processes writer.println(" jobs -pr | xargs kill"); writer.format("}%n%n"); // Write the command final StringWriter sw = new StringWriter(); PrintWriter exitWriter = new PrintWriter(sw); exitWriter.format("code=$?; if test $code -ne 0; then%n"); if (exitCodePath != null) exitWriter.format(" echo $code > \"%s\"%n", protect(exitCodePath, QUOTED_SPECIAL)); exitWriter.format(" exit $code%n"); exitWriter.format("fi%n"); String exitScript = sw.toString(); writer.format("%n%n"); switch (input.type()) { case INHERIT: break; case READ: writer.format("cat \"%s\" | ", connector.resolve(input.file())); break; default: throw new UnsupportedOperationException("Unsupported input redirection type: " + input.type()); } writer.println("("); // The prepare all the commands writeCommands(env, writer, commands()); writer.print(") "); writeRedirection(writer, output, 1); writeRedirection(writer, error, 2); writer.println(); writer.print(exitScript); if (exitCodePath != null) writer.format("echo 0 > \"%s\"%n", protect(exitCodePath, QUOTED_SPECIAL)); if (donePath != null) writer.format("touch \"%s\"%n", protect(donePath, QUOTED_SPECIAL)); writer.close(); // Set the file as executable runFile.setExecutable(true, false); processBuilder.command(protect(path, SHELL_SPECIAL)); processBuilder.detach(true); processBuilder.redirectOutput(output); processBuilder.redirectError(error); processBuilder.job(job); return processBuilder.start(); } catch (Exception e) { throw new LaunchException(e); } }
From source file:erigo.filepump.FilePumpWorker.java
protected void writeToSFTP(String filename, int random_num) { String connectionStr = baseConnectionStr + "/" + filename; if (debug)//from w w w . j av a 2 s .c o m System.err.println(connectionStr); try { // Create remote file object FileObject fo = manager.resolveFile(connectionStr, fileSystemOptions); FileContent fc = fo.getContent(); OutputStream ostream = fc.getOutputStream(); if (ostream == null) { throw new IOException("Error opening output stream to SFTP"); } // Write content to file PrintWriter pw = new PrintWriter(ostream); pw.format("%06d\n", random_num); pw.close(); // Cleanup if (fo != null) fo.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:erigo.filepump.FilePumpWorker.java
protected void writeToFTP(String filepath, String filename, int random_num) { try {/*from w ww. j a v a2 s. c om*/ if (!filepath.equals(currentDir)) { // create or change to new directory if changed from previous if (filepath.startsWith("/")) ftpClient.changeWorkingDirectory("/"); else ftpClient.changeWorkingDirectory(loginDir); // new dirs relative to loginDir ftpCreateDirectoryTree(ftpClient, filepath); // mkdirs as needed, leaves working dir @ new currentDir = filepath; } // boolean success = ftpClient.storeFile(filename, fis); // JPW: No need to create a temporary file in our case // OutputStream ostream = ftpClient.storeFileStream(filename+".tmp"); OutputStream ostream = ftpClient.storeFileStream(filename); if (debug) System.err.println("filepath: " + filepath + ", filename: " + filename); if (ostream == null) { throw new IOException("Unable to FTP file: " + ftpClient.getReplyString()); } // Write content to file PrintWriter pw = new PrintWriter(ostream); pw.format("%06d\n", random_num); pw.close(); if (!ftpClient.completePendingCommand()) throw new IOException("Unable to FTP file: " + ftpClient.getReplyString()); // We didn't create temporary file up above, so no need to rename it // if(!ftpClient.rename(filename+".tmp", filename)) // throw new IOException("Unable to rename file: " + ftpClient.getReplyString()); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.kalypso.model.hydrology.internal.postprocessing.Block.java
public void exportToFile(final File exportFile, final DateFormat dateFormat) throws IOException { final PrintWriter writer = new PrintWriter(exportFile); final Set<Entry<Date, Double>> entrySet = m_data.entrySet(); for (final Entry<Date, Double> entry : entrySet) { final Date dateKey = entry.getKey(); final Double value = entry.getValue(); final String dateString = dateFormat.format(dateKey); writer.print(dateString);//w w w.j a va 2 s . c o m writer.print(' '); writer.format("%.3f", value); //$NON-NLS-1$ } writer.close(); }
From source file:sf.net.experimaestro.scheduler.Resource.java
/** * Writes an XML description of the resource * * @param out// ww w. j a v a2 s . co m * @param config The configuration for printing * @deprecated Use {@linkplain #toJSON()} */ @Deprecated public void printXML(PrintWriter out, PrintConfig config) { out.format("<div><b>Resource id</b>: %s</h2>", getLocator()); out.format("<div><b>Status</b>: %s</div>", getState()); }
From source file:org.everrest.websockets.client.WSClient.java
private byte[] getHandshake() { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintWriter handshake = new PrintWriter(out); handshake.format("GET %s HTTP/1.1\r\n", target.getPath()); final int port = target.getPort(); if (port == 80) { handshake.format("Host: %s\r\n", target.getHost()); } else {/*from w w w . jav a 2s . c o m*/ handshake.format("Host: %s:%d\r\n", target.getHost(), port); } handshake.append("Upgrade: Websocket\r\n"); handshake.append("Connection: Upgrade\r\n"); String[] subProtocol = getSubProtocols(); if (subProtocol != null && subProtocol.length > 0) { handshake.format("Sec-WebSocket-Protocol: %s\r\n", Arrays.toString(subProtocol)); } handshake.format("Sec-WebSocket-Key: %s\r\n", secWebSocketKey); handshake.format("Sec-WebSocket-Version: %d\r\n", 13); handshake.append("Sec-WebSocket-Protocol: chat\r\n"); String origin = getOrigin(); if (origin != null) { handshake.format("Origin: %s\r\n", origin); } handshake.append('\r'); handshake.append('\n'); handshake.flush(); return out.toByteArray(); }
From source file:sf.net.experimaestro.connectors.UnixScriptProcessBuilder.java
private void writeCommands(CommandContext env, PrintWriter writer, Commands commands) throws IOException { final ArrayList<AbstractCommand> list = commands.reorder(); int detached = 0; for (AbstractCommand command : list) { // Write files final CommandContext.NamedPipeRedirections namedRedirections = env.getNamedRedirections(command, false); for (FileObject file : Iterables.concat(namedRedirections.outputRedirections, namedRedirections.errorRedirections)) { writer.format("mkfifo \"%s\"%n", protect(env.resolve(file), QUOTED_SPECIAL)); }//from w w w. j a v a2s . c om if (command instanceof Commands) { writer.println("("); writeCommands(env, writer, (Commands) command); writer.print(") "); } else { for (CommandComponent argument : ((Command) command).list()) { writer.print(' '); if (argument instanceof Command.Pipe) { writer.print(" | "); } else if (argument instanceof SubCommand) { writer.println(" ("); writeCommands(env, writer, ((SubCommand) argument).commands()); writer.println(); writer.print(" )"); } else { writer.print(protect(argument.toString(env), SHELL_SPECIAL)); } } } printRedirections(env, 1, writer, command.getOutputRedirect(), namedRedirections.outputRedirections); printRedirections(env, 2, writer, command.getErrorRedirect(), namedRedirections.errorRedirections); if (env.detached(command)) { // Just keep a pointer writer.format(" & CHILD_%d=$!%n", detached); detached++; } else { // Stop if an error occurred writer.println(" || exit $?"); } } // Monitors detached jobs for (int i = 0; i < detached; i++) { writer.format("wait $CHILD_%d || exit $?%n", i); } }