Source Code Cross Referenced for OpenEntityManagerInViewFilter.java in  » J2EE » spring-framework-2.0.6 » org » springframework » orm » jpa » support » 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.orm.jpa.support 
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.orm.jpa.support;
018:
019:        import java.io.IOException;
020:
021:        import javax.persistence.EntityManager;
022:        import javax.persistence.EntityManagerFactory;
023:        import javax.persistence.PersistenceException;
024:        import javax.servlet.FilterChain;
025:        import javax.servlet.ServletException;
026:        import javax.servlet.http.HttpServletRequest;
027:        import javax.servlet.http.HttpServletResponse;
028:
029:        import org.springframework.dao.DataAccessResourceFailureException;
030:        import org.springframework.orm.jpa.EntityManagerHolder;
031:        import org.springframework.transaction.support.TransactionSynchronizationManager;
032:        import org.springframework.web.context.WebApplicationContext;
033:        import org.springframework.web.context.support.WebApplicationContextUtils;
034:        import org.springframework.web.filter.OncePerRequestFilter;
035:
036:        /**
037:         * Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the
038:         * entire processing of the request. Intended for the "Open EntityManager in
039:         * View" pattern, i.e. to allow for lazy loading in web views despite the
040:         * original transactions already being completed.
041:         *
042:         * <p>This filter makes JPA EntityManagers available via the current thread,
043:         * which will be autodetected by transaction managers. It is suitable for service
044:         * layer transactions via {@link org.springframework.orm.jpa.JpaTransactionManager}
045:         * or {@link org.springframework.transaction.jta.JtaTransactionManager} as well
046:         * as for non-transactional read-only execution.
047:         *
048:         * <p>Looks up the EntityManagerFactory in Spring's root web application context.
049:         * Supports a "entityManagerFactoryBeanName" filter init-param in <code>web.xml</code>;
050:         * the default bean name is "entityManagerFactory". Looks up the EntityManagerFactory
051:         * on each request, to avoid initialization order issues (when using ContextLoaderServlet,
052:         * the root application context will get initialized <i>after</i> this filter).
053:         *
054:         * @author Juergen Hoeller
055:         * @since 2.0
056:         * @see OpenEntityManagerInViewInterceptor
057:         * @see org.springframework.orm.jpa.JpaInterceptor
058:         * @see org.springframework.orm.jpa.JpaTransactionManager
059:         * @see org.springframework.orm.jpa.JpaTemplate#execute
060:         * @see org.springframework.orm.jpa.SharedEntityManagerCreator
061:         * @see org.springframework.transaction.support.TransactionSynchronizationManager
062:         */
063:        public class OpenEntityManagerInViewFilter extends OncePerRequestFilter {
064:
065:            public static final String DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME = "entityManagerFactory";
066:
067:            private String entityManagerFactoryBeanName = DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME;
068:
069:            /**
070:             * Set the bean name of the EntityManagerFactory to fetch from Spring's
071:             * root application context. Default is "entityManagerFactory".
072:             * @see #DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME
073:             */
074:            public void setEntityManagerFactoryBeanName(
075:                    String entityManagerFactoryBeanName) {
076:                this .entityManagerFactoryBeanName = entityManagerFactoryBeanName;
077:            }
078:
079:            /**
080:             * Return the bean name of the EntityManagerFactory to fetch from Spring's
081:             * root application context.
082:             */
083:            protected String getEntityManagerFactoryBeanName() {
084:                return this .entityManagerFactoryBeanName;
085:            }
086:
087:            protected void doFilterInternal(HttpServletRequest request,
088:                    HttpServletResponse response, FilterChain filterChain)
089:                    throws ServletException, IOException {
090:
091:                EntityManagerFactory emf = lookupEntityManagerFactory(request);
092:                boolean participate = false;
093:
094:                if (TransactionSynchronizationManager.hasResource(emf)) {
095:                    // Do not modify the EntityManager: just set the participate flag.
096:                    participate = true;
097:                } else {
098:                    logger
099:                            .debug("Opening JPA EntityManager in OpenEntityManagerInViewFilter");
100:                    try {
101:                        EntityManager em = createEntityManager(emf);
102:                        TransactionSynchronizationManager.bindResource(emf,
103:                                new EntityManagerHolder(em));
104:                    } catch (PersistenceException ex) {
105:                        throw new DataAccessResourceFailureException(
106:                                "Could not create JPA EntityManager", ex);
107:                    }
108:                }
109:
110:                try {
111:                    filterChain.doFilter(request, response);
112:                }
113:
114:                finally {
115:                    if (!participate) {
116:                        EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
117:                                .unbindResource(emf);
118:                        logger
119:                                .debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter");
120:                        emHolder.getEntityManager().close();
121:                    }
122:                }
123:            }
124:
125:            /**
126:             * Look up the EntityManagerFactory that this filter should use,
127:             * taking the current HTTP request as argument.
128:             * <p>Default implementation delegates to the <code>lookupEntityManagerFactory</code>
129:             * without arguments.
130:             * @return the EntityManagerFactory to use
131:             * @see #lookupEntityManagerFactory()
132:             */
133:            protected EntityManagerFactory lookupEntityManagerFactory(
134:                    HttpServletRequest request) {
135:                return lookupEntityManagerFactory();
136:            }
137:
138:            /**
139:             * Look up the EntityManagerFactory that this filter should use.
140:             * The default implementation looks for a bean with the specified name
141:             * in Spring's root application context.
142:             * @return the EntityManagerFactory to use
143:             * @see #getEntityManagerFactoryBeanName
144:             */
145:            protected EntityManagerFactory lookupEntityManagerFactory() {
146:                if (logger.isDebugEnabled()) {
147:                    logger.debug("Using EntityManagerFactory '"
148:                            + getEntityManagerFactoryBeanName()
149:                            + "' for OpenEntityManagerInViewFilter");
150:                }
151:                WebApplicationContext wac = WebApplicationContextUtils
152:                        .getRequiredWebApplicationContext(getServletContext());
153:                return (EntityManagerFactory) wac.getBean(
154:                        getEntityManagerFactoryBeanName(),
155:                        EntityManagerFactory.class);
156:            }
157:
158:            /**
159:             * Create a JPA EntityManager to be bound to a request.
160:             * <p>Can be overridden in subclasses.
161:             * @param emf the EntityManagerFactory to use
162:             * @see javax.persistence.EntityManagerFactory#createEntityManager()
163:             */
164:            protected EntityManager createEntityManager(EntityManagerFactory emf) {
165:                return emf.createEntityManager();
166:            }
167:
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.