View Javadoc

1   /*
2    * $Id: MultiDirClassLoader.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.quickstart;
19  
20  import java.io.File;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.net.URLClassLoader;
24  import java.util.ArrayList;
25  
26  /***
27   * Integration with Jetty.
28   *
29   */
30  public class MultiDirClassLoader extends URLClassLoader {
31      private ClassLoader parent;
32  
33      public MultiDirClassLoader(String[] dirs, String[] cps, ClassLoader parent) throws MalformedURLException {
34          super(getAllURLs(dirs, cps), parent);
35          this.parent = parent;
36      }
37  
38      public Class loadClass(String name) throws ClassNotFoundException {
39          Class aClass;
40  
41          try {
42              aClass = parent.loadClass(name);
43              if (aClass != null) {
44                  return aClass;
45              }
46          } catch (ClassNotFoundException e) {
47              // ok, keep trying
48          }
49  
50          return super.loadClass(name);
51      }
52  
53      public URL getResource(String name) {
54          URL url = findResource(name);
55          if (url == null && parent != null) {
56              url = parent.getResource(name);
57          }
58  
59          return url;
60      }
61  
62  
63      private static URL[] getAllURLs(String[] dirs, String[] cps) throws MalformedURLException {
64          ArrayList urls = new ArrayList();
65  
66          for (int i = 0; i < cps.length; i++) {
67              String cp = cps[i];
68              urls.add(new File(cp).toURL());
69          }
70  
71          for (int i = 0; i < dirs.length; i++) {
72              String dir = dirs[i];
73              File file = new File(dir);
74              findJars(file, urls);
75          }
76  
77          return (URL[]) urls.toArray(new URL[urls.size()]);
78      }
79  
80      private static void findJars(File file, ArrayList fileList) throws MalformedURLException {
81          if (file.isDirectory()) {
82              File[] files = file.listFiles();
83              for (int i = 0; i < files.length; i++) {
84                  File f = files[i];
85                  findJars(f, fileList);
86              }
87          }
88          else if (file.getName().endsWith(".jar")) {
89              // Manually exclude the local license file so that it's possible to run
90              // clustering.
91              if (!file.getName().equals("tangosol-license-local.jar")) {
92                  fileList.add(file.toURL());
93              }
94          }
95      }
96  }