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;
020
021import java.io.IOException;
022
023import lucee.commons.io.res.type.file.FileResourceProvider;
024import lucee.commons.io.res.type.ram.RamResourceProviderOld;
025import lucee.commons.io.res.util.ResourceLockImpl;
026
027
028public final class ResourcesImpl implements Resources {
029
030        private static ResourceProvider frp=new FileResourceProvider();
031        private static Resources global=new ResourcesImpl();
032        private ResourceProvider defaultResource=frp;
033        private ResourceProvider[] resources=new ResourceProvider[0];
034        
035        /**
036         * adds a default factory, this factory is used, when shemecan't be mapped to a other factory
037         * @param provider
038         */
039        public void registerDefaultResourceProvider(ResourceProvider provider) {
040                provider.setResources(this);
041                this.defaultResource=provider;
042        }
043
044        /**
045         * adds a additional resource to System
046         * @param provider
047         */
048        public void registerResourceProvider(ResourceProvider provider) {
049                
050                provider.setResources(this);
051                String scheme = provider.getScheme();
052                if(scheme==null) return;
053                
054                ResourceProvider[] tmp=new ResourceProvider[resources.length+1];
055                for(int i=0;i<resources.length;i++) {
056                        if(scheme.equalsIgnoreCase(resources[i].getScheme())) {
057                                resources[i]=provider;
058                                return;
059                        }
060                        tmp[i]=resources[i];
061                }
062                tmp[resources.length]=provider;
063                resources=tmp;
064        }
065        
066        /**
067         * returns a resource that matching the given path
068         * @param path
069         * @return matching resource
070         */
071        public Resource getResource(String path) {
072                int index=path.indexOf("://");
073                if(index!=-1) {
074                        String scheme=path.substring(0,index).toLowerCase().trim();
075                        String subPath = path.substring(index+3);
076                        for(int i=0;i<resources.length;i++) {
077                                if(scheme.equalsIgnoreCase(resources[i].getScheme()))
078                                        return resources[i].getResource(subPath);
079                        }
080                }
081                return defaultResource.getResource(path);
082                
083        }
084
085        public static Resources getGlobal() {
086                return global;
087        }
088
089        public static void main(String[] args) throws IOException {
090                Resources rs=ResourcesImpl.getGlobal();
091                rs.registerResourceProvider(new RamResourceProviderOld());
092                
093                Resource changes = rs.getResource("d:/changes.txt");
094                changes = rs.getResource("file://d:/changes.txt");
095                System.out.println(changes.getCanonicalPath());
096                
097                Resource mem = rs.getResource("ram://changes.txt");
098                ResourceProvider mf=mem.getResourceProvider();
099                System.out.println(mem.getPath());
100                System.out.println(mem);
101                
102                mem = mf.getResource("changes.txt");
103                System.out.println(mem.getPath());
104                System.out.println(mem);
105                
106        }
107
108        /**
109         * @return the defaultResource
110         */
111        public ResourceProvider getDefaultResourceProvider() {
112                return defaultResource;
113        }
114
115        public ResourceProvider[] getResourceProviders() {
116                ResourceProvider[] tmp = new ResourceProvider[resources.length];
117                for(int i=0;i<tmp.length;i++) {
118                        tmp[i]=resources[i];
119                }
120                return tmp;
121        }
122
123        public static ResourceProvider getFileResourceProvider() {
124                return frp;
125        }
126
127        @Override
128        public ResourceLock createResourceLock(long timeout,boolean caseSensitive) {
129                return new ResourceLockImpl(timeout,caseSensitive);
130        }
131
132        public void reset() {
133                resources=new ResourceProvider[0];
134        }
135}