[58] | 1 | /* Copyright 2006-2009 the original author or authors. |
---|
| 2 | * |
---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | * you may not use this file except in compliance with the License. |
---|
| 5 | * You may obtain a copy of the License at |
---|
| 6 | * |
---|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | * |
---|
| 9 | * Unless required by applicable law or agreed to in writing, software |
---|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | * See the License for the specific language governing permissions and |
---|
| 13 | * limitations under the License. |
---|
| 14 | */ |
---|
| 15 | package org.codehaus.groovy.grails.plugins.springsecurity; |
---|
| 16 | |
---|
| 17 | import org.apache.log4j.Logger; |
---|
| 18 | import org.hibernate.Session; |
---|
| 19 | import org.hibernate.SessionFactory; |
---|
| 20 | import org.springframework.orm.hibernate3.SessionFactoryUtils; |
---|
| 21 | import org.springframework.orm.hibernate3.SessionHolder; |
---|
| 22 | import org.springframework.transaction.support.TransactionSynchronizationManager; |
---|
| 23 | import org.springframework.web.context.support.WebApplicationObjectSupport; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Grails Web Application Object Support. |
---|
| 27 | * |
---|
| 28 | * @author T.Yamamoto |
---|
| 29 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
---|
| 30 | */ |
---|
| 31 | public abstract class GrailsWebApplicationObjectSupport extends WebApplicationObjectSupport { |
---|
| 32 | |
---|
| 33 | private final Logger _log = Logger.getLogger(getClass()); |
---|
| 34 | |
---|
| 35 | private SessionFactory _sessionFactory; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Dependency injection for Hibernate session factory. |
---|
| 39 | * @param sessionFactory the factory |
---|
| 40 | */ |
---|
| 41 | public void setSessionFactory(final SessionFactory sessionFactory) { |
---|
| 42 | _sessionFactory = sessionFactory; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Holds the session created or existing session and a flag indicating whether it was |
---|
| 47 | * existing (so we know whether to close it or not). |
---|
| 48 | */ |
---|
| 49 | public static class SessionContainer { |
---|
| 50 | private final Session _session; |
---|
| 51 | private final boolean _existingSession; |
---|
| 52 | |
---|
| 53 | private SessionContainer(final Session session, final boolean existingSession) { |
---|
| 54 | _session = session; |
---|
| 55 | _existingSession = existingSession; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Get the session. |
---|
| 60 | * @return the session |
---|
| 61 | */ |
---|
| 62 | public Session getSession() { |
---|
| 63 | return _session; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * Set up hibernate session. |
---|
| 69 | * @return the session container, which holds the session and a boolean indicating if the session was pre-existing |
---|
| 70 | */ |
---|
| 71 | protected SessionContainer setUpSession() { |
---|
| 72 | SessionFactory sessionFactory = getSessionFactory(); |
---|
| 73 | |
---|
| 74 | Session session; |
---|
| 75 | boolean existing; |
---|
| 76 | if (TransactionSynchronizationManager.hasResource(sessionFactory)) { |
---|
| 77 | _log.debug("Session already has transaction attached"); |
---|
| 78 | existing = true; |
---|
| 79 | session = ((SessionHolder)TransactionSynchronizationManager.getResource(sessionFactory)).getSession(); |
---|
| 80 | } |
---|
| 81 | else { |
---|
| 82 | _log.debug("Session does not have transaction attached... Creating new one"); |
---|
| 83 | existing = false; |
---|
| 84 | session = SessionFactoryUtils.getSession(sessionFactory, true); |
---|
| 85 | SessionHolder sessionHolder = new SessionHolder(session); |
---|
| 86 | TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | return new SessionContainer(session, existing); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Release Session. |
---|
| 94 | */ |
---|
| 95 | protected void releaseSession(final SessionContainer session) { |
---|
| 96 | if (session._existingSession) { |
---|
| 97 | return; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | SessionFactory sessionFactory = getSessionFactory(); |
---|
| 101 | SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.unbindResource(sessionFactory); |
---|
| 102 | SessionFactoryUtils.releaseSession(sessionHolder.getSession(), sessionFactory); |
---|
| 103 | _log.debug("Session released"); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | private SessionFactory getSessionFactory() { |
---|
| 107 | if (_sessionFactory == null) { |
---|
| 108 | // should be set via DI, but for backwards compatibility lookup the standard bean |
---|
| 109 | _sessionFactory = (SessionFactory)getWebApplicationContext().getBean("sessionFactory"); |
---|
| 110 | } |
---|
| 111 | return _sessionFactory; |
---|
| 112 | } |
---|
| 113 | } |
---|