KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > omb > AEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AEC2.java,v 1.10 2005/04/22 17:04:57 ashah Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.omb;
27
28 import org.objectweb.jonas.common.Log;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.util.monolog.api.Logger;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.DuplicateKeyException JavaDoc;
34 import javax.ejb.EntityContext JavaDoc;
35 import javax.ejb.RemoveException JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.EJBException JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41 import javax.rmi.PortableRemoteObject JavaDoc;
42
43 import java.util.Collection JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47
48 /**
49  * @author S.Chassande-Barrioz, Helene Joanin
50  */

51 public abstract class AEC2 implements javax.ejb.EntityBean JavaDoc {
52
53     private BHomeLocal bhl = null;
54
55     public void m1(){
56     }
57
58     public void assignB(Collection JavaDoc c) throws FinderException JavaDoc {
59         logger.log(BasicLevel.DEBUG, "");
60         ArrayList JavaDoc al = new ArrayList JavaDoc(c.size());
61         for(Iterator JavaDoc it = c.iterator(); it.hasNext();)
62             al.add(bhl.findByPrimaryKey((String JavaDoc) it.next()));
63         setB(al);
64     }
65     public void assignBInNewTx(Collection JavaDoc c) throws FinderException JavaDoc {
66         logger.log(BasicLevel.DEBUG, "");
67         assignB(c);
68     }
69
70     public Collection JavaDoc retrieveB() {
71         logger.log(BasicLevel.DEBUG, "");
72         Collection JavaDoc bs = getB();
73         ArrayList JavaDoc result = new ArrayList JavaDoc(bs.size());
74         for(Iterator JavaDoc it = bs.iterator(); it.hasNext();)
75             result.add(((BLocal) it.next()).getPrimaryKey());
76         return result;
77     }
78     public Collection JavaDoc retrieveBInNewTx() {
79         logger.log(BasicLevel.DEBUG, "");
80         return retrieveB();
81     }
82     public Collection JavaDoc retrieveBisB() {
83         logger.log(BasicLevel.DEBUG, "");
84         // To reproduce the bug #300156: Error on creating Array from cmr-collection
85
// same as retrieveB(), except the bs initialization
86
List JavaDoc bs = new ArrayList JavaDoc(getB());
87         ArrayList JavaDoc result = new ArrayList JavaDoc(bs.size());
88         for(Iterator JavaDoc it = bs.iterator(); it.hasNext();)
89             result.add(((BLocal) it.next()).getPrimaryKey());
90         return result;
91     }
92
93     public void addInB(String JavaDoc pkb) throws FinderException JavaDoc {
94         logger.log(BasicLevel.DEBUG, "");
95         getB().add(bhl.findByPrimaryKey(pkb));
96     }
97     public void addInBInNewTx(String JavaDoc pkb) throws FinderException JavaDoc {
98         logger.log(BasicLevel.DEBUG, "");
99         addInB(pkb);
100     }
101
102     public void addNewB(String JavaDoc pkb) throws CreateException JavaDoc, FinderException JavaDoc {
103         logger.log(BasicLevel.DEBUG, "");
104         BLocal bl = bhl.create(pkb);
105         getB().add(bl);
106         bhl.findByName(pkb, getId());
107     }
108
109     public void addAllInB(Collection JavaDoc pkbs) throws FinderException JavaDoc {
110         logger.log(BasicLevel.DEBUG, "");
111         ArrayList JavaDoc al = new ArrayList JavaDoc();
112         for (Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
113             al.add(bhl.findByPrimaryKey((String JavaDoc) it.next()));
114         getB().addAll(al);
115     }
116     public void addAllInBInNewTx(Collection JavaDoc pkbs) throws FinderException JavaDoc {
117         logger.log(BasicLevel.DEBUG, "");
118         addAllInB(pkbs);
119     }
120
121     public void removeFromB(String JavaDoc pkb) throws FinderException JavaDoc {
122         logger.log(BasicLevel.DEBUG, "");
123         getB().remove(bhl.findByPrimaryKey(pkb));
124     }
125     public void removeFromBInNewTx(String JavaDoc pkb) throws FinderException JavaDoc {
126         logger.log(BasicLevel.DEBUG, "");
127         removeFromB(pkb);
128     }
129
130     public void clearB() {
131         logger.log(BasicLevel.DEBUG, "");
132         getB().clear();
133     }
134
135     public void clearBInNewTx() {
136         logger.log(BasicLevel.DEBUG, "");
137         clearB();
138     }
139
140     public boolean containAllInB(Collection JavaDoc pkbs) throws FinderException JavaDoc {
141         logger.log(BasicLevel.DEBUG, "");
142         ArrayList JavaDoc al = new ArrayList JavaDoc(pkbs.size());
143         for(Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
144             al.add(bhl.findByPrimaryKey((String JavaDoc) it.next()));
145         return getB().containsAll(al);
146     }
147
148     /**
149      * It returns true the multivalued relation contains the bean B defined
150      * by the primary key specified by the parameter.
151      * This method has the transactional attribut TX_SUPPORTS.
152      * @throw a FinderException if the primary key does not match to a bean.
153      */

154     public boolean containInB(String JavaDoc pkb) throws FinderException JavaDoc {
155         logger.log(BasicLevel.DEBUG, "");
156         return (getB().contains(bhl.findByPrimaryKey(pkb)));
157     }
158
159     /**
160      * This method check it isn't allowed to reset the pk
161      * and that the container throw the java.lang.IllegalStateException.
162      * It returns true if ok.
163      * See spec 2.0, chapter 10.3.5, page 134
164      */

165     public boolean testResetPkForbidden(String JavaDoc pka) {
166         logger.log(BasicLevel.DEBUG, "");
167         boolean ret = false;
168         try {
169             setId(pka);
170         } catch (IllegalStateException JavaDoc e) {
171             ret = true;
172         }
173         return ret;
174     }
175
176
177     // ------------------------------------------------------------------
178
// Get and Set accessor methods of the bean's abstract schema
179
// ------------------------------------------------------------------
180
public abstract String JavaDoc getId();
181     public abstract void setId(String JavaDoc id);
182
183     // This cmp field with an utility class type Product
184
// to test that this Product class can be resolved in the JORM adapter
185
public abstract Product getProduct();
186     public abstract void setProduct(Product p);
187
188     public abstract Collection JavaDoc getB();
189     public abstract void setB(Collection JavaDoc bl);
190
191     // ------------------------------------------------------------------
192
// EntityBean implementation
193
// ------------------------------------------------------------------
194

195     static protected Logger logger = null;
196     EntityContext JavaDoc ejbContext;
197
198     /**
199      * The Entity bean can define 0 or more ejbCreate methods.
200      *
201      * @throws CreateException Failure to create an entity EJB object.
202      * @throws DuplicateKeyException An object with the same key already exists.
203      */

204     public String JavaDoc ejbCreate(String JavaDoc id) throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
205         logger.log(BasicLevel.DEBUG, "");
206
207         // Init here the bean fields
208
setId(id);
209         setProduct(new Product());
210
211         // In CMP, should return null.
212
return null;
213     }
214
215     /**
216      * Set the associated entity context. The container invokes this method
217      * on an instance after the instance has been created.
218      * This method is called in an unspecified transaction context.
219      *
220      * @param ctx - An EntityContext interface for the instance. The instance
221      * should store the reference to the context in an instance variable.
222      * @throws EJBException Thrown by the method to indicate a failure caused by a
223      * system-level error.
224      */

225     public void setEntityContext(EntityContext JavaDoc ctx) {
226         if (logger == null)
227             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
228         logger.log(BasicLevel.DEBUG, "");
229         ejbContext = ctx;
230         try {
231             Context JavaDoc ictx = new InitialContext JavaDoc();
232             bhl = (BHomeLocal) ictx.lookup("java:comp/env/ejb/b");
233         } catch (NamingException JavaDoc e) {
234             throw new EJBException JavaDoc("Impossible to fetch the ", e);
235         }
236     }
237
238     /**
239      * Unset the associated entity context. The container calls this method
240      * before removing the instance.
241      * This is the last method that the container invokes on the instance.
242      * The Java garbage collector will eventually invoke the finalize() method
243      * on the instance.
244      * This method is called in an unspecified transaction context.
245      *
246      * @throws EJBException Thrown by the method to indicate a failure caused by a
247      * system-level error.
248      */

249     public void unsetEntityContext() {
250         logger.log(BasicLevel.DEBUG, "");
251         ejbContext = null;
252     }
253
254     /**
255      * A container invokes this method before it removes the EJB object
256      * that is currently associated with the instance. This method is
257      * invoked when a client invokes a remove operation on the enterprise Bean's
258      * home interface or the EJB object's remote interface. This method
259      * transitions the instance from the ready state to the pool of available
260      * instances.
261      *
262      * This method is called in the transaction context of the remove operation.
263      * @throws RemoveException The enterprise Bean does not allow destruction of the object.
264      * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
265      * error.
266      */

267     public void ejbRemove() throws RemoveException JavaDoc {
268         logger.log(BasicLevel.DEBUG, "");
269     }
270
271     /**
272      * A container invokes this method to instruct the instance to synchronize
273      * its state by loading it state from the underlying database.
274      * This method always executes in the proper transaction context.
275      *
276      * @throws EJBException Thrown by the method to indicate a failure caused by
277      * a system-level error.
278      */

279     public void ejbLoad() {
280         logger.log(BasicLevel.DEBUG, "");
281     }
282
283     /**
284      * A container invokes this method to instruct the instance to synchronize
285      * its state by storing it to 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 ejbStore() {
292         logger.log(BasicLevel.DEBUG, "");
293     }
294
295     /**
296      * There must be an ejbPostCreate par ejbCreate method
297      *
298      * @throws CreateException Failure to create an entity EJB object.
299      */

300     public void ejbPostCreate(String JavaDoc id) throws CreateException JavaDoc {
301         logger.log(BasicLevel.DEBUG, "id=" + id);
302     }
303
304     /**
305      * A container invokes this method on an instance before the instance
306      * becomes disassociated with a specific EJB object.
307      */

308     public void ejbPassivate() {
309         logger.log(BasicLevel.DEBUG, "");
310     }
311
312     /**
313      * A container invokes this method when the instance is taken out of
314      * the pool of available instances to become associated with a specific
315      * EJB object.
316      */

317     public void ejbActivate() {
318         logger.log(BasicLevel.DEBUG, "");
319     }
320
321 }
322
Popular Tags