001 /*
002 * Copyright (C) 2012 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.io.InputStream;
028
029 public class MountDriver<H> extends AbstractFSDriver<H> {
030
031 /** . */
032 private final Path path;
033
034 /** . */
035 private final FSDriver<H> driver;
036
037 public MountDriver(Path path, FSDriver<H> driver) {
038 if (path == null) {
039 throw new NullPointerException();
040 }
041 if (driver == null) {
042 throw new NullPointerException();
043 }
044 if (!path.isDir()) {
045 throw new IllegalArgumentException("Mount path must be a dir");
046 }
047
048 //
049 this.path = path;
050 this.driver = driver;
051 }
052
053 public H root() throws IOException {
054 H root = driver.root();
055 for (String name : path) {
056 root = driver.child(root, name);
057 if (root == null) {
058 break;
059 }
060 }
061 return root;
062 }
063
064 public String name(H handle) throws IOException {
065 return driver.name(handle);
066 }
067
068 public boolean isDir(H handle) throws IOException {
069 return driver.isDir(handle);
070 }
071
072 public Iterable<H> children(H handle) throws IOException {
073 return driver.children(handle);
074 }
075
076 public long getLastModified(H handle) throws IOException {
077 return driver.getLastModified(handle);
078 }
079
080 public InputStream open(H handle) throws IOException {
081 return driver.open(handle);
082 }
083 }