1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.extension.config.taglib.impl; |
17 | |
|
18 | |
import java.io.InputStream; |
19 | |
import java.net.JarURLConnection; |
20 | |
import java.net.URL; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.Set; |
23 | |
|
24 | |
import javax.servlet.ServletContext; |
25 | |
|
26 | |
import org.seasar.framework.log.Logger; |
27 | |
import org.seasar.framework.util.InputStreamUtil; |
28 | |
import org.seasar.framework.util.URLUtil; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class ServletContextTaglibManagerImpl extends AbstractTaglibManager { |
35 | |
|
36 | 0 | private static Logger logger = Logger |
37 | 0 | .getLogger(ServletContextTaglibManagerImpl.class); |
38 | |
|
39 | |
private ServletContext servletContext; |
40 | |
|
41 | |
public void setServletContext(ServletContext servletContext) { |
42 | 0 | this.servletContext = servletContext; |
43 | 0 | } |
44 | |
|
45 | |
public void init() { |
46 | 0 | scanTlds("/WEB-INF/"); |
47 | 0 | scanJars("/WEB-INF/lib/"); |
48 | 0 | } |
49 | |
|
50 | |
public void scanJars(String libPath) { |
51 | 0 | Set jars = servletContext.getResourcePaths(libPath); |
52 | 0 | if (jars != null) { |
53 | 0 | for (Iterator i = jars.iterator(); i.hasNext();) { |
54 | 0 | String path = (String) i.next(); |
55 | 0 | if (path.toLowerCase().endsWith(".jar")) { |
56 | 0 | scanJar(path); |
57 | |
} |
58 | |
} |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
public void scanJar(String jarPath) { |
63 | |
try { |
64 | 0 | URL url = servletContext.getResource(jarPath); |
65 | 0 | JarURLConnection conn = openJarURLConnection(url); |
66 | 0 | if (conn == null) { |
67 | 0 | return; |
68 | |
} |
69 | 0 | scanJar(conn); |
70 | 0 | } catch (Throwable t) { |
71 | 0 | logger.log(t); |
72 | 0 | } |
73 | 0 | } |
74 | |
|
75 | |
public void scanTlds(final String basePath) { |
76 | 0 | final Set tlds = servletContext.getResourcePaths(basePath); |
77 | 0 | if (tlds != null) { |
78 | 0 | for (final Iterator i = tlds.iterator(); i.hasNext();) { |
79 | 0 | final String path = (String) i.next(); |
80 | 0 | if (path.toLowerCase().endsWith(".tld")) { |
81 | 0 | scanTld(path); |
82 | 0 | } else if (path.endsWith("/")) { |
83 | 0 | scanTlds(path); |
84 | |
} |
85 | |
} |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
public void scanTld(final String tldPath) { |
90 | |
try { |
91 | 0 | final URL url = servletContext.getResource(tldPath); |
92 | 0 | final InputStream is = URLUtil.openStream(url); |
93 | |
try { |
94 | 0 | if (is == null) { |
95 | |
return; |
96 | |
} |
97 | 0 | scanTld(is, tldPath); |
98 | |
} finally { |
99 | 0 | InputStreamUtil.close(is); |
100 | 0 | } |
101 | 0 | } catch (final Throwable t) { |
102 | 0 | logger.log(t); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
} |