001 package railo.commons.io.res.type.compress; 002 003 import java.io.IOException; 004 import java.util.Map; 005 006 import railo.commons.io.res.Resource; 007 import railo.commons.io.res.ResourceProvider; 008 import railo.commons.io.res.Resources; 009 import railo.commons.io.res.util.ResourceLockImpl; 010 import railo.commons.io.res.util.ResourceUtil; 011 import railo.commons.lang.SizeOf; 012 import railo.commons.lang.StringUtil; 013 import railo.runtime.PageContext; 014 import railo.runtime.engine.ThreadLocalPageContext; 015 import railo.runtime.op.Caster; 016 import railo.runtime.type.Sizeable; 017 018 public abstract class CompressResourceProvider implements ResourceProvider,Sizeable { 019 020 private static final long serialVersionUID = 5930090603192203086L; 021 022 private Resources resources; 023 protected String scheme=null; 024 protected boolean caseSensitive=true; 025 boolean async=true; 026 private long lockTimeout=10000; 027 private final ResourceLockImpl lock=new ResourceLockImpl(lockTimeout,caseSensitive); 028 private Map arguments; 029 030 /** 031 * @see railo.commons.io.res.ResourceProvider#init(java.lang.String, java.util.Map) 032 */ 033 public ResourceProvider init(String scheme, Map arguments) {// FUTURE Map<String,String> arguments 034 if(!StringUtil.isEmpty(scheme))this.scheme=scheme; 035 if(arguments!=null) { 036 this.arguments=arguments; 037 // case-sensitive 038 String strCaseSensitive=(String) arguments.get("case-sensitive"); 039 if(strCaseSensitive!=null) { 040 caseSensitive=Caster.toBooleanValue(strCaseSensitive,true); 041 } 042 043 // sync 044 String strASync=(String) arguments.get("asynchronus"); 045 if(strASync==null)strASync=(String) arguments.get("async"); 046 if(strASync!=null) { 047 async=Caster.toBooleanValue(strASync,true); 048 } 049 050 // lock-timeout 051 String strTimeout = (String) arguments.get("lock-timeout"); 052 if(strTimeout!=null) { 053 lockTimeout=Caster.toLongValue(arguments.get("lock-timeout"),lockTimeout); 054 } 055 } 056 lock.setLockTimeout(lockTimeout); 057 lock.setCaseSensitive(caseSensitive); 058 059 return this; 060 } 061 062 public ResourceProvider init(String scheme, boolean caseSensitive, boolean async) { 063 if(!StringUtil.isEmpty(scheme))this.scheme=scheme; 064 this.caseSensitive=caseSensitive; 065 this.async=async; 066 return this; 067 } 068 069 /** 070 * @see railo.commons.io.res.ResourceProvider#getResource(java.lang.String) 071 */ 072 public Resource getResource(String path) { 073 path=ResourceUtil.removeScheme(scheme,path); 074 int index=path.lastIndexOf('!'); 075 if(index!=-1) { 076 077 Resource file = toResource(path.substring(0,index));//resources.getResource(path.substring(0,index)); 078 return new CompressResource(this,getCompress(file),path.substring(index+1),caseSensitive); 079 } 080 Resource file = toResource(path);//resources.getResource(path); 081 return new CompressResource(this,getCompress(file),"/",caseSensitive); 082 } 083 084 private Resource toResource(String path) { 085 PageContext pc = ThreadLocalPageContext.get(); 086 if(pc!=null) { 087 return ResourceUtil.toResourceNotExisting(ThreadLocalPageContext.get(), path,true); 088 } 089 return resources.getResource(path); 090 } 091 092 public abstract Compress getCompress(Resource file); 093 094 /** 095 * @see railo.commons.io.res.ResourceProvider#getScheme() 096 */ 097 public String getScheme() { 098 return scheme; 099 } 100 101 102 103 public void setResources(Resources resources) { 104 this.resources=resources; 105 } 106 107 /** 108 * @throws IOException 109 * @see railo.commons.io.res.ResourceProvider#lock(railo.commons.io.res.Resource) 110 */ 111 public void lock(Resource res) throws IOException { 112 lock.lock(res); 113 } 114 115 /** 116 * @see railo.commons.io.res.ResourceProvider#unlock(railo.commons.io.res.Resource) 117 */ 118 public void unlock(Resource res) { 119 lock.unlock(res); 120 } 121 122 /** 123 * @see railo.commons.io.res.ResourceProvider#read(railo.commons.io.res.Resource) 124 */ 125 public void read(Resource res) throws IOException { 126 lock.read(res); 127 } 128 129 /** 130 * @see railo.commons.io.res.ResourceProvider#getArguments() 131 */ 132 public Map getArguments() { 133 return arguments; 134 } 135 136 /*public static void main(String[] args) throws IOException { 137 Resources rs=ResourcesImpl.getGlobal(); 138 rs.registerResourceProvider(new ZipResourceProvider().init("zip", null)); 139 rs.registerResourceProvider(new RamResourceProvider().init("ram", null)); 140 141 142 Resource ra = rs.getResource("zip:///Users/mic/temp/test/ras111.zip!/dd/"); 143 print.ln(ra); 144 print.ln(ra.getParent()); 145 146 ra = rs.getResource("ram:///dd/"); 147 print.ln(ra); 148 print.ln(ra.getParent()); 149 150 151 Resource org = rs.getResource("/Users/mic/temp/test/org.zip"); 152 Resource trg = rs.getResource("/Users/mic/temp/test/trg.zip"); 153 Resource ras = rs.getResource("/Users/mic/temp/test/ras.zip"); 154 155 ResourceUtil.copy(org, ras); 156 157 158 159 Resource res1 = rs.getResource("zip:///Users/mic/temp/test/rasx.zip!/dd"); 160 Resource res2 = rs.getResource("zip:///Users/mic/temp/test/ras.zip!/ddd"+Math.random()+".txt"); 161 162 res1.mkdirs(); 163 res2.createNewFile(); 164 ResourceUtil.copy(ras, trg); 165 print.ln("copy"); 166 167 //Resource org2 = rs.getResource("/Users/mic/temp/test/org.zip"); 168 Resource railotmp = rs.getResource("/Users/mic/temp/railotmp/"); 169 Resource trg2 = rs.getResource("zip:///Users/mic/temp/railotmp.zip!"); 170 trg2.delete(); 171 long start=System.currentTimeMillis(); 172 ResourceUtil.copyRecursive(railotmp, trg2); 173 174 print.ln("ende "+(System.currentTimeMillis()-start)); 175 176 //print(res3); 177 178 179 } 180 181 private static void print(Resource r) { 182 183 print.ln("****************************************"); 184 print.ln(r); 185 if(r==null) return; 186 print.ln("path:"+r.getPath()); 187 print.ln("name:"+r.getName()); 188 print.ln("parent:"+r.getParent()); 189 print.ln("parent-res:"+r.getParentResource()); 190 print.ln("exists:"+r.exists()); 191 print.ln("isDirectory:"+r.isDirectory()); 192 print.ln("isFile:"+r.isFile()); 193 print.ln("lastModified:"+r.lastModified()); 194 if(r.isFile()) { 195 //print.ln("->"+IOUtil.toString(r.getI nputStream(),null)+"<-"); 196 } 197 if(r.isDirectory()) { 198 print.ln(" - children"); 199 String[] children = r.list(); 200 Resource[] ch2 = r.listResources(); 201 for(int i=0;i<children.length;i++) { 202 print.ln(" - "+children[i]); 203 print.ln(" - "+ch2[i]); 204 } 205 } 206 }*/ 207 208 209 /** 210 * @see railo.runtime.type.Sizeable#sizeOf() 211 */ 212 public long sizeOf() { 213 return SizeOf.size(lock); 214 } 215 }