KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > ClassUtil


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util;
16
17 import java.util.*;
18 import java.io.*;
19 import java.net.*;
20 import org.quickserver.util.io.*;
21 import java.util.logging.*;
22
23 /**
24  * A utility class to load class.
25  * @author Akshathkumar Shetty
26  * @since 1.3.2
27  */

28 public class ClassUtil {
29     private static Logger logger = Logger.getLogger(ClassUtil.class.getName());
30
31     /**
32      * Tries to load the classes present in the array
33      * passed has second parameter from
34      * the ClassLoader passed has first parameter.
35      * Returns the HashMap of all the classed successfully loaded.
36      * @param classLoader ClassLoader used to find the class
37      * @param classes[] array of classes to load.
38      */

39     public static Map loadClass(ClassLoader JavaDoc classLoader, String JavaDoc classNames[])
40             throws Exception JavaDoc {
41         Class JavaDoc classloded = null;
42         HashMap classHash = new HashMap();
43         for(int i=0;i<classNames.length;i++) {
44             try {
45                 classloded = classLoader.loadClass(classNames[i]);
46                 classHash.put(classNames[i], classloded);
47             } catch(Exception JavaDoc e) {
48                 logger.warning("Could not load classes : "+e);
49             }
50         }
51         return classHash;
52     }
53
54     /**
55      * Returns the ClassLoader to all the jars present in the
56      * dir passed has first parameter.
57      * @param jarDir path to the directory containing the jars
58      */

59     public static ClassLoader JavaDoc getClassLoaderFromJars(String JavaDoc jarDir)
60             throws Exception JavaDoc {
61         logger.fine("Getting ClassLoader for jars in "+jarDir);
62         File file = new File(jarDir);
63         ArrayList list = new ArrayList();
64         
65         File jars[] =file.listFiles(new JarFileList());
66         for(int j=0; j<jars.length;j++){
67             list.add(jars[j].toURL());
68         }
69
70         Object JavaDoc array[] = list.toArray();
71         URL jarurl[] = new URL[array.length];
72         for(int i=0;i<array.length;i++) {
73             jarurl[i] = (URL)array[i];
74         }
75
76         URLClassLoader classLoader = URLClassLoader.newInstance(jarurl);
77         return classLoader;
78     }
79
80     /**
81      * Returns the ClassLoader to a jar
82      * @since 1.3.3
83      */

84     public static ClassLoader JavaDoc getClassLoaderFromJar(String JavaDoc jarPath)
85             throws Exception JavaDoc {
86         File file = new File(jarPath);
87         logger.fine("Getting ClassLoader for "+file.getCanonicalPath());
88         URL jarurl[] = {file.toURL()};
89         URLClassLoader classLoader = URLClassLoader.newInstance(jarurl);
90         return classLoader;
91     }
92
93     /**
94      * Returns the ClassLoader
95      * @since 1.3.3
96      */

97     public static ClassLoader JavaDoc getClassLoader(String JavaDoc path) throws Exception JavaDoc {
98         File file = new File(path);
99         if(file.canRead()==false) {
100             logger.warning("Could not read path: "+path);
101             return null;
102         }
103         if(file.isDirectory())
104             return getClassLoaderFromJars(path);
105         else
106             return getClassLoaderFromJar(path);
107     }
108 }
109
Popular Tags