KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > booter > NanoContainerBooter


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10 package org.nanocontainer.booter;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.net.URLClassLoader JavaDoc;
19
20 /**
21  * NanoContainerBooter instantiated the NanoContainer {@link org.nanocontainer.Standalone Standalone}
22  * startup class using a tree of common and hidden classloaders.
23  *
24  * @author Paul Hammant
25  * @author Mauro Talevi
26  * @see org.nanocontainer.Standalone
27  */

28 public class NanoContainerBooter {
29
30     private static final String JavaDoc COMMON_PATH = "lib/common";
31     private static final String JavaDoc HIDDEN_PATH = "lib/hidden";
32
33     /**
34      * Static entry point to NanoContainerBooter
35      * @param args the arguments passed on to Standalone
36      * @throws Exception
37      */

38     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
39         new NanoContainerBooter(args);
40     }
41
42     /**
43      * Instantiates the NanoContainer Standalone class
44      *
45      * @param args the arguments passed on to Standalone
46      *
47      * @throws ClassNotFoundException
48      * @throws IllegalAccessException
49      * @throws InvocationTargetException
50      * @throws InstantiationException
51      * @throws IOException
52      */

53     public NanoContainerBooter(String JavaDoc[] args) throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, InstantiationException JavaDoc, IOException JavaDoc {
54
55         URLClassLoader JavaDoc commonClassLoader = new URLClassLoader JavaDoc(toURLs(COMMON_PATH),
56                         NanoContainerBooter.class.getClassLoader().getParent() );
57
58         URLClassLoader JavaDoc hiddenClassLoader = new URLClassLoader JavaDoc(toURLs(HIDDEN_PATH),
59                         commonClassLoader );
60
61         System.out.println("NanoContainer Booter: Booting...");
62         newStandalone(hiddenClassLoader, args);
63         System.out.println("NanoContainer Booter: Booted.");
64
65     }
66
67     /**
68      * Instantiates a new Standalone
69      *
70      * @param classLoader the ClassLoader used to instantiate class
71      * @param args the arguments passed to Standalone
72      *
73      * @throws ClassNotFoundException
74      * @throws InstantiationException
75      * @throws IllegalAccessException
76      * @throws InvocationTargetException
77      */

78     private void newStandalone(URLClassLoader JavaDoc classLoader, String JavaDoc[] args) throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
79         Class JavaDoc nanoStandalone = classLoader.loadClass("org.nanocontainer.Standalone");
80         Constructor JavaDoc constructor = nanoStandalone.getConstructors()[0];
81         constructor.newInstance(new Object JavaDoc[]{args});
82     }
83
84     /**
85      * Converts files path to URLs
86      * @param path the files path
87      * @return The array of URLs, one for each file in path
88      * @throws MalformedURLException
89      */

90     private URL JavaDoc[] toURLs(String JavaDoc path) throws MalformedURLException JavaDoc{
91         File JavaDoc[] files = new File JavaDoc(path).listFiles();
92         URL JavaDoc[] urls = new URL JavaDoc[files.length];
93         for (int i = 0; i < files.length; i++) {
94             urls[i]= files[i].toURL();
95         }
96         return urls;
97     }
98 }
Popular Tags