KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > MultiplexClassLoader


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
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 St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jun 19, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.util.*;
27 /**
28  * @author J. H. S.
29  */

30 public abstract class MultiplexClassLoader extends BaseClassLoader {
31     private static final BaseClassLoader[] EMPTY_CLASS_LOADERS = new BaseClassLoader[0];
32     private final BaseClassLoader[] parentLoaders;
33     /**
34      * @param parent
35      */

36     public MultiplexClassLoader(Collection classLoaders) {
37         super(null);
38         this.parentLoaders = (BaseClassLoader[]) classLoaders.toArray(EMPTY_CLASS_LOADERS);
39     }
40     
41     /* (non-Javadoc)
42      * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
43      */

44     public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
45             throws ClassNotFoundException JavaDoc {
46         // First, check if the class has already been loaded
47
Class JavaDoc c = findLoadedClass(name);
48         if (c == null) {
49             try {
50                 int len = this.parentLoaders.length;
51                 if(len == 0) {
52                     c = findSystemClass(name);
53                 }
54                 else {
55                     for(int i = 0; i < len; i++) {
56                         BaseClassLoader parent = this.parentLoaders[i];
57                         try {
58                             c = parent.loadClass(name, false);
59                             if(c != null) {
60                                 return c;
61                             }
62                         } catch(ClassNotFoundException JavaDoc cnfe) {
63                             // ignore
64
}
65                     }
66                 }
67             } catch (ClassNotFoundException JavaDoc e) {
68                 // If still not found, then invoke findClass in order
69
// to find the class.
70
c = findClass(name);
71             }
72             if(c == null) {
73                 c = findClass(name);
74             }
75         }
76         if (resolve) {
77             resolveClass(c);
78         }
79         return c;
80     }
81 }
82
Popular Tags