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