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.spec.IComponentSpecification;
020    
021    /**
022     * Injects the component's specification as the
023     * {@link org.apache.tapestry.IComponent#getSpecification() specification}property.
024     * 
025     * @author Howard M. Lewis Ship
026     * @since 4.0
027     */
028    public class InjectSpecificationWorker implements EnhancementWorker
029    {
030    
031        public static final String SPECIFICATION_PROPERTY_NAME = "specification";
032    
033        private ErrorLog _errorLog;
034    
035        public void performEnhancement(EnhancementOperation op,
036                IComponentSpecification spec)
037        {
038            try
039            {
040                injectSpecification(op, spec);
041            }
042            catch (Exception ex)
043            {
044                _errorLog.error(EnhanceMessages.errorAddingProperty(
045                        SPECIFICATION_PROPERTY_NAME, op.getBaseClass(), ex), spec
046                        .getLocation(), ex);
047            }
048        }
049    
050        public void injectSpecification(EnhancementOperation op,
051                IComponentSpecification spec)
052        {
053            Defense.notNull(op, "op");
054            Defense.notNull(spec, "spec");
055            
056            op.claimReadonlyProperty(SPECIFICATION_PROPERTY_NAME);
057            
058            String fieldName = op.addInjectedField("_$"
059                    + SPECIFICATION_PROPERTY_NAME, IComponentSpecification.class,
060                    spec);
061    
062            EnhanceUtils.createSimpleAccessor(op, fieldName,
063                    SPECIFICATION_PROPERTY_NAME, IComponentSpecification.class,
064                    spec.getLocation());
065        }
066    
067        public void setErrorLog(ErrorLog errorLog)
068        {
069            _errorLog = errorLog;
070        }
071    }