    001// Copyright 2007, 2008, 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.ioc.internal;
    016
    017import org.apache.tapestry5.commons.ObjectCreator;
    018import org.apache.tapestry5.ioc.ServiceBuilderResources;
    019import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    020
    021import java.lang.reflect.Constructor;
    022
    023/**
    024 * A service creator based on an implementation class' constructor, rather than a service builder method.
    025 */
    026public class ConstructorServiceCreator extends AbstractServiceCreator
    027{
    028    private final Constructor constructor;
    029
    030    public ConstructorServiceCreator(ServiceBuilderResources resources, String creatorDescription,
    031                                     Constructor constructor)
    032    {
    033        super(resources, creatorDescription);
    034
    035        this.constructor = constructor;
    036    }
    037
    038    @Override
    039    public String toString()
    040    {
    041        return creatorDescription;
    042    }
    043
    044    private ObjectCreator<?> plan;
    045
    046    private  ObjectCreator<?> getPlan()
    047    {
    048        if (plan == null)
    049        {
    050            String description = String.format("Invoking constructor %s (for service '%s')", creatorDescription, resources.getServiceId());
    051
    052            plan = InternalUtils.createConstructorConstructionPlan(resources.getTracker(), resources, createInjectionResources(), logger,
    053                    description, constructor);
    054        }
    055
    056        return plan;
    057    }
    058
    059    @Override
    060    public Object createObject()
    061    {
    062        return getPlan().createObject();
    063    }
    064}