forked from apache/wicket
/
Application.java
1701 lines (1518 loc) · 48.5 KB
/
Application.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.wicket.application.ComponentInitializationListenerCollection;
import org.apache.wicket.application.ComponentInstantiationListenerCollection;
import org.apache.wicket.application.ComponentOnAfterRenderListenerCollection;
import org.apache.wicket.application.ComponentOnBeforeRenderListenerCollection;
import org.apache.wicket.application.HeaderContributorListenerCollection;
import org.apache.wicket.application.IComponentInitializationListener;
import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.event.IEventSink;
import org.apache.wicket.javascript.DefaultJavaScriptCompressor;
import org.apache.wicket.markup.MarkupFactory;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.IHeaderResponseDecorator;
import org.apache.wicket.markup.html.image.resource.DefaultButtonImageResourceFactory;
import org.apache.wicket.markup.parser.filter.EnclosureHandler;
import org.apache.wicket.markup.parser.filter.InlineEnclosureHandler;
import org.apache.wicket.markup.parser.filter.RelativePathPrefixHandler;
import org.apache.wicket.markup.parser.filter.WicketLinkTagHandler;
import org.apache.wicket.markup.parser.filter.WicketMessageTagHandler;
import org.apache.wicket.markup.resolver.FragmentResolver;
import org.apache.wicket.markup.resolver.HtmlHeaderResolver;
import org.apache.wicket.markup.resolver.MarkupInheritanceResolver;
import org.apache.wicket.markup.resolver.WicketContainerResolver;
import org.apache.wicket.markup.resolver.WicketMessageResolver;
import org.apache.wicket.page.DefaultPageManagerContext;
import org.apache.wicket.page.IPageManager;
import org.apache.wicket.page.IPageManagerContext;
import org.apache.wicket.pageStore.IDataStore;
import org.apache.wicket.pageStore.IPageStore;
import org.apache.wicket.protocol.http.IRequestLogger;
import org.apache.wicket.protocol.http.RequestLogger;
import org.apache.wicket.protocol.http.RequestLoggerRequestCycleListener;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.IExceptionMapper;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.IRequestMapper;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.request.cycle.AbstractRequestCycleListener;
import org.apache.wicket.request.cycle.IRequestCycleListener;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.cycle.RequestCycleContext;
import org.apache.wicket.request.cycle.RequestCycleListenerCollection;
import org.apache.wicket.request.mapper.CompoundRequestMapper;
import org.apache.wicket.request.mapper.ICompoundRequestMapper;
import org.apache.wicket.request.mapper.IMapperContext;
import org.apache.wicket.request.resource.ResourceReferenceRegistry;
import org.apache.wicket.response.filter.EmptySrcAttributeCheckFilter;
import org.apache.wicket.session.DefaultPageFactory;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.session.ISessionStore.UnboundListener;
import org.apache.wicket.settings.IAjaxSettings;
import org.apache.wicket.settings.IApplicationSettings;
import org.apache.wicket.settings.IDebugSettings;
import org.apache.wicket.settings.IExceptionSettings;
import org.apache.wicket.settings.IFrameworkSettings;
import org.apache.wicket.settings.IMarkupSettings;
import org.apache.wicket.settings.IPageSettings;
import org.apache.wicket.settings.IRequestCycleSettings;
import org.apache.wicket.settings.IRequestLoggerSettings;
import org.apache.wicket.settings.IResourceSettings;
import org.apache.wicket.settings.ISecuritySettings;
import org.apache.wicket.settings.ISessionSettings;
import org.apache.wicket.settings.IStoreSettings;
import org.apache.wicket.settings.def.AjaxSettings;
import org.apache.wicket.settings.def.ApplicationSettings;
import org.apache.wicket.settings.def.DebugSettings;
import org.apache.wicket.settings.def.ExceptionSettings;
import org.apache.wicket.settings.def.FrameworkSettings;
import org.apache.wicket.settings.def.MarkupSettings;
import org.apache.wicket.settings.def.PageSettings;
import org.apache.wicket.settings.def.RequestCycleSettings;
import org.apache.wicket.settings.def.RequestLoggerSettings;
import org.apache.wicket.settings.def.ResourceSettings;
import org.apache.wicket.settings.def.SecuritySettings;
import org.apache.wicket.settings.def.SessionSettings;
import org.apache.wicket.settings.def.StoreSettings;
import org.apache.wicket.util.IProvider;
import org.apache.wicket.util.io.IOUtils;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Generics;
import org.apache.wicket.util.lang.PropertyResolver;
import org.apache.wicket.util.lang.WicketObjects;
import org.apache.wicket.util.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Base class for all Wicket applications. To create a Wicket application, you generally should
* <i>not </i> directly subclass this class. Instead, you will want to subclass some subclass of
* Application, like WebApplication, which is appropriate for the protocol and markup type you are
* working with.
* <p>
* Application has the following interesting features / attributes:
* <ul>
* <li><b>Name </b>- The Application's name, which is the same as its class name.
*
* <li><b>Home Page </b>- The Application's home Page class. Subclasses must override getHomePage()
* to provide this property value.
*
* <li><b>Settings </b>- Application settings are partitioned into sets of related settings using
* interfaces in the org.apache.wicket.settings package. These interfaces are returned by the
* following methods, which should be used to configure framework settings for your application:
* getApplicationSettings(), getDebugSettings(), getExceptionSettings(), getMarkupSettings(),
* getPageSettings(), getRequestCycleSettings(), getSecuritySettings and getSessionSettings(). These
* settings are configured by default through the constructor or internalInit methods. Default the
* application is configured for DEVELOPMENT. You can configure this globally to DEPLOYMENT or
* override specific settings by implementing the init() method.
*
* <li><b>Shared Resources </b>- Resources added to an Application's SharedResources have
* application-wide scope and can be referenced using a logical scope and a name with the
* ResourceReference class. ResourceReferences can then be used by multiple components in the same
* application without additional overhead (beyond the ResourceReference instance held by each
* referee) and will yield a stable URL, permitting efficient browser caching of the resource (even
* if the resource is dynamically generated). Resources shared in this manner may also be localized.
* See {@link org.apache.wicket.request.resource.ResourceReference} for more details.
*
* <li><b>Custom Session Subclasses</b>- In order to install your own {@link Session} subclass you
* must override Application{@link #newSession(Request, Response)}. For subclasses of
* {@link WebApplication} you will want to subclass {@link WebSession}.
*
* </ul>
*
* @see org.apache.wicket.protocol.http.WebApplication
* @author Jonathan Locke
*/
public abstract class Application implements UnboundListener, IEventSink
{
/** Configuration constant for the 2 types */
public static final String CONFIGURATION = "configuration";
/**
* Applications keyed on the {@link #getApplicationKey()} so that they can be retrieved even
* without being in a request/ being set in the thread local (we need that e.g. for when we are
* in a destruction thread).
*/
private static final Map<String, Application> applicationKeyToApplication = Generics.newHashMap(1);
/** Log. */
private static final Logger log = LoggerFactory.getLogger(Application.class);
/** root mapper */
private IRequestMapper rootRequestMapper;
/** The converter locator instance. */
private IConverterLocator converterLocator;
/** list of initializers. */
private final List<IInitializer> initializers = Generics.newArrayList();
/** Application level meta data. */
private MetaDataEntry<?>[] metaData;
/** Name of application subclass. */
private String name;
/** Request logger instance. */
private IRequestLogger requestLogger;
/** The session facade. */
private volatile ISessionStore sessionStore;
/** page renderer provider */
private IPageRendererProvider pageRendererProvider;
/** request cycle provider */
private IRequestCycleProvider requestCycleProvider;
/** exception mapper provider */
private IProvider<IExceptionMapper> exceptionMapperProvider;
/** session store provider */
private IProvider<ISessionStore> sessionStoreProvider;
/**
* The decorator this application uses to decorate any header responses created by Wicket
*/
private IHeaderResponseDecorator headerResponseDecorator;
/**
* Checks if the <code>Application</code> threadlocal is set in this thread
*
* @return true if {@link Application#get()} can return the instance of application, false
* otherwise
*/
public static boolean exists()
{
return ThreadContext.getApplication() != null;
}
/**
* Get Application for current thread.
*
* @return The current thread's Application
*/
public static Application get()
{
Application application = ThreadContext.getApplication();
if (application == null)
{
throw new WicketRuntimeException("There is no application attached to current thread " +
Thread.currentThread().getName());
}
return application;
}
/**
* Gets the Application based on the application key of that application. You typically never
* have to use this method unless you are working on an integration project.
*
* @param applicationKey
* The unique key of the application within a certain context (e.g. a web
* application)
* @return The application or <code>null</code> if application has not been found
*/
public static Application get(final String applicationKey)
{
return applicationKeyToApplication.get(applicationKey);
}
/**
* Gets the keys of the currently registered Wicket applications for this web application. You
* typically never have to use this method unless you are working on an integration project.
*
* @return unmodifiable set with keys that correspond with {@link #getApplicationKey()}. Never
* null, but possibly empty
*/
public static Set<String> getApplicationKeys()
{
return Collections.unmodifiableSet(applicationKeyToApplication.keySet());
}
/**
* Constructor. <strong>Use {@link #init()} for any configuration of your application instead of
* overriding the constructor.</strong>
*/
public Application()
{
// Install default component instantiation listener that uses
// authorization strategy to check component instantiations.
getComponentInstantiationListeners().add(new IComponentInstantiationListener()
{
/**
* @see org.apache.wicket.application.IComponentInstantiationListener#onInstantiation(org.apache.wicket.Component)
*/
@Override
public void onInstantiation(final Component component)
{
final Class<? extends Component> cl = component.getClass();
// If component instantiation is not authorized
if (!Session.get().getAuthorizationStrategy().isInstantiationAuthorized(cl))
{
// then call any unauthorized component instantiation
// listener
getSecuritySettings().getUnauthorizedComponentInstantiationListener()
.onUnauthorizedInstantiation(component);
}
}
});
}
/**
* Configures application settings to good defaults.
*/
public final void configure()
{
// As long as this is public api the development and deployment mode
// should counter act each other for all properties.
switch (getConfigurationType())
{
case DEVELOPMENT : {
getResourceSettings().setResourcePollFrequency(Duration.ONE_SECOND);
getResourceSettings().setJavaScriptCompressor(null);
getMarkupSettings().setStripWicketTags(false);
getExceptionSettings().setUnexpectedExceptionDisplay(
IExceptionSettings.SHOW_EXCEPTION_PAGE);
getDebugSettings().setComponentUseCheck(true);
getDebugSettings().setAjaxDebugModeEnabled(true);
getDebugSettings().setDevelopmentUtilitiesEnabled(true);
// getDebugSettings().setOutputMarkupContainerClassName(true);
getRequestCycleSettings().addResponseFilter(EmptySrcAttributeCheckFilter.INSTANCE);
break;
}
case DEPLOYMENT : {
getResourceSettings().setResourcePollFrequency(null);
getResourceSettings().setJavaScriptCompressor(new DefaultJavaScriptCompressor());
getMarkupSettings().setStripWicketTags(true);
getExceptionSettings().setUnexpectedExceptionDisplay(
IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);
getDebugSettings().setComponentUseCheck(false);
getDebugSettings().setAjaxDebugModeEnabled(false);
getDebugSettings().setDevelopmentUtilitiesEnabled(false);
break;
}
}
}
/**
* Gets the unique key of this application within a given context (like a web application). NOT
* INTENDED FOR FRAMEWORK CLIENTS.
*
* @return The unique key of this application
*/
public abstract String getApplicationKey();
/**
* Gets the configuration mode to use for configuring the app, either
* {@link RuntimeConfigurationType#DEVELOPMENT} or {@link RuntimeConfigurationType#DEPLOYMENT}.
* <p>
* The configuration type. Must currently be either DEVELOPMENT or DEPLOYMENT. Currently, if the
* configuration type is DEVELOPMENT, resources are polled for changes, component usage is
* checked, wicket tags are not stripped from output and a detailed exception page is used. If
* the type is DEPLOYMENT, component usage is not checked, wicket tags are stripped from output
* and a non-detailed exception page is used to display errors.
* <p>
* Note that you should not run Wicket in DEVELOPMENT mode on production servers - the various
* debugging checks and resource polling is inefficient and may leak resources, particularly on
* webapp redeploy.
*
* <div style="bored-width:10px;border-style:solid;">
* <p>
* To change the deployment mode, add the following to your web.xml, inside your <servlet>
* mapping (or <filter> mapping if you're using 1.3.x):
*
* <pre>
* <init-param>
* <param-name>configuration</param-name>
* <param-value>deployment</param-value>
* </init-param>
* </pre>
*
* <p>
* You can alternatively set this as a <context-param> on the whole context.
*
* <p>
* Another option is to set the "wicket.configuration" system property to either "deployment" or
* "development". The value is not case-sensitive.
*
* <p>
* The system property is checked first, allowing you to add a web.xml param for deployment, and
* a command-line override when you want to run in development mode during development.
*
* <p>
* You may also override Application.getConfigurationType() to provide your own custom switch,
* in which case none of the above logic is used. </div>
*
* <p>
* IMPORTANT NOTE
* </p>
* THIS METHOD IS CALLED OFTEN FROM MANY DIFFERENT POINTS IN CODE, INCLUDING DURING THE RENDER
* PROCESS, THEREFORE THE IMPLEMENTATION SHOULD BE FAST - PREFERRABLY USING A FAST-TO-RETRIEVE
* CACHED VALUE
*
* @return configuration
* @since 1.2.3 (function existed as a property getter)
* @since 1.3.0 (abstract, used to configure things)
*/
public abstract RuntimeConfigurationType getConfigurationType();
/**
* Application subclasses must specify a home page class by implementing this abstract method.
*
* @return Home page class for this application
*/
public abstract Class<? extends Page> getHomePage();
/**
* @return The converter locator for this application
*/
public final IConverterLocator getConverterLocator()
{
return converterLocator;
}
/**
* Gets metadata for this application using the given key.
*
* @param <T>
* @param key
* The key for the data
* @return The metadata
* @see MetaDataKey
*/
public final <T> T getMetaData(final MetaDataKey<T> key)
{
return key.get(metaData);
}
/**
* Gets the name of this application.
*
* @return The application name.
*/
public final String getName()
{
return name;
}
/**
* Gets the {@link IRequestLogger}.
*
* @return The RequestLogger
*/
public final IRequestLogger getRequestLogger()
{
if (getRequestLoggerSettings().isRequestLoggerEnabled())
{
if (requestLogger == null)
{
requestLogger = newRequestLogger();
}
}
else
{
requestLogger = null;
}
return requestLogger;
}
/**
* Gets the facade object for working getting/ storing session instances.
*
* @return The session facade
*/
public final ISessionStore getSessionStore()
{
if (sessionStore == null)
{
synchronized (this)
{
if (sessionStore == null)
{
sessionStore = sessionStoreProvider.get();
sessionStore.registerUnboundListener(this);
}
}
}
return sessionStore;
}
/**
* @see org.apache.wicket.session.ISessionStore.UnboundListener#sessionUnbound(java.lang.String)
*/
@Override
public void sessionUnbound(final String sessionId)
{
internalGetPageManager().sessionExpired(sessionId);
}
/**
* THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL.
*
* Initializes wicket components.
*/
public final void initializeComponents()
{
// Load any wicket properties files we can find
try
{
// Load properties files used by all libraries
final Iterator<URL> resources = getApplicationSettings().getClassResolver()
.getResources("wicket.properties");
while (resources.hasNext())
{
InputStream in = null;
try
{
final URL url = resources.next();
final Properties properties = new Properties();
in = url.openStream();
properties.load(in);
load(properties);
}
finally
{
IOUtils.close(in);
}
}
}
catch (IOException e)
{
throw new WicketRuntimeException("Unable to load initializers file", e);
}
// now call any initializers we read
initInitializers();
}
/**
* THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL.
*
* @param target
*/
public void logEventTarget(final IRequestHandler target)
{
}
/**
* THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL.
*
* @param requestTarget
*/
public void logResponseTarget(final IRequestHandler requestTarget)
{
}
/**
* Creates a new session. Override this method if you want to provide a custom session.
*
* @param request
* The request that will create this session.
* @param response
* The response to initialize, for example with cookies. This is important to use
* cases involving unit testing because those use cases might want to be able to sign
* a user in automatically when the session is created.
*
* @return The session
*
* @since 1.3
*/
public abstract Session newSession(Request request, Response response);
/**
* Sets the metadata for this application using the given key. If the metadata object is not of
* the correct type for the metadata key, an IllegalArgumentException will be thrown. For
* information on creating MetaDataKeys, see {@link MetaDataKey}.
*
* @param <T>
* @param key
* The singleton key for the metadata
* @param object
* The metadata object
* @throws IllegalArgumentException
* @see MetaDataKey
*/
public final synchronized <T> void setMetaData(final MetaDataKey<T> key, final Object object)
{
metaData = key.set(metaData, object);
}
/**
* Construct and add initializer from the provided class name.
*
* @param className
*/
private void addInitializer(final String className)
{
IInitializer initializer = (IInitializer)WicketObjects.newInstance(className);
if (initializer != null)
{
initializers.add(initializer);
}
}
/**
* Iterate initializers list, calling {@link IInitializer#destroy(Application)} on any instances
* found in it.
*/
private void destroyInitializers()
{
for (IInitializer initializer : initializers)
{
log.info("[" + getName() + "] destroy: " + initializer);
initializer.destroy(this);
}
}
/**
* Iterate initializers list, calling {@link IInitializer#init(Application)} on any instances
* found in it.
*/
private void initInitializers()
{
for (IInitializer initializer : initializers)
{
log.info("[" + getName() + "] init: " + initializer);
initializer.init(this);
}
}
/**
* @param properties
* Properties map with names of any library initializers in it
*/
private void load(final Properties properties)
{
addInitializer(properties.getProperty("initializer"));
addInitializer(properties.getProperty(getName() + "-initializer"));
}
/**
* Called when wicket servlet is destroyed. Overrides do not have to call super.
*/
protected void onDestroy()
{
}
/**
* Allows for initialization of the application by a subclass. <strong>Use this method for any
* application setup instead of the constructor.</strong>
*/
protected void init()
{
}
/**
* THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT.
*/
public void internalDestroy()
{
applicationListeners.onBeforeDestroyed(this);
// destroy detach listener
final IDetachListener detachListener = getFrameworkSettings().getDetachListener();
if (detachListener != null)
{
detachListener.onDestroyListener();
}
// Clear caches of Class keys so the classloader can be garbage
// collected (WICKET-625)
PropertyResolver.destroy(this);
MarkupFactory markupFactory = getMarkupSettings().getMarkupFactory();
if (markupFactory.hasMarkupCache())
{
markupFactory.getMarkupCache().shutdown();
}
onDestroy();
destroyInitializers();
internalGetPageManager().destroy();
getSessionStore().destroy();
applicationKeyToApplication.remove(getApplicationKey());
}
/**
* THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT OVERRIDE OR CALL.
*
* Internal initialization.
*/
protected void internalInit()
{
settingsAccessible = true;
IPageSettings pageSettings = getPageSettings();
// Install default component resolvers
pageSettings.addComponentResolver(new MarkupInheritanceResolver());
pageSettings.addComponentResolver(new HtmlHeaderResolver());
pageSettings.addComponentResolver(new WicketLinkTagHandler());
pageSettings.addComponentResolver(new WicketMessageResolver());
pageSettings.addComponentResolver(new WicketMessageTagHandler());
pageSettings.addComponentResolver(new FragmentResolver());
pageSettings.addComponentResolver(new RelativePathPrefixHandler());
pageSettings.addComponentResolver(new EnclosureHandler());
pageSettings.addComponentResolver(new InlineEnclosureHandler());
pageSettings.addComponentResolver(new WicketContainerResolver());
// Install button image resource factory
getResourceSettings().addResourceFactory("buttonFactory",
new DefaultButtonImageResourceFactory());
String applicationKey = getApplicationKey();
applicationKeyToApplication.put(applicationKey, this);
converterLocator = newConverterLocator();
setPageManagerProvider(new DefaultPageManagerProvider(this));
resourceReferenceRegistry = newResourceReferenceRegistry();
sharedResources = newSharedResources(resourceReferenceRegistry);
// set up default request mapper
setRootRequestMapper(new SystemMapper(this));
pageFactory = newPageFactory();
requestCycleProvider = new DefaultRequestCycleProvider();
exceptionMapperProvider = new DefaultExceptionMapperProvider();
// add a request cycle listener that logs each request for the requestlogger.
getRequestCycleListeners().add(new RequestLoggerRequestCycleListener());
}
/**
* @return the exception mapper provider
*/
public IProvider<IExceptionMapper> getExceptionMapperProvider()
{
return exceptionMapperProvider;
}
/**
*
* @return Session state provider
*/
public final IProvider<ISessionStore> getSessionStoreProvider()
{
return sessionStoreProvider;
}
/**
*
* @param sessionStoreProvider
*/
public final void setSessionStoreProvider(final IProvider<ISessionStore> sessionStoreProvider)
{
this.sessionStoreProvider = sessionStoreProvider;
}
/**
* Creates and returns a new instance of {@link IConverterLocator}.
*
* @return A new {@link IConverterLocator} instance
*/
protected IConverterLocator newConverterLocator()
{
return new ConverterLocator();
}
/**
* creates a new request logger when requests logging is enabled.
*
* @return The new request logger
*
*/
protected IRequestLogger newRequestLogger()
{
return new RequestLogger();
}
/**
* Converts the root mapper to a {@link ICompoundRequestMapper} if necessary and returns the
* converted instance.
*
* @return compound instance of the root mapper
*/
public final ICompoundRequestMapper getRootRequestMapperAsCompound()
{
IRequestMapper root = getRootRequestMapper();
if (!(root instanceof ICompoundRequestMapper))
{
root = new CompoundRequestMapper().add(root);
setRootRequestMapper(root);
}
return (ICompoundRequestMapper)root;
}
/**
* @return The root request mapper
*/
public final IRequestMapper getRootRequestMapper()
{
return rootRequestMapper;
}
/**
* Sets the root request mapper
*
* @param rootRequestMapper
*/
public final void setRootRequestMapper(final IRequestMapper rootRequestMapper)
{
this.rootRequestMapper = rootRequestMapper;
}
/**
* Initialize the application
*/
public final void initApplication()
{
if (name == null)
{
throw new IllegalStateException("setName must be called before initApplication");
}
internalInit();
init();
initializeComponents();
applicationListeners.onAfterInitialized(this);
validateInit();
}
/**
* Gives the Application object a chance to validate if it has been properly initialized
*/
protected void validateInit()
{
if (getPageRendererProvider() == null)
{
throw new IllegalStateException(
"An instance of IPageRendererProvider has not yet been set on this Application. @see Application#setPageRendererProvider");
}
if (getSessionStoreProvider() == null)
{
throw new IllegalStateException(
"An instance of ISessionStoreProvider has not yet been set on this Application. @see Application#setSessionStoreProvider");
}
if (getPageManagerProvider() == null)
{
throw new IllegalStateException(
"An instance of IPageManagerProvider has not yet been set on this Application. @see Application#setPageManagerProvider");
}
}
/**
* Sets application name. This method must be called before any other methods are invoked and
* can only be called once per application instance.
*
* @param name
* unique application name
*/
public final void setName(final String name)
{
Args.notEmpty(name, "name");
if (this.name != null)
{
throw new IllegalStateException("Application name can only be set once.");
}
if (applicationKeyToApplication.get(name) != null)
{
throw new IllegalStateException("Application with name '" + name + "' already exists.'");
}
this.name = name;
applicationKeyToApplication.put(name, this);
}
/**
* Returns the mime type for given filename.
*
* @param fileName
* @return mime type
*/
public String getMimeType(final String fileName)
{
return URLConnection.getFileNameMap().getContentTypeFor(fileName);
}
/** {@inheritDoc} */
@Override
public void onEvent(final IEvent<?> event)
{
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Listeners
//
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** */
private final ComponentOnBeforeRenderListenerCollection componentPreOnBeforeRenderListeners = new ComponentOnBeforeRenderListenerCollection();
/** */
private final ComponentOnBeforeRenderListenerCollection componentPostOnBeforeRenderListeners = new ComponentOnBeforeRenderListenerCollection();
/** */
private final ComponentOnAfterRenderListenerCollection componentOnAfterRenderListeners = new ComponentOnAfterRenderListenerCollection();
/** */
private final RequestCycleListenerCollection requestCycleListeners = new RequestCycleListenerCollection();
private final ApplicationListenerCollection applicationListeners = new ApplicationListenerCollection();
private final SessionListenerCollection sessionListeners = new SessionListenerCollection();
/** list of {@link IComponentInstantiationListener}s. */
private final ComponentInstantiationListenerCollection componentInstantiationListeners = new ComponentInstantiationListenerCollection();
/** list of {@link IComponentInitializationListener}s. */
private final ComponentInitializationListenerCollection componentInitializationListeners = new ComponentInitializationListenerCollection();
/** list of {@link IHeaderContributor}s. */
private final HeaderContributorListenerCollection headerContributorListenerCollection = new HeaderContributorListenerCollection();
private final BehaviorInstantiationListenerCollection behaviorInstantiationListeners = new BehaviorInstantiationListenerCollection();
/**
* @return Gets the application's {@link HeaderContributorListenerCollection}
*/
public final HeaderContributorListenerCollection getHeaderContributorListenerCollection()
{
return headerContributorListenerCollection;
}
/**
* @return collection of initializers
*/
public final List<IInitializer> getInitializers()
{
return Collections.unmodifiableList(initializers);
}
/**
* @return collection of application listeners
*/
public final ApplicationListenerCollection getApplicationListeners()
{
return applicationListeners;
}
/**
* @return collection of session listeners
*/
public final SessionListenerCollection getSessionListeners()
{
return sessionListeners;
}
/**
* @return collection of behavior instantiation listeners
*/
public final BehaviorInstantiationListenerCollection getBehaviorInstantiationListeners()
{
return behaviorInstantiationListeners;
}
/**
* @return Gets the application's ComponentInstantiationListenerCollection
*/
public final ComponentInstantiationListenerCollection getComponentInstantiationListeners()
{
return componentInstantiationListeners;
}
/**
* @return Gets the application's ComponentInitializationListeners
*/
public final ComponentInitializationListenerCollection getComponentInitializationListeners()
{
return componentInitializationListeners;
}
/**
*
* @return ComponentOnBeforeRenderListenerCollection
*/
public final ComponentOnBeforeRenderListenerCollection getComponentPreOnBeforeRenderListeners()
{
return componentPreOnBeforeRenderListeners;
}
/**
*
* @return ComponentOnBeforeRenderListenerCollection
*/
public final ComponentOnBeforeRenderListenerCollection getComponentPostOnBeforeRenderListeners()
{
return componentPostOnBeforeRenderListeners;
}
/**
* @return on after render listeners collection
*/
public final ComponentOnAfterRenderListenerCollection getComponentOnAfterRenderListeners()
{
return componentOnAfterRenderListeners;
}