KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > pkg > beans > 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:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.pkg.beans;
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 import org.objectweb.jonas.jtests.beans.relation.pkg.itf.BHomeLocal;
48 import org.objectweb.jonas.jtests.beans.relation.pkg.itf.BLocal;
49
50
51 /**
52  * @author S.Chassande-Barrioz
53  */

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

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

160     static protected Logger logger = null;
161     EntityContext JavaDoc ejbContext;
162
163     /**
164      * The Entity bean can define 0 or more ejbCreate methods.
165      *
166      * @throws CreateException Failure to create an entity EJB object.
167      * @throws DuplicateKeyException An object with the same key already exists.
168      */

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

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

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

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

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

255     public void ejbStore() {
256         logger.log(BasicLevel.DEBUG, "");
257     }
258
259     /**
260      * There must be an ejbPostCreate par ejbCreate method
261      *
262      * @throws CreateException Failure to create an entity EJB object.
263      */

264     public void ejbPostCreate(String JavaDoc id) throws CreateException JavaDoc {
265         logger.log(BasicLevel.DEBUG, "id=" + id);
266     }
267
268     /**
269      * A container invokes this method on an instance before the instance
270      * becomes disassociated with a specific EJB object.
271      */

272     public void ejbPassivate() {
273         logger.log(BasicLevel.DEBUG, "");
274     }
275
276     /**
277      * A container invokes this method when the instance is taken out of
278      * the pool of available instances to become associated with a specific
279      * EJB object.
280      */

281     public void ejbActivate() {
282         logger.log(BasicLevel.DEBUG, "");
283     }
284
285 }
286
Popular Tags