1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.maven.plugin;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.eclipse.jetty.util.IO;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
31 import org.eclipse.jetty.util.resource.Resource;
32 import org.eclipse.jetty.util.resource.ResourceCollection;
33 import org.eclipse.jetty.webapp.WebAppClassLoader;
34 import org.eclipse.jetty.webapp.WebAppContext;
35 import org.eclipse.jetty.webapp.WebInfConfiguration;
36
37
38
39
40
41
42
43
44
45
46 public class MavenWebInfConfiguration extends WebInfConfiguration
47 {
48 private static final Logger LOG = Log.getLogger(MavenWebInfConfiguration.class);
49
50
51 protected static int COUNTER = 0;
52 protected Resource _originalResourceBase;
53 protected List<Resource> _unpackedOverlayResources;
54
55
56
57
58
59
60
61 public void configure(WebAppContext context) throws Exception
62 {
63 JettyWebAppContext jwac = (JettyWebAppContext)context;
64
65
66 if (jwac.getClassPathFiles() != null)
67 {
68 if (LOG.isDebugEnabled()) LOG.debug("Setting up classpath ...");
69 Iterator itor = jwac.getClassPathFiles().iterator();
70 while (itor.hasNext())
71 ((WebAppClassLoader)context.getClassLoader()).addClassPath(((File)itor.next()).getCanonicalPath());
72 }
73
74 super.configure(context);
75
76
77 String[] existingServerClasses = context.getServerClasses();
78 String[] newServerClasses = new String[2+(existingServerClasses==null?0:existingServerClasses.length)];
79 newServerClasses[0] = "org.apache.maven.";
80 newServerClasses[1] = "org.codehaus.plexus.";
81 System.arraycopy( existingServerClasses, 0, newServerClasses, 2, existingServerClasses.length );
82 if (LOG.isDebugEnabled())
83 {
84 LOG.debug("Server classes:");
85 for (int i=0;i<newServerClasses.length;i++)
86 LOG.debug(newServerClasses[i]);
87 }
88 context.setServerClasses( newServerClasses );
89 }
90
91
92
93
94
95
96
97 public void preConfigure(WebAppContext context) throws Exception
98 {
99 super.preConfigure(context);
100
101 }
102
103
104
105
106
107
108
109 public void postConfigure(WebAppContext context) throws Exception
110 {
111 super.postConfigure(context);
112 }
113
114
115
116
117
118
119
120 public void deconfigure(WebAppContext context) throws Exception
121 {
122
123 if (_unpackedOverlayResources != null && !_unpackedOverlayResources.isEmpty())
124 {
125 try
126 {
127 for (Resource r:_unpackedOverlayResources)
128 {
129 IO.delete(r.getFile());
130 }
131 }
132 catch (IOException e)
133 {
134 LOG.ignore(e);
135 }
136 }
137 super.deconfigure(context);
138
139 context.setBaseResource(_originalResourceBase);
140 }
141
142
143
144
145
146
147
148 @Override
149 public void unpack(WebAppContext context) throws IOException
150 {
151
152 super.unpack(context);
153
154
155 _originalResourceBase = context.getBaseResource();
156
157 JettyWebAppContext jwac = (JettyWebAppContext)context;
158
159
160 _unpackedOverlayResources = new ArrayList<Resource>();
161
162
163
164 if (jwac.getOverlays() != null && !jwac.getOverlays().isEmpty())
165 {
166 List<Resource> resourceBaseCollection = new ArrayList<Resource>();
167
168 for (Overlay o:jwac.getOverlays())
169 {
170
171 if (o.getConfig() != null && o.getConfig().isCurrentProject() && _originalResourceBase.exists())
172 {
173 resourceBaseCollection.add(_originalResourceBase);
174 LOG.debug("Adding virtual project to resource base list");
175 continue;
176 }
177
178 Resource unpacked = unpackOverlay(jwac,o);
179 _unpackedOverlayResources.add(unpacked);
180 resourceBaseCollection.add(unpacked);
181 LOG.debug("Adding "+unpacked+" to resource base list");
182 }
183
184 if (!resourceBaseCollection.contains(_originalResourceBase) && _originalResourceBase.exists())
185 {
186 if (jwac.getBaseAppFirst())
187 {
188 LOG.debug("Adding virtual project first in resource base list");
189 resourceBaseCollection.add(0, _originalResourceBase);
190 }
191 else
192 {
193 LOG.debug("Adding virtual project last in resource base list");
194 resourceBaseCollection.add(_originalResourceBase);
195 }
196 }
197
198 jwac.setBaseResource(new ResourceCollection(resourceBaseCollection.toArray(new Resource[resourceBaseCollection.size()])));
199 }
200 }
201
202
203
204
205
206
207
208
209
210 @Override
211 protected List<Resource> findJars (WebAppContext context)
212 throws Exception
213 {
214 List<Resource> list = new ArrayList<Resource>();
215 JettyWebAppContext jwac = (JettyWebAppContext)context;
216 if (jwac.getClassPathFiles() != null)
217 {
218 for (File f: jwac.getClassPathFiles())
219 {
220 if (f.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
221 {
222 try
223 {
224 list.add(Resource.newResource(f.toURI()));
225 }
226 catch (Exception e)
227 {
228 LOG.warn("Bad url ", e);
229 }
230 }
231 }
232 }
233
234 List<Resource> superList = super.findJars(context);
235 if (superList != null)
236 list.addAll(superList);
237 return list;
238 }
239
240
241
242
243
244
245
246
247
248 @Override
249 protected List<Resource> findClassDirs(WebAppContext context) throws Exception
250 {
251 List<Resource> list = new ArrayList<Resource>();
252
253 JettyWebAppContext jwac = (JettyWebAppContext)context;
254 if (jwac.getClassPathFiles() != null)
255 {
256 for (File f: jwac.getClassPathFiles())
257 {
258 if (f.exists() && f.isDirectory())
259 {
260 try
261 {
262 list.add(Resource.newResource(f.toURI()));
263 }
264 catch (Exception e)
265 {
266 LOG.warn("Bad url ", e);
267 }
268 }
269 }
270 }
271
272 List<Resource> classesDirs = super.findClassDirs(context);
273 if (classesDirs != null)
274 list.addAll(classesDirs);
275 return list;
276 }
277
278
279
280
281
282 protected Resource unpackOverlay (WebAppContext context, Overlay overlay)
283 throws IOException
284 {
285 LOG.debug("Unpacking overlay: " + overlay);
286
287 if (overlay.getResource() == null)
288 return null;
289
290
291
292 String name = overlay.getResource().getName();
293 if (name.endsWith("!/"))
294 name = name.substring(0,name.length()-2);
295 int i = name.lastIndexOf('/');
296 if (i>0)
297 name = name.substring(i+1,name.length());
298 name = name.replace('.', '_');
299 name = name+(++COUNTER);
300 File dir = new File(context.getTempDirectory(), name);
301
302
303 File unpackDir = dir;
304 if (overlay.getConfig() != null && overlay.getConfig().getTargetPath() != null)
305 unpackDir = new File (dir, overlay.getConfig().getTargetPath());
306
307 overlay.getResource().copyTo(unpackDir);
308
309 Resource unpackedOverlay = Resource.newResource(dir.getCanonicalPath());
310
311 LOG.debug("Unpacked overlay: "+overlay+" to "+unpackedOverlay);
312 return unpackedOverlay;
313 }
314 }