List of usage examples for org.apache.commons.vfs2 FileObject getName
FileName getName();
From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java
private boolean listContainsNote(List<FileObject> list, FileObject note) { for (FileObject fileObj : list) { if (fileObj.getName().getBaseName().equals(note.getName().getBaseName())) { return true; }//from w w w. j ava 2s.c o m } return false; }
From source file:sf.net.experimaestro.connectors.SingleHostConnector.java
/** * Resolve a FileObject to a local path/*from w w w . j a v a2s. co m*/ * <p/> * Throws an exception when the file name cannot be resolved, i.e. when * the file object is not */ public String resolve(FileObject file) throws FileSystemException { if (!contains(file.getFileSystem())) { throw new FileSystemException(format("Cannot resolve file %s within filesystem %s", file, this)); } return file.getName().getPath(); }
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);//from w w w . ja v a 2s . c o m // 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:sf.net.experimaestro.scheduler.ExclusiveDependency.java
@Override synchronized protected Lock _lock(Scheduler scheduler, Resource from, String pid) throws LockException { Resource resource = getFrom(scheduler, from); try {// ww w .j a v a2 s .co m FileObject file = resource.getFileWithExtension(Resource.LOCK_EXTENSION); final Lock lockFile = resource.getMainConnector().createLockFile(file.getName().getPath(), true); return lockFile; } catch (FileSystemException e) { throw new LockException(e); } }
From source file:sf.net.experimaestro.scheduler.ResourceLocator.java
/** * Resolve a path relative to this resource * * @param path The path/*from w w w. ja va 2 s.com*/ * @param parent Is the path relative to the parent? * @return A new resource from object * @throws FileSystemException If something goes wrong while accessing the file system */ public ResourceLocator resolvePath(String path, boolean parent) throws FileSystemException { // Get the current file FileObject file = getFile(); // Get the target (revert to file if null) FileObject target = (parent ? file.getParent() : file); if (target == null) target = file; target = target.resolveFile(path); return new ResourceLocator(connector, target.getName().getPath()); }
From source file:tain.kr.test.vfs.v01.Shell.java
/** * Does a 'cp' command.// ww w. j a v a2s .co m */ private void cp(final String[] cmd) throws Exception { if (cmd.length < 3) { throw new Exception("USAGE: cp <src> <dest>"); } final FileObject src = mgr.resolveFile(cwd, cmd[1]); FileObject dest = mgr.resolveFile(cwd, cmd[2]); if (dest.exists() && dest.getType() == FileType.FOLDER) { dest = dest.resolveFile(src.getName().getBaseName()); } dest.copyFrom(src, Selectors.SELECT_ALL); }
From source file:tain.kr.test.vfs.v01.Shell.java
/** * Does a 'cd' command./*from ww w .jav a 2 s. com*/ * If the taget directory does not exist, a message is printed to <code>System.err</code>. */ private void cd(final String[] cmd) throws Exception { final String path; if (cmd.length > 1) { path = cmd[1]; } else { path = System.getProperty("user.home"); } // Locate and validate the folder final FileObject tmp = mgr.resolveFile(cwd, path); if (tmp.exists()) { cwd = tmp; } else { System.out.println("Folder does not exist: " + tmp.getName()); } System.out.println("Current folder is " + cwd.getName()); }
From source file:tain.kr.test.vfs.v01.Shell.java
/** * Does an 'ls' command.//from w ww .ja v a2 s.co m */ private void ls(final String[] cmd) throws FileSystemException { int pos = 1; final boolean recursive; if (cmd.length > pos && cmd[pos].equals("-R")) { recursive = true; pos++; } else { recursive = false; } final FileObject file; if (cmd.length > pos) { file = mgr.resolveFile(cwd, cmd[pos]); } else { file = cwd; } if (file.getType() == FileType.FOLDER) { // List the contents System.out.println("Contents of " + file.getName()); listChildren(file, recursive, ""); } else { // Stat the file System.out.println(file.getName()); final FileContent content = file.getContent(); System.out.println("Size: " + content.getSize() + " bytes."); final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime())); System.out.println("Last modified: " + lastMod); } }
From source file:tain.kr.test.vfs.v01.Shell.java
/** * Lists the children of a folder./*from ww w . j a v a2 s.c o m*/ */ private void listChildren(final FileObject dir, final boolean recursive, final String prefix) throws FileSystemException { final FileObject[] children = dir.getChildren(); for (final FileObject child : children) { System.out.print(prefix); System.out.print(child.getName().getBaseName()); if (child.getType() == FileType.FOLDER) { System.out.println("/"); if (recursive) { listChildren(child, recursive, prefix + " "); } } else { System.out.println(); } } }