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    
015    package org.apache.tapestry.services.impl;
016    
017    import java.io.IOException;
018    
019    import org.apache.tapestry.Constants;
020    import org.apache.tapestry.IEngine;
021    import org.apache.tapestry.services.EngineManager;
022    import org.apache.tapestry.services.Infrastructure;
023    import org.apache.tapestry.services.WebRequestServicer;
024    import org.apache.tapestry.web.WebRequest;
025    import org.apache.tapestry.web.WebResponse;
026    
027    /**
028     * The terminatior for the <code>tapestry.RequestProcessor</code> pipeline, this service is
029     * responsible for:
030     * <ul>
031     * <li>Locating the correct engine instance and letting it to the rest of the request.
032     * <li>Returning the engine instance to the pool at the end of the request. </ul>
033     * 
034     * @author Howard Lewis Ship
035     * @since 4.0
036     */
037    public class InvokeEngineTerminator implements WebRequestServicer
038    {
039        private EngineManager _engineManager;
040    
041        private Infrastructure _infrastructure;
042    
043        public void service(WebRequest request, WebResponse response) throws IOException
044        {
045            IEngine engine = _engineManager.getEngineInstance();
046            
047            // Until we can inject the infrastructure into the engine
048            // we do this to let the engine know about it.
049    
050            request.setAttribute(Constants.INFRASTRUCTURE_KEY, _infrastructure);
051    
052            try
053            {
054                engine.service(request, response);
055            }
056            finally
057            {
058                _engineManager.storeEngineInstance(engine);
059            }
060    
061        }
062    
063        public void setEngineManager(EngineManager manager)
064        {
065            _engineManager = manager;
066        }
067    
068        public void setInfrastructure(Infrastructure infrastructure)
069        {
070            _infrastructure = infrastructure;
071        }
072    }