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: AccountEC2.java 4371 2004-03-15 16:04:22Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.beanexc;
027:
028: import javax.ejb.EntityBean;
029: import javax.ejb.EntityContext;
030: import javax.ejb.EJBContext;
031: import javax.ejb.RemoveException;
032: import javax.ejb.CreateException;
033:
034: import org.objectweb.jonas.common.Log;
035: import org.objectweb.util.monolog.api.BasicLevel;
036:
037: /**
038: * This is an entity bean with "container managed persistence version 2".
039: * The state of an instance is stored into a relational database.
040: * @author Philippe Durieux, Philippe Coq, Helene Joanin
041: */
042: public abstract class AccountEC2 extends AccountCommon implements
043: EntityBean {
044:
045: boolean forceToFailEjbStore;
046: protected EntityContext entityContext;
047:
048: // Get and Set accessor methods of the bean's abstract schema
049: public abstract int getNumber();
050:
051: public abstract void setNumber(int n);
052:
053: public abstract long getBalance();
054:
055: public abstract void setBalance(long d);
056:
057: public abstract String getCustomer();
058:
059: public abstract void setCustomer(String c);
060:
061: public EJBContext getContext() {
062: return entityContext;
063: }
064:
065: public void ejbActivate() {
066: logger.log(BasicLevel.DEBUG, "");
067: }
068:
069: public void ejbPassivate() {
070: logger.log(BasicLevel.DEBUG, "");
071: }
072:
073: public void ejbLoad() {
074: logger.log(BasicLevel.DEBUG, "");
075: }
076:
077: public void ejbStore() {
078: logger.log(BasicLevel.DEBUG, "");
079: if (forceToFailEjbStore) {
080: forceToFailEjbStore = false;
081: throw new RuntimeException("RunTimeExceptionInEjbStore");
082: }
083: }
084:
085: /**
086: * This method is common for impl and expl bean
087: * it is used to test exception raised in ejbRemove()
088: * with unspecified transactional context (Required attribute).
089: * This method throws a RemoveException when the value of the PK is between 999990 and 999999
090: */
091: public void ejbRemove() throws RemoveException {
092: logger.log(BasicLevel.DEBUG, "");
093: AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
094: if ((pk.number >= 999990) && (pk.number <= 999999)) {
095: logger
096: .log(BasicLevel.DEBUG,
097: "RemoveException throwned by bean provider in ejbRemove");
098: throw new RemoveException(
099: "RemoveException throwned by bean provider in ejbRemove");
100: }
101: }
102:
103: public void setEntityContext(EntityContext ctx) {
104: if (logger == null) {
105: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
106: }
107: logger.log(BasicLevel.DEBUG, "");
108: entityContext = ctx;
109: }
110:
111: public void unsetEntityContext() {
112: logger.log(BasicLevel.DEBUG, "");
113: entityContext = null;
114: }
115:
116: public AccountPK ejbCreate(int val_number, String val_customer,
117: long val_balance) throws CreateException {
118: logger.log(BasicLevel.DEBUG, "");
119: setNumber(val_number);
120: setCustomer(val_customer);
121: setBalance(val_balance);
122: return (null);
123: }
124:
125: /**
126: * this method is common for impl and expl bean
127: * it is used to test exception raised in ejbCreate
128: * with unspecified transactional context (Required attribute)
129: * CAUTION: Do not call ejbCreate inside another ejbCreate (known bug)
130: */
131: public AccountPK ejbCreate(int flag) throws CreateException,
132: AppException {
133: logger.log(BasicLevel.DEBUG, "");
134: setNumber(1951);
135: setCustomer("Myself");
136: setBalance(10000);
137: if (flag == 0) {
138: entityContext.setRollbackOnly();
139: throw new AppException("AppException in ejbCreate(boolean)");
140: } else {
141: int zero = 0;
142: float f = 10 / zero;
143: }
144: return (null);
145: }
146:
147: /**
148: * this method is common for impl and expl bean
149: * NotSupported attr.
150: * CAUTION: Do not call ejbCreate inside another ejbCreate (known bug)
151: */
152: public AccountPK ejbCreate(boolean flag) throws CreateException,
153: AppException {
154: logger.log(BasicLevel.DEBUG, "");
155: setNumber(1951);
156: setCustomer("Myself");
157: setBalance(10000);
158: if (flag) {
159: throw new AppException("AppException in ejbCreate(boolean)");
160: } else {
161: int zero = 0;
162: float f = 10 / zero;
163: }
164: return (null);
165: }
166:
167: public void ejbPostCreate(int val_number, String val_customer,
168: long val_balance) {
169: logger.log(BasicLevel.DEBUG, "");
170: }
171:
172: public void ejbPostCreate(int flag) {
173: logger.log(BasicLevel.DEBUG, "");
174: }
175:
176: public void ejbPostCreate(boolean flag) {
177: logger.log(BasicLevel.DEBUG, "");
178: }
179:
180: public void doFailedEjbStore_1() {
181: logger.log(BasicLevel.DEBUG, "");
182: setBalance(1000);
183: forceToFailEjbStore = true;
184: }
185:
186: public void doFailedEjbStore_2() {
187: logger.log(BasicLevel.DEBUG, "");
188: setBalance(2000);
189: forceToFailEjbStore = true;
190: }
191:
192: public void doFailedEjbStore_3() {
193: logger.log(BasicLevel.DEBUG, "");
194: setBalance(3000);
195: forceToFailEjbStore = true;
196: }
197:
198: }
|