001    // Copyright 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.enhance;
016    
017    import org.apache.hivemind.ErrorLog;
018    import org.apache.tapestry.spec.IComponentSpecification;
019    import org.apache.tapestry.spec.InjectSpecification;
020    
021    import java.util.Iterator;
022    import java.util.Map;
023    
024    /**
025     * Iterates over the {@link org.apache.tapestry.spec.InjectSpecification}s and locates and
026     * delegates to a {@link org.apache.tapestry.enhance.InjectEnhancementWorker} for each one.
027     *
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    public class DispatchToInjectWorker implements EnhancementWorker
032    {
033        private ErrorLog _errorLog;
034    
035        private Map _injectWorkers;
036    
037        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
038        {
039            Iterator i = spec.getInjectSpecifications().iterator();
040    
041            while (i.hasNext())
042            {
043                InjectSpecification is = (InjectSpecification) i.next();
044    
045                invokeWorker(op, is);
046            }
047        }
048    
049        private void invokeWorker(EnhancementOperation op, InjectSpecification spec)
050        {
051            try
052            {
053                InjectEnhancementWorker worker = (InjectEnhancementWorker) _injectWorkers.get(spec.getType());
054    
055                if (worker == null)
056                {
057                    _errorLog.error(EnhanceMessages.unknownInjectType(spec.getProperty(), spec.getType()), spec.getLocation(), null);
058                    return;
059                }
060    
061                worker.performEnhancement(op, spec);
062            }
063            catch (Exception ex)
064            {
065                _errorLog.error(EnhanceMessages.errorAddingProperty(spec.getProperty(), op.getBaseClass(), ex),
066                                spec.getLocation(), ex);
067            }
068        }
069    
070        public void setErrorLog(ErrorLog errorLog)
071        {
072            _errorLog = errorLog;
073        }
074    
075        public void setInjectWorkers(Map injectWorkers)
076        {
077            _injectWorkers = injectWorkers;
078        }
079    }