KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > core > AntClassLoader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Tom Tromey (tromey@redhat.com) - patch for bug 40972
11  *******************************************************************************/

12 package org.eclipse.ant.internal.core;
13
14
15 import java.io.IOException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLClassLoader JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.List JavaDoc;
22
23 public class AntClassLoader extends URLClassLoader JavaDoc {
24
25     private static final String JavaDoc ANT_PACKAGES_PREFIX = "org.apache.tools"; //$NON-NLS-1$
26
private static final String JavaDoc ANT_URL_PREFIX = "org/apache/tools"; //$NON-NLS-1$
27

28     private boolean fAllowPluginLoading = false;
29
30     protected ClassLoader JavaDoc[] fPluginLoaders;
31
32     private ClassLoader JavaDoc fContextClassloader = null;
33     
34     public AntClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc[] pluginLoaders) {
35         super(urls, ClassLoader.getSystemClassLoader());
36         fPluginLoaders = pluginLoaders;
37     }
38
39     /*
40      * @see java.net.URLClassLoader#findClass(java.lang.String)
41      */

42     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
43         Class JavaDoc result = null;
44         //check whether to load the Apache Ant classes from the plug-in class loaders
45
//or to only load from the URLs specified from the Ant runtime classpath preferences setting
46
if (fAllowPluginLoading || !(name.startsWith(ANT_PACKAGES_PREFIX))) {
47             result = loadClassPlugins(name);
48         }
49         
50         if (result != null) {
51             return result;
52         }
53         
54         return super.findClass(name);
55     }
56
57     protected Class JavaDoc loadClassPlugins(String JavaDoc name) {
58         //remove this class loader as the context class loader
59
//when loading classes from plug-ins...see bug 94471
60
ClassLoader JavaDoc originalClassLoader = Thread.currentThread().getContextClassLoader();
61         if (fContextClassloader != null) {
62             Thread.currentThread().setContextClassLoader(fContextClassloader);
63         }
64         try {
65             Class JavaDoc result = null;
66             if (fPluginLoaders != null) {
67                 for (int i = 0; (i < fPluginLoaders.length) && (result == null); i++) {
68                     try {
69                         result = fPluginLoaders[i].loadClass(name);
70                     } catch (ClassNotFoundException JavaDoc e) {
71                         // Ignore exception now. If necessary we'll throw
72
// a ClassNotFoundException in loadClass(String)
73
}
74                 }
75             }
76             return result;
77         } finally {
78             Thread.currentThread().setContextClassLoader(originalClassLoader);
79         }
80     }
81     
82     /*
83      * @see java.net.URLClassLoader#findResource(java.lang.String)
84      */

85     public URL JavaDoc findResource(String JavaDoc name) {
86          if (fAllowPluginLoading || !(name.startsWith(ANT_URL_PREFIX))) {
87              URL JavaDoc result = findResourcePlugins(name);
88              if (result != null) {
89                  return result;
90              }
91          }
92         
93         return super.findResource(name);
94     }
95     
96     private URL JavaDoc findResourcePlugins(String JavaDoc name) {
97         //remove this class loader as the context class loader
98
//when loading resources from plug-ins...see bug 94471
99
ClassLoader JavaDoc originalClassLoader = Thread.currentThread().getContextClassLoader();
100         if (fContextClassloader != null) {
101             Thread.currentThread().setContextClassLoader(fContextClassloader);
102         }
103         try {
104             URL JavaDoc result = null;
105             if (fPluginLoaders != null) {
106                 for (int i = 0; i < fPluginLoaders.length; i++) {
107                     result = fPluginLoaders[i].getResource(name);
108                     if (result != null) {
109                         return result;
110                     }
111                 }
112             }
113         } finally {
114             Thread.currentThread().setContextClassLoader(originalClassLoader);
115         }
116         return null;
117     }
118     
119     /*
120      * @see java.net.URLClassLoader#findResources(java.lang.String)
121      */

122     public Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc {
123         ClassLoader JavaDoc originalClassLoader = Thread.currentThread().getContextClassLoader();
124         if (fContextClassloader != null) {
125             Thread.currentThread().setContextClassLoader(fContextClassloader);
126         }
127         List JavaDoc all = new ArrayList JavaDoc();
128         try {
129             if (fAllowPluginLoading || !(name.startsWith(ANT_URL_PREFIX) || name.startsWith(ANT_URL_PREFIX, 1))) {
130                 if (fPluginLoaders != null) {
131                     Enumeration JavaDoc result = null;
132                     for (int i = 0; i < fPluginLoaders.length; i++) {
133                         result = fPluginLoaders[i].getResources(name);
134                         while (result.hasMoreElements()) {
135                             all.add(result.nextElement());
136                         }
137                     }
138                 }
139             }
140
141             Enumeration JavaDoc superResources = super.findResources(name);
142             if (all.isEmpty()) {
143                 return superResources;
144             }
145
146             while (superResources.hasMoreElements()) {
147                 all.add(superResources.nextElement());
148             }
149             return Collections.enumeration(all);
150         } finally {
151             Thread.currentThread().setContextClassLoader(originalClassLoader);
152         }
153     }
154     
155     /**
156      * Sets whether this class loader will allow Apache Ant classes or resources to be found or
157      * loaded from its set of plug-in class loaders.
158      *
159      * @param allowLoading whether or not to allow the plug-in class loaders
160      * to load the Apache Ant classes or resources
161      */

162     public void allowPluginClassLoadersToLoadAnt(boolean allowLoading) {
163         fAllowPluginLoading = allowLoading;
164     }
165     
166     public void setPluginContextClassloader(ClassLoader JavaDoc classLoader) {
167         fContextClassloader = classLoader;
168     }
169 }
Popular Tags