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
020 /**
021 * NanoContainerBooter instantiated the NanoContainer {@link org.nanocontainer.Standalone Standalone}
022 * startup class using a tree of common and hidden classloaders.
023 *
024 * @author Paul Hammant
025 * @author Mauro Talevi
026 * @see org.nanocontainer.Standalone
027 */
028 public class NanoContainerBooter {
029
030 private static final String COMMON_PATH = "lib/common";
031 private static final String HIDDEN_PATH = "lib/hidden";
032
033 /**
034 * Static entry point to NanoContainerBooter
035 * @param args the arguments passed on to Standalone
036 * @throws Exception
037 */
038 public static void main(String[] args) throws Exception {
039 new NanoContainerBooter(args);
040 }
041
042 /**
043 * Instantiates the NanoContainer Standalone class
044 *
045 * @param args the arguments passed on to Standalone
046 *
047 * @throws ClassNotFoundException
048 * @throws IllegalAccessException
049 * @throws InvocationTargetException
050 * @throws InstantiationException
051 * @throws IOException
052 */
053 public NanoContainerBooter(String[] args) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
054
055 URLClassLoader commonClassLoader = new URLClassLoader(toURLs(COMMON_PATH),
056 NanoContainerBooter.class.getClassLoader().getParent() );
057
058 URLClassLoader hiddenClassLoader = new URLClassLoader(toURLs(HIDDEN_PATH),
059 commonClassLoader );
060
061 System.out.println("NanoContainer Booter: Booting...");
062 newStandalone(hiddenClassLoader, args);
063 System.out.println("NanoContainer Booter: Booted.");
064
065 }
066
067 /**
068 * Instantiates a new Standalone
069 *
070 * @param classLoader the ClassLoader used to instantiate class
071 * @param args the arguments passed to Standalone
072 *
073 * @throws ClassNotFoundException
074 * @throws InstantiationException
075 * @throws IllegalAccessException
076 * @throws InvocationTargetException
077 */
078 private void newStandalone(URLClassLoader classLoader, String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
079 Class nanoStandalone = classLoader.loadClass("org.nanocontainer.Standalone");
080 Constructor constructor = nanoStandalone.getConstructors()[0];
081 constructor.newInstance(new Object[]{args});
082 }
083
084 /**
085 * Converts files path to URLs
086 * @param path the files path
087 * @return The array of URLs, one for each file in path
088 * @throws MalformedURLException
089 */
090 private URL[] toURLs(String path) throws MalformedURLException{
091 File[] files = new File(path).listFiles();
092 URL[] urls = new URL[files.length];
093 for (int i = 0; i < files.length; i++) {
094 urls[i]= files[i].toURL();
095 }
096 return urls;
097 }
098 }