KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > pkg > beans > 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.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.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 import org.objectweb.jonas.jtests.beans.relation.pkg.itf.AHomeLocal;
48 import org.objectweb.jonas.jtests.beans.relation.pkg.itf.ALocal;
49
50 /**
51  * @author S.Chassande-Barrioz
52  */

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

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

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

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

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

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

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

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

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

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

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

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