001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration;
023:
024: import org.hibernate.Session;
025: import org.hibernate.SessionFactory;
026: import org.hibernate.StatelessSession;
027: import org.hibernate.Transaction;
028: import org.hibernate.cfg.Configuration;
029:
030: /**
031: * @author <a href="mailto:boleslaw.dawidowicz@jboss.org">Boleslaw Dawidowicz</a>
032: * @version $Revision: 8784 $
033: */
034: public class MigrationModule {
035: protected SessionFactory fromSessionFactory;
036: protected SessionFactory toSessionFactory;
037: protected Session fromSession;
038: protected Session toSession;
039: protected StatelessSession fromStatelessSession;
040: protected StatelessSession toStatelessSession;
041: protected Transaction fromTx;
042: protected Transaction toTx;
043: protected Configuration fromConfiguration;
044: protected Configuration toConfiguration;
045:
046: public Configuration getFromConfiguration() {
047: return fromConfiguration;
048: }
049:
050: public void setFromConfiguration(Configuration fromConfiguration) {
051: this .fromConfiguration = fromConfiguration;
052: }
053:
054: public Configuration getToConfiguration() {
055: return toConfiguration;
056: }
057:
058: public void setToConfiguration(Configuration toConfiguration) {
059: this .toConfiguration = toConfiguration;
060: }
061:
062: public SessionFactory getFromSessionFactory() {
063: return fromSessionFactory;
064: }
065:
066: public void setFromSessionFactory(SessionFactory fromSessionFactory) {
067: this .fromSessionFactory = fromSessionFactory;
068: }
069:
070: public SessionFactory getToSessionFactory() {
071: return toSessionFactory;
072: }
073:
074: public void setToSessionFactory(SessionFactory toSessionFactory) {
075: this .toSessionFactory = toSessionFactory;
076: }
077:
078: public void nextFromSession() throws Exception {
079: if (fromTx != null && (fromSession != null)) {
080: if (fromTx.isActive()) {
081: fromTx.commit();
082: }
083: fromSession.close();
084: }
085: fromSession = fromSessionFactory.openSession();
086: fromTx = fromSession.beginTransaction();
087: }
088:
089: public void nextToSession() throws Exception {
090: if (toTx != null && (toSession != null)) {
091: if (toTx.isActive()) {
092: toTx.commit();
093: }
094: toSession.close();
095: }
096: toSession = toSessionFactory.openSession();
097: toTx = toSession.beginTransaction();
098: }
099:
100: public void nextFromStatelessSession() throws Exception {
101: if (fromTx != null && (fromStatelessSession != null)) {
102: if (fromTx.isActive()) {
103: fromTx.commit();
104: }
105: fromStatelessSession.close();
106: }
107: fromStatelessSession = fromSessionFactory
108: .openStatelessSession();
109: fromTx = fromStatelessSession.beginTransaction();
110: }
111:
112: public void nextToStatelessSession() throws Exception {
113: if (toTx != null && (toStatelessSession != null)) {
114: if (toTx.isActive()) {
115: toTx.commit();
116: }
117: toStatelessSession.close();
118: }
119: toStatelessSession = toSessionFactory.openStatelessSession();
120: toTx = toStatelessSession.beginTransaction();
121: }
122:
123: public Session getCurrentFromSession() {
124: return fromSession;
125: }
126:
127: public Session getCurrentToSession() {
128: return toSession;
129: }
130:
131: protected void tearDown() throws Exception {
132: fromTx.commit();
133: fromSession.close();
134: fromSessionFactory.close();
135: toTx.commit();
136: toSession.close();
137: toSessionFactory.close();
138: }
139:
140: /** @throws Exception */
141: public void close() throws Exception {
142: if (fromTx != null && (fromSession != null)) {
143: if (fromTx.isActive()) {
144: fromTx.commit();
145: }
146: fromSession.close();
147: }
148: // if(fromSessionFactory != null)
149: // {
150: // fromSessionFactory.close();
151: // }
152:
153: if (toTx != null && (toSession != null)) {
154: if (toTx.isActive()) {
155: toTx.commit();
156: }
157: toSession.close();
158: }
159: }
160: }
|