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
46 public class MavenWebInfConfiguration extends WebInfConfiguration
47 {
48 private static final Logger LOG = Log.getLogger(MavenWebInfConfiguration.class);
49
50 public static final String RESOURCE_BASES_POST_OVERLAY = "org.eclipse.jetty.resource.postOverlay";
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 super.deconfigure(context);
123
124 context.setBaseResource(_originalResourceBase);
125
126 context.removeAttribute(RESOURCE_BASES_POST_OVERLAY);
127 }
128
129
130
131
132
133
134
135 @Override
136 public void unpack(WebAppContext context) throws IOException
137 {
138
139 super.unpack(context);
140
141
142 _originalResourceBase = context.getBaseResource();
143
144 JettyWebAppContext jwac = (JettyWebAppContext)context;
145
146
147 _unpackedOverlayResources = new ArrayList<Resource>();
148
149
150
151 if (jwac.getOverlays() != null && !jwac.getOverlays().isEmpty())
152 {
153 List<Resource> resourceBaseCollection = new ArrayList<Resource>();
154
155 for (Overlay o:jwac.getOverlays())
156 {
157
158 if (o.getConfig() != null && o.getConfig().isCurrentProject() && _originalResourceBase.exists())
159 {
160 resourceBaseCollection.add(_originalResourceBase);
161 LOG.debug("Adding virtual project to resource base list");
162 continue;
163 }
164
165 Resource unpacked = unpackOverlay(jwac,o);
166 _unpackedOverlayResources.add(unpacked);
167 resourceBaseCollection.add(unpacked);
168 LOG.debug("Adding "+unpacked+" to resource base list");
169 }
170
171 if (!resourceBaseCollection.contains(_originalResourceBase) && _originalResourceBase.exists())
172 {
173 if (jwac.getBaseAppFirst())
174 {
175 LOG.debug("Adding virtual project first in resource base list");
176 resourceBaseCollection.add(0, _originalResourceBase);
177 }
178 else
179 {
180 LOG.debug("Adding virtual project last in resource base list");
181 resourceBaseCollection.add(_originalResourceBase);
182 }
183 }
184 jwac.setBaseResource(new ResourceCollection(resourceBaseCollection.toArray(new Resource[resourceBaseCollection.size()])));
185 }
186
187 jwac.setAttribute(RESOURCE_BASES_POST_OVERLAY, jwac.getBaseResource());
188 }
189
190
191
192
193
194
195
196
197
198 @Override
199 protected List<Resource> findJars (WebAppContext context)
200 throws Exception
201 {
202 List<Resource> list = new ArrayList<Resource>();
203 JettyWebAppContext jwac = (JettyWebAppContext)context;
204 if (jwac.getClassPathFiles() != null)
205 {
206 for (File f: jwac.getClassPathFiles())
207 {
208 if (f.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
209 {
210 try
211 {
212 list.add(Resource.newResource(f.toURI()));
213 }
214 catch (Exception e)
215 {
216 LOG.warn("Bad url ", e);
217 }
218 }
219 }
220 }
221
222 List<Resource> superList = super.findJars(context);
223 if (superList != null)
224 list.addAll(superList);
225 return list;
226 }
227
228
229
230
231
232
233
234
235
236 @Override
237 protected List<Resource> findClassDirs(WebAppContext context) throws Exception
238 {
239 List<Resource> list = new ArrayList<Resource>();
240
241 JettyWebAppContext jwac = (JettyWebAppContext)context;
242 if (jwac.getClassPathFiles() != null)
243 {
244 for (File f: jwac.getClassPathFiles())
245 {
246 if (f.exists() && f.isDirectory())
247 {
248 try
249 {
250 list.add(Resource.newResource(f.toURI()));
251 }
252 catch (Exception e)
253 {
254 LOG.warn("Bad url ", e);
255 }
256 }
257 }
258 }
259
260 List<Resource> classesDirs = super.findClassDirs(context);
261 if (classesDirs != null)
262 list.addAll(classesDirs);
263 return list;
264 }
265
266
267
268
269
270 protected Resource unpackOverlay (WebAppContext context, Overlay overlay)
271 throws IOException
272 {
273 LOG.debug("Unpacking overlay: " + overlay);
274
275 if (overlay.getResource() == null)
276 return null;
277
278
279
280 String name = overlay.getResource().getName();
281 if (name.endsWith("!/"))
282 name = name.substring(0,name.length()-2);
283 int i = name.lastIndexOf('/');
284 if (i>0)
285 name = name.substring(i+1,name.length());
286 name = name.replace('.', '_');
287 name = name+(++COUNTER);
288 File dir = new File(context.getTempDirectory(), name);
289
290
291 File unpackDir = dir;
292 if (overlay.getConfig() != null && overlay.getConfig().getTargetPath() != null)
293 unpackDir = new File (dir, overlay.getConfig().getTargetPath());
294
295 overlay.getResource().copyTo(unpackDir);
296
297 Resource unpackedOverlay = Resource.newResource(dir.getCanonicalPath());
298
299 LOG.debug("Unpacked overlay: "+overlay+" to "+unpackedOverlay);
300 return unpackedOverlay;
301 }
302 }