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: AEC2.java 6633 2005-04-22 17:04:57Z ashah $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.omb;
027:
028: import org.objectweb.jonas.common.Log;
029: import org.objectweb.util.monolog.api.BasicLevel;
030: import org.objectweb.util.monolog.api.Logger;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.DuplicateKeyException;
034: import javax.ejb.EntityContext;
035: import javax.ejb.RemoveException;
036: import javax.ejb.FinderException;
037: import javax.ejb.EJBException;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041: import javax.rmi.PortableRemoteObject;
042:
043: import java.util.Collection;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: /**
049: * @author S.Chassande-Barrioz, Helene Joanin
050: */
051: public abstract class AEC2 implements javax.ejb.EntityBean {
052:
053: private BHomeLocal bhl = null;
054:
055: public void m1() {
056: }
057:
058: public void assignB(Collection c) throws FinderException {
059: logger.log(BasicLevel.DEBUG, "");
060: ArrayList al = new ArrayList(c.size());
061: for (Iterator it = c.iterator(); it.hasNext();)
062: al.add(bhl.findByPrimaryKey((String) it.next()));
063: setB(al);
064: }
065:
066: public void assignBInNewTx(Collection c) throws FinderException {
067: logger.log(BasicLevel.DEBUG, "");
068: assignB(c);
069: }
070:
071: public Collection retrieveB() {
072: logger.log(BasicLevel.DEBUG, "");
073: Collection bs = getB();
074: ArrayList result = new ArrayList(bs.size());
075: for (Iterator it = bs.iterator(); it.hasNext();)
076: result.add(((BLocal) it.next()).getPrimaryKey());
077: return result;
078: }
079:
080: public Collection retrieveBInNewTx() {
081: logger.log(BasicLevel.DEBUG, "");
082: return retrieveB();
083: }
084:
085: public Collection retrieveBisB() {
086: logger.log(BasicLevel.DEBUG, "");
087: // To reproduce the bug #300156: Error on creating Array from cmr-collection
088: // same as retrieveB(), except the bs initialization
089: List bs = new ArrayList(getB());
090: ArrayList result = new ArrayList(bs.size());
091: for (Iterator it = bs.iterator(); it.hasNext();)
092: result.add(((BLocal) it.next()).getPrimaryKey());
093: return result;
094: }
095:
096: public void addInB(String pkb) throws FinderException {
097: logger.log(BasicLevel.DEBUG, "");
098: getB().add(bhl.findByPrimaryKey(pkb));
099: }
100:
101: public void addInBInNewTx(String pkb) throws FinderException {
102: logger.log(BasicLevel.DEBUG, "");
103: addInB(pkb);
104: }
105:
106: public void addNewB(String pkb) throws CreateException,
107: FinderException {
108: logger.log(BasicLevel.DEBUG, "");
109: BLocal bl = bhl.create(pkb);
110: getB().add(bl);
111: bhl.findByName(pkb, getId());
112: }
113:
114: public void addAllInB(Collection pkbs) throws FinderException {
115: logger.log(BasicLevel.DEBUG, "");
116: ArrayList al = new ArrayList();
117: for (Iterator it = pkbs.iterator(); it.hasNext();)
118: al.add(bhl.findByPrimaryKey((String) it.next()));
119: getB().addAll(al);
120: }
121:
122: public void addAllInBInNewTx(Collection pkbs)
123: throws FinderException {
124: logger.log(BasicLevel.DEBUG, "");
125: addAllInB(pkbs);
126: }
127:
128: public void removeFromB(String pkb) throws FinderException {
129: logger.log(BasicLevel.DEBUG, "");
130: getB().remove(bhl.findByPrimaryKey(pkb));
131: }
132:
133: public void removeFromBInNewTx(String pkb) throws FinderException {
134: logger.log(BasicLevel.DEBUG, "");
135: removeFromB(pkb);
136: }
137:
138: public void clearB() {
139: logger.log(BasicLevel.DEBUG, "");
140: getB().clear();
141: }
142:
143: public void clearBInNewTx() {
144: logger.log(BasicLevel.DEBUG, "");
145: clearB();
146: }
147:
148: public boolean containAllInB(Collection pkbs)
149: throws FinderException {
150: logger.log(BasicLevel.DEBUG, "");
151: ArrayList al = new ArrayList(pkbs.size());
152: for (Iterator it = pkbs.iterator(); it.hasNext();)
153: al.add(bhl.findByPrimaryKey((String) it.next()));
154: return getB().containsAll(al);
155: }
156:
157: /**
158: * It returns true the multivalued relation contains the bean B defined
159: * by the primary key specified by the parameter.
160: * This method has the transactional attribut TX_SUPPORTS.
161: * @throw a FinderException if the primary key does not match to a bean.
162: */
163: public boolean containInB(String pkb) throws FinderException {
164: logger.log(BasicLevel.DEBUG, "");
165: return (getB().contains(bhl.findByPrimaryKey(pkb)));
166: }
167:
168: /**
169: * This method check it isn't allowed to reset the pk
170: * and that the container throw the java.lang.IllegalStateException.
171: * It returns true if ok.
172: * See spec 2.0, chapter 10.3.5, page 134
173: */
174: public boolean testResetPkForbidden(String pka) {
175: logger.log(BasicLevel.DEBUG, "");
176: boolean ret = false;
177: try {
178: setId(pka);
179: } catch (IllegalStateException e) {
180: ret = true;
181: }
182: return ret;
183: }
184:
185: // ------------------------------------------------------------------
186: // Get and Set accessor methods of the bean's abstract schema
187: // ------------------------------------------------------------------
188: public abstract String getId();
189:
190: public abstract void setId(String id);
191:
192: // This cmp field with an utility class type Product
193: // to test that this Product class can be resolved in the JORM adapter
194: public abstract Product getProduct();
195:
196: public abstract void setProduct(Product p);
197:
198: public abstract Collection getB();
199:
200: public abstract void setB(Collection bl);
201:
202: // ------------------------------------------------------------------
203: // EntityBean implementation
204: // ------------------------------------------------------------------
205:
206: static protected Logger logger = null;
207: EntityContext ejbContext;
208:
209: /**
210: * The Entity bean can define 0 or more ejbCreate methods.
211: *
212: * @throws CreateException Failure to create an entity EJB object.
213: * @throws DuplicateKeyException An object with the same key already exists.
214: */
215: public String ejbCreate(String id) throws CreateException,
216: DuplicateKeyException {
217: logger.log(BasicLevel.DEBUG, "");
218:
219: // Init here the bean fields
220: setId(id);
221: setProduct(new Product());
222:
223: // In CMP, should return null.
224: return null;
225: }
226:
227: /**
228: * Set the associated entity context. The container invokes this method
229: * on an instance after the instance has been created.
230: * This method is called in an unspecified transaction context.
231: *
232: * @param ctx - An EntityContext interface for the instance. The instance
233: * should store the reference to the context in an instance variable.
234: * @throws EJBException Thrown by the method to indicate a failure caused by a
235: * system-level error.
236: */
237: public void setEntityContext(EntityContext ctx) {
238: if (logger == null)
239: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
240: logger.log(BasicLevel.DEBUG, "");
241: ejbContext = ctx;
242: try {
243: Context ictx = new InitialContext();
244: bhl = (BHomeLocal) ictx.lookup("java:comp/env/ejb/b");
245: } catch (NamingException e) {
246: throw new EJBException("Impossible to fetch the ", e);
247: }
248: }
249:
250: /**
251: * Unset the associated entity context. The container calls this method
252: * before removing the instance.
253: * This is the last method that the container invokes on the instance.
254: * The Java garbage collector will eventually invoke the finalize() method
255: * on the instance.
256: * This method is called in an unspecified transaction context.
257: *
258: * @throws EJBException Thrown by the method to indicate a failure caused by a
259: * system-level error.
260: */
261: public void unsetEntityContext() {
262: logger.log(BasicLevel.DEBUG, "");
263: ejbContext = null;
264: }
265:
266: /**
267: * A container invokes this method before it removes the EJB object
268: * that is currently associated with the instance. This method is
269: * invoked when a client invokes a remove operation on the enterprise Bean's
270: * home interface or the EJB object's remote interface. This method
271: * transitions the instance from the ready state to the pool of available
272: * instances.
273: *
274: * This method is called in the transaction context of the remove operation.
275: * @throws RemoveException The enterprise Bean does not allow destruction of the object.
276: * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
277: * error.
278: */
279: public void ejbRemove() throws RemoveException {
280: logger.log(BasicLevel.DEBUG, "");
281: }
282:
283: /**
284: * A container invokes this method to instruct the instance to synchronize
285: * its state by loading it state from the underlying database.
286: * This method always executes in the proper transaction context.
287: *
288: * @throws EJBException Thrown by the method to indicate a failure caused by
289: * a system-level error.
290: */
291: public void ejbLoad() {
292: logger.log(BasicLevel.DEBUG, "");
293: }
294:
295: /**
296: * A container invokes this method to instruct the instance to synchronize
297: * its state by storing it to the underlying database.
298: * This method always executes in the proper transaction context.
299: *
300: * @throws EJBException Thrown by the method to indicate a failure caused by
301: * a system-level error.
302: */
303: public void ejbStore() {
304: logger.log(BasicLevel.DEBUG, "");
305: }
306:
307: /**
308: * There must be an ejbPostCreate par ejbCreate method
309: *
310: * @throws CreateException Failure to create an entity EJB object.
311: */
312: public void ejbPostCreate(String id) throws CreateException {
313: logger.log(BasicLevel.DEBUG, "id=" + id);
314: }
315:
316: /**
317: * A container invokes this method on an instance before the instance
318: * becomes disassociated with a specific EJB object.
319: */
320: public void ejbPassivate() {
321: logger.log(BasicLevel.DEBUG, "");
322: }
323:
324: /**
325: * A container invokes this method when the instance is taken out of
326: * the pool of available instances to become associated with a specific
327: * EJB object.
328: */
329: public void ejbActivate() {
330: logger.log(BasicLevel.DEBUG, "");
331: }
332:
333: }
|