Source Code Cross Referenced for MethodInvokingFactoryBean.java in  » J2EE » spring-framework-2.5 » org » springframework » beans » factory » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.5 » org.springframework.beans.factory.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * &lt;bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
064:         *   &lt;property name="staticMethod">&lt;value>com.whatever.MyClassFactory.getInstance&lt;/value>&lt;/property>
065:         * &lt;/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:         * &lt;bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
072:         *   &lt;property name="targetClass">&lt;value>java.lang.System&lt;/value>&lt;/property>
073:         *   &lt;property name="targetMethod">&lt;value>getProperties&lt;/value>&lt;/property>
074:         * &lt;/bean>
075:         *
076:         * &lt;bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
077:         *   &lt;property name="targetObject">&lt;ref local="sysProps"/>&lt;/property>
078:         *   &lt;property name="targetMethod">&lt;value>getProperty&lt;/value>&lt;/property>
079:         *   &lt;property name="arguments">
080:         *     &lt;list>
081:         *       &lt;value>java.version&lt;/value>
082:         *     &lt;/list>
083:         *   &lt;/property>
084:         * &lt;/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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.