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.hivemind.service.impl;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.hivemind.Location;
021    import org.apache.hivemind.ServiceImplementationFactoryParameters;
022    import org.apache.hivemind.internal.Module;
023    import org.apache.hivemind.schema.Translator;
024    import org.apache.hivemind.util.ConstructorUtils;
025    
026    /**
027     * Implementation of {@link org.apache.hivemind.service.impl.BuilderFacet} that stores a value. This
028     * corresponds to the <set> type elements and all constructor parameter elements. The value is
029     * not resolved until needed using a specified {@link Translator}.
030     * 
031     * @author Howard Lewis Ship
032     */
033    public class BuilderPropertyFacet extends BuilderFacet
034    {
035        private String _translatorName;
036    
037        private String _literalValue;
038    
039        /**
040         * Cache for translated values to prevent calling
041         * {@link Translator#translate(Module, Class, String, Location)} twice.
042         */
043        private Map _valuesCache = new HashMap();
044    
045        public Object getFacetValue(ServiceImplementationFactoryParameters factoryParameters,
046                Class targetType)
047        {
048            Object result = _valuesCache.get(targetType);
049    
050            if (result == null)
051            {
052                Translator translator = factoryParameters.getInvokingModule().getTranslator(
053                        _translatorName);
054    
055                result = translator.translate(
056                        factoryParameters.getInvokingModule(),
057                        targetType,
058                        _literalValue,
059                        getLocation());
060    
061                _valuesCache.put(targetType, result);
062            }
063    
064            return result;
065        }
066    
067        public boolean isAssignableToType(ServiceImplementationFactoryParameters factoryParameters,
068                Class targetType)
069        {
070            // TODO should Translator declare an analoguous isAssignableToType method?
071            Object facetValue = getFacetValue(factoryParameters, targetType);
072    
073            if (facetValue == null)
074                return !targetType.isPrimitive();
075    
076            return ConstructorUtils.isCompatible(targetType, facetValue.getClass());
077        }
078    
079        /** @since 1.1 */
080        public void setTranslator(String translatorName)
081        {
082            _translatorName = translatorName;
083        }
084    
085        public void setValue(String value)
086        {
087            _literalValue = value;
088        }
089    
090    }