/
WebAppContext.java
1540 lines (1383 loc) · 51.1 KB
/
WebAppContext.java
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.webapp;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.PermissionCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.ServletSecurityElement;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionIdListener;
import javax.servlet.http.HttpSessionListener;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.security.ConstraintAware;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.server.ClassLoaderDump;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HandlerContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.handler.ManagedAttributeListener;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.AttributesMap;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.TopologicalSort;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.DumpableCollection;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Web Application Context Handler.
* <p>
* The WebAppContext handler is an extension of ContextHandler that
* coordinates the construction and configuration of nested handlers:
* {@link org.eclipse.jetty.security.ConstraintSecurityHandler}, {@link org.eclipse.jetty.server.session.SessionHandler}
* and {@link org.eclipse.jetty.servlet.ServletHandler}.
* The handlers are configured by pluggable configuration classes, with
* the default being {@link org.eclipse.jetty.webapp.WebXmlConfiguration} and
* {@link org.eclipse.jetty.webapp.JettyWebXmlConfiguration}.
*
*
* <p>
* The Start/Configuration of a WebAppContext is rather complex so as to allow
* pluggable behaviour to be added in almost arbitrary ordering. The
* sequence of a WebappContext start is as follows:
* <blockquote>
* {@link #doStart()}:
* <ul>
* <li>{@link #preConfigure()}
* <ul>
* <li>Add all Server class inclusions from all known configurations {@link Configurations#getKnown()}</li>
* <li>{@link #loadConfigurations()}, which uses either explicitly set Configurations or takes the server
* default (which is all known {@link Configuration#isEnabledByDefault()} Configurations.</li>
* <li>Sort the configurations using {@link TopologicalSort} in {@link Configurations#sort()}.</li>
* <li>Add all Server class exclusions from this webapps {@link Configurations}</li>
* <li>Add all System classes inclusions and exclusions for this webapps {@link Configurations}</li>
* <li>Instantiate the WebAppClassLoader (if one not already explicitly set)</li>
* <li>{@link Configuration#preConfigure(WebAppContext)} which calls
* {@link Configuration#preConfigure(WebAppContext)} for this webapps {@link Configurations}</li>
* </ul>
* </li>
* <li>{@link ServletContextHandler#doStart()}
* <ul>
* <li>{@link ContextHandler#doStart()}
* <ul>
* <li>Init {@link MimeTypes}</li>
* <li>enterScope
* <ul>
* <li>{@link #startContext()}
* <ul>
* <li>{@link #configure()}
* <ul>
* <li>Call {@link Configuration#configure(WebAppContext)} on enabled {@link Configurations}</li>
* </ul>
* </li>
* <li>{@link MetaData#resolve(WebAppContext)}</li>
* <li>{@link #startContext()}
* <li>QuickStart may generate here and/or abort start
* <ul>
* <li>{@link ServletContextHandler#startContext}
* <ul>
* <li>Decorate listeners</li>
* <li>{@link ContextHandler#startContext}
* <ul>
* <li>add {@link ManagedAttributeListener}</li>
* <li>{@link AbstractHandler#doStart}</li>
* <li>{@link #callContextInitialized(javax.servlet.ServletContextListener, javax.servlet.ServletContextEvent)}</li>
* </ul>
* </li>
* <li>{@link ServletHandler#initialize()}</li>
* </ul>
* </li>
* </ul>
* </li>
* </ul>
* </li>
* </ul>
* </li>
* <li>exitScope</li>
* </ul>
* </li>
* </ul>
* </li>
* <li>{@link #postConfigure()}</li>
* </ul>
*
* </blockquote>
*/
@ManagedObject("Web Application ContextHandler")
public class WebAppContext extends ServletContextHandler implements WebAppClassLoader.Context
{
static final Logger LOG = LoggerFactory.getLogger(WebAppContext.class);
public static final String TEMPDIR = "javax.servlet.context.tempdir";
public static final String BASETEMPDIR = "org.eclipse.jetty.webapp.basetempdir";
public static final String WEB_DEFAULTS_XML = "org/eclipse/jetty/webapp/webdefault.xml";
public static final String ERROR_PAGE = "org.eclipse.jetty.server.error_page";
public static final String SERVER_SYS_CLASSES = "org.eclipse.jetty.webapp.systemClasses";
public static final String SERVER_SRV_CLASSES = "org.eclipse.jetty.webapp.serverClasses";
private static String[] __dftProtectedTargets = {"/web-inf", "/meta-inf"};
// System classes are classes that cannot be replaced by
// the web application, and they are *always* loaded via
// system classloader.
public static final ClassMatcher __dftSystemClasses = new ClassMatcher(
"java.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"javax.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"org.xml.", // javax.xml
"org.w3c." // javax.xml
);
// Server classes are classes that are hidden from being
// loaded by the web application using system classloader,
// so if web application needs to load any of such classes,
// it has to include them in its distribution.
public static final ClassMatcher __dftServerClasses = new ClassMatcher(
"org.eclipse.jetty." // hide jetty classes
);
private final ClassMatcher _systemClasses = new ClassMatcher(__dftSystemClasses);
private final ClassMatcher _serverClasses = new ClassMatcher(__dftServerClasses);
private Configurations _configurations;
private String _defaultsDescriptor = WEB_DEFAULTS_XML;
private String _descriptor = null;
private final List<String> _overrideDescriptors = new ArrayList<>();
private boolean _distributable = false;
private boolean _extractWAR = true;
private boolean _copyDir = false;
private boolean _copyWebInf = false;
private boolean _logUrlOnStart = false;
private boolean _parentLoaderPriority = Boolean.getBoolean("org.eclipse.jetty.server.webapp.parentLoaderPriority");
private PermissionCollection _permissions;
private String[] _contextWhiteList = null;
private File _tmpDir;
private boolean _persistTmpDir = false;
private String _war;
private List<Resource> _extraClasspath;
private Throwable _unavailableException;
private Map<String, String> _resourceAliases;
private boolean _ownClassLoader = false;
private boolean _configurationDiscovered = true;
private boolean _allowDuplicateFragmentNames = false;
private boolean _throwUnavailableOnStartupException = false;
private MetaData _metadata = new MetaData();
public static WebAppContext getCurrentWebAppContext()
{
ContextHandler.Context context = ContextHandler.getCurrentContext();
if (context != null)
{
ContextHandler handler = context.getContextHandler();
if (handler instanceof WebAppContext)
return (WebAppContext)handler;
}
return null;
}
public WebAppContext()
{
this(null, null, null, null, null, new ErrorPageErrorHandler(), SESSIONS | SECURITY);
}
/**
* @param contextPath The context path
* @param webApp The URL or filename of the webapp directory or war file.
*/
public WebAppContext(String webApp, String contextPath)
{
this(null, contextPath, null, null, null, new ErrorPageErrorHandler(), SESSIONS | SECURITY);
setWar(webApp);
}
/**
* @param contextPath The context path
* @param webApp The URL or filename of the webapp directory or war file.
*/
public WebAppContext(Resource webApp, String contextPath)
{
this(null, contextPath, null, null, null, new ErrorPageErrorHandler(), SESSIONS | SECURITY);
setWarResource(webApp);
}
/**
* @param parent The parent HandlerContainer.
* @param contextPath The context path
* @param webApp The URL or filename of the webapp directory or war file.
*/
public WebAppContext(HandlerContainer parent, String webApp, String contextPath)
{
this(parent, contextPath, null, null, null, new ErrorPageErrorHandler(), SESSIONS | SECURITY);
setWar(webApp);
}
/**
* @param parent The parent HandlerContainer.
* @param contextPath The context path
* @param webApp The webapp directory or war file.
*/
public WebAppContext(HandlerContainer parent, Resource webApp, String contextPath)
{
this(parent, contextPath, null, null, null, new ErrorPageErrorHandler(), SESSIONS | SECURITY);
setWarResource(webApp);
}
/**
* This constructor is used in the geronimo integration.
*
* @param sessionHandler SessionHandler for this web app
* @param securityHandler SecurityHandler for this web app
* @param servletHandler ServletHandler for this web app
* @param errorHandler ErrorHandler for this web app
*/
public WebAppContext(SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
{
this(null, null, sessionHandler, securityHandler, servletHandler, errorHandler, 0);
}
/**
* This constructor is used in the geronimo integration.
*
* @param parent the parent handler
* @param contextPath the context path
* @param sessionHandler SessionHandler for this web app
* @param securityHandler SecurityHandler for this web app
* @param servletHandler ServletHandler for this web app
* @param errorHandler ErrorHandler for this web app
* @param options the options ({@link ServletContextHandler#SESSIONS} and/or {@link ServletContextHandler#SECURITY})
*/
public WebAppContext(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler, int options)
{
// always pass parent as null and then set below, so that any resulting setServer call
// is done after this instance is constructed.
super(null, contextPath, sessionHandler, securityHandler, servletHandler, errorHandler, options);
_scontext = new Context();
setErrorHandler(errorHandler != null ? errorHandler : new ErrorPageErrorHandler());
setProtectedTargets(__dftProtectedTargets);
if (parent != null)
setParent(parent);
}
/**
* @param servletContextName The servletContextName to set.
*/
@Override
public void setDisplayName(String servletContextName)
{
super.setDisplayName(servletContextName);
ClassLoader cl = getClassLoader();
if (cl instanceof WebAppClassLoader && servletContextName != null)
((WebAppClassLoader)cl).setName(servletContextName);
}
/**
* Get an exception that caused the webapp to be unavailable
*
* @return A throwable if the webapp is unavailable or null
*/
public Throwable getUnavailableException()
{
return _unavailableException;
}
/**
* Set Resource Alias.
* Resource aliases map resource uri's within a context.
* They may optionally be used by a handler when looking for
* a resource.
*
* @param alias the alias for a resource
* @param uri the uri for the resource
*/
public void setResourceAlias(String alias, String uri)
{
if (_resourceAliases == null)
_resourceAliases = new HashMap<>(5);
_resourceAliases.put(alias, uri);
}
public Map<String, String> getResourceAliases()
{
if (_resourceAliases == null)
return null;
return _resourceAliases;
}
public void setResourceAliases(Map<String, String> map)
{
_resourceAliases = map;
}
public String getResourceAlias(String path)
{
if (_resourceAliases == null)
return null;
String alias = _resourceAliases.get(path);
int slash = path.length();
while (alias == null)
{
slash = path.lastIndexOf("/", slash - 1);
if (slash < 0)
break;
String match = _resourceAliases.get(path.substring(0, slash + 1));
if (match != null)
alias = match + path.substring(slash + 1);
}
return alias;
}
public String removeResourceAlias(String alias)
{
if (_resourceAliases == null)
return null;
return _resourceAliases.remove(alias);
}
@Override
public void setClassLoader(ClassLoader classLoader)
{
super.setClassLoader(classLoader);
String name = getDisplayName();
if (name == null)
name = getContextPath();
if (classLoader instanceof WebAppClassLoader && getDisplayName() != null)
((WebAppClassLoader)classLoader).setName(name);
}
@Override
public Resource getResource(String uriInContext) throws MalformedURLException
{
if (uriInContext == null || !uriInContext.startsWith(URIUtil.SLASH))
throw new MalformedURLException(uriInContext);
IOException ioe = null;
Resource resource = null;
int loop = 0;
while (uriInContext != null && loop++ < 100)
{
try
{
resource = super.getResource(uriInContext);
if (resource != null && resource.exists())
return resource;
uriInContext = getResourceAlias(uriInContext);
}
catch (IOException e)
{
LOG.trace("IGNORED", e);
if (ioe == null)
ioe = e;
}
}
if (ioe instanceof MalformedURLException)
throw (MalformedURLException)ioe;
return resource;
}
/**
* Is the context Automatically configured.
*
* @return true if configuration discovery.
*/
public boolean isConfigurationDiscovered()
{
return _configurationDiscovered;
}
/**
* Set the configuration discovery mode.
* If configuration discovery is set to true, then the JSR315
* servlet 3.0 discovered configuration features are enabled.
* These are:<ul>
* <li>Web Fragments</li>
* <li>META-INF/resource directories</li>
* </ul>
*
* @param discovered true if configuration discovery is enabled for automatic configuration from the context
*/
public void setConfigurationDiscovered(boolean discovered)
{
_configurationDiscovered = discovered;
}
/**
* Pre configure the web application.
* <p>
* The method is normally called from {@link #start()}. It performs
* the discovery of the configurations to be applied to this context,
* specifically:
* <ul>
* <li>Instantiate the {@link Configuration} instances with a call to {@link #loadConfigurations()}.
* <li>Instantiates a classloader (if one is not already set)
* <li>Calls the {@link Configuration#preConfigure(WebAppContext)} method of all
* Configuration instances.
* </ul>
*
* @throws Exception if unable to pre configure
*/
public void preConfigure() throws Exception
{
// Add the known server class inclusions for all known configurations
for (Configuration configuration : Configurations.getKnown())
{
_serverClasses.include(configuration.getServerClasses().getInclusions());
}
// Setup Configuration classes for this webapp!
loadConfigurations();
_configurations.sort();
for (Configuration configuration : _configurations)
{
_systemClasses.add(configuration.getSystemClasses().getPatterns());
_serverClasses.exclude(configuration.getServerClasses().getExclusions());
}
// Configure classloader
_ownClassLoader = false;
if (getClassLoader() == null)
{
WebAppClassLoader classLoader = new WebAppClassLoader(this);
setClassLoader(classLoader);
_ownClassLoader = true;
}
if (LOG.isDebugEnabled())
{
ClassLoader loader = getClassLoader();
LOG.debug("Thread Context classloader {}", loader);
loader = loader.getParent();
while (loader != null)
{
LOG.debug("Parent class loader: {} ", loader);
loader = loader.getParent();
}
}
_configurations.preConfigure(this);
}
public boolean configure() throws Exception
{
return _configurations.configure(this);
}
public void postConfigure() throws Exception
{
_configurations.postConfigure(this);
}
@Override
protected void doStart() throws Exception
{
try
{
_metadata.setAllowDuplicateFragmentNames(isAllowDuplicateFragmentNames());
Boolean validate = (Boolean)getAttribute(MetaData.VALIDATE_XML);
_metadata.setValidateXml((validate != null && validate));
wrapConfigurations();
preConfigure();
super.doStart();
postConfigure();
if (isLogUrlOnStart())
dumpUrl();
}
catch (Throwable t)
{
// start up of the webapp context failed, make sure it is not started
LOG.warn("Failed startup of context {}", this, t);
_unavailableException = t;
setAvailable(false); // webapp cannot be accessed (results in status code 503)
if (isThrowUnavailableOnStartupException())
throw t;
}
}
private void wrapConfigurations()
{
Collection<Configuration.WrapperFunction> wrappers = getBeans(Configuration.WrapperFunction.class);
if (wrappers == null || wrappers.isEmpty())
return;
List<Configuration> configs = new ArrayList<>(_configurations.getConfigurations());
_configurations.clear();
for (Configuration config : configs)
{
Configuration wrapped = config;
for (Configuration.WrapperFunction wrapperFunction : getBeans(Configuration.WrapperFunction.class))
{
wrapped = wrapperFunction.wrapConfiguration(wrapped);
}
_configurations.add(wrapped);
}
}
@Override
public void destroy()
{
// Prepare for configuration
MultiException mx = new MultiException();
if (_configurations != null)
{
for (Configuration configuration : _configurations)
{
try
{
configuration.destroy(this);
}
catch (Exception e)
{
mx.add(e);
}
}
}
_configurations = null;
super.destroy();
mx.ifExceptionThrowRuntime();
}
/*
* Dumps the current web app name and URL to the log
*/
private void dumpUrl()
{
Connector[] connectors = getServer().getConnectors();
for (int i = 0; i < connectors.length; i++)
{
String displayName = getDisplayName();
if (displayName == null)
displayName = "WebApp@" + Arrays.hashCode(connectors);
LOG.info("{} at http://{}{}", displayName, connectors[i].toString(), getContextPath());
}
}
/**
* @return Returns the configurations.
*/
@ManagedAttribute(value = "configuration classes used to configure webapp", readonly = true)
public String[] getConfigurationClasses()
{
loadConfigurations();
return _configurations.toArray();
}
/**
* @return Returns the configurations.
*/
public Configurations getConfigurations()
{
loadConfigurations();
return _configurations;
}
/**
* The default descriptor is a web.xml format file that is applied to the context before the standard WEB-INF/web.xml
*
* @return Returns the defaultsDescriptor.
*/
@ManagedAttribute(value = "default web.xml deascriptor applied before standard web.xml", readonly = true)
public String getDefaultsDescriptor()
{
return _defaultsDescriptor;
}
/**
* The override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
*
* @return Returns the Override Descriptor.
*/
public String getOverrideDescriptor()
{
if (_overrideDescriptors.size() != 1)
return null;
return _overrideDescriptors.get(0);
}
/**
* An override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
*
* @return Returns the Override Descriptor list
*/
@ManagedAttribute(value = "web.xml deascriptors applied after standard web.xml", readonly = true)
public List<String> getOverrideDescriptors()
{
return Collections.unmodifiableList(_overrideDescriptors);
}
/**
* @return Returns the permissions.
*/
@Override
public PermissionCollection getPermissions()
{
return _permissions;
}
/**
* Set the server classes patterns.
* <p>
* Server classes/packages are classes used to implement the server and are hidden
* from the context. If the context needs to load these classes, it must have its
* own copy of them in WEB-INF/lib or WEB-INF/classes.
*
* @param serverClasses the server classes pattern
*/
public void setServerClassMatcher(ClassMatcher serverClasses)
{
_serverClasses.clear();
_serverClasses.add(serverClasses.getPatterns());
}
/**
* Set the system classes patterns.
* <p>
* System classes/packages are classes provided by the JVM and that
* cannot be replaced by classes of the same name from WEB-INF,
* regardless of the value of {@link #setParentLoaderPriority(boolean)}.
*
* @param systemClasses the system classes pattern
*/
public void setSystemClassMatcher(ClassMatcher systemClasses)
{
_systemClasses.clear();
_systemClasses.add(systemClasses.getPatterns());
}
/**
* Add a ClassMatcher for server classes by combining with
* any existing matcher.
*
* @param serverClasses The class matcher of patterns to add to the server ClassMatcher
*/
public void addServerClassMatcher(ClassMatcher serverClasses)
{
_serverClasses.add(serverClasses.getPatterns());
}
/**
* Add a ClassMatcher for system classes by combining with
* any existing matcher.
*
* @param systemClasses The class matcher of patterns to add to the system ClassMatcher
*/
public void addSystemClassMatcher(ClassMatcher systemClasses)
{
_systemClasses.add(systemClasses.getPatterns());
}
/**
* @return The ClassMatcher used to match System (protected) classes
*/
public ClassMatcher getSystemClassMatcher()
{
return _systemClasses;
}
/**
* @return The ClassMatcher used to match Server (hidden) classes
*/
public ClassMatcher getServerClassMatcher()
{
return _serverClasses;
}
@ManagedAttribute(value = "classes and packages protected by context classloader", readonly = true)
public String[] getSystemClasses()
{
return _systemClasses.getPatterns();
}
@ManagedAttribute(value = "classes and packages hidden by the context classloader", readonly = true)
public String[] getServerClasses()
{
return _serverClasses.getPatterns();
}
@Override
public boolean isServerClass(Class<?> clazz)
{
boolean result = _serverClasses.match(clazz);
if (LOG.isDebugEnabled())
LOG.debug("isServerClass=={} {}", result, clazz);
return result;
}
@Override
public boolean isSystemClass(Class<?> clazz)
{
boolean result = _systemClasses.match(clazz);
if (LOG.isDebugEnabled())
LOG.debug("isSystemClass=={} {}", result, clazz);
return result;
}
@Override
public boolean isServerResource(String name, URL url)
{
boolean result = _serverClasses.match(name, url);
if (LOG.isDebugEnabled())
LOG.debug("isServerResource=={} {} {}", result, name, url);
return result;
}
@Override
public boolean isSystemResource(String name, URL url)
{
boolean result = _systemClasses.match(name, url);
if (LOG.isDebugEnabled())
LOG.debug("isSystemResource=={} {} {}", result, name, url);
return result;
}
@Override
public void setServer(Server server)
{
super.setServer(server);
if (server != null)
{
if (__dftSystemClasses.equals(_systemClasses))
{
Object systemClasses = server.getAttribute(SERVER_SYS_CLASSES);
if (systemClasses instanceof String[])
systemClasses = new ClassMatcher((String[])systemClasses);
if (systemClasses instanceof ClassMatcher)
_systemClasses.add(((ClassMatcher)systemClasses).getPatterns());
}
if (__dftServerClasses.equals(_serverClasses))
{
Object serverClasses = server.getAttribute(SERVER_SRV_CLASSES);
if (serverClasses instanceof String[])
serverClasses = new ClassMatcher((String[])serverClasses);
if (serverClasses instanceof ClassMatcher)
_serverClasses.add(((ClassMatcher)serverClasses).getPatterns());
}
}
}
/**
* @return Returns the war as a file or URL string (Resource).
* The war may be different to the @link {@link #getResourceBase()}
* if the war has been expanded and/or copied.
*/
@ManagedAttribute(value = "war file location", readonly = true)
public String getWar()
{
if (_war == null)
_war = getResourceBase();
return _war;
}
public Resource getWebInf() throws IOException
{
if (super.getBaseResource() == null)
return null;
// Iw there a WEB-INF directory?
Resource webInf = super.getBaseResource().addPath("WEB-INF/");
if (!webInf.exists() || !webInf.isDirectory())
return null;
return webInf;
}
/**
* @return Returns the distributable.
*/
@ManagedAttribute("web application distributable")
public boolean isDistributable()
{
return _distributable;
}
/**
* @return Returns the extractWAR.
*/
@ManagedAttribute(value = "extract war", readonly = true)
public boolean isExtractWAR()
{
return _extractWAR;
}
/**
* @return True if the webdir is copied (to allow hot replacement of jars on windows)
*/
@ManagedAttribute(value = "webdir copied on deploy (allows hot replacement on windows)", readonly = true)
public boolean isCopyWebDir()
{
return _copyDir;
}
/**
* @return True if the web-inf lib and classes directories are copied (to allow hot replacement of jars on windows)
*/
public boolean isCopyWebInf()
{
return _copyWebInf;
}
/**
* @return True if the classloader should delegate first to the parent
* classloader (standard java behaviour) or false if the classloader
* should first try to load from WEB-INF/lib or WEB-INF/classes (servlet
* spec recommendation). Default is false or can be set by the system
* property org.eclipse.jetty.server.webapp.parentLoaderPriority
*/
@Override
@ManagedAttribute(value = "parent classloader given priority", readonly = true)
public boolean isParentLoaderPriority()
{
return _parentLoaderPriority;
}
protected void loadConfigurations()
{
//if the configuration instances have been set explicitly, use them
if (_configurations != null)
return;
if (isStarted())
throw new IllegalStateException();
_configurations = newConfigurations();
}
protected Configurations newConfigurations()
{
Configurations configurations = new Configurations();
configurations.add(Configurations.getServerDefault(getServer()).toArray());
return configurations;
}
@Override
public String toString()
{
if (_war != null)
return super.toString() + "{" + _war + "}";
return super.toString();
}
@Override
public void dump(Appendable out, String indent) throws IOException
{
List<String> systemClasses = null;
if (_systemClasses != null)
{
systemClasses = new ArrayList<>(_systemClasses);
Collections.sort(systemClasses);
}
List<String> serverClasses = null;
if (_serverClasses != null)
{
serverClasses = new ArrayList<>(_serverClasses);
Collections.sort(serverClasses);
}
String name = getDisplayName();
if (name == null)
{
if (_war != null)
{
int webapps = _war.indexOf("/webapps/");
if (webapps >= 0)
name = _war.substring(webapps + 8);
else
name = _war;
}
else if (getResourceBase() != null)
{
name = getResourceBase();
int webapps = name.indexOf("/webapps/");
if (webapps >= 0)
name = name.substring(webapps + 8);
}
else
{
name = this.getClass().getSimpleName();
}
}
name = String.format("%s@%x", name, hashCode());
dumpObjects(out, indent,
new ClassLoaderDump(getClassLoader()),
new DumpableCollection("Systemclasses " + name, systemClasses),
new DumpableCollection("Serverclasses " + name, serverClasses),
new DumpableCollection("Configurations " + name, _configurations),
new DumpableCollection("Handler attributes " + name, ((AttributesMap)getAttributes()).getAttributeEntrySet()),
new DumpableCollection("Context attributes " + name, getServletContext().getAttributeEntrySet()),
new DumpableCollection("EventListeners " + this, getEventListeners()),
new DumpableCollection("Initparams " + name, getInitParams().entrySet())
);
}
/**
* @param configurations The configuration class names. If setConfigurations is not called
* these classes are used to create a configurations array.
*/
public void setConfigurationClasses(String[] configurations)
{
if (_configurations == null)
_configurations = new Configurations();
_configurations.set(configurations);
}
public void setConfigurationClasses(List<String> configurations)
{
setConfigurationClasses(configurations.toArray(new String[0]));
}
/**
* @param configurations The configurations to set.
*/
public void setConfigurations(Configuration[] configurations)
{
if (_configurations == null)
_configurations = new Configurations();
_configurations.set(configurations);
}
public void addConfiguration(Configuration... configuration)
{