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: BEC2.java 6567 2005-04-15 14:42:39Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.omb;
027:
028: import org.objectweb.util.monolog.api.Logger;
029: import org.objectweb.util.monolog.api.BasicLevel;
030: import org.objectweb.jonas.common.Log;
031:
032: import javax.ejb.EntityContext;
033: import javax.ejb.CreateException;
034: import javax.ejb.DuplicateKeyException;
035: import javax.ejb.EJBException;
036: import javax.ejb.FinderException;
037: import javax.ejb.RemoveException;
038:
039: import javax.naming.Context;
040: import javax.naming.InitialContext;
041: import javax.naming.NamingException;
042: import javax.rmi.PortableRemoteObject;
043:
044: /**
045: * @author S.Chassande-Barrioz, Helene Joanin
046: */
047: public abstract class BEC2 implements javax.ejb.EntityBean {
048:
049: private AHomeLocal ahl = null;
050:
051: public void m1() {
052: }
053:
054: public void testSetCmrWithDeleted() throws EJBException {
055: logger.log(BasicLevel.DEBUG, "");
056: ALocal ax9;
057: try {
058: ax9 = ahl.create("ax9");
059: ax9.remove();
060: } catch (Exception e) {
061: throw new EJBException("Initial state creation problem: "
062: + e);
063: }
064: try {
065: this .setA(ax9);
066: throw new EJBException(
067: "entity was not deleted, expected IllegalArgumentException");
068: } catch (IllegalArgumentException e) {
069: // Pass
070: } catch (Exception e) {
071: throw new EJBException("Caugth unexpected exception: " + e);
072: }
073: }
074:
075: public void assignA(String pkA) throws FinderException {
076: logger.log(BasicLevel.DEBUG, "param=" + pkA);
077: ALocal a = null;
078: if (pkA != null) {
079: a = ahl.findByPrimaryKey(pkA);
080: }
081: logger.log(BasicLevel.DEBUG, "assign:" + a);
082: setA(a);
083: }
084:
085: public void assignAInNewTx(String pkA) throws FinderException {
086: assignA(pkA);
087: }
088:
089: public String retrieveA() {
090: if (getA() == null) {
091: logger.log(BasicLevel.DEBUG, "return null");
092: return null;
093: }
094: logger.log(BasicLevel.DEBUG, "return : "
095: + getA().getPrimaryKey());
096: return (String) (getA().getPrimaryKey());
097: }
098:
099: public String retrieveAInNewTx() {
100: return retrieveA();
101: }
102:
103: public boolean equalsRelA(String pka) throws FinderException {
104: return (pka == retrieveA());
105: }
106:
107: // ------------------------------------------------------------------
108: // Get and Set accessor methods of the bean's abstract schema
109: // ------------------------------------------------------------------
110: public abstract String getId();
111:
112: public abstract void setId(String id);
113:
114: // This cmp field with an utility class type Product
115: // to test that this Product class can be resolved in the JORM adapter
116: public abstract Product getProduct();
117:
118: public abstract void setProduct(Product p);
119:
120: public abstract ALocal getA();
121:
122: public abstract void setA(ALocal a);
123:
124: // ------------------------------------------------------------------
125: // EntityBean implementation
126: // ------------------------------------------------------------------
127:
128: static protected Logger logger = null;
129:
130: EntityContext ejbContext;
131:
132: /**
133: * The Entity bean can define 0 or more ejbCreate methods.
134: * @throws CreateException Failure to create an entity EJB object.
135: * @throws DuplicateKeyException An object with the same key already exists.
136: */
137: public String ejbCreate(String id) throws CreateException,
138: DuplicateKeyException {
139: logger.log(BasicLevel.DEBUG, "");
140:
141: // Init here the bean fields
142: setId(id);
143: setProduct(new Product());
144:
145: // In CMP, should return null.
146: return null;
147: }
148:
149: /**
150: * Set the associated entity context. The container invokes this method on
151: * an instance after the instance has been created. This method is called in
152: * an unspecified transaction context.
153: * @param ctx - An EntityContext interface for the instance. The instance
154: * should store the reference to the context in an instance variable.
155: * @throws EJBException Thrown by the method to indicate a failure caused by
156: * a system-level error.
157: */
158: public void setEntityContext(EntityContext ctx) {
159: if (logger == null)
160: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
161: logger.log(BasicLevel.DEBUG, "");
162: ejbContext = ctx;
163: try {
164: Context ictx = new InitialContext();
165: ahl = (AHomeLocal) ictx.lookup("java:comp/env/ejb/a");
166: } catch (NamingException e) {
167: throw new EJBException("Impossible to fetch the ", e);
168: }
169: }
170:
171: /**
172: * Unset the associated entity context. The container calls this method
173: * before removing the instance. This is the last method that the container
174: * invokes on the instance. The Java garbage collector will eventually
175: * invoke the finalize() method on the instance. This method is called in an
176: * unspecified transaction context.
177: * @throws EJBException Thrown by the method to indicate a failure caused by
178: * a system-level error.
179: */
180: public void unsetEntityContext() {
181: logger.log(BasicLevel.DEBUG, "");
182: ejbContext = null;
183: }
184:
185: /**
186: * A container invokes this method before it removes the EJB object that is
187: * currently associated with the instance. This method is invoked when a
188: * client invokes a remove operation on the enterprise Bean's home interface
189: * or the EJB object's remote interface. This method transitions the
190: * instance from the ready state to the pool of available instances. This
191: * method is called in the transaction context of the remove operation.
192: * @throws RemoveException The enterprise Bean does not allow destruction of
193: * the object.
194: * @throws EJBException - Thrown by the method to indicate a failure caused
195: * by a system-level error.
196: */
197: public void ejbRemove() throws RemoveException {
198: logger.log(BasicLevel.DEBUG, "");
199: }
200:
201: /**
202: * A container invokes this method to instruct the instance to synchronize
203: * its state by loading it state from the underlying database. This method
204: * always executes in the proper transaction context.
205: * @throws EJBException Thrown by the method to indicate a failure caused by
206: * a system-level error.
207: */
208: public void ejbLoad() {
209: logger.log(BasicLevel.DEBUG, "");
210: }
211:
212: /**
213: * A container invokes this method to instruct the instance to synchronize
214: * its state by storing it to the underlying database. This method always
215: * executes in the proper transaction context.
216: * @throws EJBException Thrown by the method to indicate a failure caused by
217: * a system-level error.
218: */
219: public void ejbStore() {
220: logger.log(BasicLevel.DEBUG, "");
221: }
222:
223: /**
224: * There must be an ejbPostCreate par ejbCreate method
225: * @throws CreateException Failure to create an entity EJB object.
226: */
227: public void ejbPostCreate(String id) throws CreateException {
228: logger.log(BasicLevel.DEBUG, "id=" + id);
229: setA(null);
230: }
231:
232: /**
233: * A container invokes this method on an instance before the instance
234: * becomes disassociated with a specific EJB object.
235: */
236: public void ejbPassivate() {
237: logger.log(BasicLevel.DEBUG, "");
238: }
239:
240: /**
241: * A container invokes this method when the instance is taken out of the
242: * pool of available instances to become associated with a specific EJB
243: * object.
244: */
245: public void ejbActivate() {
246: logger.log(BasicLevel.DEBUG, "");
247: }
248:
249: }
|