Coverage Report - org.webslinger.vfs.TypeVFSDelegate
 
Classes in this File Line Coverage Branch Coverage Complexity
TypeVFSDelegate
67%
44/66
89%
8/9
0
TypeVFSDelegate$Resolver
N/A
N/A
0
 
 1  
 package org.webslinger.vfs;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.InputStream;
 5  
 import java.io.IOException;
 6  
 import java.io.OutputStream;
 7  
 import java.util.Collection;
 8  
 import java.util.HashMap;
 9  
 import java.util.Map;
 10  
 
 11  
 import org.apache.commons.vfs.FileObject;
 12  
 import org.apache.commons.vfs.provider.DelegateFileObject;
 13  
 
 14  
 import org.webslinger.lang.AtomicMap;
 15  
 
 16  
 public class TypeVFSDelegate extends VFSDelegate<Object, Object, Object> {
 17  1
     public static final TypeVFSDelegate DEFAULT = new TypeVFSDelegate();
 18  
 
 19  27
     protected final AtomicMap<Class<?>, VFSDelegate<?, ?, ?>> delegates = new AtomicMap<Class<?>, VFSDelegate<?, ?, ?>>();
 20  
     protected final Resolver resolver;
 21  
 
 22  
     public TypeVFSDelegate() {
 23  1
         this(null);
 24  1
     }
 25  
 
 26  27
     public TypeVFSDelegate(Resolver resolver) {
 27  27
         this.resolver = resolver;
 28  27
         addVFSDelegate(File.class, new FileVFSDelegate());
 29  27
         addVFSDelegate(String.class, new StringVFSDelegate());
 30  27
         CommonsVfsFileObjectVFSDelegate vfsVFSDelegate = new CommonsVfsFileObjectVFSDelegate();
 31  27
         addVFSDelegate(FileObject.class, vfsVFSDelegate);
 32  27
         addVFSDelegate(DelegateFileObject.class, vfsVFSDelegate);
 33  27
     }
 34  
 
 35  
     public Resolver getResolver() {
 36  0
         return resolver;
 37  
     }
 38  
 
 39  
     public <T> void addVFSDelegate(Class<T> type, VFSDelegate<?, ? super T, ?> delegate) {
 40  238
         delegates.put(type, delegate);
 41  238
     }
 42  
 
 43  
     public void removeVFSDelegate(Class<?> type) {
 44  0
         delegates.remove(type);
 45  0
     }
 46  
 
 47  
     public <T> VFSDelegate<?, T, ?> getVFSDelegate(Class<? extends T> type) throws IOException {
 48  7735
         VFSDelegate<?, T, ?> delegate = getVFSDelegateInternal(delegates.map(), type);
 49  
         //System.err.println("getVFSDelegate(" + type + ")=" + delegate);
 50  7742
         if (delegate != null) return delegate;
 51  0
         throw new IOException("no delegate for: " + type + " -> " + delegates.map().keySet());
 52  
     }
 53  
 
 54  
     private <T> VFSDelegate<?, T, ?> getVFSDelegateInternal(Map<Class<?>, VFSDelegate<?, ?, ?>> delegates, Class<? extends T> type) throws IOException {
 55  17805
         if (type == null) return null;
 56  16136
         VFSDelegate delegate = delegates.get(type);
 57  16130
         if (delegate == null) {
 58  8390
             delegate = getVFSDelegateInternal(delegates, type.getSuperclass());
 59  8390
             if (delegate == null) {
 60  3356
                 for (Class<?> intf: type.getInterfaces()) {
 61  1678
                     delegate = getVFSDelegateInternal(delegates, intf);
 62  1678
                     if (delegate != null) break;
 63  
                 }
 64  
             }
 65  
         }
 66  16134
         return delegate;
 67  
     }
 68  
 
 69  
     public Object getId(Object id) throws IOException {
 70  4419
         return doGetId(id.getClass(), id);
 71  
     }
 72  
 
 73  
     private <T> Object doGetId(Class<T> type, Object id) throws IOException {
 74  4418
         return getVFSDelegate(type).getId(type.cast(id));
 75  
     }
 76  
 
 77  
     public Type getType(Object id) throws IOException {
 78  0
         return doGetType(id.getClass(), id);
 79  
     }
 80  
 
 81  
     private <T> Type doGetType(Class<T> type, Object id) throws IOException {
 82  0
         return getVFSDelegate(type).getType(type.cast(id));
 83  
     }
 84  
 
 85  
     public long getLastModifiedTime(Object id) throws IOException {
 86  1708
         return doGetLastModifiedTime(id.getClass(), id);
 87  
     }
 88  
 
 89  
     private <T> long doGetLastModifiedTime(Class<T> type, Object id) throws IOException {
 90  1708
         return getVFSDelegate(type).getLastModifiedTime(type.cast(id));
 91  
     }
 92  
 
 93  
     public Collection<Permission> getPermissions(Object id) throws IOException {
 94  0
         return doGetPermissions(id.getClass(), id);
 95  
     }
 96  
 
 97  
     private <T> Collection<Permission> doGetPermissions(Class<T> type, Object id) throws IOException {
 98  0
         return getVFSDelegate(type).getPermissions(type.cast(id));
 99  
     }
 100  
 
 101  
     public String absolutePath(Object id) throws IOException {
 102  984
         return doAbsolutePath(id.getClass(), id);
 103  
     }
 104  
 
 105  
     private <T> String doAbsolutePath(Class<T> type, Object id) throws IOException {
 106  984
         return getVFSDelegate(type).absolutePath(type.cast(id));
 107  
     }
 108  
 
 109  
     public String getBaseName(Object id) throws IOException {
 110  0
         return doGetBaseName(id.getClass(), id);
 111  
     }
 112  
 
 113  
     private <T> String doGetBaseName(Class<T> type, Object id) throws IOException {
 114  0
         return getVFSDelegate(type).getBaseName(type.cast(id));
 115  
     }
 116  
 
 117  
     public Object getParent(Object id) throws IOException {
 118  30
         return doGetParent(id.getClass(), id);
 119  
     }
 120  
 
 121  
     private <T> Object doGetParent(Class<T> type, Object id) throws IOException {
 122  30
         return getVFSDelegate(type).getParent(type.cast(id));
 123  
     }
 124  
 
 125  
     public Object resolve(Object id, String name) throws IOException {
 126  6
         return doResolve(id.getClass(), id, name);
 127  
     }
 128  
 
 129  
     private <T> Object doResolve(Class<T> type, Object id, String name) throws IOException {
 130  6
         VFSDelegate<?, T, ?> delegate = id != null ? getVFSDelegate(type) : null;
 131  6
         if (delegate != null) return delegate.resolve(type.cast(id), name);
 132  0
         Resolver resolver = getResolver();
 133  0
         return resolver != null ? resolver.resolve(name) : null;
 134  
     }
 135  
 
 136  
     public Object[] getChildren(Object id) throws IOException {
 137  0
         return doGetChildren(id.getClass(), id);
 138  
     }
 139  
 
 140  
     private <T> Object[] doGetChildren(Class<T> type, Object id) throws IOException {
 141  0
         return getVFSDelegate(type).getChildren(type.cast(id));
 142  
     }
 143  
 
 144  
     public boolean attributeExists(Object id, String name) throws IOException {
 145  0
         return doAttributeExists(id.getClass(), id, name);
 146  
     }
 147  
 
 148  
     private <T> boolean doAttributeExists(Class<T> type, Object id, String name) throws IOException {
 149  0
         return getVFSDelegate(type).attributeExists(type.cast(id), name);
 150  
     }
 151  
 
 152  
     public Object getAttribute(Object id, String name) throws IOException {
 153  3
         return doGetAttribute(id.getClass(), id, name);
 154  
     }
 155  
 
 156  
     private <T> Object doGetAttribute(Class<T> type, Object id, String name) throws IOException {
 157  3
         return getVFSDelegate(type).getAttribute(type.cast(id), name);
 158  
     }
 159  
 
 160  
     public String getContentType(Object id) throws IOException {
 161  0
         return doGetContentType(id.getClass(), id);
 162  
     }
 163  
 
 164  
     private <T> String doGetContentType(Class<T> type, Object id) throws IOException {
 165  0
         return getVFSDelegate(type).getContentType(type.cast(id));
 166  
     }
 167  
 
 168  
     public long getLength(Object id) throws IOException {
 169  12
         return doGetLength(id.getClass(), id);
 170  
     }
 171  
 
 172  
     private <T> long doGetLength(Class<T> type, Object id) throws IOException {
 173  12
         return getVFSDelegate(type).getLength(type.cast(id));
 174  
     }
 175  
 
 176  
     public String getString(Object id) throws IOException {
 177  565
         return doGetString(id.getClass(), id);
 178  
     }
 179  
 
 180  
     private <T> String doGetString(Class<T> type, Object id) throws IOException {
 181  565
         return getVFSDelegate(type).getString(type.cast(id));
 182  
     }
 183  
 
 184  
     public InputStream openInput(Object id) throws IOException {
 185  12
         return doOpenInput(id.getClass(), id);
 186  
     }
 187  
 
 188  
     private <T> InputStream doOpenInput(Class<T> type, Object id) throws IOException {
 189  12
         return getVFSDelegate(type).openInput(type.cast(id));
 190  
     }
 191  
 
 192  
     public OutputStream openOutput(Object id, boolean append) throws IOException {
 193  0
         return doOpenOutput(id.getClass(), id, append);
 194  
     }
 195  
 
 196  
     private <T> OutputStream doOpenOutput(Class<T> type, Object id, boolean append) throws IOException {
 197  0
         return getVFSDelegate(type).openOutput(type.cast(id), append);
 198  
     }
 199  
 
 200  
     public RandomAccess openRandom(Object id, boolean read) throws IOException {
 201  0
         return doOpenRandom(id.getClass(), id, read);
 202  
     }
 203  
 
 204  
     private <T> RandomAccess doOpenRandom(Class<T> type, Object id, boolean read) throws IOException {
 205  0
         return getVFSDelegate(type).openRandom(type.cast(id), read);
 206  
     }
 207  
 
 208  
     public interface Resolver {
 209  
         Object resolve(String name) throws IOException;
 210  
     }
 211  
 }