001 // Copyright 2004, 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.ApplicationRuntimeException; 018 import org.apache.hivemind.Location; 019 import org.apache.hivemind.service.MethodSignature; 020 import org.apache.hivemind.util.Defense; 021 import org.apache.tapestry.services.InjectedValueProvider; 022 import org.apache.tapestry.spec.InjectSpecification; 023 024 import java.lang.reflect.Modifier; 025 026 /** 027 * Implementation for injection type "object" (the default). Adds read-only 028 * properties to the enhanced class that contain objects injected from HiveMind. 029 * 030 * @author Howard M. Lewis Ship 031 * @since 4.0 032 */ 033 public class InjectObjectWorker implements InjectEnhancementWorker 034 { 035 036 private InjectedValueProvider _provider; 037 038 public void performEnhancement(EnhancementOperation op, 039 InjectSpecification is) 040 { 041 String name = is.getProperty(); 042 String objectReference = is.getObject(); 043 Location location = is.getLocation(); 044 045 injectObject(op, objectReference, name, location); 046 } 047 048 public void injectObject(EnhancementOperation op, String objectReference, 049 String propertyName, Location location) 050 { 051 Defense.notNull(op, "op"); 052 Defense.notNull(propertyName, "propertyName"); 053 Defense.notNull(objectReference, "objectReference"); 054 055 Class propertyType = op.getPropertyType(propertyName); 056 if (propertyType == null) 057 propertyType = Object.class; 058 059 op.claimReadonlyProperty(propertyName); 060 061 Object injectedValue = _provider.obtainValue(objectReference, location); 062 063 if (injectedValue == null) 064 throw new ApplicationRuntimeException(EnhanceMessages 065 .locatedValueIsNull(objectReference), location, null); 066 067 if (!propertyType.isInstance(injectedValue)) 068 throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType(objectReference, injectedValue, propertyType), 069 location, null); 070 071 String fieldName = op.addInjectedField("_$" + propertyName, 072 propertyType, injectedValue); 073 074 String methodName = EnhanceUtils.createAccessorMethodName(propertyName); 075 076 op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType, 077 methodName, null, null), "return " + fieldName + ";", location); 078 } 079 080 public void setProvider(InjectedValueProvider provider) 081 { 082 _provider = provider; 083 } 084 }