    001// Copyright 2006, 2007, 2010, 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
    015package org.apache.tapestry5.internal.services;
    016
    017import org.apache.tapestry5.commons.AnnotationProvider;
    018import org.apache.tapestry5.commons.ObjectLocator;
    019import org.apache.tapestry5.ioc.services.MasterObjectProvider;
    020import org.apache.tapestry5.model.MutableComponentModel;
    021import org.apache.tapestry5.plastic.PlasticField;
    022import org.apache.tapestry5.services.transform.InjectionProvider2;
    023
    024import java.lang.annotation.Annotation;
    025
    026/**
    027 * Worker for the {@link org.apache.tapestry5.ioc.annotations.Inject} annotation that delegates out to the master
    028 * {@link org.apache.tapestry5.ioc.services.MasterObjectProvider} to access the value. This worker must be scheduled
    029 * after certain other workers, such as {@link BlockInjectionProvider} (which is keyed off a combination of type and
    030 * the Inject annotation).
    031 *
    032 * @see org.apache.tapestry5.ioc.services.MasterObjectProvider
    033 */
    034public class DefaultInjectionProvider implements InjectionProvider2
    035{
    036    private final MasterObjectProvider masterObjectProvider;
    037
    038    private final ObjectLocator locator;
    039
    040    private final ComponentClassCache classCache;
    041
    042    public DefaultInjectionProvider(MasterObjectProvider masterObjectProvider, ObjectLocator locator, ComponentClassCache classCache)
    043    {
    044        this.masterObjectProvider = masterObjectProvider;
    045        this.locator = locator;
    046        this.classCache = classCache;
    047    }
    048
    049    public boolean provideInjection(final PlasticField field, ObjectLocator locator, MutableComponentModel componentModel)
    050    {
    051        Class fieldType = classCache.forName(field.getTypeName());
    052
    053        Object injectionValue = masterObjectProvider.provide(fieldType, new AnnotationProvider()
    054        {
    055            public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
    056            {
    057                return field.getAnnotation(annotationClass);
    058            }
    059        }, this.locator, false);
    060
    061        // Null means that no ObjectProvider could provide the value. We have set up the chain of
    062        // command so that InjectResources can give it a try next. Later, we'll try to match against
    063        // a service.
    064
    065        if (injectionValue != null)
    066        {
    067            field.inject(injectionValue);
    068            return true;
    069        }
    070
    071        return false;
    072    }
    073}