001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.services.impl;
016
017import java.io.IOException;
018
019import org.apache.tapestry.Constants;
020import org.apache.tapestry.IEngine;
021import org.apache.tapestry.services.EngineManager;
022import org.apache.tapestry.services.Infrastructure;
023import org.apache.tapestry.services.RequestGlobals;
024import org.apache.tapestry.services.WebRequestServicer;
025import org.apache.tapestry.web.WebRequest;
026import org.apache.tapestry.web.WebResponse;
027
028/**
029 * The terminatior for the <code>tapestry.RequestProcessor</code> pipeline, this service is
030 * responsible for:
031 * <ul>
032 * <li>Storing the request and response into {@link org.apache.tapestry.services.RequestGlobals}.
033 * <li>Locating the correct engine instance and letting it to the rest of the request.
034 * <li>Returning the engine instance to the pool at the end of the request. </ul
035 * 
036 * @author Howard Lewis Ship
037 * @since 4.0
038 */
039public class InvokeEngineTerminator implements WebRequestServicer
040{
041    private EngineManager _engineManager;
042
043    private Infrastructure _infrastructure;
044
045    private RequestGlobals _requestGlobals;
046
047    public void service(WebRequest request, WebResponse response) throws IOException
048    {
049        _requestGlobals.store(request, response);
050
051        IEngine engine = _engineManager.getEngineInstance();
052
053        // Until we can inject the infrastructure into the engine
054        // we do this to let the engine know about it.
055
056        request.setAttribute(Constants.INFRASTRUCTURE_KEY, _infrastructure);
057
058        try
059        {
060            engine.service(request, response);
061        }
062        finally
063        {
064            _engineManager.storeEngineInstance(engine);
065        }
066
067    }
068
069    public void setEngineManager(EngineManager manager)
070    {
071        _engineManager = manager;
072    }
073
074    public void setInfrastructure(Infrastructure infrastructure)
075    {
076        _infrastructure = infrastructure;
077    }
078
079    public void setRequestGlobals(RequestGlobals requestGlobals)
080    {
081        _requestGlobals = requestGlobals;
082    }
083}