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
015package org.apache.tapestry.enhance;
016
017import java.util.Iterator;
018import java.util.Map;
019
020import org.apache.hivemind.ErrorLog;
021import org.apache.tapestry.spec.IComponentSpecification;
022import org.apache.tapestry.spec.InjectSpecification;
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 */
031public 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
054                    .getType());
055
056            if (worker == null)
057            {
058                _errorLog.error(EnhanceMessages.unknownInjectType(spec.getProperty(), spec
059                        .getType()), spec.getLocation(), null);
060                return;
061            }
062
063            worker.performEnhancement(op, spec);
064
065        }
066        catch (Exception ex)
067        {
068            _errorLog.error(EnhanceMessages.errorAddingProperty(spec.getProperty(), op
069                    .getBaseClass(), ex), spec.getLocation(), ex);
070        }
071    }
072
073    public void setErrorLog(ErrorLog errorLog)
074    {
075        _errorLog = errorLog;
076    }
077
078    public void setInjectWorkers(Map injectWorkers)
079    {
080        _injectWorkers = injectWorkers;
081    }
082}