KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > mnb > BEC2


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: BEC2.java,v 1.5 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.util.monolog.api.Logger;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.jonas.common.Log;
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  * @author S.Chassande-Barrioz
48  */

49 public abstract class BEC2 implements javax.ejb.EntityBean JavaDoc {
50
51     private AHomeLocal ahl = null;
52
53     public void m1(){
54     }
55
56     public void assignA(Collection JavaDoc c) throws FinderException JavaDoc {
57     ArrayList JavaDoc al;
58     if (c==null)
59             al = new ArrayList JavaDoc();
60     else {
61             if (c.size()==-1)
62         al = new ArrayList JavaDoc();
63         else {
64                al = new ArrayList JavaDoc(c.size());
65                for(Iterator JavaDoc it = c.iterator(); it.hasNext();) {
66            al.add(ahl.findByPrimaryKey((String JavaDoc) it.next()));
67            }
68         }
69     }
70     setA(al);
71     }
72
73     public void assignAInNewTx(Collection JavaDoc c) throws FinderException JavaDoc {
74         assignA(c);
75     }
76
77     public Collection JavaDoc retrieveA() {
78        Collection JavaDoc bs = getA();
79         ArrayList JavaDoc result ;
80         if (bs.size()==-1)
81         result = new ArrayList JavaDoc();
82         else result = new ArrayList JavaDoc(bs.size());
83         
84         for(Iterator JavaDoc it = bs.iterator(); it.hasNext();)
85             result.add(((ALocal) it.next()).getPrimaryKey());
86         return result;
87     }
88
89     public Collection JavaDoc retrieveAInNewTx() {
90         return retrieveA();
91     }
92
93     public void addInA(String JavaDoc pkb) throws FinderException JavaDoc {
94         getA().add(ahl.findByPrimaryKey(pkb));
95     }
96     public void addInAInNewTx(String JavaDoc pkb) throws FinderException JavaDoc {
97         addInA(pkb);
98     }
99
100     public void addAllInA(Collection JavaDoc pkbs) throws FinderException JavaDoc {
101         ArrayList JavaDoc al = new ArrayList JavaDoc();
102         for (Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
103             al.add(ahl.findByPrimaryKey((String JavaDoc) it.next()));
104         getA().addAll(al);
105     }
106     public void addAllInAInNewTx(Collection JavaDoc pkbs) throws FinderException JavaDoc {
107         addAllInA(pkbs);
108     }
109
110     public void removeFromA(String JavaDoc pkb) throws FinderException JavaDoc {
111         getA().remove(ahl.findByPrimaryKey(pkb));
112     }
113     public void removeFromAInNewTx(String JavaDoc pkb) throws FinderException JavaDoc {
114         removeFromA(pkb);
115     }
116
117     public void clearA() {
118         getA().clear();
119     }
120
121     public void clearAInNewTx() {
122         clearA();
123     }
124
125     public boolean containAllInA(Collection JavaDoc pkbs) throws FinderException JavaDoc {
126         ArrayList JavaDoc al = new ArrayList JavaDoc(pkbs.size());
127         for(Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
128             al.add(ahl.findByPrimaryKey((String JavaDoc) it.next()));
129         return getA().containsAll(al);
130     }
131
132     /**
133      * It returns true the multivalued relation contains the bean A defined
134      * by the primary key specified by the parameter.
135      * This method has the transactional attribut TX_SUPPORTS.
136      * @throw a FinderException if the primary key does not match to a bean.
137      */

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

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

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

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

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

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

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

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

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

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

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