KickJava   Java API By Example, From Geeks To Geeks.

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

25
26 package org.objectweb.jonas.jtests.beans.relation.s3pkcomp;
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 H. Joanin
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 = new ArrayList JavaDoc(c.size());
59         for(Iterator JavaDoc it = c.iterator(); it.hasNext();)
60             al.add(bhl.findByPrimaryKey((Pk) it.next()));
61         setB(al);
62     }
63     public void assignBInNewTx(Collection JavaDoc c) throws FinderException JavaDoc {
64         assignB(c);
65     }
66
67     public Collection JavaDoc retrieveB() {
68         Collection JavaDoc bs = getB();
69         ArrayList JavaDoc result = new ArrayList JavaDoc(bs.size());
70         for(Iterator JavaDoc it = bs.iterator(); it.hasNext();)
71             result.add(((BLocal) it.next()).getPrimaryKey());
72         return result;
73     }
74     public Collection JavaDoc retrieveBInNewTx() {
75         return retrieveB();
76     }
77
78     public void addInB(Pk pkb) throws FinderException JavaDoc {
79         getB().add(bhl.findByPrimaryKey(pkb));
80     }
81     public void addInBInNewTx(Pk pkb) throws FinderException JavaDoc {
82         addInB(pkb);
83     }
84
85     public void addAllInB(Collection JavaDoc pkbs) throws FinderException JavaDoc {
86         ArrayList JavaDoc al = new ArrayList JavaDoc();
87         for (Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
88             al.add(bhl.findByPrimaryKey((Pk) it.next()));
89         getB().addAll(al);
90     }
91     public void addAllInBInNewTx(Collection JavaDoc pkbs) throws FinderException JavaDoc {
92         addAllInB(pkbs);
93     }
94
95     public void removeFromB(Pk pkb) throws FinderException JavaDoc {
96         getB().remove(bhl.findByPrimaryKey(pkb));
97     }
98     public void removeFromBInNewTx(Pk pkb) throws FinderException JavaDoc {
99         removeFromB(pkb);
100     }
101
102     public void clearB() {
103         getB().clear();
104     }
105
106     public void clearBInNewTx() {
107         clearB();
108     }
109
110     public boolean containAllInB(Collection JavaDoc pkbs) throws FinderException JavaDoc {
111         ArrayList JavaDoc al = new ArrayList JavaDoc(pkbs.size());
112         for(Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
113             al.add(bhl.findByPrimaryKey((Pk) it.next()));
114         return getB().containsAll(al);
115     }
116
117     /**
118      * It returns true the multivalued relation contains the bean B defined
119      * by the primary key specified by the parameter.
120      * This method has the transactional attribut TX_SUPPORTS.
121      * @throw a FinderException if the primary key does not match to a bean.
122      */

123     public boolean containInB(Pk pkb) throws FinderException JavaDoc {
124         return (getB().contains(bhl.findByPrimaryKey(pkb)));
125     }
126
127     // ------------------------------------------------------------------
128
// Get and Set accessor methods of the bean's abstract schema
129
// ------------------------------------------------------------------
130
public abstract Integer JavaDoc getId();
131
132     public abstract void setId(Integer JavaDoc id);
133
134     public abstract Collection JavaDoc getB();
135
136     public abstract void setB(Collection JavaDoc bl);
137
138     // ------------------------------------------------------------------
139
// EntityBean implementation
140
// ------------------------------------------------------------------
141

142     static protected Logger logger = null;
143     EntityContext JavaDoc ejbContext;
144
145     /**
146      * The Entity bean can define 0 or more ejbCreate methods.
147      *
148      * @throws CreateException Failure to create an entity EJB object.
149      * @throws DuplicateKeyException An object with the same key already exists.
150      */

151     public String JavaDoc ejbCreate(int id) throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
152         logger.log(BasicLevel.DEBUG, "");
153
154         // Init here the bean fields
155
setId(new Integer JavaDoc(id));
156
157         // In CMP, should return null.
158
return null;
159     }
160
161     /**
162      * Set the associated entity context. The container invokes this method
163      * on an instance after the instance has been created.
164      * This method is called in an unspecified transaction context.
165      *
166      * @param ctx - An EntityContext interface for the instance. The instance
167      * should store the reference to the context in an instance variable.
168      * @throws EJBException Thrown by the method to indicate a failure caused by a
169      * system-level error.
170      */

171     public void setEntityContext(EntityContext JavaDoc ctx) {
172         if (logger == null)
173             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
174         logger.log(BasicLevel.DEBUG, "");
175         ejbContext = ctx;
176         try {
177             Context JavaDoc ictx = new InitialContext JavaDoc();
178             bhl = (BHomeLocal) ictx.lookup("java:comp/env/ejb/b");
179         } catch (NamingException JavaDoc e) {
180             throw new EJBException JavaDoc("Impossible to fetch the ", e);
181         }
182     }
183
184     /**
185      * Unset the associated entity context. The container calls this method
186      * before removing the instance.
187      * This is the last method that the container invokes on the instance.
188      * The Java garbage collector will eventually invoke the finalize() method
189      * on the instance.
190      * This method is called in an unspecified transaction context.
191      *
192      * @throws EJBException Thrown by the method to indicate a failure caused by a
193      * system-level error.
194      */

195     public void unsetEntityContext() {
196         logger.log(BasicLevel.DEBUG, "");
197         ejbContext = null;
198     }
199
200     /**
201      * A container invokes this method before it removes the EJB object
202      * that is currently associated with the instance. This method is
203      * invoked when a client invokes a remove operation on the enterprise Bean's
204      * home interface or the EJB object's remote interface. This method
205      * transitions the instance from the ready state to the pool of available
206      * instances.
207      *
208      * This method is called in the transaction context of the remove operation.
209      * @throws RemoveException The enterprise Bean does not allow destruction of the object.
210      * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
211      * error.
212      */

213     public void ejbRemove() throws RemoveException JavaDoc {
214         logger.log(BasicLevel.DEBUG, "");
215     }
216
217     /**
218      * A container invokes this method to instruct the instance to synchronize
219      * its state by loading it state from the underlying database.
220      * This method always executes in the proper transaction context.
221      *
222      * @throws EJBException Thrown by the method to indicate a failure caused by
223      * a system-level error.
224      */

225     public void ejbLoad() {
226         logger.log(BasicLevel.DEBUG, "");
227     }
228
229     /**
230      * A container invokes this method to instruct the instance to synchronize
231      * its state by storing it to the underlying database.
232      * This method always executes in the proper transaction context.
233      *
234      * @throws EJBException Thrown by the method to indicate a failure caused by
235      * a system-level error.
236      */

237     public void ejbStore() {
238         logger.log(BasicLevel.DEBUG, "");
239     }
240
241     /**
242      * There must be an ejbPostCreate par ejbCreate method
243      *
244      * @throws CreateException Failure to create an entity EJB object.
245      */

246     public void ejbPostCreate(int id) throws CreateException JavaDoc {
247         logger.log(BasicLevel.DEBUG, "id=" + id);
248     }
249
250     /**
251      * A container invokes this method on an instance before the instance
252      * becomes disassociated with a specific EJB object.
253      */

254     public void ejbPassivate() {
255         logger.log(BasicLevel.DEBUG, "");
256     }
257
258     /**
259      * A container invokes this method when the instance is taken out of
260      * the pool of available instances to become associated with a specific
261      * EJB object.
262      */

263     public void ejbActivate() {
264         logger.log(BasicLevel.DEBUG, "");
265     }
266
267 }
268
Popular Tags