Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.fs;
 
 
 import java.io.File;
 import java.net.URI;
 import java.net.URL;
 import java.util.*;

Configures and creates nodes. You'll usually create a single world instance in your application, configure it and afterwards use it through-out your application to create nodes via World.node or World.file.

Sushi's FS subsystem forms a tree: An world object is the root, having filesystems as it's children, roots as grand-children and nodes as leaves. This tree is traversable from nodes up to the world object via Node.getRoot(), Root.getFilesystem() and Filesystem.getWorld(), which is used internally e.g. to pick default encoding settings from world. (Traversing in reverse order is not implemented, that too resource consuming)

Technically, you can creates as many world objects as you which, but nodes from different world instances cannot interact, so you'll usually stick with a single world instance. Note that there's no predefined static world instance in Sushi. You can either get the world from node instances, or, if you feel a single world instance should be available everywhere in your application use dependency injection.

TODO: Multi-threading. Currently, you need to know fs system internals to properly synchronized ...

 
 public class World {
     public final OS os;

    
never null
 
     private final Buffer buffer;
 
     private final Settings settings;

    
never null
 
     private final Xml xml;
 
     private Node home;

    
Intentionally not a file -- see Tempfiles for a rationale
 
     private FileNode temp;
     private Node working;
 
     private final List<StringdefaultExcludes;
 
     private final Map<StringFilesystemfilesystems;
     private final FileFilesystem fileFilesystem;
     private final MemoryFilesystem memoryFilesystem;
 
     public World() {
         this(.new Settings(), new Buffer(), "**/.svn""**/.svn/**/*");
         addStandardFilesystems();
     }
 
     public World(OS osSettings settingsBuffer bufferString... defaultExcludes) {
         this. = os;
         this. = settings;
         this. = buffer;
         this. = new HashMap<StringFilesystem>();
         this. = this.addFilesystem(new FileFilesystem(this"file"));
         this. = this.addFilesystem(new MemoryFilesystem(this"mem"));
         this. = init("java.io.tmpdir");
         this. = init("user.home");
         this. = init("user.dir");
         this. = new Xml();
        this. = new ArrayList<String>(Arrays.asList(defaultExcludes));
    }
    //-- configuration
    public Node getHome() {
        return ;
    }
    public World setHome(Node home) {
        this. = home;
        return this;
    }

    
Current working directory. May be null. Use during node creation to resolve relative URIs and in Node.toString().
    public Node getWorking() {
        return ;
    }

    
current working directory
    public World setWorking(Node working) {
        this. = working;
        return this;
    }
    public FileNode getTemp() {
        return ;
    }
    public World setTemp(FileNode temp) {
        this. = temp;
        return this;
    }
    public Buffer getBuffer() {
        return ;
    }
    public Settings getSettings() {
        return ;
    }
    public Xml getXml() {
        return ;
    }
    //--  filesystems
    public World addStandardFilesystems() {
        addFilesystem(new ConsoleFilesystem(this"console"));
        addFilesystem(new ZipFilesystem(this"zip"));
        addFilesystem(new ZipFilesystem(this"jar"));
        addFilesystem(new TimeMachineFilesystem(this"tm"));
        addFilesystemOpt("net.sf.beezle.sushi.fs.ssh.SshFilesystem"this"ssh");
        addFilesystemOpt("net.sf.beezle.sushi.fs.svn.SvnFilesystem"this"svn");
        addFilesystemOpt("net.sf.beezle.sushi.fs.webdav.WebdavFilesystem"this"http""http"false);
        addFilesystemOpt("net.sf.beezle.sushi.fs.webdav.WebdavFilesystem"this"https""https"false);
        addFilesystemOpt("net.sf.beezle.sushi.fs.webdav.WebdavFilesystem"this"dav""http"true);
        addFilesystemOpt("net.sf.beezle.sushi.fs.webdav.WebdavFilesystem"this"davs""https"true);
        return this;
    }
    public <T extends Filesystem> T addFilesystem(T filesystem) {
    	String name;
    	name = filesystem.getScheme();
        if (.containsKey(name)) {
            throw new IllegalArgumentException("duplicate filesystem scheme: " + name);
        }
        .put(namefilesystem);
        return filesystem;
    }
    public Filesystem addFilesystemOpt(String filesystemClassObject ... args) {
        Class<?> clazz;
        Constructor<?> constructor;
        Filesystem filesystem;
        try {
            clazz = Class.forName(filesystemClass);
        } catch (NoClassDefFoundError e) {
            return null;
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("class not found: " + filesystemClasse);
        }
        try {
            constructor = null;
            for (Constructor<?> c : clazz.getConstructors()) {
                if (Reflect.matches(c.getParameterTypes(), args)) {
                    if (constructor != null) {
                        throw new IllegalArgumentException("constructor ambiguous");
                    }
                    constructor = c;
                }
            }
            if (constructor == null) {
                throw new IllegalArgumentException("no constructor: " + filesystemClass);
            }
            try {
                filesystem = (Filesystemconstructor.newInstance(args);
            } catch (InvocationTargetException e) {
                // for SshFilesystem for example, if fails with a NoClassDefFoundException on Mac OS
                // TODO: logging
                return null;
            }
            addFilesystem(filesystem);
            return filesystem;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalArgumentException("cannot instantiate " + filesystemClasse);
        }
    }
    public Filesystem getFilesystem(String scheme) {
        Filesystem result;
        result = .get(scheme);
        if (result == null) {
            throw new IllegalArgumentException("unknown scheme: " + scheme);
        }
        return result;
    }
    public FileFilesystem getFileFilesystem() {
        return ;
    }
        return ;
    }
    public <T extends Filesystem> T getFilesystem(String schemeClass<T> clazz) {
        Filesystem filesystem;
        filesystem = lookupFilesystem(scheme);
        if (filesystem == null) {
            throw new IllegalArgumentException("no such filesystem: " + scheme);
        }
        if (!clazz.isInstance(filesystem)) {
            throw new IllegalArgumentException("unexpected file system type: " + filesystem.getClass().getName());
        }
        return (T) filesystem;
    }
    public Filesystem lookupFilesystem(String scheme) {
        return .get(scheme);
    }
    //-- Node creation
    public FileNode file(File file) {
        return file(file.getPath());
    }
    public FileNode file(String filePath) {
        FileRoot root;
        String path;
        root = .lookupRoot(filePath);
        if (root != null) {
            filePath = filePath.substring(root.getAbsolute().length());
        }
        filePath = Strings.removeEndOpt(filePath.);
        if (root == null) {
            if ( == null) {
                throw new IllegalStateException("working directory is missing");
            } else if ( instanceof FileNode) {
                return ((FileNode).join(path);
            } else {
                throw new IllegalStateException("working directory is not a file: " + .getURI());
            }
        } else {
            return root.node(pathnull);
        }
    }
    public Node validNode(String urithrows NodeInstantiationException {
        try {
            return node(uri);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(uri);
        }
    }

    
Make sure the uri is properly encoded, in particular when passing relative uris, the contained path needs to be encoded.
    public Node node(String urithrows URISyntaxExceptionNodeInstantiationException {
        return node(new URI(uri));
    }
    public Node node(URI urithrows NodeInstantiationException {
        return node(urinull);
    }
    public Node node(URI uriObject extrathrows NodeInstantiationException {
        String scheme;
        Filesystem fs;
        Node result;
        if (!uri.isAbsolute()) {
            // because I cannot reliably create relative URIs. E.g. paths with ':' will always result a URI with scheme
            throw new IllegalStateException("cannot create node for relative URI, use getWorking().join instead: " + uri);
        }
        scheme = uri.getScheme();
        if (scheme == null) {
            throw new IllegalStateException("missing scheme: " + uri);
        }
        fs = .get(scheme);
        if (fs == null) {
            throw new NodeInstantiationException(uri"unknown scheme: " + scheme);
        }
        result = fs.node(uriextra);
        return result;
    }
    private MemoryNode memoryNode() {
        return .root().node("tmp"null);
    }
    public MemoryNode memoryNode(String content) {
        try {
            return (MemoryNodememoryNode().writeString(content);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
    public MemoryNode memoryNode(byte ... bytes) {
        try {
            return (MemoryNodememoryNode().writeBytes(bytes);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    

Parameters:
name must not start with a slash
    public Node resource(String namethrows IOException {
        List<Noderesult;
        result = resources(name);
        switch (result.size()) {
        case 0:
            throw new FileNotFoundException("no such resource: " + name);
        case 1:
            return result.get(0);
        default:
            throw new IOException("resource ambiguous: " + name + "(" + result + ")");
        }
    }

    
Throws an IllegalStateException is the classpath contains duplicate items

Parameters:
name must not start with a slash
    public List<Noderesources(String namethrows IOException {
        return resources(nametrue);
    }

    

Parameters:
name must not start with a slash
    public List<NoderesourcesUnchecked(String namethrows IOException {
        return resources(namefalse);
    }

    

Parameters:
name must not start with a slash
    private List<Noderesources(String nameboolean rejectDuplicatesthrows IOException {
        Enumeration<URLe;
        List<Noderesult;
        Node add;
        if (name.startsWith("/")) {
            throw new IllegalArgumentException();
        }
        e = getClass().getClassLoader().getResources(name);
        result = new ArrayList<Node>();
        while (e.hasMoreElements()) {
            try {
                add = node(e.nextElement().toURI());
            } catch (URISyntaxException ex) {
                throw new IllegalStateException(ex);
            }
            if (result.contains(add)) {
                if (rejectDuplicates) {
                    throw new IllegalStateException("duplicate classpath item: " + add);
                }
            } else {
                result.add(add);
            }
        }
        return result;
    }
    //--
    public Filter filter() {
        Filter filter;
        filter = new Filter();
        filter.exclude();
        return filter;
    }
    public List<StringdefaultExcludes() {
        return ;
    }
    //--
        List<FileNoderesult;
        result = new ArrayList<FileNode>();
        for (String str: Strings.split(.path)) {
            result.add(file(str));
        }
        return result;
    }
    public List<FileNodeclasspath(String paththrows URISyntaxExceptionIOException {
        List<FileNoderesult;
        result = path(path);
        for (Node node : result) {
            node.checkExists();
        }
        return result;
    }
        List<Noderesult;
        result = new ArrayList<Node>();
        for (String str: Strings.split(.path)) {
            result.add(node(str));
        }
        return result;
    }
    //-- classpath

    
Returns the file or directory containing the specified resource.
    public FileNode locateClasspathItem(String resourcename) {
        return locateClasspathItem(getClass(), resourcename);
    }

    
Returns the file or directory containing the specified class.

Parameters:
c the source class
Returns:
the physical file defining the class
    public FileNode locateClasspathItem(Class<?> c) {
        return locateClasspathItem(c, Reflect.resourceName(c));
    }

    
Throws a RuntimeException if the resource is not found
    public FileNode locateClasspathItem(Class<?> baseString resourcename) {
        URL url;
        FileNode file;
        url = base.getResource(resourcename);
        if (url == null) {
            throw new RuntimeException("no such resource: " + resourcename);
        }
        file = locateClasspathItem(urlresourcename);
        if (!file.exists()) {
            throw new RuntimeException(url + ": no such file or directory: " + file);
        }
        file = locateClasspathItem(urlresourcename);
        if (!file.exists()) {
            throw new RuntimeException(url + ": no such file or directory: " + file);
        }
        return file;
    }
    public FileNode guessProjectHome(Class<?> c) {
        FileNode node;
        node = locateClasspathItem(c);
        if (node.isDirectory()) {
            if (node.getName().endsWith("classes")) {
                node = node.getParent();
            }
        } else {
            if (node.getName().endsWith(".jar")) {
                node = node.getParent();
            }
        }
        if (node.getName().endsWith("target")) {
            node = node.getParent();
        }
        return node;
    }

    
Returns the file of a certain class at a special location. e.g. jar files

Parameters:
url the destination path to the resource
resourcename absolute resource name; redundant, but necessary to strip from urls
Returns:
the physical file referring to the class
    public FileNode locateClasspathItem(URL urlString resourcename) {
        String filename;
        FileNode file;
        String protocol;
        int idx;
        if (!resourcename.startsWith("/")) {
            throw new IllegalArgumentException("absolute resourcename expected: " + resourcename);
        }
        protocol = url.getProtocol();
        if ("file".equals(protocol)) {
            try {
                file = file(new File(url.toURI()));
            } catch (URISyntaxException e) {
                throw new IllegalStateException(e);
            }
            filename = file.getAbsolute();
            if (!filename.endsWith(resourcename.replace('/'.))) {
                throw new RuntimeException("classname not found in file url: " + filename + " " + resourcename);
            }
            file = file(filename.substring(0, filename.length() - resourcename.length()));
        } else if ("jar".equals(protocol)) {
            filename = url.getFile();
            idx = filename.indexOf("!/");
            if (idx == -1) {
                throw new RuntimeException("!/ not found: " + filename);
            }
            filename = filename.substring(0, idx);
            try {
				file = (FileNodenode(filename);
				throw new IllegalStateException(filenamee);
catch (URISyntaxException e) {
				throw new IllegalStateException(filenamee);
			}
        } else {
            throw new RuntimeException("protocol not supported: " + protocol);
        }
        return file;
    }
    //--
    private FileNode init(String name) {
        String value;
        File file;
        value = System.getProperty(name);
        if (value == null) {
            throw new IllegalStateException("property not found: " + name);
        }
        file = new File(value);
        if (!file.isDirectory()) {
            throw new IllegalStateException(
                "property " + name + " does not point to a directory: " + value);
        }
        return file(file);
    }
New to GrepCode? Check out our FAQ X