001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: PeopleEC2.java 5995 2004-12-17 15:08:36Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.family;
027:
028: import java.rmi.RemoteException;
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import javax.ejb.CreateException;
034: import javax.ejb.EJBException;
035: import javax.ejb.EntityContext;
036: import javax.ejb.FinderException;
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.naming.NamingException;
040: import javax.rmi.PortableRemoteObject;
041:
042: import org.objectweb.jonas.common.Log;
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * Bean implementation. This is an example of bean with relation between
048: * instances of the same bean. Each instance represents a people, the PK
049: * is the name, and he may have a mother, a father, and a spouse.
050: * @author Philippe Durieux
051: */
052: public abstract class PeopleEC2 implements javax.ejb.EntityBean {
053:
054: private static Logger logger = null;
055: private EntityContext ejbContext;
056: private InitialContext ictx;
057: private Context myEnv;
058: private PeopleHomeLocal homeLocal = null;
059: private PeopleHome home = null;
060:
061: // ---------------------------------------------------------------------
062: // CMP fields
063: // ---------------------------------------------------------------------
064: public abstract String getName();
065:
066: public abstract void setName(String name);
067:
068: // ---------------------------------------------------------------------
069: // CMR fields
070: // ---------------------------------------------------------------------
071: public abstract Collection getChildren();
072:
073: public abstract void setChildren(Collection c);
074:
075: public abstract PeopleLocal getFather();
076:
077: public abstract void setFather(PeopleLocal p);
078:
079: public abstract PeopleLocal getMother();
080:
081: public abstract void setMother(PeopleLocal p);
082:
083: public abstract PeopleLocal getUnion();
084:
085: public abstract void setUnion(PeopleLocal p);
086:
087: // ---------------------------------------------------------------------
088: // Home operations
089: // ---------------------------------------------------------------------
090:
091: public java.lang.String ejbCreate(String name, String papa,
092: String maman) throws javax.ejb.CreateException {
093: logger.log(BasicLevel.DEBUG, getName());
094: setName(name);
095: return null;
096: }
097:
098: public void ejbPostCreate(String name, String papa, String maman)
099: throws javax.ejb.CreateException {
100: logger.log(BasicLevel.DEBUG, getName());
101: if (papa != null) {
102: try {
103: PeopleLocal father = homeLocal.findByPrimaryKey(papa);
104: setFather(father);
105: } catch (FinderException e) {
106: throw new CreateException("Father unknown");
107: }
108: }
109: if (maman != null) {
110: try {
111: PeopleLocal mother = homeLocal.findByPrimaryKey(maman);
112: setMother(mother);
113: } catch (FinderException e) {
114: throw new CreateException("Mother unknown");
115: }
116: }
117: }
118:
119: /**
120: * Home method.
121: */
122: public void ejbHomeUnion(String name1, String name2)
123: throws FinderException, RemoteException {
124: logger.log(BasicLevel.DEBUG, name1 + " with " + name2);
125: try {
126: PeopleLocal p1 = homeLocal.findByPrimaryKey(name1);
127: PeopleLocal p2 = homeLocal.findByPrimaryKey(name2);
128: p1.setUnion(p2);
129: } catch (FinderException e) {
130: logger.log(BasicLevel.ERROR, "Cannot find people");
131: throw e;
132: }
133: }
134:
135: /**
136: * Home method.
137: */
138: public void ejbHomeDivorce(String name1, String name2)
139: throws FinderException, RemoteException {
140: logger.log(BasicLevel.DEBUG, name1 + " with " + name2);
141: try {
142: PeopleLocal p1 = homeLocal.findByPrimaryKey(name1);
143: PeopleLocal p2 = homeLocal.findByPrimaryKey(name2);
144: if (p1.getUnion() != p2) {
145: logger.log(BasicLevel.ERROR, name1 + " and " + name2
146: + " were not united yet");
147: throw new RemoteException(name1 + " and " + name2
148: + " were not united yet");
149: }
150: p1.setUnion(null);
151: } catch (FinderException e) {
152: logger.log(BasicLevel.ERROR, "Cannot find people");
153: throw e;
154: }
155: }
156:
157: // ---------------------------------------------------------------------
158: // EJB standard callbacks
159: // ---------------------------------------------------------------------
160:
161: public void setEntityContext(javax.ejb.EntityContext ctx) {
162: // init the logger
163: if (logger == null) {
164: logger = Log.getLogger("org.objectweb.jonas_tests");
165: }
166: logger.log(BasicLevel.DEBUG, getName());
167: ejbContext = ctx;
168: try {
169: // Get initial Context
170: ictx = new InitialContext();
171: myEnv = (Context) ictx.lookup("java:comp/env");
172: } catch (NamingException e) {
173: throw new EJBException("PeopleEC2: Cannot get filehome:"
174: + e);
175: }
176: checkEnv("setEntityContext");
177: home = (PeopleHome) ejbContext.getEJBHome();
178: homeLocal = (PeopleHomeLocal) ejbContext.getEJBLocalHome();
179: }
180:
181: public void unsetEntityContext() {
182: logger.log(BasicLevel.DEBUG, getName());
183: ejbContext = null;
184: }
185:
186: public void ejbRemove() throws javax.ejb.RemoveException {
187: logger.log(BasicLevel.DEBUG, getName());
188: }
189:
190: public void ejbLoad() {
191: logger.log(BasicLevel.DEBUG, getName());
192: checkEnv("ejbLoad");
193: }
194:
195: public void ejbStore() {
196: logger.log(BasicLevel.DEBUG, getName());
197: checkEnv("ejbStore");
198: }
199:
200: public void ejbPassivate() {
201: logger.log(BasicLevel.DEBUG, getName());
202: checkEnv("ejbPassivate");
203: }
204:
205: public void ejbActivate() {
206: logger.log(BasicLevel.DEBUG, getName());
207: checkEnv("ejbActivate");
208: }
209:
210: // ---------------------------------------------------------------------
211: // Implementation of the Remote interface
212: // ---------------------------------------------------------------------
213:
214: /**
215: * Retrieve my Father, as a Remote Object.
216: * Mainly convert Local object to Remote Object.
217: */
218: public People myFather() throws RemoteException {
219: logger.log(BasicLevel.DEBUG, getName());
220: People father = null;
221: PeopleLocal f = getFather();
222: if (f == null) {
223: return null;
224: }
225: try {
226: father = home.findByPrimaryKey(f.getName());
227: } catch (FinderException e) {
228: throw new RemoteException("Lost father");
229: }
230: return father;
231: }
232:
233: /**
234: * Retrieve my Mother, as a Remote Object.
235: * Mainly convert Local object to Remote Object.
236: */
237: public People myMother() throws RemoteException {
238: logger.log(BasicLevel.DEBUG, getName());
239: People mother = null;
240: PeopleLocal f = getMother();
241: if (f == null) {
242: return null;
243: }
244: try {
245: mother = home.findByPrimaryKey(f.getName());
246: } catch (FinderException e) {
247: throw new RemoteException("Lost mother");
248: }
249: return mother;
250: }
251:
252: /**
253: * Retrieve my children, as a Collection of Remote Objects.
254: * Mainly convert Local objects to Remote Objects.
255: */
256: public Collection myChildren() throws RemoteException {
257: logger.log(BasicLevel.DEBUG, getName());
258: Collection ret = new ArrayList();
259: Collection c = getChildren();
260: if (c.size() == 0) {
261: // If no children, maybe it's because I'm the father.
262: // In this case, must use a finder method (uni-directional relation)
263: try {
264: c = homeLocal.findChildren(getName());
265: } catch (FinderException e) {
266: throw new RemoteException("Cannot get children");
267: }
268: }
269: for (Iterator i = c.iterator(); i.hasNext();) {
270: PeopleLocal p = (PeopleLocal) i.next();
271: try {
272: People pr = home.findByPrimaryKey(p.getName());
273: ret.add(pr);
274: } catch (FinderException e) {
275: throw new RemoteException("Lost child");
276: }
277: }
278: return ret;
279: }
280:
281: /**
282: * Return true if not married
283: */
284: public boolean isSingle() throws RemoteException {
285: logger.log(BasicLevel.DEBUG, getName());
286: return (getUnion() == null);
287: }
288:
289: /**
290: * Return true if no father
291: */
292: public boolean hasNoFather() throws RemoteException {
293: logger.log(BasicLevel.DEBUG, getName());
294: return (getFather() == null);
295: }
296:
297: /**
298: * Return true if no mother
299: */
300: public boolean hasNoMother() throws RemoteException {
301: logger.log(BasicLevel.DEBUG, getName());
302: return (getMother() == null);
303: }
304:
305: /**
306: * @return the People we are married with
307: */
308: public People mySpouse() throws RemoteException {
309: logger.log(BasicLevel.DEBUG, getName());
310: People ret = null;
311: PeopleLocal f = getUnion();
312: if (f == null) {
313: return null;
314: }
315: try {
316: ret = home.findByPrimaryKey(f.getName());
317: } catch (FinderException e) {
318: throw new RemoteException("Lost spouse");
319: }
320: return ret;
321: }
322:
323: public int kidNumber() throws RemoteException {
324: logger.log(BasicLevel.DEBUG, getName());
325: int ret = getChildren().size();
326: if (ret == 0) {
327: // maybe I'm the father...
328: try {
329: ret = homeLocal.findChildren(getName()).size();
330: } catch (FinderException e) {
331: throw new RemoteException("Cannot get my children");
332: }
333: }
334: return ret;
335: }
336:
337: /**
338: * @return true if orphan
339: */
340: public boolean isOrphan() throws RemoteException {
341: logger.log(BasicLevel.DEBUG, getName());
342: return (getMother() == null && getFather() == null);
343: }
344:
345: /**
346: * @return true if is brother or sister with other People
347: * @param c name of the other people.
348: */
349: public boolean brotherSisterOf(String c) throws RemoteException {
350: logger.log(BasicLevel.DEBUG, getName());
351: try {
352: PeopleLocal p = homeLocal.findByPrimaryKey(c);
353: if (getMother() != null && p.getMother() == getMother()) {
354: return true;
355: }
356: if (getFather() != null && p.getFather() == getFather()) {
357: return true;
358: }
359: } catch (FinderException e) {
360: }
361: return false;
362: }
363:
364: // ---------------------------------------------------------------------
365: // private methods
366: // ---------------------------------------------------------------------
367:
368: /**
369: * Check environment variables
370: */
371: void checkEnv(String method) {
372: try {
373: String value = (String) myEnv.lookup("myname");
374: if (!value.equals("myentity")) {
375: logger.log(BasicLevel.ERROR,
376: ": myEnv.lookup failed: myname=" + value);
377: throw new EJBException("PeopleEC2 1: " + method);
378: }
379: } catch (NamingException e) {
380: logger.log(BasicLevel.ERROR,
381: ": myEnv.lookup raised exception:\n" + e);
382: throw new EJBException("PeopleEC2 2: " + method);
383: }
384: }
385:
386: }
|