1 25 26 package org.objectweb.jonas.jtests.beans.folder; 27 28 import java.io.Serializable ; 29 import java.rmi.RemoteException ; 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 import javax.ejb.CreateException ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.FinderException ; 35 import javax.ejb.NoSuchObjectLocalException ; 36 import javax.ejb.RemoveException ; 37 import javax.ejb.SessionBean ; 38 import javax.ejb.SessionSynchronization ; 39 import javax.ejb.SessionContext ; 40 import javax.ejb.Timer ; 41 import javax.ejb.TimerHandle ; 42 import javax.naming.Context ; 43 import javax.naming.InitialContext ; 44 import javax.naming.NamingException ; 45 import javax.rmi.PortableRemoteObject ; 46 import javax.sql.DataSource ; 47 import org.objectweb.jonas.common.Log; 48 import org.objectweb.util.monolog.api.Logger; 49 import org.objectweb.util.monolog.api.BasicLevel; 50 51 56 public class FolderSY implements SessionBean , SessionSynchronization { 57 58 protected static Logger logger = null; 59 SessionContext ejbContext; 60 InitialContext ictx; 61 Context myEnv; 62 FileHome filehome; 63 boolean rollbackOnly; 64 65 68 void checkEnv(String method) { 69 70 logger.log(BasicLevel.DEBUG, "Check directly in my context"); 72 try { 73 String value = (String ) myEnv.lookup("myname"); 74 if (!value.equals("mysession")) { 75 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value); 76 throw new EJBException ("FolderSY 1: " + method); 77 } 78 } catch (NamingException e) { 79 logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e); 80 throw new EJBException ("FolderSY 2: " + method); 81 } 82 logger.log(BasicLevel.DEBUG, "Idem with compound name"); 84 try { 85 String value = (String ) myEnv.lookup("dir1/dir2/name"); 86 if (!value.equals("sessionvalue")) { 87 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: dir1/dir2/name=" + value); 88 throw new EJBException ("FolderSY 3: " + method); 89 } 90 } catch (NamingException e) { 91 logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e); 92 throw new EJBException ("FolderSY 4: " + method); 93 } 94 logger.log(BasicLevel.DEBUG, "Check from initial Context"); 96 try { 97 String value = (String ) ictx.lookup("java:comp/env/myname"); 98 if (!value.equals("mysession")) { 99 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: myname=" + value); 100 throw new EJBException ("FolderSY 6: " + method); 101 } 102 } catch (NamingException e) { 103 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 104 throw new EJBException ("FolderSY 7: " + method); 105 } 106 107 logger.log(BasicLevel.DEBUG, "Check datasource directly"); 109 DataSource ds1 = null; 110 try { 111 ds1 = (DataSource ) ictx.lookup("jdbc_1"); 112 } catch (NamingException e) { 113 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 114 throw new EJBException ("FolderSY 8: " + method); 115 } 116 Connection con = null; 117 if (!method.equals("afterCompletion")) { 118 try { 119 con = (Connection ) ds1.getConnection(); 120 if (con.isClosed()) { 121 logger.log(BasicLevel.ERROR, ": connection is closed"); 122 throw new EJBException ("FolderSY 8a: " + method); 123 } 124 con.close(); 125 } catch (SQLException e) { 126 logger.log(BasicLevel.ERROR, ": getConnection:\n" + e); 127 throw new EJBException ("FolderSY 8b: " + method); 128 } 129 } 130 131 logger.log(BasicLevel.DEBUG, "Check DataSource from resource ref"); 133 DataSource ds2 = null; 134 try { 135 ds2 = (DataSource ) myEnv.lookup("jdbc/mydb"); 137 } catch (NamingException e) { 138 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 139 throw new EJBException ("FolderSY 9: " + method); 140 } 141 if (!method.equals("afterCompletion")) { 142 try { 143 con = (Connection ) ds2.getConnection(); 144 if (con.isClosed()) { 145 logger.log(BasicLevel.ERROR, ": connection is closed"); 146 throw new EJBException ("FolderSY 9a: " + method); 147 } 148 con.close(); 149 } catch (SQLException e) { 150 logger.log(BasicLevel.ERROR, ": getConnection:\n" + e); 151 throw new EJBException ("FolderSY 9b: " + method); 152 } 153 } 154 155 logger.log(BasicLevel.DEBUG, "Check boolean values"); 157 try { 158 Boolean value = (Boolean ) ictx.lookup("java:comp/env/bVrai"); 159 if (!value.booleanValue()) { 160 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bVrai=" + value); 161 throw new EJBException ("FolderSY 10a: " + method); 162 } 163 } catch (NamingException e) { 164 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 165 throw new EJBException ("FolderSY10a: " + method); 166 } catch (ClassCastException e) { 167 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 168 throw new EJBException ("FolderSY10a1: " + method); 169 } 170 try { 171 Boolean value = (Boolean ) ictx.lookup("java:comp/env/bFaux"); 172 if (value.booleanValue()) { 173 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bFaux=" + value); 174 throw new EJBException ("FolderSY 10b: " + method); 175 } 176 Character c = (Character ) ictx.lookup("java:comp/env/testChar"); 178 179 180 } catch (NamingException e) { 181 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 182 throw new EJBException ("FolderSY10b: " + method); 183 } catch (ClassCastException e) { 184 logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e); 185 throw new EJBException ("FolderSY10b1: " + method); 186 } 187 188 logger.log(BasicLevel.DEBUG, ": checkEnv OK"); 189 } 190 191 195 public void setSessionContext(SessionContext ctx) { 196 if (logger == null) { 197 logger = Log.getLogger(Log.JONAS_TESTS_PREFIX); 198 } 199 logger.log(BasicLevel.DEBUG, ""); 200 ejbContext = ctx; 201 try { 202 ictx = new InitialContext (); 204 myEnv = (Context ) ictx.lookup("java:comp/env"); 205 filehome = (FileHome) PortableRemoteObject.narrow(ictx.lookup("java:comp/env/ejb/file"), FileHome.class); 207 } catch (NamingException e) { 208 throw new EJBException ("FolderSY: Cannot get filehome:" + e); 209 } 210 checkEnv("setSessionContext"); 211 } 212 213 public void ejbRemove() { 214 logger.log(BasicLevel.DEBUG, ""); 215 checkEnv("ejbRemove"); 216 } 217 public void ejbCreate() throws CreateException { 218 logger.log(BasicLevel.DEBUG, ""); 219 checkEnv("ejbCreate"); 220 rollbackOnly = false; 221 } 222 public void ejbCreateForRollback() throws CreateException { 223 logger.log(BasicLevel.DEBUG, ""); 224 checkEnv("ejbCreate"); 225 rollbackOnly = true; 226 } 227 public void ejbPassivate() { 228 logger.log(BasicLevel.DEBUG, ""); 229 checkEnv("ejbPassivate"); 230 } 231 public void ejbActivate() { 232 logger.log(BasicLevel.DEBUG, ""); 233 checkEnv("ejbActivate"); 234 } 235 236 240 public void afterBegin() { 241 logger.log(BasicLevel.DEBUG, ""); 242 checkEnv("afterBegin"); 243 try { 244 ejbContext.getRollbackOnly(); 245 } catch (IllegalStateException e) { 246 throw new EJBException ("Cannot call getRollbackOnly in afterBegin"); 247 } 248 if (rollbackOnly) { 249 try { 250 ejbContext.setRollbackOnly(); 251 } catch (IllegalStateException e) { 252 throw new EJBException ("Cannot call setRollbackOnly in afterBegin"); 253 } 254 } 255 } 256 257 public void beforeCompletion() { 258 logger.log(BasicLevel.DEBUG, ""); 259 checkEnv("beforeCompletion"); 260 try { 261 ejbContext.getRollbackOnly(); 262 } catch (IllegalStateException e) { 263 throw new EJBException ("Cannot call getRollbackOnly in beforeCompletion"); 264 } 265 if (rollbackOnly) { 266 try { 267 ejbContext.setRollbackOnly(); 268 } catch (IllegalStateException e) { 269 throw new EJBException ("Cannot call setRollbackOnly in afterBegin"); 270 } 271 } 272 } 273 274 public void afterCompletion(boolean committed) { 275 logger.log(BasicLevel.DEBUG, ""); 276 checkEnv("afterCompletion"); 277 try { 279 ejbContext.getRollbackOnly(); 280 throw new EJBException ("getRollbackOnly forbidden in afterCompletion"); 281 } catch (IllegalStateException e) { 282 } 283 if (rollbackOnly) { 284 try { 285 ejbContext.setRollbackOnly(); 286 throw new EJBException ("setRollbackOnly forbidden in afterCompletion"); 287 } catch (IllegalStateException e) { 288 } 289 } 290 } 291 292 296 public File newFile(String fname) throws RemoteException , CreateException { 297 logger.log(BasicLevel.DEBUG, ""); 298 checkEnv("newFile"); 299 File ret = filehome.create(fname); 300 return ret; 301 } 302 303 public File getFile(String fname) throws RemoteException , FinderException { 304 logger.log(BasicLevel.DEBUG, ""); 305 checkEnv("getFile"); 306 File ret = filehome.findByPrimaryKey(fname); 307 return ret; 308 } 309 310 public boolean testTimerCancel(String fname) throws RemoteException , CreateException { 311 logger.log(BasicLevel.DEBUG, ""); 312 boolean ret = true; 313 314 File file = filehome.create(fname); 316 317 TimerHandle th = file.getTimerHandle(); 319 if (th == null) { 320 logger.log(BasicLevel.WARN, "Null Handle returned"); 321 return false; 322 } 323 324 Timer t = th.getTimer(); 326 Serializable sz = t.getInfo(); 327 if (!(sz instanceof Integer )) { 328 logger.log(BasicLevel.WARN, "Non-integer info value returned"); 329 return false; 330 } 331 332 file.cancelTimer(th); 334 335 try { 337 th.getTimer(); 338 logger.log(BasicLevel.WARN, "Timer still accessible after bean remove"); 339 ret = false; 340 } catch (NoSuchObjectLocalException e) { 341 } 342 343 try { 345 file.remove(); 346 } catch (Exception e) { 347 logger.log(BasicLevel.WARN, "Cannot remove the bean after use."); 348 return false; 349 } 350 return ret; 351 } 352 353 public boolean testTimerRemoved(String fname) throws RemoteException , CreateException , RemoveException { 354 logger.log(BasicLevel.DEBUG, ""); 355 boolean ret = true; 356 357 File file = filehome.create(fname); 359 360 TimerHandle th = file.getTimerHandle(); 362 if (th == null) { 363 logger.log(BasicLevel.WARN, "Null Handle returned"); 364 return false; 365 } 366 367 Serializable sz = th.getTimer().getInfo(); 369 if (!(sz instanceof Integer )) { 370 logger.log(BasicLevel.WARN, "Non-integer info value returned"); 371 return false; 372 } 373 374 file.remove(); 376 377 try { 379 th.getTimer(); 380 logger.log(BasicLevel.WARN, "Timer still accessible after bean remove"); 381 ret = false; 382 } catch (NoSuchObjectLocalException e) { 383 } 384 return ret; 385 } 386 387 public void sendRef(Folder f) throws RemoteException { 388 } 389 390 public void sendInt(int i) throws RemoteException { 391 } 392 393 public void sendRefTS(Folder f) throws RemoteException { 394 } 395 396 public void sendIntTS(int i) throws RemoteException { 397 } 398 399 public Folder getRef(Folder f) throws RemoteException { 400 return f; 401 } 402 403 public int getInt(int i) throws RemoteException { 404 return i; 405 } 406 407 public Folder getRefTS(Folder f) throws RemoteException { 408 return f; 409 } 410 411 public int getIntTS(int i) throws RemoteException { 412 return i; 413 } 414 415 public void sendArray(long[] x) throws RemoteException { 416 } 417 418 public long[] getArray() throws RemoteException { 419 long[] anArray; 420 anArray = new long [100]; 421 for (int i = 0; i < anArray.length; i++) { 422 anArray[i] = i; 423 } 424 return anArray; 425 } 426 } 427 | Popular Tags |