01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: *
18: * StandaloneWebappClassLoader.java
19: * Created on October 20, 2006, 11:11 PM
20: */
21:
22: package org.apache.roller.util;
23:
24: import java.io.File;
25: import java.io.FilenameFilter;
26: import java.net.URL;
27: import java.net.URLClassLoader;
28: import java.util.ArrayList;
29: import java.util.List;
30:
31: /**
32: * ClassLoader to enable running webapp classes outside of webapp.
33: * You provide webappDir and jarsDir paths and the classloader will include
34: * webappDir/WEB-INF/classes, webappDir/WEB-INF/lib/*jar and jarsDir/*.jar.
35: */
36: public class StandaloneWebappClassLoader extends URLClassLoader {
37: public static String FS = File.separator;
38:
39: /** Use calling class's parent classloader */
40: public StandaloneWebappClassLoader(String webappDir, String jarsDir)
41: throws Exception {
42: super (buildURLsArray(webappDir, jarsDir));
43: }
44:
45: /** Use a specific parent classloader, or null for no parent */
46: public StandaloneWebappClassLoader(String webappDir,
47: String jarsDir, ClassLoader cl) throws Exception {
48: super (buildURLsArray(webappDir, jarsDir), cl);
49: }
50:
51: private static URL[] buildURLsArray(String webappDir, String jarsDir)
52: throws Exception {
53: // Create collection of URLs needed for classloader
54: List urlList = new ArrayList();
55:
56: // Add WEB-INF/lib jars
57: String libPath = webappDir + FS + "WEB-INF" + FS + "lib";
58: addURLs(libPath, urlList);
59:
60: // Added WEB-INF/classes
61: String classesPath = webappDir + FS + "WEB-INF" + FS
62: + "classes" + FS;
63: urlList.add(new URL("file://" + classesPath));
64:
65: // Add additional jars
66: addURLs(jarsDir, urlList);
67:
68: return (URL[]) urlList.toArray(new URL[urlList.size()]);
69: }
70:
71: private static void addURLs(String dirPath, List urlList)
72: throws Exception {
73: File libDir = new File(dirPath);
74: String[] libJarNames = libDir.list(new FilenameFilter() {
75: public boolean accept(File dir, String pathname) {
76: if (pathname.endsWith(".jar")) {
77: return true;
78: }
79: return false;
80: }
81: });
82: for (int i = 0; i < libJarNames.length; i++) {
83: String url = "file://" + dirPath + FS + libJarNames[i];
84: urlList.add(new URL(url));
85: }
86: }
87: }
|