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.List;
015    import railo.runtime.type.Sizeable;
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            /**
064             * @see res.ResourceProvider#getResource(java.lang.String)
065             */
066            public Resource getResource(String path) {
067                    path=ResourceUtil.removeScheme(scheme,path);
068                    return new RamResource(this,path);
069            }
070            
071            /**
072             * returns core for this path if exists, otherwise return null
073             * @param path
074             * @return core or null
075             */
076            RamResourceCore getCore(String path) {
077                    String[] names = List.listToStringArray(path,'/');
078                    
079                    
080                    RamResourceCore rrc=root;
081                    for(int i=0;i<names.length;i++) {
082                            rrc=rrc.getChild(names[i],caseSensitive);
083                            if(rrc==null) return null;
084                    }
085                    return rrc;
086            }
087    
088            /**
089             * create a new core 
090             * @param path
091             * @param type
092             * @return created core
093             * @throws IOException
094             */
095            RamResourceCore createCore(String path, int type) throws IOException {
096                    String[] names = List.listToStringArray(path,'/');
097                    RamResourceCore rrc=root;
098                    for(int i=0;i<names.length-1;i++) {
099                            rrc=rrc.getChild(names[i],caseSensitive);
100                            if(rrc==null) throw new IOException("can't create resource "+path+", missing parent resource");
101                    }
102                    rrc = new RamResourceCore(rrc,type,names[names.length-1]);
103                    return rrc;
104            }
105    
106    
107            /**
108             * @see res.ResourceProvider#getScheme()
109             */
110            public String getScheme() {
111                    return scheme;
112            }
113            /**
114             * @see railo.commons.io.res.ResourceProvider#setResources(railo.commons.io.res.Resources)
115             */
116            public void setResources(Resources resources) {
117                    //this.resources=resources;
118            }
119    
120            /**
121             * @throws IOException 
122             * @see railo.commons.io.res.ResourceProvider#lock(railo.commons.io.res.Resource)
123             */
124            public void lock(Resource res) throws IOException {
125                    lock.lock(res);
126            }
127    
128            /**
129             * @see railo.commons.io.res.ResourceProvider#unlock(railo.commons.io.res.Resource)
130             */
131            public void unlock(Resource res) {
132                    lock.unlock(res);
133            }
134    
135            /**
136             * @see railo.commons.io.res.ResourceProvider#read(railo.commons.io.res.Resource)
137             */
138            public void read(Resource res) throws IOException {
139                    lock.read(res);
140            }
141    
142            /**
143             *
144             * @see railo.commons.io.res.ResourceProvider#isAttributesSupported()
145             */
146            public boolean isAttributesSupported() {
147                    return true;
148            }
149    
150            /**
151             *
152             * @see railo.commons.io.res.ResourceProvider#isCaseSensitive()
153             */
154            public boolean isCaseSensitive() {
155                    return caseSensitive;
156            }
157    
158            /**
159             *
160             * @see railo.commons.io.res.ResourceProvider#isModeSupported()
161             */
162            public boolean isModeSupported() {
163                    return true;
164            }
165    
166            /**
167             * @see railo.runtime.type.Sizeable#sizeOf()
168             */
169            public long sizeOf() {
170                    return SizeOf.size(root)+SizeOf.size(lock);
171            }
172    
173            /**
174             * @see railo.commons.io.res.ResourceProvider#getArguments()
175             */
176            public Map getArguments() {
177                    return arguments;
178            }
179    }