001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.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: SimpleSY.java 5996 2004-12-17 15:09:45Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.transacted;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.EJBException;
030: import javax.ejb.RemoveException;
031: import javax.ejb.SessionSynchronization;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035: import javax.rmi.PortableRemoteObject;
036:
037: import org.objectweb.util.monolog.api.BasicLevel;
038:
039: /*
040: * Stateful session bean
041: */
042: public class SimpleSY extends SimpleSL implements
043: SessionSynchronization {
044:
045: private boolean rollbackonly = false;
046: private Context ictx = null;
047: private SimpleELocalHome lhome = null;
048:
049: // ------------------------------------------------------------------
050: // SynchroSimple implementation
051: // ------------------------------------------------------------------
052:
053: /**
054: * Ask bean to set transaction rollback only
055: */
056: public void setRollbackOnly() {
057: logger.log(BasicLevel.DEBUG, "");
058: rollbackonly = true;
059: }
060:
061: /**
062: * REQUIRED -> should return true.
063: */
064: public boolean call_requires_new_local() {
065: logger.log(BasicLevel.DEBUG, "");
066: SimpleLocal loc = getLocalBean();
067: boolean tx = loc.opwith_requires_new();
068: if (!tx) {
069: logger.log(BasicLevel.ERROR,
070: "opwith_requires_new was outside tx");
071: return false; // error
072: }
073: try {
074: loc.remove(); // cleaning
075: } catch (RemoveException e) {
076: logger.log(BasicLevel.ERROR, "cannot remove local bean");
077: }
078: return isAssociated();
079: }
080:
081: /**
082: * REQUIRED -> should return true.
083: */
084: public boolean call_notsupported_local() {
085: logger.log(BasicLevel.DEBUG, "");
086: SimpleLocal loc = getLocalBean();
087: boolean tx = loc.opwith_notsupported();
088: if (tx) {
089: logger.log(BasicLevel.ERROR,
090: "opwith_notsupported was in tx");
091: return false; // error
092: }
093: try {
094: loc.remove(); // cleaning
095: } catch (RemoveException e) {
096: logger.log(BasicLevel.ERROR, "cannot remove local bean");
097: }
098: return isAssociated();
099: }
100:
101: // ------------------------------------------------------------------
102: // SessionSynchronization implementation
103: // ------------------------------------------------------------------
104:
105: /**
106: * The afterBegin method notifies a session Bean instance that a new
107: * transaction has started, and that the subsequent business methods on
108: * the instance will be invoked in the context of the transaction.
109: * This method executes in the proper transaction context.
110: *
111: * @throws EJBException Thrown if the instance could not perform
112: * the function requested by the container because of a system-level error.
113: */
114: public void afterBegin() {
115: logger.log(BasicLevel.DEBUG, "");
116: }
117:
118: /**
119: * The beforeCompletion method notifies a session Bean instance that a
120: * transaction is about to be committed.
121: * This method executes in the proper transaction context.
122: * Note: The instance may still cause the container to rollback the transaction
123: * by invoking the setRollbackOnly() method on the instance context, or by throwing
124: * an exception.
125: *
126: * @throws EJBException Thrown by the method to indicate a failure caused by a
127: * system-level error.
128: */
129: public void beforeCompletion() {
130: logger.log(BasicLevel.DEBUG, "");
131: if (rollbackonly) {
132: sessionContext.setRollbackOnly();
133: }
134: }
135:
136: /**
137: * The afterCompletion method notifies a session Bean instance that a
138: * transaction commit protocol has completed, and tells the instance whether
139: * the transaction has been committed or rolled back.
140: * This method executes with no transaction context.
141: *
142: * @param : committed - True if the transaction has been committed, false if
143: * it has been rolled back.
144: *
145: * @throws EJBException Thrown by the method to indicate a failure caused by a
146: * system-level error.
147: */
148: public void afterCompletion(boolean committed) {
149: logger.log(BasicLevel.DEBUG, "");
150: }
151:
152: // ------------------------------------------------------------------
153: // private methods
154: // ------------------------------------------------------------------
155:
156: /**
157: * init non persistent bean data.
158: * This should be called when instance is created or activated.
159: */
160: private void stateUpdate() throws NamingException {
161: // Get initial Context
162: if (ictx == null) {
163: ictx = new InitialContext();
164: }
165: // lookup paperhome in JNDI
166: if (lhome == null) {
167: lhome = (SimpleELocalHome) ictx
168: .lookup("java:comp/env/ejb/SimpleEC");
169: }
170: }
171:
172: /**
173: * get local bean
174: */
175: private SimpleLocal getLocalBean() {
176: SimpleLocal ret = null;
177: try {
178: stateUpdate();
179: } catch (NamingException e) {
180: logger.log(BasicLevel.ERROR,
181: "getLocalBean raised exception " + e);
182: throw new EJBException("Error in getLocalBean:" + e);
183: }
184: try {
185: ret = lhome.create(123);
186: } catch (CreateException e) {
187: logger.log(BasicLevel.ERROR,
188: "Cannot create local entity bean " + e);
189: throw new EJBException("Cannot create local entity bean:"
190: + e);
191: }
192: return ret;
193: }
194: }
|