KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > mnu > 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.5 2004/12/17 15:08:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.mnu;
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
47 /**
48  * @author S.Chassande-Barrioz
49  */

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

147     public boolean containInB(String JavaDoc pkb) throws FinderException JavaDoc {
148         return (getB().contains(bhl.findByPrimaryKey(pkb)));
149     }
150
151     // ------------------------------------------------------------------
152
// Get and Set accessor methods of the bean's abstract schema
153
// ------------------------------------------------------------------
154
public abstract String JavaDoc getId();
155
156     public abstract void setId(String JavaDoc id);
157
158     public abstract Collection JavaDoc getB();
159
160     public abstract void setB(Collection JavaDoc bl);
161
162     // ------------------------------------------------------------------
163
// EntityBean implementation
164
// ------------------------------------------------------------------
165

166     protected static Logger logger = null;
167     EntityContext JavaDoc ejbContext;
168
169     /**
170      * The Entity bean can define 0 or more ejbCreate methods.
171      *
172      * @throws CreateException Failure to create an entity EJB object.
173      * @throws DuplicateKeyException An object with the same key already exists.
174      */

175     public String JavaDoc ejbCreate(String JavaDoc id) throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
176         logger.log(BasicLevel.DEBUG, "");
177
178         // Init here the bean fields
179
setId(id);
180
181         // In CMP, should return null.
182
return null;
183     }
184
185     /**
186      * Set the associated entity context. The container invokes this method
187      * on an instance after the instance has been created.
188      * This method is called in an unspecified transaction context.
189      *
190      * @param ctx - An EntityContext interface for the instance. The instance
191      * should store the reference to the context in an instance variable.
192      * @throws EJBException Thrown by the method to indicate a failure caused by a
193      * system-level error.
194      */

195     public void setEntityContext(EntityContext JavaDoc ctx) {
196         if (logger == null) {
197             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
198         }
199         logger.log(BasicLevel.DEBUG, "");
200         ejbContext = ctx;
201         try {
202             Context JavaDoc ictx = new InitialContext JavaDoc();
203             bhl = (BHomeLocal) ictx.lookup("java:comp/env/ejb/b");
204         } catch (NamingException JavaDoc e) {
205             throw new EJBException JavaDoc("Impossible to fetch the ", e);
206         }
207     }
208
209     /**
210      * Unset the associated entity context. The container calls this method
211      * before removing the instance.
212      * This is the last method that the container invokes on the instance.
213      * The Java garbage collector will eventually invoke the finalize() method
214      * on the instance.
215      * This method is called in an unspecified transaction context.
216      *
217      * @throws EJBException Thrown by the method to indicate a failure caused by a
218      * system-level error.
219      */

220     public void unsetEntityContext() {
221         logger.log(BasicLevel.DEBUG, "");
222         ejbContext = null;
223     }
224
225     /**
226      * A container invokes this method before it removes the EJB object
227      * that is currently associated with the instance. This method is
228      * invoked when a client invokes a remove operation on the enterprise Bean's
229      * home interface or the EJB object's remote interface. This method
230      * transitions the instance from the ready state to the pool of available
231      * instances.
232      *
233      * This method is called in the transaction context of the remove operation.
234      * @throws RemoveException The enterprise Bean does not allow destruction of the object.
235      * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
236      * error.
237      */

238     public void ejbRemove() throws RemoveException JavaDoc {
239         logger.log(BasicLevel.DEBUG, "");
240     }
241
242     /**
243      * A container invokes this method to instruct the instance to synchronize
244      * its state by loading it state from the underlying database.
245      * This method always executes in the proper transaction context.
246      *
247      * @throws EJBException Thrown by the method to indicate a failure caused by
248      * a system-level error.
249      */

250     public void ejbLoad() {
251         logger.log(BasicLevel.DEBUG, "");
252     }
253
254     /**
255      * A container invokes this method to instruct the instance to synchronize
256      * its state by storing it to the underlying database.
257      * This method always executes in the proper transaction context.
258      *
259      * @throws EJBException Thrown by the method to indicate a failure caused by
260      * a system-level error.
261      */

262     public void ejbStore() {
263         logger.log(BasicLevel.DEBUG, "");
264     }
265
266     /**
267      * There must be an ejbPostCreate par ejbCreate method
268      *
269      * @throws CreateException Failure to create an entity EJB object.
270      */

271     public void ejbPostCreate(String JavaDoc id) throws CreateException JavaDoc {
272         logger.log(BasicLevel.DEBUG, "id=" + id);
273     }
274
275     /**
276      * A container invokes this method on an instance before the instance
277      * becomes disassociated with a specific EJB object.
278      */

279     public void ejbPassivate() {
280         logger.log(BasicLevel.DEBUG, "");
281     }
282
283     /**
284      * A container invokes this method when the instance is taken out of
285      * the pool of available instances to become associated with a specific
286      * EJB object.
287      */

288     public void ejbActivate() {
289         logger.log(BasicLevel.DEBUG, "");
290     }
291
292 }
293
Popular Tags