1 25 26 package org.objectweb.jonas.jtests.beans.relation.family; 27 28 import java.rmi.RemoteException ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EntityContext ; 36 import javax.ejb.FinderException ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.rmi.PortableRemoteObject ; 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 52 public abstract class PeopleEC2 implements javax.ejb.EntityBean { 53 54 private static Logger logger = null; 55 private EntityContext ejbContext; 56 private InitialContext ictx; 57 private Context myEnv; 58 private PeopleHomeLocal homeLocal = null; 59 private PeopleHome home = null; 60 61 public abstract String getName(); 65 public abstract void setName(String name); 66 67 public abstract Collection getChildren(); 71 public abstract void setChildren(Collection 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 86 public java.lang.String ejbCreate(String name, String papa, String maman) throws javax.ejb.CreateException { 87 logger.log(BasicLevel.DEBUG, getName()); 88 setName(name); 89 return null; 90 } 91 92 public void ejbPostCreate(String name, String papa, String maman) throws javax.ejb.CreateException { 93 logger.log(BasicLevel.DEBUG, getName()); 94 if (papa != null) { 95 try { 96 PeopleLocal father = homeLocal.findByPrimaryKey(papa); 97 setFather(father); 98 } catch (FinderException e) { 99 throw new CreateException ("Father unknown"); 100 } 101 } 102 if (maman != null) { 103 try { 104 PeopleLocal mother = homeLocal.findByPrimaryKey(maman); 105 setMother(mother); 106 } catch (FinderException e) { 107 throw new CreateException ("Mother unknown"); 108 } 109 } 110 } 111 112 115 public void ejbHomeUnion(String name1, String name2) throws FinderException , RemoteException { 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 e) { 122 logger.log(BasicLevel.ERROR, "Cannot find people"); 123 throw e; 124 } 125 } 126 127 130 public void ejbHomeDivorce(String name1, String name2) throws FinderException , RemoteException { 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 (name1 + " and " + name2 + " were not united yet"); 138 } 139 p1.setUnion(null); 140 } catch (FinderException e) { 141 logger.log(BasicLevel.ERROR, "Cannot find people"); 142 throw e; 143 } 144 } 145 146 147 151 public void setEntityContext(javax.ejb.EntityContext ctx) { 152 if (logger == null) { 154 logger = Log.getLogger("org.objectweb.jonas_tests"); 155 } 156 logger.log(BasicLevel.DEBUG, getName()); 157 ejbContext = ctx; 158 try { 159 ictx = new InitialContext (); 161 myEnv = (Context ) ictx.lookup("java:comp/env"); 162 } catch (NamingException e) { 163 throw new EJBException ("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 { 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 207 211 public People myFather() throws RemoteException { 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 e) { 221 throw new RemoteException ("Lost father"); 222 } 223 return father; 224 } 225 226 230 public People myMother() throws RemoteException { 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 e) { 240 throw new RemoteException ("Lost mother"); 241 } 242 return mother; 243 } 244 245 249 public Collection myChildren() throws RemoteException { 250 logger.log(BasicLevel.DEBUG, getName()); 251 Collection ret = new ArrayList (); 252 Collection c = getChildren(); 253 if (c.size() == 0) { 254 try { 257 c = homeLocal.findChildren(getName()); 258 } catch (FinderException e) { 259 throw new RemoteException ("Cannot get children"); 260 } 261 } 262 for (Iterator 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 e) { 268 throw new RemoteException ("Lost child"); 269 } 270 } 271 return ret; 272 } 273 274 277 public boolean isSingle() throws RemoteException { 278 logger.log(BasicLevel.DEBUG, getName()); 279 return (getUnion() == null); 280 } 281 282 285 public boolean hasNoFather() throws RemoteException { 286 logger.log(BasicLevel.DEBUG, getName()); 287 return (getFather() == null); 288 } 289 290 293 public boolean hasNoMother() throws RemoteException { 294 logger.log(BasicLevel.DEBUG, getName()); 295 return (getMother() == null); 296 } 297 298 301 public People mySpouse() throws RemoteException { 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 e) { 311 throw new RemoteException ("Lost spouse"); 312 } 313 return ret; 314 } 315 316 public int kidNumber() throws RemoteException { 317 logger.log(BasicLevel.DEBUG, getName()); 318 int ret = getChildren().size(); 319 if (ret == 0) { 320 try { 322 ret = homeLocal.findChildren(getName()).size(); 323 } catch (FinderException e) { 324 throw new RemoteException ("Cannot get my children"); 325 } 326 } 327 return ret; 328 } 329 330 333 public boolean isOrphan() throws RemoteException { 334 logger.log(BasicLevel.DEBUG, getName()); 335 return (getMother() == null && getFather() == null); 336 } 337 338 342 public boolean brotherSisterOf(String c) throws RemoteException { 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 e) { 353 } 354 return false; 355 } 356 357 361 364 void checkEnv(String method) { 365 try { 366 String value = (String ) myEnv.lookup("myname"); 367 if (!value.equals("myentity")) { 368 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value); 369 throw new EJBException ("PeopleEC2 1: " + method); 370 } 371 } catch (NamingException e) { 372 logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e); 373 throw new EJBException ("PeopleEC2 2: " + method); 374 } 375 } 376 377 } 378 | Popular Tags |