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