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
015 package org.apache.tapestry5.ioc.internal;
016
017 import org.apache.tapestry5.ioc.ObjectCreator;
018 import org.apache.tapestry5.ioc.ServiceBuilderResources;
019 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
020 import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
021
022 import 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")
030 public 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 protected Object createInstance(Class clazz)
043 {
044 final Constructor constructor = InternalUtils.findAutobuildConstructor(clazz);
045
046 if (constructor == null)
047 throw new RuntimeException(String.format(
048 "Service implementation class %s does not have a suitable public constructor.", clazz.getName()));
049
050 ObjectCreator constructorServiceCreator = new ConstructorServiceCreator(resources, constructor.toString(),
051 constructor);
052
053 return constructorServiceCreator.createObject();
054 }
055 }