All the http requests are handled by Asta4dServlet which allows developers to override several methods to customize its actions. The web.xml would be like following:
Example 16.1.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- standalone without spring --> <servlet> <servlet-name>Asta4D Servlet</servlet-name> <servlet-class>com.astamuse.asta4d.web.servlet.Asta4dServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Asta4D Servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
The following methods can be overridden:
createConfiguration()
Overriding this methods allows developers to configure the framework by code.
Example 16.2.
@Override protected WebApplicationConfiguration createConfiguration() { WebApplicationConfiguration conf = super.createConfiguration(); conf.setSnippetResolver(new DefaultSnippetResolver() { @Override protected Object createInstance(String snippetName) throws SnippetNotResovlableException { return super.createInstance("com.astamuse.asta4d.sample.snippet." + snippetName); } }); conf.setUrlMappingRuleInitializer(new UrlRules()); conf.getPageInterceptorList().add(new SamplePageInterceptor()); return conf; }
service()
Developers can intercept the http request by overriding this method. The following example shows how to rewrite the access url by adding pre request logic:
Example 16.3.
@Override protected void service() throws Exception { WebApplicationContext context = Context.getCurrentThreadContext(); HttpServletRequest request = context.getRequest(); String requestURI = request.getRequestURI(); if (requestURI.startsWith("/abc/")) { context.setAccessURI(requestURI.substring(5)); } super.service(); }
Request time measuring can be performed too:
Example 16.4.
@Override protected void service() throws Exception { long startTime = System.currentTimeMillis(); try { super.service(); } finally { long endTime = System.currentTimeMillis(); System.out.println("request time:" + (endTime - startTime)); } }
createAsta4dContext()
Overriding this methods allows developers to afford customized Context implementation.
createConfigurationInitializer()
Overriding this methods allows developers to customize the configuration file initialization logic. See details at next section.
In the previous section, we introduced that Asta4D can be configured by overriding createConfiguration() method and we also introduced a method called createConfigurationInitializer which allows customize the configuration file initialization logic. Asta4D allows to be configured by an extra configuration file which will be analyzed by the created configuration initializer.
The default implementation of configuration initializer will seek the configuration file by following order:
Calling retrievePossibleConfigurationFileNames(Overridable) to retrieve all possible configuration file names which could be an array of String. The default value is "asta4d.conf.properties" only.
Search all the possible names in classpath by order. The first found one will be used.
If there is no configuration file found in classpath, calling retrieveConfigurationFileNameKey(Overridable) to get a key. The default key is "asta4d.conf".
Retrieve the value of key retrieve in previous step by the order as: ServletConfig, JDNI Context, System properties.
If there is a value found in the previous step, open a file as configuration file by treating the found value as a file path.
As mentioned above, the two methods of retrievePossibleConfigurationFileNames and retrieveConfigurationFileNameKey can be overridden to customize configuration file name and the file path can be also configured by the key "asta4d.conf" at servlet mapping configuration(ServletConfig), JDNI configuration(JDNI context) or command line parameter(System properties).
Further, the default implementation does only support the format of configuration file as Java standard properties file. All the configured key/value will be treated as setXXX method calling on WebApplicationConfiguration instance. The key can be a dot split string which will be treated as recursive property accessing on WebApplicationConfiguration instance.
Example 16.5.
urlMappingRuleInitializer=com.astamuse.asta4d.sample.UrlRules cacheEnable=false snippetExecutorFactory.threadName=asta4d-sample-snippet snippetExecutorFactory.poolSize=100 listExecutorFactory.threadName=asta4d-sample-list listExecutorFactory.poolSize=100
The above configuration equals to the following source:
Example 16.6.
WebApplicationConfiguration conf = super.createConfiguration(); conf.setUrlMappingRuleInitializer(new UrlRules()); conf.setCacheEnable(false); ((DefaultExecutorServiceFactory) conf.getSnippetExecutorFactory()).setThreadName("asta4d-sample-snippet"); ((DefaultExecutorServiceFactory) conf.getSnippetExecutorFactory()).setPoolSize(100); ((DefaultExecutorServiceFactory) conf.getListExecutorFactory()).setThreadName("asta4d-sample-list"); ((DefaultExecutorServiceFactory) conf.getListExecutorFactory()).setPoolSize(100);
All the configurable items can be found at JavaDoc.