001    /*****************************************************************************
002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by Aslak Hellesoy and Paul Hammant                          *
009     *****************************************************************************/
010    package org.nanocontainer.booter;
011    
012    import java.io.File;
013    import java.io.IOException;
014    import java.lang.reflect.Constructor;
015    import java.lang.reflect.InvocationTargetException;
016    import java.net.MalformedURLException;
017    import java.net.URL;
018    import java.net.URLClassLoader;
019    import java.util.List;
020    import java.util.ArrayList;
021    
022    /**
023     * NanoContainerBooter instantiated the NanoContainer {@link org.nanocontainer.Standalone Standalone} 
024     * startup class using a tree of common and hidden classloaders.
025     * 
026     * @author Paul Hammant
027     * @author Mauro Talevi
028     * @see org.nanocontainer.Standalone
029     */
030    public class NanoContainerBooter {
031    
032        public static void main(String[] args) throws Exception {
033            new NanoContainerBooter(args);
034        }
035    
036        /**
037         * Instantiates the NanoContainer Standalone class
038         * @param args the arguments passed on to Standalone
039         * 
040         * @throws ClassNotFoundException
041         * @throws IllegalAccessException
042         * @throws InvocationTargetException
043         * @throws InstantiationException
044         * @throws IOException
045         */
046        public NanoContainerBooter(String[] args) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
047    
048            URLClassLoader commonClassLoader = new URLClassLoader(buildClassLoaderURLs("lib/common"),
049                            NanoContainerBooter.class.getClassLoader().getParent() );
050    
051            URLClassLoader hiddenClassLoader = new URLClassLoader(buildClassLoaderURLs("lib/hidden"), 
052                            commonClassLoader );
053    
054            Class nanoStandalone = hiddenClassLoader.loadClass("org.nanocontainer.Standalone");
055            Constructor ctor = nanoStandalone.getConstructors()[0];
056            System.out.println("NanoContainer-Booter: Booting...");
057            ctor.newInstance(new Object[]{args});
058            System.out.println("NanoContainer-Booter: Booted.");
059    
060        }
061    
062        private URL[] buildClassLoaderURLs(String path) throws MalformedURLException{
063            List urls = new ArrayList();
064            File[] libs = new File(path).listFiles();
065            for (int i = 0; i < libs.length; i++) {
066                File file = libs[i];
067                URL fileURL = file.toURL();
068                urls.add(fileURL);
069            }
070            return (URL[])urls.toArray(new URL[urls.size()]);
071        }
072    }