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.file;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.Map;
024
025import lucee.commons.io.SystemUtil;
026import lucee.commons.io.res.Resource;
027import lucee.commons.io.res.ResourceProvider;
028import lucee.commons.io.res.ResourceProviderPro;
029import lucee.commons.io.res.Resources;
030import lucee.commons.io.res.util.ResourceLockImpl;
031import lucee.commons.io.res.util.ResourceUtil;
032import lucee.commons.lang.SizeOf;
033import lucee.commons.lang.StringUtil;
034import lucee.runtime.op.Caster;
035import lucee.runtime.type.Sizeable;
036
037public final class FileResourceProvider implements ResourceProviderPro,Sizeable {
038
039        private String scheme="file";
040        
041        private long lockTimeout=10000;
042        private boolean caseSensitive=SystemUtil.isFSCaseSensitive();
043        private final ResourceLockImpl lock=new ResourceLockImpl(lockTimeout,caseSensitive);
044        private Map arguments;
045
046        @Override
047        public ResourceProvider init(String scheme, Map arguments) {
048                if(!StringUtil.isEmpty(scheme))this.scheme=scheme;
049                this.arguments=arguments;
050                if(arguments!=null) {
051                        // lock-timeout
052                        String strTimeout = (String) arguments.get("lock-timeout");
053                        if(strTimeout!=null) {
054                                lockTimeout=Caster.toLongValue(arguments.get("lock-timeout"),lockTimeout);
055                        }
056                }
057                lock.setLockTimeout(lockTimeout);
058                
059                return this;
060        }
061        /**
062         * Constructor of the class
063         */
064        public FileResourceProvider() {}
065        
066        @Override
067        public Resource getResource(String path) {
068                return new FileResource(this,ResourceUtil.removeScheme("file",path));
069        }
070
071        @Override
072        public String getScheme() {
073                return scheme;
074        }
075        
076        @Override
077        public void setResources(Resources resources) {
078                //this.resources=resources;
079        }
080        
081
082        @Override
083        public void lock(Resource res) throws IOException {
084                lock.lock(res);
085        }
086
087        @Override
088        public void unlock(Resource res) {
089                lock.unlock(res);
090        }
091
092        @Override
093        public void read(Resource res) throws IOException {
094                lock.read(res);
095        }
096        
097        @Override
098        public boolean isAttributesSupported() {
099                return SystemUtil.isWindows();
100        }
101        
102        @Override
103        public boolean isCaseSensitive() {
104                return caseSensitive;
105        }
106        
107        @Override
108        public boolean isModeSupported() {
109                return false;//SystemUtil.isUnix(); FUTURE add again
110        }
111        @Override
112        public long sizeOf() {
113                return SizeOf.size(lock);
114        }
115        
116        public Map getArguments() {
117                return arguments;
118        }
119        
120        @Override
121        public char getSeparator() {
122                return File.separatorChar;
123        }
124}