001    package railo.commons.io.res.type.ram;
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.op.Caster;
014    import railo.runtime.type.Sizeable;
015    import railo.runtime.type.util.ListUtil;
016    
017    /**
018     * Resource Provider for ram resource
019     */
020    public final class RamResourceProviderOld implements ResourceProvider,Sizeable {
021    
022            private String scheme="ram";
023            private RamResourceCore root;
024            
025            boolean caseSensitive=true;
026            //private Resources resources;
027            private long lockTimeout=1000;
028            private ResourceLockImpl lock=new ResourceLockImpl(lockTimeout,caseSensitive);
029            private Map arguments;
030    
031            
032            /**
033             * initalize ram resource
034             * @param scheme
035             * @param arguments
036             * @return RamResource
037             */
038            public ResourceProvider init(String scheme,Map arguments) {
039                    if(!StringUtil.isEmpty(scheme))this.scheme=scheme;
040                    
041                    if(arguments!=null) {
042                            this.arguments=arguments;
043                            Object oCaseSensitive= arguments.get("case-sensitive");
044                            if(oCaseSensitive!=null) {
045                                    caseSensitive=Caster.toBooleanValue(oCaseSensitive,true);
046                            }
047                            
048                            // lock-timeout
049                            Object oTimeout = arguments.get("lock-timeout");
050                            if(oTimeout!=null) {
051                                    lockTimeout=Caster.toLongValue(oTimeout,lockTimeout);
052                            }
053                    }
054                    lock.setLockTimeout(lockTimeout);
055                    lock.setCaseSensitive(caseSensitive);
056                    
057                    root=new RamResourceCore(null,RamResourceCore.TYPE_DIRECTORY,"");
058                    return this;
059            }
060            
061            
062            
063            @Override
064            public Resource getResource(String path) {
065                    path=ResourceUtil.removeScheme(scheme,path);
066                    return new RamResource(this,path);
067            }
068            
069            /**
070             * returns core for this path if exists, otherwise return null
071             * @param path
072             * @return core or null
073             */
074            RamResourceCore getCore(String path) {
075                    String[] names = ListUtil.listToStringArray(path,'/');
076                    
077                    
078                    RamResourceCore rrc=root;
079                    for(int i=0;i<names.length;i++) {
080                            rrc=rrc.getChild(names[i],caseSensitive);
081                            if(rrc==null) return null;
082                    }
083                    return rrc;
084            }
085    
086            /**
087             * create a new core 
088             * @param path
089             * @param type
090             * @return created core
091             * @throws IOException
092             */
093            RamResourceCore createCore(String path, int type) throws IOException {
094                    String[] names = ListUtil.listToStringArray(path,'/');
095                    RamResourceCore rrc=root;
096                    for(int i=0;i<names.length-1;i++) {
097                            rrc=rrc.getChild(names[i],caseSensitive);
098                            if(rrc==null) throw new IOException("can't create resource "+path+", missing parent resource");
099                    }
100                    rrc = new RamResourceCore(rrc,type,names[names.length-1]);
101                    return rrc;
102            }
103    
104    
105            @Override
106            public String getScheme() {
107                    return scheme;
108            }
109            @Override
110            public void setResources(Resources resources) {
111                    //this.resources=resources;
112            }
113    
114            @Override
115            public void lock(Resource res) throws IOException {
116                    lock.lock(res);
117            }
118    
119            @Override
120            public void unlock(Resource res) {
121                    lock.unlock(res);
122            }
123    
124            @Override
125            public void read(Resource res) throws IOException {
126                    lock.read(res);
127            }
128    
129            @Override
130            public boolean isAttributesSupported() {
131                    return true;
132            }
133    
134            @Override
135            public boolean isCaseSensitive() {
136                    return caseSensitive;
137            }
138    
139            @Override
140            public boolean isModeSupported() {
141                    return true;
142            }
143    
144            @Override
145            public long sizeOf() {
146                    return SizeOf.size(root)+SizeOf.size(lock);
147            }
148    
149            @Override
150            public Map getArguments() {
151                    return arguments;
152            }
153    }