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.hivemind.util.Defense;
019    import org.apache.tapestry.services.ComponentRenderWorker;
020    import org.apache.tapestry.spec.IComponentSpecification;
021    
022    /**
023     * Injects the component's render worker chain as the
024     * {@link org.apache.tapestry.IComponent renderWorker}property.
025     * 
026     * @author jkuhnert
027     * @since 4.1
028     */
029    public class InjectRenderWorker implements EnhancementWorker
030    {
031    
032        public static final String PROPERTY_NAME = "renderWorker";
033        
034        private ErrorLog _errorLog;
035        
036        private ComponentRenderWorker _renderWorker;
037        
038        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
039        {
040            try
041            {
042                injectRenderWorker(op, spec);
043            }
044            catch (Exception ex)
045            {
046                _errorLog.error(EnhanceMessages.errorAddingProperty(PROPERTY_NAME, op.getBaseClass(), ex), 
047                        spec.getLocation(), ex);
048            }
049        }
050        
051        public void injectRenderWorker(EnhancementOperation op, IComponentSpecification spec)
052        {
053            Defense.notNull(op, "op");
054            Defense.notNull(spec, "spec");
055            
056            op.claimReadonlyProperty(PROPERTY_NAME);
057            
058            String fieldName = op.addInjectedField("_$" + PROPERTY_NAME, ComponentRenderWorker.class, _renderWorker);
059            
060            EnhanceUtils.createSimpleAccessor(op, fieldName,
061                    PROPERTY_NAME, ComponentRenderWorker.class,
062                    spec.getLocation());
063        }
064        
065        public void setErrorLog(ErrorLog errorLog)
066        {
067            _errorLog = errorLog;
068        }
069        
070        public void setRenderWorker(ComponentRenderWorker worker)
071        {
072            _renderWorker = worker;
073        }
074    }