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 org.apache.hivemind.ErrorLog; 018import org.apache.hivemind.util.Defense; 019import 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 */ 028public class InjectSpecificationWorker implements EnhancementWorker 029{ 030 public static final String SPECIFICATION_PROPERTY_NAME = "specification"; 031 032 private ErrorLog _errorLog; 033 034 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) 035 { 036 try 037 { 038 injectSpecification(op, spec); 039 } 040 catch (Exception ex) 041 { 042 _errorLog.error(EnhanceMessages.errorAddingProperty(SPECIFICATION_PROPERTY_NAME, op 043 .getBaseClass(), ex), spec.getLocation(), ex); 044 } 045 } 046 047 public void injectSpecification(EnhancementOperation op, IComponentSpecification spec) 048 { 049 Defense.notNull(op, "op"); 050 Defense.notNull(spec, "spec"); 051 052 op.claimReadonlyProperty(SPECIFICATION_PROPERTY_NAME); 053 054 String fieldName = op.addInjectedField( 055 "_$" + SPECIFICATION_PROPERTY_NAME, 056 IComponentSpecification.class, 057 spec); 058 059 EnhanceUtils.createSimpleAccessor( 060 op, 061 fieldName, 062 SPECIFICATION_PROPERTY_NAME, 063 IComponentSpecification.class, 064 spec.getLocation()); 065 } 066 067 public void setErrorLog(ErrorLog errorLog) 068 { 069 _errorLog = errorLog; 070 } 071}