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;
021    
022    import org.crsh.util.IO;
023    import org.crsh.vfs.spi.FSDriver;
024    
025    import java.io.IOException;
026    import java.io.InputStream;
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     */
034    class Handle<H> {
035    
036      /** . */
037      private final FSDriver<H> driver;
038    
039      /** . */
040      final Key key;
041    
042      /** . */
043      final H handle;
044    
045      Handle(FSDriver<H> driver, H handle) throws IOException {
046        String name = driver.name(handle);
047        boolean dir = driver.isDir(handle);
048    
049        //
050        this.driver = driver;
051        this.handle = handle;
052        this.key = new Key(name, dir);
053      }
054    
055      Iterable<Handle<H>> children() throws IOException {
056        List<Handle<H>> children = new ArrayList<Handle<H>>();
057        for (H h : driver.children(handle)) {
058          children.add(new Handle<H>(driver, h));
059        }
060        return children;
061      }
062    
063      Resource getResource() throws IOException {
064        InputStream in = open();
065        byte[] bytes = IO.readAsBytes(in);
066        long lastModified = getLastModified();
067        return new Resource(bytes, lastModified);
068      }
069    
070      InputStream open() throws IOException {
071        return driver.open(handle);
072      }
073    
074      long getLastModified() throws IOException {
075        return driver.getLastModified(handle);
076      }
077    }