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.vfs.spi.FSDriver; 023 import org.crsh.vfs.spi.file.FileDriver; 024 import org.crsh.vfs.spi.jarurl.JarURLDriver; 025 import org.crsh.vfs.spi.mount.MountDriver; 026 import org.crsh.vfs.spi.servlet.ServletContextDriver; 027 028 import javax.servlet.ServletContext; 029 import java.io.IOException; 030 import java.net.JarURLConnection; 031 import java.net.URISyntaxException; 032 import java.net.URL; 033 import java.util.ArrayList; 034 import java.util.Enumeration; 035 import java.util.List; 036 037 /** 038 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 039 * @version $Revision$ 040 */ 041 public class FS { 042 043 /** . */ 044 final List<Mount<?>> mounts; 045 046 public FS() { 047 this.mounts = new ArrayList<Mount<?>>(); 048 } 049 050 public File get(Path path) throws IOException { 051 return new File(this, path); 052 } 053 054 public <H> FS mount(FSDriver<H> driver, Path path) { 055 if (driver == null) { 056 throw new NullPointerException(); 057 } 058 if (path.equals(Path.get("/"))) { 059 mounts.add(Mount.wrap(driver)); 060 } else { 061 mounts.add(Mount.wrap(new MountDriver<H>(path, driver))); 062 } 063 return this; 064 } 065 066 public <H> FS mount(FSDriver<H> driver, String path) { 067 return mount(driver, Path.get(path)); 068 } 069 070 public <H> FS mount(FSDriver<H> driver) { 071 return mount(driver, "/"); 072 } 073 074 public FS mount(java.io.File root) { 075 return mount(new FileDriver(root)); 076 } 077 078 public FS mount(ClassLoader cl, Path path) throws IOException, URISyntaxException { 079 if (cl == null) { 080 throw new NullPointerException(); 081 } 082 if (path == null) { 083 throw new NullPointerException(); 084 } 085 if (!path.isDir()) { 086 throw new IllegalArgumentException("Path " + path + " must be a dir"); 087 } 088 Enumeration<URL> en = cl.getResources(path.getValue().substring(1)); 089 while (en.hasMoreElements()) { 090 URL url = en.nextElement(); 091 String protocol = url.getProtocol(); 092 if ("file".equals(protocol)) { 093 java.io.File root = new java.io.File(url.toURI()); 094 mount(root); 095 } else if ("jar".equals(protocol)) { 096 JarURLConnection conn = (JarURLConnection)url.openConnection(); 097 JarURLDriver jarDriver = new JarURLDriver(conn); 098 mount(jarDriver, path); 099 } 100 } 101 return this; 102 } 103 104 public FS mount(Class<?> clazz) throws IOException, URISyntaxException { 105 if (clazz == null) { 106 throw new NullPointerException(); 107 } 108 URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); 109 String protocol = url.getProtocol(); 110 FSDriver<?> driver; 111 if (protocol.equals("file")) { 112 driver = new FileDriver(new java.io.File(url.toURI())); 113 } else if (protocol.equals("jar")) { 114 JarURLConnection conn = (JarURLConnection)url.openConnection(); 115 driver = new JarURLDriver(conn); 116 } else { 117 throw new IllegalArgumentException("Protocol " + protocol + " not supported"); 118 } 119 return mount(driver); 120 } 121 }