001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SessionSynchronizationListener.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.transaction;
025:
026: import java.rmi.RemoteException;
027:
028: import javax.ejb.EJBException;
029: import javax.ejb.SessionSynchronization;
030: import javax.transaction.Synchronization;
031: import static javax.transaction.Status.STATUS_COMMITTED;
032:
033: /**
034: * This listener will be notified by the transaction manager and will call
035: * methods on the bean.
036: * @author Florent Benoit
037: */
038: public class SessionSynchronizationListener implements Synchronization {
039:
040: /**
041: * Bean on which synchonization will be done.
042: */
043: private SessionSynchronization synchronizedBean;
044:
045: /**
046: * Creates a listener which will act on the given bean.
047: * @param synchronizedBean bean on which call synchronization methods.
048: */
049: public SessionSynchronizationListener(
050: final SessionSynchronization synchronizedBean) {
051: this .synchronizedBean = synchronizedBean;
052: }
053:
054: /**
055: * 4.3.11 Interceptors for Session Beans.<br>
056: * For stateful session beans that implement the SessionSynchronization
057: * interface, afterBegin occurs before any AroundInvoke method invocation,
058: * and beforeCompletion after all AroundInvoke invocations are finished.<br>
059: * The beforeCompletion method is called by the transaction manager prior to
060: * the start of the two-phase transaction commit process. This call is
061: * executed with the transaction context of the transaction that is being
062: * committed.
063: */
064: public void beforeCompletion() {
065: try {
066: synchronizedBean.beforeCompletion();
067: } catch (EJBException e) {
068: throw e;
069: } catch (RemoteException e) {
070: throw new EJBException("Error in beforeCompletion()", e);
071: }
072: }
073:
074: /**
075: * This method is called by the transaction manager after the transaction is
076: * committed or rolled back.
077: * @param status The status of the transaction completion.
078: */
079: public void afterCompletion(final int status) {
080: try {
081: synchronizedBean
082: .afterCompletion(status == STATUS_COMMITTED);
083: } catch (EJBException e) {
084: throw e;
085: } catch (RemoteException e) {
086: throw new EJBException("Error in afterCompletion()", e);
087: } finally {
088: ready = true;
089: }
090:
091: }
092:
093: /**
094: * This listener is ready to receive event from the transaction manager.
095: */
096: private boolean ready = true;
097:
098: /**
099: * Gets the ready state of this listener.
100: * @return true/false
101: */
102: public boolean isReady() {
103: return ready;
104: }
105:
106: /**
107: * Sets the ready state to false as the transaction is in progress.
108: */
109: public void inTX() {
110: ready = false;
111: }
112:
113: }
|