1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.startup;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.URLClassLoader;
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Set;
30
31 import org.apache.juli.logging.Log;
32 import org.apache.juli.logging.LogFactory;
33
34 /**
35  * <p>Utility class for building class loaders for Catalina.  The factory
36  * method requires the following parameters in order to build a new class
37  * loader (with suitable defaults in all cases):</p>
38  * <ul>
39  * <li>A set of directories containing unpacked classes (and resources)
40  *     that should be included in the class loader's
41  *     repositories.</li>
42  * <li>A set of directories containing classes and resources in JAR files.
43  *     Each readable JAR file discovered in these directories will be
44  *     added to the class loader's repositories.</li>
45  * <li><code>ClassLoader</code> instance that should become the parent of
46  *     the new class loader.</li>
47  * </ul>
48  *
49  * @author Craig R. McClanahan
50  */

51 public final class ClassLoaderFactory {
52
53
54     private static final Log log = LogFactory.getLog(ClassLoaderFactory.class);
55
56     // --------------------------------------------------------- Public Methods
57
58
59     /**
60      * Create and return a new class loader, based on the configuration
61      * defaults and the specified directory paths:
62      *
63      * @param unpacked Array of pathnames to unpacked directories that should
64      *  be added to the repositories of the class loader, or <code>null</code>
65      * for no unpacked directories to be considered
66      * @param packed Array of pathnames to directories containing JAR files
67      *  that should be added to the repositories of the class loader,
68      * or <code>null</code> for no directories of JAR files to be considered
69      * @param parent Parent class loader for the new class loader, or
70      *  <code>null</code> for the system class loader.
71      * @return the new class loader
72      *
73      * @exception Exception if an error occurs constructing the class loader
74      */

75     public static ClassLoader createClassLoader(File unpacked[],
76                                                 File packed[],
77                                                 final ClassLoader parent)
78         throws Exception {
79
80         if (log.isDebugEnabled())
81             log.debug("Creating new class loader");
82
83         // Construct the "class path" for this class loader
84         Set<URL> set = new LinkedHashSet<>();
85
86         // Add unpacked directories
87         if (unpacked != null) {
88             for (int i = 0; i < unpacked.length; i++)  {
89                 File file = unpacked[i];
90                 if (!file.canRead())
91                     continue;
92                 file = new File(file.getCanonicalPath() + File.separator);
93                 URL url = file.toURI().toURL();
94                 if (log.isDebugEnabled())
95                     log.debug("  Including directory " + url);
96                 set.add(url);
97             }
98         }
99
100         // Add packed directory JAR files
101         if (packed != null) {
102             for (int i = 0; i < packed.length; i++) {
103                 File directory = packed[i];
104                 if (!directory.isDirectory() || !directory.canRead())
105                     continue;
106                 String filenames[] = directory.list();
107                 if (filenames == null) {
108                     continue;
109                 }
110                 for (int j = 0; j < filenames.length; j++) {
111                     String filename = filenames[j].toLowerCase(Locale.ENGLISH);
112                     if (!filename.endsWith(".jar"))
113                         continue;
114                     File file = new File(directory, filenames[j]);
115                     if (log.isDebugEnabled())
116                         log.debug("  Including jar file " + file.getAbsolutePath());
117                     URL url = file.toURI().toURL();
118                     set.add(url);
119                 }
120             }
121         }
122
123         // Construct the class loader itself
124         final URL[] array = set.toArray(new URL[set.size()]);
125         return AccessController.doPrivileged(
126                 new PrivilegedAction<URLClassLoader>() {
127                     @Override
128                     public URLClassLoader run() {
129                         if (parent == null)
130                             return new URLClassLoader(array);
131                         else
132                             return new URLClassLoader(array, parent);
133                     }
134                 });
135     }
136
137
138     /**
139      * Create and return a new class loader, based on the configuration
140      * defaults and the specified directory paths:
141      *
142      * @param repositories List of class directories, jar files, jar directories
143      *                     or URLS that should be added to the repositories of
144      *                     the class loader.
145      * @param parent Parent class loader for the new class loader, or
146      *  <code>null</code> for the system class loader.
147      * @return the new class loader
148      *
149      * @exception Exception if an error occurs constructing the class loader
150      */

151     public static ClassLoader createClassLoader(List<Repository> repositories,
152                                                 final ClassLoader parent)
153         throws Exception {
154
155         if (log.isDebugEnabled())
156             log.debug("Creating new class loader");
157
158         // Construct the "class path" for this class loader
159         Set<URL> set = new LinkedHashSet<>();
160
161         if (repositories != null) {
162             for (Repository repository : repositories)  {
163                 if (repository.getType() == RepositoryType.URL) {
164                     URL url = buildClassLoaderUrl(repository.getLocation());
165                     if (log.isDebugEnabled())
166                         log.debug("  Including URL " + url);
167                     set.add(url);
168                 } else if (repository.getType() == RepositoryType.DIR) {
169                     File directory = new File(repository.getLocation());
170                     directory = directory.getCanonicalFile();
171                     if (!validateFile(directory, RepositoryType.DIR)) {
172                         continue;
173                     }
174                     URL url = buildClassLoaderUrl(directory);
175                     if (log.isDebugEnabled())
176                         log.debug("  Including directory " + url);
177                     set.add(url);
178                 } else if (repository.getType() == RepositoryType.JAR) {
179                     File file=new File(repository.getLocation());
180                     file = file.getCanonicalFile();
181                     if (!validateFile(file, RepositoryType.JAR)) {
182                         continue;
183                     }
184                     URL url = buildClassLoaderUrl(file);
185                     if (log.isDebugEnabled())
186                         log.debug("  Including jar file " + url);
187                     set.add(url);
188                 } else if (repository.getType() == RepositoryType.GLOB) {
189                     File directory=new File(repository.getLocation());
190                     directory = directory.getCanonicalFile();
191                     if (!validateFile(directory, RepositoryType.GLOB)) {
192                         continue;
193                     }
194                     if (log.isDebugEnabled())
195                         log.debug("  Including directory glob "
196                             + directory.getAbsolutePath());
197                     String filenames[] = directory.list();
198                     if (filenames == null) {
199                         continue;
200                     }
201                     for (int j = 0; j < filenames.length; j++) {
202                         String filename = filenames[j].toLowerCase(Locale.ENGLISH);
203                         if (!filename.endsWith(".jar"))
204                             continue;
205                         File file = new File(directory, filenames[j]);
206                         file = file.getCanonicalFile();
207                         if (!validateFile(file, RepositoryType.JAR)) {
208                             continue;
209                         }
210                         if (log.isDebugEnabled())
211                             log.debug("    Including glob jar file "
212                                 + file.getAbsolutePath());
213                         URL url = buildClassLoaderUrl(file);
214                         set.add(url);
215                     }
216                 }
217             }
218         }
219
220         // Construct the class loader itself
221         final URL[] array = set.toArray(new URL[set.size()]);
222         if (log.isDebugEnabled())
223             for (int i = 0; i < array.length; i++) {
224                 log.debug("  location " + i + " is " + array[i]);
225             }
226
227         return AccessController.doPrivileged(
228                 new PrivilegedAction<URLClassLoader>() {
229                     @Override
230                     public URLClassLoader run() {
231                         if (parent == null)
232                             return new URLClassLoader(array);
233                         else
234                             return new URLClassLoader(array, parent);
235                     }
236                 });
237     }
238
239     private static boolean validateFile(File file,
240             RepositoryType type) throws IOException {
241         if (RepositoryType.DIR == type || RepositoryType.GLOB == type) {
242             if (!file.isDirectory() || !file.canRead()) {
243                 String msg = "Problem with directory [" + file +
244                         "], exists: [" + file.exists() +
245                         "], isDirectory: [" + file.isDirectory() +
246                         "], canRead: [" + file.canRead() + "]";
247
248                 File home = new File (Bootstrap.getCatalinaHome());
249                 home = home.getCanonicalFile();
250                 File base = new File (Bootstrap.getCatalinaBase());
251                 base = base.getCanonicalFile();
252                 File defaultValue = new File(base, "lib");
253
254                 // Existence of ${catalina.base}/lib directory is optional.
255                 // Hide the warning if Tomcat runs with separate catalina.home
256                 // and catalina.base and that directory is absent.
257                 if (!home.getPath().equals(base.getPath())
258                         && file.getPath().equals(defaultValue.getPath())
259                         && !file.exists()) {
260                     log.debug(msg);
261                 } else {
262                     log.warn(msg);
263                 }
264                 return false;
265             }
266         } else if (RepositoryType.JAR == type) {
267             if (!file.canRead()) {
268                 log.warn("Problem with JAR file [" + file +
269                         "], exists: [" + file.exists() +
270                         "], canRead: [" + file.canRead() + "]");
271                 return false;
272             }
273         }
274         return true;
275     }
276
277
278     /*
279      * These two methods would ideally be in the utility class
280      * org.apache.tomcat.util.buf.UriUtil but that class is not visible until
281      * after the class loaders have been constructed.
282      */

283     private static URL buildClassLoaderUrl(String urlString) throws MalformedURLException {
284         // URLs passed to class loaders may point to directories that contain
285         // JARs. If these URLs are used to construct URLs for resources in a JAR
286         // the URL will be used as is. It is therefore necessary to ensure that
287         // the sequence "!/" is not present in a class loader URL.
288         String result = urlString.replaceAll("!/""%21/");
289         return new URL(result);
290     }
291
292
293     private static URL buildClassLoaderUrl(File file) throws MalformedURLException {
294         // Could be a directory or a file
295         String fileUrlString = file.toURI().toString();
296         fileUrlString = fileUrlString.replaceAll("!/""%21/");
297         return new URL(fileUrlString);
298     }
299
300
301     public enum RepositoryType {
302         DIR,
303         GLOB,
304         JAR,
305         URL
306     }
307
308     public static class Repository {
309         private final String location;
310         private final RepositoryType type;
311
312         public Repository(String location, RepositoryType type) {
313             this.location = location;
314             this.type = type;
315         }
316
317         public String getLocation() {
318             return location;
319         }
320
321         public RepositoryType getType() {
322             return type;
323         }
324     }
325 }
326