KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > util > ClassLoaderUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
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  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ClassLoaderUtil.java 3:03:53 PM alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.util;
23
24 import java.io.File JavaDoc;
25 import java.io.FileFilter JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 /**
32  * This class is used to create classloader for JBI components
33  *
34  * @version $Revision: $ $Date: $
35  * @since Petals 1.1
36  * @author alouis
37  */

38 public class ClassLoaderUtil {
39
40     /**
41      * Return an URLClassLoader object, with all Jar contained in the given
42      * directory path.
43      *
44      * @param rootPath
45      * directory containing the Jars
46      * @param parent
47      * the parent classloader to use
48      * @return a classLoader
49      */

50     public static URLClassLoader JavaDoc createClassLoader(String JavaDoc rootPath, ClassLoader JavaDoc parent) {
51
52         File JavaDoc path = new File JavaDoc(rootPath);
53
54         FileFilter JavaDoc jarFileFilter = new FileFilter JavaDoc() {
55             public boolean accept(File JavaDoc f) {
56                 return f.getName().toLowerCase().endsWith(".jar");
57             }
58         };
59
60         File JavaDoc[] files = path.listFiles(jarFileFilter);
61
62         ArrayList JavaDoc<URL JavaDoc> urls = new ArrayList JavaDoc<URL JavaDoc>();
63
64         if (files != null && files.length > 0) {
65             for (File JavaDoc file : files) {
66                 try {
67                     urls.add(file.toURL());
68                 } catch (MalformedURLException JavaDoc e) {
69                     // nothing
70
}
71             }
72         }
73         return new URLClassLoader JavaDoc(urls.toArray(new URL JavaDoc[] {}), parent);
74     }
75 }
76
Popular Tags