001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.res.type.ram; 020 021import java.io.IOException; 022import java.util.Map; 023 024import lucee.commons.io.res.Resource; 025import lucee.commons.io.res.ResourceProvider; 026import lucee.commons.io.res.ResourceProviderPro; 027import lucee.commons.io.res.Resources; 028import lucee.commons.io.res.util.ResourceLockImpl; 029import lucee.commons.io.res.util.ResourceUtil; 030import lucee.commons.lang.SizeOf; 031import lucee.commons.lang.StringUtil; 032import lucee.runtime.op.Caster; 033import lucee.runtime.type.Sizeable; 034import lucee.runtime.type.util.ListUtil; 035 036/** 037 * Resource Provider for ram resource 038 */ 039public final class RamResourceProviderOld implements ResourceProviderPro,Sizeable { 040 041 private String scheme="ram"; 042 private RamResourceCore root; 043 044 boolean caseSensitive=true; 045 //private Resources resources; 046 private long lockTimeout=1000; 047 private ResourceLockImpl lock=new ResourceLockImpl(lockTimeout,caseSensitive); 048 private Map arguments; 049 050 051 /** 052 * initalize ram resource 053 * @param scheme 054 * @param arguments 055 * @return RamResource 056 */ 057 public ResourceProvider init(String scheme,Map arguments) { 058 if(!StringUtil.isEmpty(scheme))this.scheme=scheme; 059 060 if(arguments!=null) { 061 this.arguments=arguments; 062 Object oCaseSensitive= arguments.get("case-sensitive"); 063 if(oCaseSensitive!=null) { 064 caseSensitive=Caster.toBooleanValue(oCaseSensitive,true); 065 } 066 067 // lock-timeout 068 Object oTimeout = arguments.get("lock-timeout"); 069 if(oTimeout!=null) { 070 lockTimeout=Caster.toLongValue(oTimeout,lockTimeout); 071 } 072 } 073 lock.setLockTimeout(lockTimeout); 074 lock.setCaseSensitive(caseSensitive); 075 076 root=new RamResourceCore(null,RamResourceCore.TYPE_DIRECTORY,""); 077 return this; 078 } 079 080 081 082 @Override 083 public Resource getResource(String path) { 084 path=ResourceUtil.removeScheme(scheme,path); 085 return new RamResource(this,path); 086 } 087 088 /** 089 * returns core for this path if exists, otherwise return null 090 * @param path 091 * @return core or null 092 */ 093 RamResourceCore getCore(String path) { 094 String[] names = ListUtil.listToStringArray(path,'/'); 095 096 097 RamResourceCore rrc=root; 098 for(int i=0;i<names.length;i++) { 099 rrc=rrc.getChild(names[i],caseSensitive); 100 if(rrc==null) return null; 101 } 102 return rrc; 103 } 104 105 /** 106 * create a new core 107 * @param path 108 * @param type 109 * @return created core 110 * @throws IOException 111 */ 112 RamResourceCore createCore(String path, int type) throws IOException { 113 String[] names = ListUtil.listToStringArray(path,'/'); 114 RamResourceCore rrc=root; 115 for(int i=0;i<names.length-1;i++) { 116 rrc=rrc.getChild(names[i],caseSensitive); 117 if(rrc==null) throw new IOException("can't create resource "+path+", missing parent resource"); 118 } 119 rrc = new RamResourceCore(rrc,type,names[names.length-1]); 120 return rrc; 121 } 122 123 124 @Override 125 public String getScheme() { 126 return scheme; 127 } 128 @Override 129 public void setResources(Resources resources) { 130 //this.resources=resources; 131 } 132 133 @Override 134 public void lock(Resource res) throws IOException { 135 lock.lock(res); 136 } 137 138 @Override 139 public void unlock(Resource res) { 140 lock.unlock(res); 141 } 142 143 @Override 144 public void read(Resource res) throws IOException { 145 lock.read(res); 146 } 147 148 @Override 149 public boolean isAttributesSupported() { 150 return true; 151 } 152 153 @Override 154 public boolean isCaseSensitive() { 155 return caseSensitive; 156 } 157 158 @Override 159 public boolean isModeSupported() { 160 return true; 161 } 162 163 @Override 164 public long sizeOf() { 165 return SizeOf.size(root)+SizeOf.size(lock); 166 } 167 168 @Override 169 public Map getArguments() { 170 return arguments; 171 } 172 173 @Override 174 public char getSeparator() { 175 return '/'; 176 } 177}