KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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

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

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

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

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

277     public void ejbActivate() {
278         logger.log(BasicLevel.DEBUG, "");
279     }
280
281 }
282
Popular Tags