KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > family > PeopleEC2


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: PeopleEC2.java,v 1.8 2004/12/17 15:08:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.family;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.EntityContext JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import javax.rmi.PortableRemoteObject JavaDoc;
41
42 import org.objectweb.jonas.common.Log;
43 import org.objectweb.util.monolog.api.BasicLevel;
44 import org.objectweb.util.monolog.api.Logger;
45
46 /**
47  * Bean implementation. This is an example of bean with relation between
48  * instances of the same bean. Each instance represents a people, the PK
49  * is the name, and he may have a mother, a father, and a spouse.
50  * @author Philippe Durieux
51  */

52 public abstract class PeopleEC2 implements javax.ejb.EntityBean JavaDoc {
53
54     private static Logger logger = null;
55     private EntityContext JavaDoc ejbContext;
56     private InitialContext JavaDoc ictx;
57     private Context JavaDoc myEnv;
58     private PeopleHomeLocal homeLocal = null;
59     private PeopleHome home = null;
60
61     // ---------------------------------------------------------------------
62
// CMP fields
63
// ---------------------------------------------------------------------
64
public abstract String JavaDoc getName();
65     public abstract void setName(String JavaDoc name);
66
67     // ---------------------------------------------------------------------
68
// CMR fields
69
// ---------------------------------------------------------------------
70
public abstract Collection JavaDoc getChildren();
71     public abstract void setChildren(Collection JavaDoc c);
72
73     public abstract PeopleLocal getFather();
74     public abstract void setFather(PeopleLocal p);
75
76     public abstract PeopleLocal getMother();
77     public abstract void setMother(PeopleLocal p);
78
79     public abstract PeopleLocal getUnion();
80     public abstract void setUnion(PeopleLocal p);
81
82     // ---------------------------------------------------------------------
83
// Home operations
84
// ---------------------------------------------------------------------
85

86     public java.lang.String JavaDoc ejbCreate(String JavaDoc name, String JavaDoc papa, String JavaDoc maman) throws javax.ejb.CreateException JavaDoc {
87         logger.log(BasicLevel.DEBUG, getName());
88         setName(name);
89         return null;
90     }
91
92     public void ejbPostCreate(String JavaDoc name, String JavaDoc papa, String JavaDoc maman) throws javax.ejb.CreateException JavaDoc {
93         logger.log(BasicLevel.DEBUG, getName());
94         if (papa != null) {
95             try {
96                 PeopleLocal father = homeLocal.findByPrimaryKey(papa);
97                 setFather(father);
98             } catch (FinderException JavaDoc e) {
99                 throw new CreateException JavaDoc("Father unknown");
100             }
101         }
102         if (maman != null) {
103             try {
104                 PeopleLocal mother = homeLocal.findByPrimaryKey(maman);
105                 setMother(mother);
106             } catch (FinderException JavaDoc e) {
107                 throw new CreateException JavaDoc("Mother unknown");
108             }
109         }
110     }
111
112     /**
113      * Home method.
114      */

115     public void ejbHomeUnion(String JavaDoc name1, String JavaDoc name2) throws FinderException JavaDoc, RemoteException JavaDoc {
116         logger.log(BasicLevel.DEBUG, name1 + " with " + name2);
117         try {
118             PeopleLocal p1 = homeLocal.findByPrimaryKey(name1);
119             PeopleLocal p2 = homeLocal.findByPrimaryKey(name2);
120             p1.setUnion(p2);
121         } catch (FinderException JavaDoc e) {
122             logger.log(BasicLevel.ERROR, "Cannot find people");
123             throw e;
124         }
125     }
126
127     /**
128      * Home method.
129      */

130     public void ejbHomeDivorce(String JavaDoc name1, String JavaDoc name2) throws FinderException JavaDoc, RemoteException JavaDoc {
131         logger.log(BasicLevel.DEBUG, name1 + " with " + name2);
132         try {
133             PeopleLocal p1 = homeLocal.findByPrimaryKey(name1);
134             PeopleLocal p2 = homeLocal.findByPrimaryKey(name2);
135             if (p1.getUnion() != p2) {
136                 logger.log(BasicLevel.ERROR, name1 + " and " + name2 + " were not united yet");
137                 throw new RemoteException JavaDoc(name1 + " and " + name2 + " were not united yet");
138             }
139             p1.setUnion(null);
140         } catch (FinderException JavaDoc e) {
141             logger.log(BasicLevel.ERROR, "Cannot find people");
142             throw e;
143         }
144     }
145
146
147     // ---------------------------------------------------------------------
148
// EJB standard callbacks
149
// ---------------------------------------------------------------------
150

151     public void setEntityContext(javax.ejb.EntityContext JavaDoc ctx) {
152         // init the logger
153
if (logger == null) {
154             logger = Log.getLogger("org.objectweb.jonas_tests");
155         }
156         logger.log(BasicLevel.DEBUG, getName());
157         ejbContext = ctx;
158         try {
159             // Get initial Context
160
ictx = new InitialContext JavaDoc();
161             myEnv = (Context JavaDoc) ictx.lookup("java:comp/env");
162         } catch (NamingException JavaDoc e) {
163             throw new EJBException JavaDoc("PeopleEC2: Cannot get filehome:" + e);
164         }
165         checkEnv("setEntityContext");
166         home = (PeopleHome) ejbContext.getEJBHome();
167         homeLocal = (PeopleHomeLocal) ejbContext.getEJBLocalHome();
168     }
169
170     public void unsetEntityContext() {
171         logger.log(BasicLevel.DEBUG, getName());
172         ejbContext = null;
173     }
174
175     public void ejbRemove() throws javax.ejb.RemoveException JavaDoc {
176         logger.log(BasicLevel.DEBUG, getName());
177     }
178
179  
180     public void ejbLoad() {
181         logger.log(BasicLevel.DEBUG, getName());
182         checkEnv("ejbLoad");
183     }
184
185
186     public void ejbStore() {
187         logger.log(BasicLevel.DEBUG, getName());
188         checkEnv("ejbStore");
189     }
190         
191
192     public void ejbPassivate() {
193         logger.log(BasicLevel.DEBUG, getName());
194         checkEnv("ejbPassivate");
195     }
196
197
198     public void ejbActivate() {
199         logger.log(BasicLevel.DEBUG, getName());
200         checkEnv("ejbActivate");
201     }
202
203     // ---------------------------------------------------------------------
204
// Implementation of the Remote interface
205
// ---------------------------------------------------------------------
206

207     /**
208      * Retrieve my Father, as a Remote Object.
209      * Mainly convert Local object to Remote Object.
210      */

211     public People myFather() throws RemoteException JavaDoc {
212         logger.log(BasicLevel.DEBUG, getName());
213         People father = null;
214         PeopleLocal f = getFather();
215         if (f == null) {
216             return null;
217         }
218         try {
219             father = home.findByPrimaryKey(f.getName());
220         } catch (FinderException JavaDoc e) {
221             throw new RemoteException JavaDoc("Lost father");
222         }
223         return father;
224     }
225
226     /**
227      * Retrieve my Mother, as a Remote Object.
228      * Mainly convert Local object to Remote Object.
229      */

230     public People myMother() throws RemoteException JavaDoc {
231         logger.log(BasicLevel.DEBUG, getName());
232         People mother = null;
233         PeopleLocal f = getMother();
234         if (f == null) {
235             return null;
236         }
237         try {
238             mother = home.findByPrimaryKey(f.getName());
239         } catch (FinderException JavaDoc e) {
240             throw new RemoteException JavaDoc("Lost mother");
241         }
242         return mother;
243     }
244
245     /**
246      * Retrieve my children, as a Collection of Remote Objects.
247      * Mainly convert Local objects to Remote Objects.
248      */

249     public Collection JavaDoc myChildren() throws RemoteException JavaDoc {
250         logger.log(BasicLevel.DEBUG, getName());
251         Collection JavaDoc ret = new ArrayList JavaDoc();
252         Collection JavaDoc c = getChildren();
253         if (c.size() == 0) {
254             // If no children, maybe it's because I'm the father.
255
// In this case, must use a finder method (uni-directional relation)
256
try {
257                 c = homeLocal.findChildren(getName());
258             } catch (FinderException JavaDoc e) {
259                 throw new RemoteException JavaDoc("Cannot get children");
260             }
261         }
262         for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ) {
263             PeopleLocal p = (PeopleLocal) i.next();
264             try {
265                 People pr = home.findByPrimaryKey(p.getName());
266                 ret.add(pr);
267             } catch (FinderException JavaDoc e) {
268                 throw new RemoteException JavaDoc("Lost child");
269             }
270         }
271         return ret;
272     }
273
274     /**
275      * Return true if not married
276      */

277     public boolean isSingle() throws RemoteException JavaDoc {
278         logger.log(BasicLevel.DEBUG, getName());
279         return (getUnion() == null);
280     }
281
282     /**
283      * Return true if no father
284      */

285     public boolean hasNoFather() throws RemoteException JavaDoc {
286         logger.log(BasicLevel.DEBUG, getName());
287         return (getFather() == null);
288     }
289
290     /**
291      * Return true if no mother
292      */

293     public boolean hasNoMother() throws RemoteException JavaDoc {
294         logger.log(BasicLevel.DEBUG, getName());
295         return (getMother() == null);
296     }
297
298     /**
299      * @return the People we are married with
300      */

301     public People mySpouse() throws RemoteException JavaDoc {
302         logger.log(BasicLevel.DEBUG, getName());
303         People ret = null;
304         PeopleLocal f = getUnion();
305         if (f == null) {
306             return null;
307         }
308         try {
309             ret = home.findByPrimaryKey(f.getName());
310         } catch (FinderException JavaDoc e) {
311             throw new RemoteException JavaDoc("Lost spouse");
312         }
313         return ret;
314     }
315
316     public int kidNumber() throws RemoteException JavaDoc {
317         logger.log(BasicLevel.DEBUG, getName());
318         int ret = getChildren().size();
319         if (ret == 0) {
320             // maybe I'm the father...
321
try {
322                 ret = homeLocal.findChildren(getName()).size();
323             } catch (FinderException JavaDoc e) {
324                 throw new RemoteException JavaDoc("Cannot get my children");
325             }
326         }
327         return ret;
328     }
329
330     /**
331      * @return true if orphan
332      */

333     public boolean isOrphan() throws RemoteException JavaDoc {
334         logger.log(BasicLevel.DEBUG, getName());
335         return (getMother() == null && getFather() == null);
336     }
337
338     /**
339      * @return true if is brother or sister with other People
340      * @param c name of the other people.
341      */

342     public boolean brotherSisterOf(String JavaDoc c) throws RemoteException JavaDoc {
343         logger.log(BasicLevel.DEBUG, getName());
344         try {
345             PeopleLocal p = homeLocal.findByPrimaryKey(c);
346             if (getMother() != null && p.getMother() == getMother()) {
347                 return true;
348             }
349             if (getFather() != null && p.getFather() == getFather()) {
350                 return true;
351             }
352         } catch (FinderException JavaDoc e) {
353         }
354         return false;
355     }
356
357     // ---------------------------------------------------------------------
358
// private methods
359
// ---------------------------------------------------------------------
360

361     /**
362      * Check environment variables
363      */

364     void checkEnv(String JavaDoc method) {
365         try {
366             String JavaDoc value = (String JavaDoc) myEnv.lookup("myname");
367             if (!value.equals("myentity")) {
368                 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value);
369                 throw new EJBException JavaDoc("PeopleEC2 1: " + method);
370             }
371         } catch (NamingException JavaDoc e) {
372             logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
373             throw new EJBException JavaDoc("PeopleEC2 2: " + method);
374         }
375     }
376
377 }
378
Popular Tags