KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > Main


1 /*
2  * CoadunationBase: The base for a Coadunation instance.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Main.java
20  *
21  * The main class responsible for starting up the coadunation base.
22  */

23
24 // package path
25
package com.rift.coad;
26
27 // java imports
28
import java.util.StringTokenizer JavaDoc;
29 import java.io.File JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.net.URLClassLoader JavaDoc;
34
35 /**
36  * The main class responsible for starting up the coadunation base.
37  *
38  * @author Brett Chaldecott
39  */

40 public class Main {
41     
42     /**
43      * The main method responsible for starting the coadunation base.
44      *
45      * @param args the command line arguments
46      */

47     public static void main(String JavaDoc[] args) {
48         try {
49             String JavaDoc libdirs = System.getenv("COAD_LIB_DIRS");
50             StringTokenizer JavaDoc stringTok = null;
51             if (libdirs.contains(";")) {
52                 stringTok = new StringTokenizer JavaDoc(libdirs,";");
53                 System.out.println("Contains ;");
54             } else {
55                 stringTok = new StringTokenizer JavaDoc(libdirs,":");
56                 System.out.println("Contains :");
57             }
58             System.out.println("Load from path : " +
59                     System.getenv("COAD_LIB_DIRS"));
60             Vector JavaDoc urls = new Vector JavaDoc();
61             while(stringTok.hasMoreTokens()) {
62                 String JavaDoc dirPath = stringTok.nextToken();
63                 File JavaDoc dir = new File JavaDoc(dirPath);
64                 if (dir.isFile() == true) {
65                     System.out.println(
66                         "Add File [" + dir.toString() + "]");
67                     urls.add(dir.toURL());
68                     continue;
69                 }
70                 if (dir.isDirectory() == false) {
71                     System.out.println("The path [" + dirPath
72                             + "] does not point at a valid directory.");
73                     System.exit(-1);
74                 }
75                 File JavaDoc[] files = dir.listFiles();
76                 for (int index = 0; index < files.length; index++) {
77                     String JavaDoc filePath = files[index].getAbsolutePath();
78                     if (filePath.endsWith(".jar") || filePath.endsWith(".zip")) {
79                         System.out.println(
80                             "Add File [" + filePath + "]");
81                         urls.add(files[index].toURL());
82                     }
83                 }
84             }
85             URL JavaDoc[] urlArray = new URL JavaDoc[urls.size()];
86             urls.toArray(urlArray);
87             BaseClassLoader baseClassLoader = new BaseClassLoader(urlArray,
88                     Main.class.getClassLoader());
89             Class JavaDoc classRef = baseClassLoader.loadClass("com.rift.coad.Runner");
90             if (classRef == null) {
91                 System.out.println(
92                         "Failed to run the Coadunation base Runner reference " +
93                         "is null.");
94                 throw new Exception JavaDoc("Failed to retrieve the appropriate class " +
95                         "reference to start coadunation.");
96             }
97             System.out.println("Start Coadunation");
98             Method JavaDoc method = classRef.getMethod("main");
99             System.out.println("Invoking Coadunation");
100             //System.setProperty("java.rmi.server.RMIClassLoaderSpi",
101
// "com.rift.coad.RemoteClassLoaderSpi");
102
Thread.currentThread().setContextClassLoader(baseClassLoader);
103             method.invoke(null);
104             
105         } catch (Exception JavaDoc ex) {
106             System.out.println("Failed to run the Coadunation base [" +
107                     ex.getMessage() + "]");
108             ex.printStackTrace(System.out);
109             System.exit(-1);
110         }
111     }
112     
113 }
114
Popular Tags