001: /*
002: * Copyright 2006-2007, Unitils.org
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: package org.unitils.hibernate.util;
017:
018: import java.sql.Connection;
019: import java.util.HashSet;
020: import java.util.Set;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.hibernate.HibernateException;
025: import org.hibernate.Interceptor;
026: import org.hibernate.SessionFactory;
027: import org.hibernate.classic.Session;
028: import org.unitils.core.Unitils;
029: import org.unitils.database.DatabaseModule;
030: import org.unitils.hibernate.HibernateModule;
031:
032: /**
033: * A wrapper for a Hibernate session factory that intercepts all opened session. These sessions can
034: * later be retrieved for e.g. closing them or flushing their updates to the database.
035: *
036: * @author Filip Neven
037: * @author Tim Ducheyne
038: */
039: public class SessionInterceptingSessionFactory extends
040: BaseSessionInterceptingSessionFactoryProxy {
041:
042: /* The logger instance for this class */
043: private static Log logger = LogFactory
044: .getLog(SessionInterceptingSessionFactory.class);
045:
046: /**
047: * The intercepted sessions.
048: */
049: protected Set<org.hibernate.Session> sessions = new HashSet<org.hibernate.Session>();
050:
051: /**
052: * Creates a wrapper for the given session factory.
053: *
054: * @param sessionFactory The factory, not null
055: */
056: public SessionInterceptingSessionFactory(
057: SessionFactory sessionFactory) {
058: super (sessionFactory);
059: }
060:
061: /**
062: * Opens a new hibernate session. Overriden to store the opened session.
063: *
064: * @return the session, not null
065: */
066: @Override
067: public Session openSession() throws HibernateException {
068: Session session = super .openSession();
069: sessions.add(session);
070: return session;
071: }
072:
073: /**
074: * Opens a new hibernate session. Overriden to store the opened session.
075: *
076: * @param connection The connection to use
077: * @return the session, not null
078: */
079: @Override
080: public Session openSession(Connection connection) {
081: Session session = super .openSession(connection);
082: sessions.add(session);
083: return session;
084: }
085:
086: /**
087: * Opens a new hibernate session. Overriden to store the opened session.
088: *
089: * @param connection The connection to use
090: * @param interceptor The session interceptor to use
091: * @return the session, not null
092: */
093: @Override
094: public Session openSession(Connection connection,
095: Interceptor interceptor) {
096: Session session = super .openSession(connection, interceptor);
097: sessions.add(session);
098: return session;
099: }
100:
101: /**
102: * Opens a new hibernate session. Overriden to store the opened session.
103: *
104: * @param interceptor The session interceptor to use
105: * @return the session, not null
106: */
107: @Override
108: public Session openSession(Interceptor interceptor)
109: throws HibernateException {
110: Session session = super .openSession(interceptor);
111: sessions.add(session);
112: return session;
113: }
114:
115: /**
116: * Gets the current session if <code>CurrentSessionContext</code> is configured.
117: *
118: * @return The current session
119: */
120: @Override
121: public Session getCurrentSession() throws HibernateException {
122: Session session = super .getCurrentSession();
123: sessions.add(session);
124: return session;
125: }
126:
127: /**
128: * Gets all open intercepted sessions.
129: *
130: * @return The sessions, not null
131: */
132: public Set<org.hibernate.Session> getOpenedSessions() {
133: return sessions;
134: }
135:
136: /**
137: * Closes and clears all open sessions.
138: */
139: public void closeOpenSessions() {
140: for (org.hibernate.Session session : sessions) {
141: if (session.isOpen()) {
142: logger.info("Closing hibernate session " + session);
143: session.close();
144: }
145: }
146: clearInterceptedSessions();
147: }
148:
149: /**
150: * Flushes all open sessions.
151: */
152: public void flushOpenSessions() {
153: for (org.hibernate.Session session : sessions) {
154: if (session.isOpen()) {
155: logger.info("Flushing hibernate session " + session);
156: session.flush();
157: }
158: }
159: }
160:
161: /**
162: * Removes all intercepted sessions
163: */
164: public void clearInterceptedSessions() {
165: sessions.clear();
166: }
167:
168: protected HibernateModule getHibernateModule() {
169: return Unitils.getInstance().getModulesRepository()
170: .getModuleOfType(HibernateModule.class);
171: }
172:
173: }
|