View Javadoc

1   package org.apache.maven.j2ee;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 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   */
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.net.URLClassLoader;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Set;
29  import java.util.jar.JarEntry;
30  
31  /**
32   * A {@link ClassLoader} that loads classes from a {@link WarFile}
33   *
34   * @author <a href="mailto:dion@multitask.com.au">dIon Gillard</a>
35   * @version $Id: WarClassLoader.java 170200 2005-05-15 06:24:19Z brett $
36   */
37  public class WarClassLoader extends URLClassLoader
38  {
39      /** temp files created by classloader */
40      private List tempFiles = new ArrayList();
41      /** war file to be used for class loading */
42      private WarFile war;
43      
44      /**
45       * Creates a new instance of WarClassLoader
46       * 
47       * @param war a {@link WarFile}
48       * @throws IOException when an I/O error occurs extracting the jars from the
49       *         war
50       * @throws MalformedURLException if the jar: URL is not supported on the
51       *         underlying platform
52       */
53      public WarClassLoader(WarFile war) throws IOException, MalformedURLException
54      {
55          super (new URL[0]);
56          this.war = war;
57          addURLs();
58      }
59      
60      /**
61       * Creates a new instance of WarClassLoader with the provided parent 
62       * classloader as the one to delegate to if classes can't be found
63       * 
64       * @param war a {@link WarFile}
65       * @param classloader a {@link ClassLoader} to delegate to.
66       * @throws IOException when an I/O error occurs extracting the jars from
67       *      the war
68       * @throws MalformedURLException if the jar: URL is not supported on the
69       *      underlying platform
70       */
71      public WarClassLoader(WarFile war, ClassLoader classloader) throws
72          IOException, MalformedURLException
73      {
74          super(new URL[0], classloader);
75          this.war = war;
76          addURLs();
77      }
78      
79      /**
80       * Add WEB-INF/classes and WEB-INF/lib/*.jar as extra classpath URLs
81       * 
82       * @throws IOException when an I/O error occurs extracting the jars from
83       *      the war
84       * @throws MalformedURLException if the jar: URL is not supported on the
85       *      underlying platform
86       */
87      private void addURLs() throws IOException, MalformedURLException
88      {
89          File warFile = new File(war.getName());
90          URL webInfClasses = new URL("jar:" + warFile.toURL() + "!/"
91              + "WEB-INF/classes/");
92          addURL(webInfClasses);
93          Set jars = war.getLibEntries();
94          JarEntry entry = null;
95          for (Iterator jarEntries = jars.iterator(); jarEntries.hasNext();)
96          {
97              entry = (JarEntry) jarEntries.next();
98              File jar = war.extract(entry);
99              tempFiles.add(jar);
100             jar.deleteOnExit();
101             addURL(new URL("jar:" + jar.toURL() + "!/"));
102         }
103     }
104 }