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