Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b4b52441dfa1eafa693f6c5e52c4b3cd7e17f4e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.maven.plugin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.quickstart.PreconfigureDescriptorProcessor;
import org.eclipse.jetty.quickstart.QuickStartDescriptorGenerator;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.JettyWebXmlConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

/**
 * JettyWebAppContext
 * 
 * Extends the WebAppContext to specialize for the maven environment.
 * We pass in the list of files that should form the classpath for
 * the webapp when executing in the plugin, and any jetty-env.xml file
 * that may have been configured.
 *
 */
public class JettyWebAppContext extends WebAppContext
{
    private static final Logger LOG = Log.getLogger(JettyWebAppContext.class);
    
   

    private static final String DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN = ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$|.*javax.servlet.jsp.jstl-[^/]*\\.jar|.*taglibs-standard-impl-.*\\.jar";
    private static final String WEB_INF_CLASSES_PREFIX = "/WEB-INF/classes";
    private static final String WEB_INF_LIB_PREFIX = "/WEB-INF/lib";

    private final Configuration[] _defaultConfigurations = {
                                                             new MavenWebInfConfiguration(),
                                                             new WebXmlConfiguration(),
                                                             new MetaInfConfiguration(),
                                                             new FragmentConfiguration(),
                                                             new EnvConfiguration(),
                                                             new PlusConfiguration(),
                                                             new AnnotationConfiguration(),
                                                             new JettyWebXmlConfiguration()
                                                            };

    private final Configuration[] _quickStartConfigurations = {
                                                                new MavenQuickStartConfiguration(),
                                                                new EnvConfiguration(),
                                                                new PlusConfiguration(),
                                                                new JettyWebXmlConfiguration()
                                                               };

    private File _classes = null;
    private File _testClasses = null;
    private final List<File> _webInfClasses = new ArrayList<File>();
    private final List<File> _webInfJars = new ArrayList<File>();
    private final Map<String, File> _webInfJarMap = new HashMap<String, File>();
    private List<File> _classpathFiles;  //webInfClasses+testClasses+webInfJars
    private String _jettyEnvXml;
    private List<Overlay> _overlays;
    private Resource _quickStartWebXml;
    
 
    
    /**
     * Set the "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern" with a pattern for matching jars on
     * container classpath to scan. This is analogous to the WebAppContext.setAttribute() call.
     */
    private String _containerIncludeJarPattern = null;
    
    /**
     * Set the "org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern" with a pattern for matching jars on
     * webapp's classpath to scan. This is analogous to the WebAppContext.setAttribute() call.
     */
    private String _webInfIncludeJarPattern = null;
  

    
    /**
     * If there is no maven-war-plugin config for ordering of the current project in the
     * sequence of overlays, use this to control whether the current project is added 
     * first or last in list of overlaid resources
     */
    private boolean _baseAppFirst = true;



    private boolean _isGenerateQuickStart;
    private PreconfigureDescriptorProcessor _preconfigProcessor;
   

  
    /* ------------------------------------------------------------ */
    public JettyWebAppContext ()
    throws Exception
    {
        super();   
        // Turn off copyWebInf option as it is not applicable for plugin.
        super.setCopyWebInf(false);
    }
    
    /* ------------------------------------------------------------ */
    public void setContainerIncludeJarPattern(String pattern)
    {
        _containerIncludeJarPattern = pattern;
    }
    
    /* ------------------------------------------------------------ */
    public String getContainerIncludeJarPattern()
    {
        return _containerIncludeJarPattern;
    }
    
    /* ------------------------------------------------------------ */
    public String getWebInfIncludeJarPattern()
    {
        return _webInfIncludeJarPattern;
    }
    
    /* ------------------------------------------------------------ */
    public void setWebInfIncludeJarPattern(String pattern)
    {
        _webInfIncludeJarPattern = pattern;
    }
   
    /* ------------------------------------------------------------ */
    public List<File> getClassPathFiles()
    {
        return this._classpathFiles;
    }
    
    /* ------------------------------------------------------------ */
    public void setJettyEnvXml (String jettyEnvXml)
    {
        this._jettyEnvXml = jettyEnvXml;
    }
    
    /* ------------------------------------------------------------ */
    public String getJettyEnvXml()
    {
        return this._jettyEnvXml;
    }

    /* ------------------------------------------------------------ */
    public void setClasses(File dir)
    {
        _classes = dir;
    }
    
    /* ------------------------------------------------------------ */
    public File getClasses()
    {
        return _classes;
    }
    
    /* ------------------------------------------------------------ */
    public void setWebInfLib (List<File> jars)
    {
        _webInfJars.addAll(jars);
    }
    
    /* ------------------------------------------------------------ */
    public void setTestClasses (File dir)
    {
        _testClasses = dir;
    }
    
    /* ------------------------------------------------------------ */
    public File getTestClasses ()
    {
        return _testClasses;
    }
    
    
    /* ------------------------------------------------------------ */
    /**
     * Ordered list of wars to overlay on top of the current project. The list
     * may contain an overlay that represents the current project.
     * @param overlays the list of overlays
     */
    public void setOverlays (List<Overlay> overlays)
    {
        _overlays = overlays;
    }
    
    /* ------------------------------------------------------------ */
    public List<Overlay> getOverlays()
    {
        return _overlays;
    }

    /* ------------------------------------------------------------ */
    public void setBaseAppFirst(boolean value)
    {
        _baseAppFirst = value;
    }

    /* ------------------------------------------------------------ */
    public boolean getBaseAppFirst()
    {
        return _baseAppFirst;
    }
    
    /* ------------------------------------------------------------ */
    public void setQuickStartWebDescriptor (Resource quickStartWebXml)
    {
        _quickStartWebXml = quickStartWebXml;
    }
    
    /* ------------------------------------------------------------ */
    public Resource getQuickStartWebDescriptor ()
    {
        return _quickStartWebXml;
    }
    
    /* ------------------------------------------------------------ */
    /**
     * This method is provided as a convenience for jetty maven plugin configuration 
     * @param resourceBases Array of resources strings to set as a {@link ResourceCollection}. Each resource string may be a comma separated list of resources
     * @see Resource
     */
    public void setResourceBases(String[] resourceBases)
    {
        List<String> resources = new ArrayList<String>();
        for (String rl:resourceBases)
        {
            String[] rs = StringUtil.csvSplit(rl);
            for (String r:rs)
                resources.add(r);
        }
        
        setBaseResource(new ResourceCollection(resources.toArray(new String[resources.size()])));
    }
    
    /* ------------------------------------------------------------ */
    public List<File> getWebInfLib()
    {
        return _webInfJars;
    }
    
    /* ------------------------------------------------------------ */
    public void setGenerateQuickStart (boolean quickStart)
    {
        _isGenerateQuickStart = quickStart;
    }
    
    /* ------------------------------------------------------------ */
    public boolean isGenerateQuickStart()
    {
        return _isGenerateQuickStart;
    }
    
   
    
    /* ------------------------------------------------------------ */
    @Override
    protected void startWebapp() throws Exception
    {
        if (isGenerateQuickStart())
        {
            if (getQuickStartWebDescriptor() == null)
                throw new IllegalStateException ("No location to generate quickstart descriptor");

            QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, _preconfigProcessor.getXML());
            try (FileOutputStream fos = new FileOutputStream(getQuickStartWebDescriptor().getFile()))
            {
                generator.generateQuickStartWebXml(fos);
            }
        }
        else
        {
            if (LOG.isDebugEnabled()) { LOG.debug("Calling full start on webapp");}
            super.startWebapp();
        }
    }
    
    /* ------------------------------------------------------------ */
    @Override
    protected void stopWebapp() throws Exception
    {
        if (isGenerateQuickStart())
            return;

        if (LOG.isDebugEnabled()) { LOG.debug("Calling stop of fully started webapp");}
        super.stopWebapp();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void doStart () throws Exception
    {
        //choose if this will be a quickstart or normal start
        if (!isGenerateQuickStart() && getQuickStartWebDescriptor() != null)
            setConfigurations(_quickStartConfigurations);
        else
        {
            setConfigurations(_defaultConfigurations);
            if (isGenerateQuickStart())
            {
                _preconfigProcessor = new PreconfigureDescriptorProcessor();
                getMetaData().addDescriptorProcessor(_preconfigProcessor);
            }
        }
        
        //inject configurations with config from maven plugin    
        for (Configuration c:getConfigurations())
        {
            if (c instanceof EnvConfiguration && getJettyEnvXml() != null)
                ((EnvConfiguration)c).setJettyEnvXml(Resource.toURL(new File(getJettyEnvXml())));
            else if (c instanceof MavenQuickStartConfiguration && getQuickStartWebDescriptor() != null)
                ((MavenQuickStartConfiguration)c).setQuickStartWebXml(getQuickStartWebDescriptor());         
        }

        //Set up the pattern that tells us where the jars are that need scanning

        //Allow user to set up pattern for names of jars from the container classpath
        //that will be scanned - note that by default NO jars are scanned
        String tmp = _containerIncludeJarPattern;
        if (tmp==null || "".equals(tmp))
            tmp = (String)getAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN);           
        
        tmp = addPattern(tmp, DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN);
        setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, tmp);
        
        //Allow user to set up pattern of jar names from WEB-INF that will be scanned.
        //Note that by default ALL jars considered to be in WEB-INF will be scanned - setting
        //a pattern restricts scanning
        if (_webInfIncludeJarPattern != null)
            setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, _webInfIncludeJarPattern);
   
        //Set up the classes dirs that comprises the equivalent of WEB-INF/classes
        if (_testClasses != null)
            _webInfClasses.add(_testClasses);
        if (_classes != null)
            _webInfClasses.add(_classes);
        
        // Set up the classpath
        _classpathFiles = new ArrayList<File>();
        _classpathFiles.addAll(_webInfClasses);
        _classpathFiles.addAll(_webInfJars);

        // Initialize map containing all jars in /WEB-INF/lib
        _webInfJarMap.clear();
        for (File file : _webInfJars)
        {
            // Return all jar files from class path
            String fileName = file.getName();
            if (fileName.endsWith(".jar"))
                _webInfJarMap.put(fileName, file);
        }
        
        //check for CDI
        initCDI();
        
        // CHECK setShutdown(false);
        super.doStart();
    }
    
    
    /* ------------------------------------------------------------ */
    public void doStop () throws Exception
    { 
        if (_classpathFiles != null)
            _classpathFiles.clear();
        _classpathFiles = null;
        
        _classes = null;
        _testClasses = null;
        
        if (_webInfJarMap != null)
            _webInfJarMap.clear();
       
        _webInfClasses.clear();
        _webInfJars.clear();
        
        
        
        // CHECK setShutdown(true);
        //just wait a little while to ensure no requests are still being processed
        Thread.currentThread().sleep(500L);

        super.doStop();

        //remove all listeners, servlets and filters. This is because we will re-apply
        //any context xml file, which means they would potentially be added multiple times.
        setEventListeners(new EventListener[0]);
        getServletHandler().setFilters(new FilterHolder[0]);
        getServletHandler().setFilterMappings(new FilterMapping[0]);
        getServletHandler().setServlets(new ServletHolder[0]);
        getServletHandler().setServletMappings(new ServletMapping[0]);
    }
    
    
    /* ------------------------------------------------------------ */
    @Override
    public Resource getResource(String uriInContext) throws MalformedURLException
    {
        Resource resource = null;
        // Try to get regular resource
        resource = super.getResource(uriInContext);

        // If no regular resource exists check for access to /WEB-INF/lib or /WEB-INF/classes
        if ((resource == null || !resource.exists()) && uriInContext != null && _classes != null)
        {
            String uri = URIUtil.canonicalPath(uriInContext);
            if (uri == null)
                return null;

            try
            {
                // Replace /WEB-INF/classes with candidates for the classpath
                if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
                {
                    if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX+"/"))
                    {
                        //exact match for a WEB-INF/classes, so preferentially return the resource matching the web-inf classes
                        //rather than the test classes
                        if (_classes != null)
                            return Resource.newResource(_classes);
                        else if (_testClasses != null)
                            return Resource.newResource(_testClasses);
                    }
                    else
                    {
                        //try matching                       
                        Resource res = null;
                        int i=0;
                        while (res == null && (i < _webInfClasses.size()))
                        {
                            String newPath = uri.replace(WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
                            res = Resource.newResource(newPath);
                            if (!res.exists())
                            {
                                res = null; 
                                i++;
                            }
                        }
                        return res;
                    }
                }       
                else if (uri.startsWith(WEB_INF_LIB_PREFIX))
                {
                    // Return the real jar file for all accesses to
                    // /WEB-INF/lib/*.jar
                    String jarName = uri.replace(WEB_INF_LIB_PREFIX, "");
                    if (jarName.startsWith("/") || jarName.startsWith("\\")) 
                        jarName = jarName.substring(1);
                    if (jarName.length()==0) 
                        return null;
                    File jarFile = _webInfJarMap.get(jarName);
                    if (jarFile != null)
                        return Resource.newResource(jarFile.getPath());

                    return null;
                }
            }
            catch (MalformedURLException e)
            {
                throw e;
            }
            catch (IOException e)
            {
                LOG.ignore(e);
            }
        }
        return resource;
    }
    
    
    /* ------------------------------------------------------------ */
    @Override
    public Set<String> getResourcePaths(String path)
    {
        // Try to get regular resource paths - this will get appropriate paths from any overlaid wars etc
        Set<String> paths = super.getResourcePaths(path);
        
        if (path != null)
        {
            TreeSet<String> allPaths = new TreeSet<String>();
            allPaths.addAll(paths);
            
            //add in the dependency jars as a virtual WEB-INF/lib entry
            if (path.startsWith(WEB_INF_LIB_PREFIX))
            {
                for (String fileName : _webInfJarMap.keySet())
                {
                    // Return all jar files from class path
                    allPaths.add(WEB_INF_LIB_PREFIX + "/" + fileName);
                }
            }
            else if (path.startsWith(WEB_INF_CLASSES_PREFIX))
            {
                int i=0;
               
                while (i < _webInfClasses.size())
                {
                    String newPath = path.replace(WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
                    allPaths.addAll(super.getResourcePaths(newPath));
                    i++;
                }
            }
            return allPaths;
        }
        return paths;
    }
    
    /* ------------------------------------------------------------ */
    public String addPattern (String s, String pattern)
    {
        if (s == null)
            s = "";
        else
            s = s.trim();
        
        if (!s.contains(pattern))
        {
            if (s.length() != 0)
                s = s + "|";
            s = s + pattern;
        }
        
        return s;
    }
    
    
    /* ------------------------------------------------------------ */
    public void initCDI()
    {
        Class cdiInitializer = null;
        try
        {
            cdiInitializer = Thread.currentThread().getContextClassLoader().loadClass("org.eclipse.jetty.cdi.servlet.JettyWeldInitializer");
            Method initWebAppMethod = cdiInitializer.getMethod("initWebApp", new Class[]{WebAppContext.class});
            initWebAppMethod.invoke(null, new Object[]{this});
        }
        catch (ClassNotFoundException e)
        {
            LOG.debug("o.e.j.cdi.servlet.JettyWeldInitializer not found, no cdi integration available");
        }
        catch (NoSuchMethodException e)
        {
            LOG.warn("o.e.j.cdi.servlet.JettyWeldInitializer.initWebApp() not found, no cdi integration available");
        }
        catch (Exception e)
        {
           LOG.warn("Problem initializing cdi", e);
        }
    }
}

Back to the top