Mercurial > public > ssdtforms
comparison src/main/java/org/ssdt_ohio/devel/forms/services/AppModule.java @ 0:f4f8570d1c56
initial OTP form
author | smith@nwoca.org |
---|---|
date | Mon, 12 Nov 2012 16:29:11 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f4f8570d1c56 |
---|---|
1 package org.ssdt_ohio.devel.forms.services; | |
2 | |
3 import org.apache.tapestry5.SymbolConstants; | |
4 import org.apache.tapestry5.ioc.MappedConfiguration; | |
5 import org.apache.tapestry5.ioc.OrderedConfiguration; | |
6 import org.apache.tapestry5.ioc.ServiceBinder; | |
7 import org.apache.tapestry5.ioc.annotations.Local; | |
8 import org.apache.tapestry5.services.Request; | |
9 import org.apache.tapestry5.services.RequestFilter; | |
10 import org.apache.tapestry5.services.RequestHandler; | |
11 import org.apache.tapestry5.services.Response; | |
12 import org.slf4j.Logger; | |
13 | |
14 import java.io.IOException; | |
15 | |
16 /** | |
17 * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to configure and extend | |
18 * Tapestry, or to place your own service definitions. | |
19 */ | |
20 public class AppModule { | |
21 public static void bind(ServiceBinder binder) { | |
22 // binder.bind(MyServiceInterface.class, MyServiceImpl.class); | |
23 | |
24 // Make bind() calls on the binder object to define most IoC services. | |
25 // Use service builder methods (example below) when the implementation | |
26 // is provided inline, or requires more initialization than simply | |
27 // invoking the constructor. | |
28 } | |
29 | |
30 public static void contributeFactoryDefaults( | |
31 MappedConfiguration<String, Object> configuration) { | |
32 // The application version number is incorprated into URLs for some | |
33 // assets. Web browsers will cache assets because of the far future expires | |
34 // header. If existing assets are changed, the version number should also | |
35 // change, to force the browser to download new versions. This overrides Tapesty's default | |
36 // (a random hexadecimal number), but may be further overriden by DevelopmentModule or | |
37 // QaModule. | |
38 configuration.override(SymbolConstants.APPLICATION_VERSION, "1.0-SNAPSHOT"); | |
39 } | |
40 | |
41 public static void contributeApplicationDefaults( | |
42 MappedConfiguration<String, Object> configuration) { | |
43 // Contributions to ApplicationDefaults will override any contributions to | |
44 // FactoryDefaults (with the same key). Here we're restricting the supported | |
45 // locales to just "en" (English). As you add localised message catalogs and other assets, | |
46 // you can extend this list of locales (it's a comma separated series of locale names; | |
47 // the first locale name is the default when there's no reasonable match). | |
48 configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en"); | |
49 | |
50 // The factory default is true but during the early stages of an application | |
51 // overriding to false is a good idea. In addition, this is often overridden | |
52 // on the command line as -Dtapestry.production-mode=false | |
53 configuration.add(SymbolConstants.PRODUCTION_MODE, false); | |
54 | |
55 // The application version number is incorprated into URLs for some | |
56 // assets. Web browsers will cache assets because of the far future expires | |
57 // header. If existing assets are changed, the version number should also | |
58 // change, to force the browser to download new versions. | |
59 configuration.add(SymbolConstants.APPLICATION_VERSION, "1.0-SNAPSHOT-DEV"); | |
60 } | |
61 | |
62 | |
63 /** | |
64 * This is a service definition, the service will be named "TimingFilter". The interface, RequestFilter, is used | |
65 * within the RequestHandler service pipeline, which is built from the RequestHandler service configuration. | |
66 * Tapestry IoC is responsible for passing in an appropriate Logger instance. Requests for static resources are | |
67 * handled at a higher level, so this filter will only be invoked for Tapestry related requests. | |
68 * <p/> | |
69 * <p/> | |
70 * Service builder methods are useful when the implementation is inline as an inner class (as here) or require some | |
71 * other kind of special initialization. In most cases, use the static bind() method instead. | |
72 * <p/> | |
73 * <p/> | |
74 * If this method was named "build", then the service id would be taken from the service interface and would be | |
75 * "RequestFilter". Since Tapestry already defines a service named "RequestFilter" we use an explicit service id | |
76 * that we can reference inside the contribution method. | |
77 */ | |
78 public RequestFilter buildTimingFilter(final Logger log) { | |
79 return new RequestFilter() { | |
80 public boolean service(Request request, Response response, RequestHandler handler) | |
81 throws IOException { | |
82 long startTime = System.currentTimeMillis(); | |
83 | |
84 try { | |
85 // The responsibility of a filter is to invoke the corresponding method | |
86 // in the handler. When you chain multiple filters together, each filter | |
87 // received a handler that is a bridge to the next filter. | |
88 | |
89 return handler.service(request, response); | |
90 } finally { | |
91 long elapsed = System.currentTimeMillis() - startTime; | |
92 | |
93 log.info(String.format("Request time: %d ms", elapsed)); | |
94 } | |
95 } | |
96 }; | |
97 } | |
98 | |
99 /** | |
100 * This is a contribution to the RequestHandler service configuration. This is how we extend Tapestry using the | |
101 * timing filter. A common use for this kind of filter is transaction management or security. The @Local annotation | |
102 * selects the desired service by type, but only from the same module. Without @Local, there would be an error due | |
103 * to the other service(s) that implement RequestFilter (defined in other modules). | |
104 */ | |
105 public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration, | |
106 @Local | |
107 RequestFilter filter) { | |
108 // Each contribution to an ordered configuration has a name, When necessary, you may | |
109 // set constraints to precisely control the invocation order of the contributed filter | |
110 // within the pipeline. | |
111 | |
112 configuration.add("Timing", filter); | |
113 } | |
114 } |