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