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 resolveTempDirectory(context);
46
47
48 unpack (context);
49
50
51
52
53 String tmp = (String)context.getAttribute(WEBINF_JAR_PATTERN);
54 Pattern webInfPattern = (tmp==null?null:Pattern.compile(tmp));
55 tmp = (String)context.getAttribute(CONTAINER_JAR_PATTERN);
56 Pattern containerPattern = (tmp==null?null:Pattern.compile(tmp));
57
58
59
60 PatternMatcher containerJarNameMatcher = new PatternMatcher ()
61 {
62 public void matched(URI uri) throws Exception
63 {
64 context.getMetaData().addContainerResource(Resource.newResource(uri));
65 }
66 };
67 ClassLoader loader = context.getClassLoader();
68 if (loader != null)
69 {
70 loader = loader.getParent();
71 if (loader != null)
72 {
73 URI[] containerUris = null;
74
75 if (loader instanceof URLClassLoader)
76 {
77 URL[] urls = ((URLClassLoader)loader).getURLs();
78 if (urls != null)
79 {
80 containerUris = new URI[urls.length];
81 int i=0;
82 for (URL u : urls)
83 {
84 try
85 {
86 containerUris[i] = u.toURI();
87 }
88 catch (URISyntaxException e)
89 {
90 containerUris[i] = new URI(u.toString().replaceAll(" ", "%20"));
91 }
92 i++;
93 }
94 }
95 }
96 else if (loader instanceof AntClassLoader)
97 {
98 AntClassLoader antLoader = (AntClassLoader)loader;
99 String[] paths = antLoader.getClasspath().split(new String(new char[]{File.pathSeparatorChar}));
100 if (paths != null)
101 {
102 containerUris = new URI[paths.length];
103 int i=0;
104 for (String p:paths)
105 {
106 File f = new File(p);
107 containerUris[i] = f.toURI();
108 i++;
109 }
110 }
111 }
112
113 containerJarNameMatcher.match(containerPattern, containerUris, false);
114 }
115 }
116
117
118 PatternMatcher webInfJarNameMatcher = new PatternMatcher ()
119 {
120 @Override
121 public void matched(URI uri) throws Exception
122 {
123 context.getMetaData().addWebInfJar(Resource.newResource(uri));
124 }
125 };
126 List<Resource> jars = findJars(context);
127
128
129 URI[] uris = null;
130 if (jars != null)
131 {
132 uris = new URI[jars.size()];
133 int i=0;
134 for (Resource r: jars)
135 {
136 uris[i++] = r.getURI();
137 }
138 }
139 webInfJarNameMatcher.match(webInfPattern, uris, true);
140
141
142 context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
143 }
144
145
146
147
148
149
150
151
152 public void configure(WebAppContext context) throws Exception
153 {
154 if (context instanceof AntWebAppContext)
155 {
156 List<File> classPathFiles = ((AntWebAppContext)context).getClassPathFiles();
157 if (classPathFiles != null)
158 {
159 for (File cpFile:classPathFiles)
160 {
161 if (cpFile.exists())
162 {
163 ((WebAppClassLoader) context.getClassLoader()).addClassPath(cpFile.getCanonicalPath());
164 }
165 }
166 }
167 }
168 super.configure(context);
169 }
170 }