01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.integration.app1.services;
16:
17: import java.io.IOException;
18: import java.net.URL;
19: import java.util.List;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.tapestry.integration.app1.data.Track;
23: import org.apache.tapestry.ioc.MappedConfiguration;
24: import org.apache.tapestry.ioc.OrderedConfiguration;
25: import org.apache.tapestry.ioc.annotations.InjectService;
26: import org.apache.tapestry.services.Request;
27: import org.apache.tapestry.services.RequestFilter;
28: import org.apache.tapestry.services.RequestHandler;
29: import org.apache.tapestry.services.Response;
30:
31: /**
32: * I was just dying to see how fast requests are!
33: */
34: public class AppModule {
35: public RequestFilter buildTimingFilter(final Log log) {
36: return new RequestFilter() {
37: public boolean service(Request request, Response response,
38: RequestHandler handler) throws IOException {
39: long startTime = System.currentTimeMillis();
40:
41: try {
42: return handler.service(request, response);
43: } finally {
44: long elapsed = System.currentTimeMillis()
45: - startTime;
46:
47: log.info(String.format("Request time: %d ms",
48: elapsed));
49: }
50: }
51: };
52: }
53:
54: public void contributeRequestHandler(
55: OrderedConfiguration<RequestFilter> configuration,
56: @InjectService("TimingFilter")
57: RequestFilter filter) {
58: configuration.add("Timing", filter);
59: }
60:
61: public void contributeClasspathAssetAliasManager(
62: MappedConfiguration<String, String> configuration) {
63: configuration.add("app1/",
64: "org/apache/tapestry/integration/app1/");
65: }
66:
67: public UserAuthenticator buildUserAuthenticator() {
68: return new UserAuthenticator() {
69: public boolean isValid(String userName,
70: String plaintextPassword) {
71: return plaintextPassword.equals("tapestry");
72: }
73: };
74: }
75:
76: public static void contributeApplicationDefaults(
77: MappedConfiguration<String, String> configuration) {
78: configuration.add("tapestry.supported-locales", "en,fr");
79: }
80:
81: public ToDoDatabase buildToDoDatabase() {
82: return new ToDoDatabaseImpl();
83: }
84:
85: public MusicLibrary buildMusicLibrary(Log log) {
86: URL library = getClass().getResource("iTunes.xml");
87:
88: final List<Track> tracks = new MusicLibraryParser(log)
89: .parseTracks(library);
90:
91: return new MusicLibrary() {
92: public List<Track> getTracks() {
93: return tracks;
94: }
95: };
96: }
97: }
|