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
020package org.crsh.vfs;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.Iterator;
026import java.util.LinkedHashMap;
027import java.util.LinkedList;
028import java.util.List;
029
030public final class File {
031
032  /** . */
033  private final FS fs;
034
035  /** . */
036  private final Path path;
037
038  /** . */
039  private LinkedList<Handle<?>> handles;
040
041  /** . */
042  private LinkedHashMap<Key, File> children;
043
044  public File(FS fs, Path path) {
045    this.fs = fs;
046    this.path = path;
047    this.handles = null;
048  }
049
050  public Path getPath() {
051    return path;
052  }
053
054  public boolean isDir() {
055    return path.isDir();
056  }
057
058  public String getName() {
059    return path.getName();
060  }
061
062  public Resource getResource() throws IOException {
063    if (path.isDir()) {
064      throw new IllegalStateException("Cannot get url of a dir");
065    }
066    Handle handle = getHandles().peekFirst();
067    return handle != null ? handle.getResource() : null;
068
069  }
070
071  public Iterable<Resource> getResources() throws IOException {
072    if (path.isDir()) {
073      throw new IllegalStateException("Cannot get url of a dir");
074    }
075    List<Resource> urls = Collections.emptyList();
076    for (Handle<?> handle : getHandles()) {
077      if (urls.isEmpty()) {
078        urls = new ArrayList<Resource>();
079      }
080      Iterator<Resource> resources = handle.getResources();
081      while (resources.hasNext()) {
082        Resource resource = resources.next();
083        urls.add(resource);
084      }
085    }
086    return urls;
087  }
088
089  public File child(String name, boolean dir) throws IOException {
090    if (children == null) {
091      children();
092    }
093    return children.get(new Key(name, dir));
094  }
095
096  public Iterable<File> children() throws IOException {
097    if (children == null) {
098      LinkedHashMap<Key, File> children = new LinkedHashMap<Key, File>();
099      for (Handle<?> handle : getHandles()) {
100        for (Handle<?> childHandle : handle.children()) {
101          File child = children.get(childHandle.key);
102          if (child == null) {
103            child = new File(fs, Path.get(path, childHandle.key.name, childHandle.key.dir));
104            children.put(childHandle.key, child);
105          }
106          if (child.handles == null) {
107            child.handles = new LinkedList<Handle<?>>();
108          }
109          child.handles.add(childHandle);
110        }
111      }
112      this.children = children;
113    }
114    return children.values();
115  }
116
117  LinkedList<Handle<?>> getHandles() {
118    if (handles == null) {
119      LinkedList<Handle<?>> handles = new LinkedList<Handle<?>>();
120      for (Mount<?> mount : fs.mounts) {
121        Handle<?> handle = null;
122        try {
123          handle = mount.getHandle(path);
124        }
125        catch (IOException e) {
126          e.printStackTrace();
127        }
128        if (handle != null) {
129          handles.add(handle);
130        }
131      }
132      this.handles = handles;
133    }
134    return handles;
135  }
136
137  @Override
138  public String toString() {
139    return "File[path=" + path.getValue() + "]";
140  }
141}