001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import java.lang.reflect.InvocationTargetException;
020:
021: import org.springframework.beans.TypeConverter;
022: import org.springframework.beans.factory.BeanClassLoaderAware;
023: import org.springframework.beans.factory.BeanFactory;
024: import org.springframework.beans.factory.BeanFactoryAware;
025: import org.springframework.beans.factory.FactoryBean;
026: import org.springframework.beans.factory.FactoryBeanNotInitializedException;
027: import org.springframework.beans.factory.InitializingBean;
028: import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
029: import org.springframework.util.ClassUtils;
030:
031: /**
032: * FactoryBean which returns a value which is the result of a static or instance
033: * method invocation. For most use cases it is better to just use the container's
034: * built-in factory method support for the same purpose, since that is smarter at
035: * converting arguments. This factory bean is still useful though when you need to
036: * call a method which doesn't return any value (for example, a static class method
037: * to force some sort of initialization to happen). This use case is not supported
038: * by factory methods, since a return value is needed to obtain the bean instance.
039: *
040: * <p>Note that as it is expected to be used mostly for accessing factory methods,
041: * this factory by default operates in a <b>singleton</b> fashion. The first request
042: * to {@link #getObject} by the owning bean factory will cause a method invocation,
043: * whose return value will be cached for subsequent requests. An internal
044: * {@link #setSingleton singleton} property may be set to "false", to cause this
045: * factory to invoke the target method each time it is asked for an object.
046: *
047: * <p>A static target method may be specified by setting the
048: * {@link #setTargetMethod targetMethod} property to a String representing the static
049: * method name, with {@link #setTargetClass targetClass} specifying the Class that
050: * the static method is defined on. Alternatively, a target instance method may be
051: * specified, by setting the {@link #setTargetObject targetObject} property as the target
052: * object, and the {@link #setTargetMethod targetMethod} property as the name of the
053: * method to call on that target object. Arguments for the method invocation may be
054: * specified by setting the {@link #setArguments arguments} property.
055: *
056: * <p>This class depends on {@link #afterPropertiesSet()} being called once
057: * all properties have been set, as per the InitializingBean contract.
058: *
059: * <p>An example (in an XML based bean factory definition) of a bean definition
060: * which uses this class to call a static factory method:
061: *
062: * <pre class="code">
063: * <bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
064: * <property name="staticMethod"><value>com.whatever.MyClassFactory.getInstance</value></property>
065: * </bean></pre>
066: *
067: * <p>An example of calling a static method then an instance method to get at a
068: * Java system property. Somewhat verbose, but it works.
069: *
070: * <pre class="code">
071: * <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
072: * <property name="targetClass"><value>java.lang.System</value></property>
073: * <property name="targetMethod"><value>getProperties</value></property>
074: * </bean>
075: *
076: * <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
077: * <property name="targetObject"><ref local="sysProps"/></property>
078: * <property name="targetMethod"><value>getProperty</value></property>
079: * <property name="arguments">
080: * <list>
081: * <value>java.version</value>
082: * </list>
083: * </property>
084: * </bean></pre>
085: *
086: * @author Colin Sampaleanu
087: * @author Juergen Hoeller
088: * @since 21.11.2003
089: */
090: public class MethodInvokingFactoryBean extends
091: ArgumentConvertingMethodInvoker implements FactoryBean,
092: BeanClassLoaderAware, BeanFactoryAware, InitializingBean {
093:
094: private boolean singleton = true;
095:
096: private ClassLoader beanClassLoader = ClassUtils
097: .getDefaultClassLoader();
098:
099: private ConfigurableBeanFactory beanFactory;
100:
101: private boolean initialized = false;
102:
103: /** Method call result in the singleton case */
104: private Object singletonObject;
105:
106: /**
107: * Set if a singleton should be created, or a new object on each
108: * request else. Default is "true".
109: */
110: public void setSingleton(boolean singleton) {
111: this .singleton = singleton;
112: }
113:
114: public boolean isSingleton() {
115: return this .singleton;
116: }
117:
118: public void setBeanClassLoader(ClassLoader classLoader) {
119: this .beanClassLoader = classLoader;
120: }
121:
122: protected Class resolveClassName(String className)
123: throws ClassNotFoundException {
124: return ClassUtils.forName(className, this .beanClassLoader);
125: }
126:
127: public void setBeanFactory(BeanFactory beanFactory) {
128: if (beanFactory instanceof ConfigurableBeanFactory) {
129: this .beanFactory = (ConfigurableBeanFactory) beanFactory;
130: }
131: }
132:
133: /**
134: * Obtain the TypeConverter from the BeanFactory that this bean runs in,
135: * if possible.
136: * @see ConfigurableBeanFactory#getTypeConverter()
137: */
138: protected TypeConverter getDefaultTypeConverter() {
139: if (this .beanFactory != null) {
140: return this .beanFactory.getTypeConverter();
141: } else {
142: return super .getDefaultTypeConverter();
143: }
144: }
145:
146: public void afterPropertiesSet() throws Exception {
147: prepare();
148: if (this .singleton) {
149: this .initialized = true;
150: this .singletonObject = doInvoke();
151: }
152: }
153:
154: /**
155: * Perform the invocation and convert InvocationTargetException
156: * into the underlying target exception.
157: */
158: private Object doInvoke() throws Exception {
159: try {
160: return invoke();
161: } catch (InvocationTargetException ex) {
162: if (ex.getTargetException() instanceof Exception) {
163: throw (Exception) ex.getTargetException();
164: }
165: if (ex.getTargetException() instanceof Error) {
166: throw (Error) ex.getTargetException();
167: }
168: throw ex;
169: }
170: }
171:
172: /**
173: * Returns the same value each time if the singleton property is set
174: * to "true", otherwise returns the value returned from invoking the
175: * specified method on the fly.
176: */
177: public Object getObject() throws Exception {
178: if (this .singleton) {
179: if (!this .initialized) {
180: throw new FactoryBeanNotInitializedException();
181: }
182: // Singleton: return shared object.
183: return this .singletonObject;
184: } else {
185: // Prototype: new object on each call.
186: return doInvoke();
187: }
188: }
189:
190: /**
191: * Return the type of object that this FactoryBean creates,
192: * or <code>null</code> if not known in advance.
193: */
194: public Class getObjectType() {
195: if (!isPrepared()) {
196: // Not fully initialized yet -> return null to indicate "not known yet".
197: return null;
198: }
199: return getPreparedMethod().getReturnType();
200: }
201:
202: }
|