1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.ant;
20
21 import java.io.File;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.net.URLClassLoader;
26 import java.util.List;
27 import java.util.regex.Pattern;
28
29 import org.apache.tools.ant.AntClassLoader;
30 import org.eclipse.jetty.util.PatternMatcher;
31 import org.eclipse.jetty.util.resource.Resource;
32 import org.eclipse.jetty.webapp.WebAppClassLoader;
33 import org.eclipse.jetty.webapp.WebAppContext;
34 import org.eclipse.jetty.webapp.WebInfConfiguration;
35 import org.eclipse.jetty.webapp.WebXmlConfiguration;
36
37 public class AntWebInfConfiguration extends WebInfConfiguration
38 {
39
40
41 @Override
42 public void preConfigure(final WebAppContext context) throws Exception
43 {
44
45 File work = findWorkDirectory(context);
46 if (work != null)
47 makeTempDirectory(work, context, false);
48
49
50 resolveTempDirectory(context);
51
52
53 unpack (context);
54
55
56
57
58 String tmp = (String)context.getAttribute(WEBINF_JAR_PATTERN);
59 Pattern webInfPattern = (tmp==null?null:Pattern.compile(tmp));
60 tmp = (String)context.getAttribute(CONTAINER_JAR_PATTERN);
61 Pattern containerPattern = (tmp==null?null:Pattern.compile(tmp));
62
63
64
65 PatternMatcher containerJarNameMatcher = new PatternMatcher ()
66 {
67 public void matched(URI uri) throws Exception
68 {
69 context.getMetaData().addContainerResource(Resource.newResource(uri));
70 }
71 };
72 ClassLoader loader = context.getClassLoader();
73 if (loader != null)
74 {
75 loader = loader.getParent();
76 if (loader != null)
77 {
78 URI[] containerUris = null;
79
80 if (loader instanceof URLClassLoader)
81 {
82 URL[] urls = ((URLClassLoader)loader).getURLs();
83 if (urls != null)
84 {
85 containerUris = new URI[urls.length];
86 int i=0;
87 for (URL u : urls)
88 {
89 try
90 {
91 containerUris[i] = u.toURI();
92 }
93 catch (URISyntaxException e)
94 {
95 containerUris[i] = new URI(u.toString().replaceAll(" ", "%20"));
96 }
97 i++;
98 }
99 }
100 }
101 else if (loader instanceof AntClassLoader)
102 {
103 AntClassLoader antLoader = (AntClassLoader)loader;
104 String[] paths = antLoader.getClasspath().split(new String(new char[]{File.pathSeparatorChar}));
105 if (paths != null)
106 {
107 containerUris = new URI[paths.length];
108 int i=0;
109 for (String p:paths)
110 {
111 File f = new File(p);
112 containerUris[i] = f.toURI();
113 i++;
114 }
115 }
116 }
117
118 containerJarNameMatcher.match(containerPattern, containerUris, false);
119 }
120 }
121
122
123 PatternMatcher webInfJarNameMatcher = new PatternMatcher ()
124 {
125 @Override
126 public void matched(URI uri) throws Exception
127 {
128 context.getMetaData().addWebInfJar(Resource.newResource(uri));
129 }
130 };
131 List<Resource> jars = findJars(context);
132
133
134 URI[] uris = null;
135 if (jars != null)
136 {
137 uris = new URI[jars.size()];
138 int i=0;
139 for (Resource r: jars)
140 {
141 uris[i++] = r.getURI();
142 }
143 }
144 webInfJarNameMatcher.match(webInfPattern, uris, true);
145 }
146
147
148
149
150
151
152
153
154 public void configure(WebAppContext context) throws Exception
155 {
156 if (context instanceof AntWebAppContext)
157 {
158 List<File> classPathFiles = ((AntWebAppContext)context).getClassPathFiles();
159 if (classPathFiles != null)
160 {
161 for (File cpFile:classPathFiles)
162 {
163 if (cpFile.exists())
164 {
165 ((WebAppClassLoader) context.getClassLoader()).addClassPath(cpFile.getCanonicalPath());
166 }
167 }
168 }
169 }
170 super.configure(context);
171 }
172 }