KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > RequestCycleFactoryImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.services.impl;
16
17 import java.util.Iterator JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.hivemind.ErrorHandler;
23 import org.apache.tapestry.IEngine;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.Tapestry;
26 import org.apache.tapestry.engine.IEngineService;
27 import org.apache.tapestry.engine.IMonitor;
28 import org.apache.tapestry.engine.IMonitorFactory;
29 import org.apache.tapestry.engine.RequestCycle;
30 import org.apache.tapestry.engine.RequestCycleEnvironment;
31 import org.apache.tapestry.engine.ServiceEncoder;
32 import org.apache.tapestry.engine.ServiceEncodingImpl;
33 import org.apache.tapestry.record.PropertyPersistenceStrategySource;
34 import org.apache.tapestry.request.RequestContext;
35 import org.apache.tapestry.services.AbsoluteURLBuilder;
36 import org.apache.tapestry.services.Infrastructure;
37 import org.apache.tapestry.services.RequestCycleFactory;
38 import org.apache.tapestry.services.ServiceConstants;
39 import org.apache.tapestry.util.QueryParameterMap;
40 import org.apache.tapestry.web.WebRequest;
41
42 /**
43  * Service that creates instances of {@link org.apache.tapestry.IRequestCycle}on behalf of an
44  * engine.
45  *
46  * @author Howard M. Lewis Ship
47  * @since 4.0
48  */

49 public class RequestCycleFactoryImpl implements RequestCycleFactory
50 {
51     private ServiceEncoder[] _encoders;
52
53     private IMonitorFactory _monitorFactory;
54
55     private PropertyPersistenceStrategySource _strategySource;
56
57     private ErrorHandler _errorHandler;
58
59     private Infrastructure _infrastructure;
60
61     private AbsoluteURLBuilder _absoluteURLBuilder;
62
63     private RequestCycleEnvironment _environment;
64
65     private HttpServletRequest JavaDoc _servletRequest;
66
67     private HttpServletResponse JavaDoc _servletResponse;
68
69     public void initializeService()
70     {
71         RequestContext context = new RequestContext(_servletRequest, _servletResponse);
72
73         _environment = new RequestCycleEnvironment(_errorHandler, _infrastructure, context,
74                 _strategySource, _absoluteURLBuilder);
75     }
76
77     public IRequestCycle newRequestCycle(IEngine engine)
78     {
79         WebRequest request = _infrastructure.getRequest();
80
81         IMonitor monitor = _monitorFactory.createMonitor(request);
82
83         QueryParameterMap parameters = extractParameters(request);
84
85         decodeParameters(request.getActivationPath(), parameters);
86
87         IEngineService service = findService(parameters);
88
89         return new RequestCycle(engine, parameters, service, monitor, _environment);
90     }
91
92     private IEngineService findService(QueryParameterMap parameters)
93     {
94         String JavaDoc serviceName = parameters.getParameterValue(ServiceConstants.SERVICE);
95
96         if (serviceName == null)
97             serviceName = Tapestry.HOME_SERVICE;
98
99         return _infrastructure.getServiceMap().getService(serviceName);
100     }
101
102     /**
103      * Constructs a {@link org.apache.tapestry.util.QueryParameterMap}using the parameters
104      * available from the {@link org.apache.tapestry.request.RequestContext} (but ignoring any
105      * file upload parameters!).
106      */

107
108     private QueryParameterMap extractParameters(WebRequest request)
109     {
110         QueryParameterMap result = new QueryParameterMap();
111
112         Iterator JavaDoc i = request.getParameterNames().iterator();
113
114         while (i.hasNext())
115         {
116             String JavaDoc name = (String JavaDoc) i.next();
117
118             String JavaDoc[] values = request.getParameterValues(name);
119
120             if (values.length == 1)
121                 result.setParameterValue(name, values[0]);
122             else
123                 result.setParameterValues(name, values);
124         }
125
126         return result;
127     }
128
129     private void decodeParameters(String JavaDoc servletPath, QueryParameterMap map)
130     {
131         ServiceEncodingImpl se = new ServiceEncodingImpl(servletPath, map);
132
133         for (int i = 0; i < _encoders.length; i++)
134         {
135             _encoders[i].decode(se);
136
137             if (se.isModified())
138                 return;
139         }
140     }
141
142     public void setEncoders(ServiceEncoder[] encoders)
143     {
144         _encoders = encoders;
145     }
146
147     public void setMonitorFactory(IMonitorFactory monitorFactory)
148     {
149         _monitorFactory = monitorFactory;
150     }
151
152     public void setStrategySource(PropertyPersistenceStrategySource strategySource)
153     {
154         _strategySource = strategySource;
155     }
156
157     public void setErrorHandler(ErrorHandler errorHandler)
158     {
159         _errorHandler = errorHandler;
160     }
161
162     public void setInfrastructure(Infrastructure infrastructure)
163     {
164         _infrastructure = infrastructure;
165     }
166
167     public void setAbsoluteURLBuilder(AbsoluteURLBuilder absoluteURLBuilder)
168     {
169         _absoluteURLBuilder = absoluteURLBuilder;
170     }
171
172     public void setServletRequest(HttpServletRequest JavaDoc servletRequest)
173     {
174         _servletRequest = servletRequest;
175     }
176
177     public void setServletResponse(HttpServletResponse JavaDoc servletResponse)
178     {
179         _servletResponse = servletResponse;
180     }
181 }
Popular Tags