Source Code Cross Referenced for AbstractFactoryBean.java in  » J2EE » spring-framework-2.0.6 » 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.0.6 » 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.InvocationHandler;
020:        import java.lang.reflect.InvocationTargetException;
021:        import java.lang.reflect.Method;
022:        import java.lang.reflect.Proxy;
023:
024:        import org.apache.commons.logging.Log;
025:        import org.apache.commons.logging.LogFactory;
026:
027:        import org.springframework.beans.SimpleTypeConverter;
028:        import org.springframework.beans.TypeConverter;
029:        import org.springframework.beans.factory.BeanFactory;
030:        import org.springframework.beans.factory.BeanFactoryAware;
031:        import org.springframework.beans.factory.DisposableBean;
032:        import org.springframework.beans.factory.FactoryBean;
033:        import org.springframework.beans.factory.FactoryBeanNotInitializedException;
034:        import org.springframework.beans.factory.InitializingBean;
035:
036:        /**
037:         * Simple template superclass for {@link FactoryBean} implementations that
038:         * creates a singleton or a prototype object, depending on a flag.
039:         *
040:         * <p>If the "singleton" flag is <code>true</code> (the default),
041:         * this class will create the object that it creates exactly once
042:         * on initialization and subsequently return said singleton instance
043:         * on all calls to the {@link #getObject()} method.
044:         * 
045:         * <p>Else, this class will create a new instance every time the
046:         * {@link #getObject()} method is invoked. Subclasses are responsible
047:         * for implementing the abstract {@link #createInstance()} template
048:         * method to actually create the object(s) to expose.
049:         *
050:         * @author Juergen Hoeller
051:         * @author Keith Donald
052:         * @since 1.0.2
053:         * @see #setSingleton(boolean)
054:         * @see #createInstance()
055:         */
056:        public abstract class AbstractFactoryBean implements  FactoryBean,
057:                BeanFactoryAware, InitializingBean, DisposableBean {
058:
059:            /** Logger available to subclasses */
060:            protected final Log logger = LogFactory.getLog(getClass());
061:
062:            private boolean singleton = true;
063:
064:            private BeanFactory beanFactory;
065:
066:            private boolean initialized = false;
067:
068:            private Object singletonInstance;
069:
070:            private Object earlySingletonInstance;
071:
072:            /**
073:             * Set if a singleton should be created, or a new object
074:             * on each request else. Default is <code>true</code>  (a singleton).
075:             */
076:            public void setSingleton(boolean singleton) {
077:                this .singleton = singleton;
078:            }
079:
080:            public boolean isSingleton() {
081:                return this .singleton;
082:            }
083:
084:            public void setBeanFactory(BeanFactory beanFactory) {
085:                this .beanFactory = beanFactory;
086:            }
087:
088:            /**
089:             * Return the BeanFactory that this bean runs in.
090:             */
091:            protected BeanFactory getBeanFactory() {
092:                return this .beanFactory;
093:            }
094:
095:            /**
096:             * Obtain a bean type converter from the BeanFactory that this bean
097:             * runs in. This is typically a fresh instance for each call,
098:             * since TypeConverters are usually <i>not</i> thread-safe.
099:             * <p>Falls back to a SimpleTypeConverter when not running in a BeanFactory.
100:             * @see ConfigurableBeanFactory#getTypeConverter()
101:             * @see org.springframework.beans.SimpleTypeConverter
102:             */
103:            protected TypeConverter getBeanTypeConverter() {
104:                BeanFactory beanFactory = getBeanFactory();
105:                if (beanFactory instanceof  ConfigurableBeanFactory) {
106:                    return ((ConfigurableBeanFactory) beanFactory)
107:                            .getTypeConverter();
108:                } else {
109:                    return new SimpleTypeConverter();
110:                }
111:            }
112:
113:            /**
114:             * Eagerly create the singleton instance, if necessary.
115:             */
116:            public void afterPropertiesSet() throws Exception {
117:                if (isSingleton()) {
118:                    this .initialized = true;
119:                    this .singletonInstance = createInstance();
120:                    this .earlySingletonInstance = null;
121:                }
122:            }
123:
124:            /**
125:             * Expose the singleton instance or create a new prototype instance.
126:             * @see #createInstance()
127:             * @see #getEarlySingletonInterfaces()
128:             */
129:            public final Object getObject() throws Exception {
130:                if (isSingleton()) {
131:                    return (this .initialized ? this .singletonInstance
132:                            : getEarlySingletonInstance());
133:                } else {
134:                    return createInstance();
135:                }
136:            }
137:
138:            /**
139:             * Determine an 'eager singleton' instance, exposed in case of a
140:             * circular reference. Not called in a non-circular scenario.
141:             */
142:            private Object getEarlySingletonInstance() throws Exception {
143:                Class[] ifcs = getEarlySingletonInterfaces();
144:                if (ifcs == null) {
145:                    throw new FactoryBeanNotInitializedException(getClass()
146:                            .getName()
147:                            + " does not support circular references");
148:                }
149:                if (this .earlySingletonInstance == null) {
150:                    this .earlySingletonInstance = Proxy.newProxyInstance(
151:                            getClass().getClassLoader(), ifcs,
152:                            new InvocationHandler() {
153:                                public Object invoke(Object proxy,
154:                                        Method method, Object[] args)
155:                                        throws Throwable {
156:                                    try {
157:                                        return method.invoke(
158:                                                getSingletonInstance(), args);
159:                                    } catch (InvocationTargetException ex) {
160:                                        throw ex.getTargetException();
161:                                    }
162:                                }
163:                            });
164:                }
165:                return this .earlySingletonInstance;
166:            }
167:
168:            /**
169:             * Expose the singleton instance (for access through the 'early singleton' proxy).
170:             * @return the singleton instance that this FactoryBean holds
171:             * @throws IllegalStateException if the singleton instance is not initialized
172:             */
173:            private Object getSingletonInstance() throws IllegalStateException {
174:                if (!this .initialized) {
175:                    throw new IllegalStateException(
176:                            "Singleton instance not initialized yet");
177:                }
178:                return this .singletonInstance;
179:            }
180:
181:            /**
182:             * Destroy the singleton instance, if any.
183:             * @see #destroyInstance(Object)
184:             */
185:            public void destroy() throws Exception {
186:                if (isSingleton()) {
187:                    destroyInstance(this .singletonInstance);
188:                }
189:            }
190:
191:            /**
192:             * This abstract method declaration shadows the method in the FactoryBean interface.
193:             * This is necessary to make the <code>getEarlySingletonInterfaces</code> implementation
194:             * in this class work on Sun's JDK 1.3 classic VM, which can't find the method
195:             * when executing <code>getEarlySingletonInterfaces</code> else.
196:             * @see org.springframework.beans.factory.FactoryBean#getObjectType()
197:             * @see #getEarlySingletonInterfaces()
198:             */
199:            public abstract Class getObjectType();
200:
201:            /**
202:             * Template method that subclasses must override to construct
203:             * the object returned by this factory.
204:             * <p>Invoked on initialization of this FactoryBean in case of
205:             * a singleton; else, on each {@link #getObject()} call.
206:             * @return the object returned by this factory
207:             * @throws Exception if an exception occured during object creation
208:             * @see #getObject()
209:             */
210:            protected abstract Object createInstance() throws Exception;
211:
212:            /**
213:             * Return an array of interfaces that a singleton object exposed by this
214:             * FactoryBean is supposed to implement, for use with an 'early singleton
215:             * proxy' that will be exposed in case of a circular reference.
216:             * <p>The default implementation returns this FactoryBean's object type,
217:             * provided that it is an interface, or <code>null</code> else. The latter
218:             * indicates that early singleton access is not supported by this FactoryBean.
219:             * This will lead to a FactoryBeanNotInitializedException getting thrown.
220:             * @return the interfaces to use for 'early singletons',
221:             * or <code>null</code> to indicate a FactoryBeanNotInitializedException
222:             * @see org.springframework.beans.factory.FactoryBeanNotInitializedException
223:             */
224:            protected Class[] getEarlySingletonInterfaces() {
225:                Class type = getObjectType();
226:                return (type != null && type.isInterface() ? new Class[] { type }
227:                        : null);
228:            }
229:
230:            /**
231:             * Callback for destroying a singleton instance. Subclasses may
232:             * override this to destroy the previously created instance.
233:             * <p>The default implementation is empty.
234:             * @param instance the singleton instance, as returned by
235:             * {@link #createInstance()}
236:             * @throws Exception in case of shutdown errors
237:             * @see #createInstance()
238:             */
239:            protected void destroyInstance(Object instance) throws Exception {
240:            }
241:
242:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.