001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.vfs.spi.mount;
021    
022    import org.crsh.vfs.Path;
023    import org.crsh.vfs.spi.AbstractFSDriver;
024    import org.crsh.vfs.spi.FSDriver;
025    
026    import java.io.IOException;
027    import java.net.URL;
028    
029    /**
030     * The mount driver mounts path of a driver.
031     *
032     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
033     * @version $Revision$
034     */
035    public class MountDriver<H> extends AbstractFSDriver<H> {
036    
037      /** . */
038      private final Path path;
039    
040      /** . */
041      private final FSDriver<H> driver;
042    
043      public MountDriver(Path path, FSDriver<H> driver) {
044        if (path == null) {
045          throw new NullPointerException();
046        }
047        if (driver == null) {
048          throw new NullPointerException();
049        }
050        if (!path.isDir()) {
051          throw new IllegalArgumentException("Mount path must be a dir");
052        }
053    
054        //
055        this.path = path;
056        this.driver = driver;
057      }
058    
059      public H root() throws IOException {
060        H root = driver.root();
061        for (String name : path) {
062          root = driver.child(root, name);
063          if (root == null) {
064            break;
065          }
066        }
067        return root;
068      }
069    
070      public String name(H handle) throws IOException {
071        return driver.name(handle);
072      }
073    
074      public boolean isDir(H handle) throws IOException {
075        return driver.isDir(handle);
076      }
077    
078      public Iterable<H> children(H handle) throws IOException {
079        return driver.children(handle);
080      }
081    
082      public URL toURL(H handle) throws IOException {
083        return driver.toURL(handle);
084      }
085    }