001 // Copyright 2007, 2011 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.tapestry5.internal.services; 016 017 import org.apache.tapestry5.ioc.ObjectLocator; 018 import org.apache.tapestry5.ioc.annotations.Inject; 019 import org.apache.tapestry5.model.MutableComponentModel; 020 import org.apache.tapestry5.plastic.PlasticField; 021 import org.apache.tapestry5.services.transform.InjectionProvider2; 022 023 /** 024 * A very late worker related to the {@link Inject} annotation that, when all other forms of injection have failed, 025 * matches the field type to a service interface. 026 */ 027 public class ServiceInjectionProvider implements InjectionProvider2 028 { 029 private final ObjectLocator locator; 030 031 private final ComponentClassCache classCache; 032 033 public ServiceInjectionProvider(ObjectLocator locator, ComponentClassCache classCache) 034 { 035 this.locator = locator; 036 this.classCache = classCache; 037 } 038 039 public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel) 040 { 041 Class fieldType = classCache.forName(field.getTypeName()); 042 043 Object inject = this.locator.getService(fieldType); 044 045 assert inject != null; 046 047 field.inject(inject); 048 049 // If we make it this far without an exception, then we were successful 050 // and should claim the field. 051 052 return true; 053 } 054 055 }