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.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.hivemind.ErrorLog;
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.internal.Module;
023    import org.apache.hivemind.service.ObjectProvider;
024    import org.apache.hivemind.util.PropertyUtils;
025    import org.apache.tapestry.services.Infrastructure;
026    
027    /**
028     * An ObjectProvider that streamlines access to the central
029     * {@link org.apache.tapestry.services.Infrastructure}object. The locator for this provider is the
030     * name of a property of the Infrastructure.
031     * 
032     * @author Howard Lewis Ship
033     * @since 4.0
034     */
035    
036    public class InfrastructureObjectProvider implements ObjectProvider
037    {
038        private ErrorLog _errorLog;
039    
040        private Infrastructure _infrastructure;
041    
042        private Map _cache = new HashMap();
043    
044        public synchronized Object provideObject(Module contributingModule, Class propertyType,
045                String locator, Location location)
046        {
047            Object result = _cache.get(locator);
048    
049            if (result == null)
050            {
051                result = readProperty(locator, location);
052                _cache.put(locator, result);
053            }
054    
055            return result;
056        }
057    
058        Object readProperty(String locator, Location location)
059        {
060            try
061            {            
062                if (PropertyUtils.isReadable(_infrastructure, locator))
063                    return PropertyUtils.read(_infrastructure, locator);
064    
065                return _infrastructure.getProperty(locator);
066            }
067            catch (Throwable ex)
068            {
069                _errorLog.error(ImplMessages.unableToReadInfrastructureProperty(
070                        locator,
071                        _infrastructure,
072                        ex), location, ex);
073    
074                return null;
075            }
076        }
077    
078        public void setErrorLog(ErrorLog errorLog)
079        {
080            _errorLog = errorLog;
081        }
082    
083        public void setInfrastructure(Infrastructure infrastructure)
084        {
085            _infrastructure = infrastructure;
086        }
087    
088    }