1.0.3
[scannotation] / src / main / java / org / scannotation / WarUrlFinder.java
1 package org.scannotation;
2
3 import javax.servlet.ServletContext;
4 import javax.servlet.ServletContextEvent;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Set;
9 import java.io.File;
10
11 /**
12  * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
13  * @version $Revision: 1 $
14  */
15 public class WarUrlFinder
16 {
17    public static URL[] findWebInfLibClasspaths(ServletContextEvent servletContextEvent)
18    {
19       ServletContext servletContext = servletContextEvent.getServletContext();
20       return findWebInfLibClasspaths(servletContext);
21    }
22
23    public static URL[] findWebInfLibClasspaths(ServletContext servletContext)
24    {
25       ArrayList<URL> list = new ArrayList<URL>();
26       Set libJars = servletContext.getResourcePaths("/WEB-INF/lib");
27       if (libJars == null)
28       {
29          URL[] empty = {};
30          return empty;
31       }
32         
33  
34       for (Object jar : libJars)
35       {
36          try
37          {
38             list.add(servletContext.getResource((String) jar));
39          }
40          catch (MalformedURLException e)
41          {
42             throw new RuntimeException(e);
43          }
44       }
45       return list.toArray(new URL[list.size()]);
46    }
47
48    public static URL findWebInfClassesPath(ServletContextEvent servletContextEvent)
49    {
50       ServletContext servletContext = servletContextEvent.getServletContext();
51       return findWebInfClassesPath(servletContext);
52    }
53
54    /**
55     * Find the URL pointing to "/WEB-INF/classes"  This method may not work in conjunction with IteratorFactory
56     * if your servlet container does not extract the /WEB-INF/classes into a real file-based directory
57     *
58     * @param servletContext
59     * @return null if cannot determin /WEB-INF/classes
60     */
61    public static URL findWebInfClassesPath(ServletContext servletContext)
62    {
63       String path = servletContext.getRealPath("/WEB-INF/classes");
64       if (path == null) return null;
65       File fp = new File(path);
66       if (fp.exists() == false) return null;
67       try
68       {
69          return fp.toURL();
70       }
71       catch (MalformedURLException e)
72       {
73          throw new RuntimeException(e);
74       }
75    }
76 }