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.ram;
021    
022    import org.crsh.vfs.Path;
023    import org.crsh.vfs.spi.AbstractFSDriver;
024    
025    import java.io.ByteArrayInputStream;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.lang.reflect.UndeclaredThrowableException;
029    import java.net.MalformedURLException;
030    import java.net.URL;
031    import java.util.ArrayList;
032    import java.util.Collections;
033    import java.util.HashMap;
034    import java.util.List;
035    
036    public class RAMDriver extends AbstractFSDriver<Path> {
037    
038      /** . */
039      private final Path root;
040    
041      /** . */
042      final HashMap<Path, String> entries;
043    
044      /** . */
045      URL baseURL;
046    
047      public RAMDriver() {
048        try {
049          this.root = Path.get("/");
050          this.entries = new HashMap<Path, String>();
051          this.baseURL = new URL("ram", null, 0, "/", new RAMURLStreamHandler(this));
052        }
053        catch (MalformedURLException e) {
054          throw new UndeclaredThrowableException(e);
055        }
056      }
057    
058      public void add(String path, String file) {
059        add(Path.get(path), file);
060      }
061    
062      public void add(Path path, String file) {
063        entries.put(path, file);
064      }
065    
066      public Path root() throws IOException {
067        return root;
068      }
069    
070      public String name(Path handle) throws IOException {
071        return handle.getName();
072      }
073    
074      public boolean isDir(Path handle) throws IOException {
075        return handle.isDir();
076      }
077    
078      public Iterable<Path> children(Path handle) throws IOException {
079        List<Path> children = Collections.emptyList();
080        for (Path entry : entries.keySet()) {
081          if (entry.isChildOf(handle)) {
082            if (children.isEmpty()) {
083              children = new ArrayList<Path>();
084            }
085            children.add(entry);
086          }
087        }
088        return children;
089      }
090    
091      public long getLastModified(Path handle) throws IOException {
092        return 0;
093      }
094    
095      public InputStream open(Path handle) throws IOException {
096        return new ByteArrayInputStream(entries.get(handle).getBytes("UTF-8"));
097      }
098    }