001// Copyright 2006, 2007, 2008, 2009, 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.ioc.ObjectCreator; 018import org.apache.tapestry5.ioc.ServiceBuilderResources; 019import org.apache.tapestry5.ioc.internal.util.InternalUtils; 020 021import java.lang.reflect.Method; 022 023/** 024 * Basic implementation of {@link org.apache.tapestry5.ioc.ObjectCreator} that handles invoking a method on the module 025 * builder, and figures out the correct parameters to pass into the annotated method. 026 */ 027public class ServiceBuilderMethodInvoker extends AbstractServiceCreator 028{ 029 private final Method builderMethod; 030 031 public ServiceBuilderMethodInvoker(ServiceBuilderResources resources, String creatorDescription, Method method) 032 { 033 super(resources, creatorDescription); 034 035 builderMethod = method; 036 } 037 038 private ObjectCreator<Object> plan; 039 040 private ObjectCreator<Object> getPlan() 041 { 042 if (plan == null) 043 { 044 // Defer getting (and possibly instantiating) the module instance until the last possible 045 // moment. If the method is static, there's no need to even get the builder. 046 047 final Object moduleInstance = InternalUtils.isStatic(builderMethod) ? null : resources.getModuleBuilder(); 048 049 plan = InternalUtils.createMethodInvocationPlan(resources.getTracker(), resources, createInjectionResources(), logger, "Constructing service implementation via " + creatorDescription, moduleInstance, builderMethod); 050 } 051 052 return plan; 053 } 054 055 /** 056 * Invoked from the proxy to create the actual service implementation. 057 */ 058 @Override 059 public Object createObject() 060 { 061 Object result = getPlan().createObject(); 062 063 if (result == null) 064 { 065 throw new RuntimeException(String.format("Builder method %s (for service '%s') returned null.", creatorDescription, serviceId)); 066 } 067 068 return result; 069 } 070 071 @Override 072 public String toString() 073 { 074 return creatorDescription; 075 } 076}