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.util.Locale;
018
019import org.apache.tapestry.IEngine;
020import org.apache.tapestry.services.EngineFactory;
021import org.apache.tapestry.services.EngineManager;
022import org.apache.tapestry.services.RequestLocaleManager;
023import org.apache.tapestry.services.ObjectPool;
024
025/**
026 * Implementation of service {@link org.apache.tapestry.services.EngineManager}.
027 * Service point tapestry.request.EngineManager.
028 * 
029 * @author Howard Lewis Ship
030 * @since 4.0
031 */
032public class EngineManagerImpl implements EngineManager
033{
034    private ObjectPool _enginePool;
035
036    private EngineFactory _engineFactory;
037
038    private RequestLocaleManager _localeManager;
039
040    public IEngine getEngineInstance()
041    {
042        Locale locale = _localeManager.extractLocaleForCurrentRequest();
043
044        IEngine result = (IEngine) _enginePool.get(locale);
045
046        // This happens when either the pool is empty, or when a session exists
047        // but the engine has not been stored into it (which should never happen, and
048        // probably indicates an error in the framework or the application).
049
050        if (result == null)
051            result = _engineFactory.constructNewEngineInstance(locale);
052
053        return result;
054    }
055
056    public void storeEngineInstance(IEngine engine)
057    {
058        _enginePool.store(engine.getLocale(), engine);
059    }
060
061    public void setEngineFactory(EngineFactory factory)
062    {
063        _engineFactory = factory;
064    }
065
066    public void setEnginePool(ObjectPool pool)
067    {
068        _enginePool = pool;
069    }
070
071    public void setLocaleManager(RequestLocaleManager manager)
072    {
073        _localeManager = manager;
074    }
075}