001    package railo.commons.io.res.util;
002    
003    import java.io.Closeable;
004    import java.io.IOException;
005    import java.net.URL;
006    import java.net.URLClassLoader;
007    import java.util.ArrayList;
008    import java.util.List;
009    
010    import railo.commons.io.res.Resource;
011    import railo.commons.io.res.type.file.FileResource;
012    import railo.runtime.exp.PageException;
013    
014    /**
015     * Classloader that load classes from resources
016     */
017    public final class ResourceClassLoader extends URLClassLoader implements Closeable {
018    
019            private List<Resource> resources=new ArrayList<Resource>();
020    
021            /* *
022             * Constructor of the class
023             * @param resources
024             * @throws PageException
025             
026            ResourceClassLoader(Resource[] resources) throws IOException {
027                    super(doURLs(resources));
028            }*/
029            
030            /**
031             * Constructor of the class
032             * @param reses
033             * @param parent
034             * @throws PageException
035             */
036            public ResourceClassLoader(Resource[] resources, ClassLoader parent) throws IOException {
037                    super(doURLs(resources), parent);
038                    for(int i=0;i<resources.length;i++){
039                            this.resources.add(resources[i]);
040                    }
041            }
042            
043            public ResourceClassLoader(ClassLoader parent) {
044                    super(new URL[0], parent);
045            }
046    
047            /**
048             * @return the resources
049             */
050            public Resource[] getResources() {
051                    return resources.toArray(new Resource[resources.size()]);
052            }
053    
054            /**
055             * translate resources to url Objects
056             * @param reses
057             * @return
058             * @throws PageException
059             */
060            public static URL[] doURLs(Resource[] reses) throws IOException {
061                    List<URL> list=new ArrayList<URL>();
062                    for(int i=0;i<reses.length;i++) {
063                            if(reses[i].isDirectory() || "jar".equalsIgnoreCase(ResourceUtil.getExtension(reses[i],null)))
064                                    list.add(doURL(reses[i]));
065                    }
066                    return list.toArray(new URL[list.size()]);
067            
068            }
069            private static URL doURL(Resource res) throws IOException {
070                            if(!(res instanceof FileResource))
071                                    throw new IOException("resource ["+res.getPath()+"] must be a local file");
072                            return ((FileResource)res).toURL();
073            }
074            
075            /**
076             * @see java.io.Closeable#close()
077             */
078            public void close(){}
079    
080            public synchronized void addResources(Resource[] reses) throws IOException {
081                    
082                    for(int i=0;i<reses.length;i++){
083                            if(!this.resources.contains(reses[i])){
084                                    this.resources.add(reses[i]);
085                                    addURL(doURL(reses[i]));
086                            }
087                    }
088            }
089    
090    }