    001// Copyright 2010, 2012 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.commons.services.PlasticProxyFactory;
    019import org.apache.tapestry5.ioc.ServiceBuilderResources;
    020import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    021
    022import java.lang.reflect.Constructor;
    023
    024/**
    025 * Returns an {@link ObjectCreator} for lazily instantiating a given implementation class (with dependencies).
    026 * Once an instance is instantiated, it is cached ... until any underlying .class file changes, at which point
    027 * the class (and its class dependencies, such as base classes) are reloaded and a new instance instantiated.
    028 */
    029@SuppressWarnings("all")
    030public class ReloadableServiceImplementationObjectCreator extends AbstractReloadableObjectCreator
    031{
    032    private final ServiceBuilderResources resources;
    033
    034    public ReloadableServiceImplementationObjectCreator(PlasticProxyFactory proxyFactory, ServiceBuilderResources resources, ClassLoader baseClassLoader,
    035                                                        String implementationClassName)
    036    {
    037        super(proxyFactory, baseClassLoader, implementationClassName, resources.getLogger(), resources.getTracker());
    038
    039        this.resources = resources;
    040    }
    041
    042    @Override
    043    protected Object createInstance(Class clazz)
    044    {
    045        final Constructor constructor = InternalUtils.findAutobuildConstructor(clazz);
    046
    047        if (constructor == null)
    048            throw new RuntimeException(String.format(
    049                    "Service implementation class %s does not have a suitable public constructor.", clazz.getName()));
    050
    051        ObjectCreator constructorServiceCreator = new ConstructorServiceCreator(resources, constructor.toString(),
    052                constructor);
    053
    054        return constructorServiceCreator.createObject();
    055    }
    056}