1 25 26 package org.objectweb.jonas.jtests.beans.ebasic; 27 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.util.Collection ; 33 import java.util.Enumeration ; 34 import java.util.Vector ; 35 36 import javax.ejb.CreateException ; 37 import javax.ejb.EntityBean ; 38 import javax.ejb.FinderException ; 39 import javax.ejb.ObjectNotFoundException ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.sql.DataSource ; 43 44 import org.objectweb.util.monolog.api.BasicLevel; 45 46 52 public class SimpleEB extends SimpleEC implements EntityBean { 53 54 static final String tableName = "ebasicSimpleEB"; 55 56 private DataSource dataSource = null; 58 private static String dataSourceName; 59 60 61 public void ejbLoad() { 62 logger.log(BasicLevel.DEBUG, ""); 63 String testname = (String )entityContext.getPrimaryKey(); 64 PreparedStatement loadPstmt = null; 65 Connection conn = null; 66 try { 67 conn = getConnection(); 68 loadPstmt = conn.prepareStatement("select c_info, c_numtest, c_testname from "+tableName+" where c_testname=?"); 69 70 loadPstmt.setString(1, testname); 72 ResultSet rs = loadPstmt.executeQuery(); 73 if (rs.next() == false) { 74 throw new javax.ejb.EJBException ("Failed to load bean from database "+testname); 75 }; 76 info = rs.getInt(1); 77 testname = rs.getString(3); 78 numtest = rs.getInt(2); 79 80 } catch (SQLException e) { 81 throw new javax.ejb.EJBException ("Failed to load bean from database" +e); 82 } finally { 83 try{ 84 loadPstmt.close(); 85 conn.close(); 86 } catch(Exception e) { 87 System.err.println("ejbLoad: Ignore exception:"+e); 88 } 89 } 90 91 } 92 93 public void ejbStore() { 94 logger.log(BasicLevel.DEBUG, ""); 95 String testname = (String )entityContext.getPrimaryKey(); 96 PreparedStatement storePstmt = null; 97 Connection conn = null; 98 99 try { 100 conn = getConnection(); 101 storePstmt = conn.prepareStatement("update "+tableName+" set c_info=?,c_numtest=? where c_testname=?"); 102 103 storePstmt.setString(3, testname); 104 storePstmt.setInt(1, info); 105 storePstmt.setInt(2, numtest); 106 storePstmt.executeUpdate(); 107 } catch (SQLException e) { 108 throw new javax.ejb.EJBException ("Failed to store bean to database "+testname); 109 110 } finally { 111 try{ 112 storePstmt.close(); 113 conn.close(); 114 } catch(Exception e) { 115 System.err.println("ejbStore: Ignore exception:"+e); 116 } 117 } 118 } 119 120 public void ejbRemove() { 121 logger.log(BasicLevel.DEBUG, ""); 122 String testname = (String )entityContext.getPrimaryKey(); 123 PreparedStatement removePstmt = null; 124 Connection conn = null; 125 try { 126 conn = getConnection(); 127 removePstmt = conn.prepareStatement("delete from "+tableName+" where c_testname=?"); 128 removePstmt.setString(1, testname ); 130 removePstmt.executeUpdate(); 131 } catch (SQLException e) { 132 throw new javax.ejb.EJBException ("Failed to delete bean from database "+testname); 133 } finally { 134 try{ 135 removePstmt.close(); 136 conn.close(); 137 } catch(Exception e) { 138 System.err.println("ejbRemove: Ignore exception:"+e); 139 } 140 } 141 } 142 143 144 public String ejbCreate(String name, int info, int num) throws CreateException { 145 146 super.ejbCreate(name, info, num); 147 148 PreparedStatement createPstmt = null; 149 Connection conn = null; 150 151 try { 153 conn = getConnection(); 154 createPstmt = conn.prepareStatement("insert into "+tableName+" values (?, ?, ?)"); 155 createPstmt.setString(1, testname); 157 createPstmt.setInt(2, info); 158 createPstmt.setInt(3, numtest); 159 createPstmt.executeUpdate(); 160 } catch (SQLException e) { 161 logger.log(BasicLevel.ERROR, "ejbCreate failed: " + e); 162 throw new javax.ejb.EJBException ("Failed to create bean in database "+testname); 163 } finally { 164 try{ 165 createPstmt.close(); 166 conn.close(); 167 } catch(Exception e) { 168 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 170 } 171 } 172 return name; 173 } 174 175 public void ejbPostCreate(String name, int info, int num) { 176 super.ejbPostCreate(name, info, num); 177 } 178 179 180 181 public String ejbFindByPrimaryKey(String name ) throws ObjectNotFoundException , FinderException { 182 logger.log(BasicLevel.DEBUG, "name="+ name); 183 PreparedStatement loadPstmt = null; 184 Connection conn = null; 185 try { 186 conn = getConnection(); 187 loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?"); 188 loadPstmt.setString(1,name ); 189 ResultSet rs = loadPstmt.executeQuery(); 190 if (rs.next() == false) { 191 throw new javax.ejb.ObjectNotFoundException ("primary key : "+name); 192 } 193 } catch (SQLException e) { 194 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 195 } finally { 196 try{ 197 loadPstmt.close(); 198 conn.close(); 199 } catch(Exception e) { 200 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 202 } 203 } 204 return name; 205 } 206 207 208 public String ejbFindByTestName(String name) 209 throws ObjectNotFoundException , FinderException { 210 logger.log(BasicLevel.DEBUG, "name="+ name); 211 PreparedStatement loadPstmt = null; 212 Connection conn = null; 213 214 try { 215 conn = getConnection(); 216 loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?"); 217 loadPstmt.setString(1, name); 218 ResultSet rs = loadPstmt.executeQuery(); 219 if (rs.next() == false) { 220 throw new javax.ejb.ObjectNotFoundException ("primary key : "+name); 221 } 222 } catch (SQLException e) { 223 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 224 } finally { 225 try{ 226 loadPstmt.close(); 227 conn.close(); 228 } catch(Exception e) { 229 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 231 } 232 } 233 234 return name; 235 } 236 237 240 public String ejbFindByName(String name) 241 throws ObjectNotFoundException , FinderException { 242 logger.log(BasicLevel.DEBUG, "name="+ name); 243 PreparedStatement loadPstmt = null; 244 Connection conn = null; 245 246 try { 247 conn = getConnection(); 248 loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?"); 249 loadPstmt.setString(1, name); 250 ResultSet rs = loadPstmt.executeQuery(); 251 if (rs.next() == false) { 252 throw new javax.ejb.ObjectNotFoundException ("primary key : "+name); 253 } 254 } catch (SQLException e) { 255 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 256 } finally { 257 try{ 258 loadPstmt.close(); 259 conn.close(); 260 } catch(Exception e) { 261 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 263 } 264 } 265 if (entityContext != null) { 267 try { 268 entityContext.getTimerService(); 269 throw new FinderException ("Should not get TimerService from a finder method"); 270 } catch (IllegalStateException e) { 271 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown"); 272 } 273 try { 274 entityContext.getPrimaryKey(); 275 throw new FinderException ("Should not get PK from a finder method"); 276 } catch (IllegalStateException e) { 277 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown"); 278 } 279 } 280 return name; 281 } 282 283 public Enumeration ejbFindInfoForNum(int num) throws FinderException { 284 logger.log(BasicLevel.DEBUG, "num="+ num); 285 PreparedStatement loadPstmt = null; 286 Connection conn = null; 287 Vector pkV = new Vector (); 288 try { 289 conn = getConnection(); 290 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest=?"); 291 loadPstmt.setInt(1, num); 292 ResultSet rs = loadPstmt.executeQuery(); 293 294 while(rs.next()) { 295 String pk = rs.getString(1); 296 pkV.addElement(pk); 297 298 } 299 300 } catch (SQLException e) { 301 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 302 } finally { 303 try{ 304 loadPstmt.close(); 305 conn.close(); 306 } catch(Exception e) { 307 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 309 } 310 } 311 return (pkV.elements()); 312 } 313 314 public String ejbFindOneByNum(int numTest) 315 throws ObjectNotFoundException , FinderException { 316 logger.log(BasicLevel.DEBUG, "numTest=" + numTest); 317 throw new FinderException (); 318 } 319 320 public Collection ejbFindInCollection() throws FinderException { 321 logger.log(BasicLevel.DEBUG, ""); 322 PreparedStatement loadPstmt = null; 323 Connection conn = null; 324 Vector pkV = new Vector (); 325 try { 326 conn = getConnection(); 327 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest =?"); 328 loadPstmt.setInt(1, 8); 329 ResultSet rs = loadPstmt.executeQuery(); 330 while (rs.next()) { 331 String pk = rs.getString(1); 332 pkV.addElement(pk); 333 } 334 } catch (SQLException e) { 335 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 336 } finally { 337 try{ 338 loadPstmt.close(); 339 conn.close(); 340 } catch(Exception e) { 341 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 343 } 344 } 345 return (pkV); 346 } 347 348 public Enumeration ejbFindInfo_beetwen(int p1, int p2) throws FinderException { 349 logger.log(BasicLevel.DEBUG, ""); 350 Vector pkC = new Vector (); 351 Connection conn = null; 352 PreparedStatement loadPstmt = null; 353 try { 354 conn = getConnection(); 355 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where ?<=c_info and c_info<=? and ?<=c_numtest and c_numtest<=?"); 356 loadPstmt.setInt(1, p1); 357 loadPstmt.setInt(2, p2); 358 loadPstmt.setInt(3, p1); 359 loadPstmt.setInt(4, p2); 360 361 ResultSet rs = loadPstmt.executeQuery(); 362 while (rs.next()) { 363 String pk = rs.getString(1); 364 pkC.addElement(pk); 365 } 366 } catch (Exception e) { 367 throw new javax.ejb.FinderException ("Failed to find bean from database (findInfo_beetwen)"); 368 } finally { 369 try{ 370 loadPstmt.close(); 371 conn.close(); 372 } catch(Exception e) { 373 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 375 } 376 } 377 return(pkC.elements()); 378 } 379 380 public Enumeration ejbFindEqualOne(int p1) throws FinderException { 381 logger.log(BasicLevel.DEBUG, ""); 382 Vector pkC = new Vector (); 383 Connection conn = null; 384 PreparedStatement loadPstmt = null; 385 try { 386 conn = getConnection(); 387 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_info = ? and c_numtest = ?"); 388 loadPstmt.setInt(1, p1); 389 loadPstmt.setInt(2, p1); 390 391 ResultSet rs = loadPstmt.executeQuery(); 392 while (rs.next()) { 393 String pk = rs.getString(1); 394 pkC.addElement(pk); 395 } 396 } catch (Exception e) { 397 throw new javax.ejb.FinderException ("Failed to find bean from database (findInfo_beetwen)"); 398 } finally { 399 try{ 400 loadPstmt.close(); 401 conn.close(); 402 } catch(Exception e) { 403 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 405 } 406 } 407 return(pkC.elements()); 408 } 409 410 public Enumeration ejbFindEqualTwo(int p1, int p2) throws FinderException { 411 logger.log(BasicLevel.DEBUG, ""); 412 Vector pkC = new Vector (); 413 Connection conn = null; 414 PreparedStatement loadPstmt = null; 415 try { 416 conn = getConnection(); 417 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_info = ? and c_numtest = ?"); 418 loadPstmt.setInt(1, p1); 419 loadPstmt.setInt(2, p2); 420 421 ResultSet rs = loadPstmt.executeQuery(); 422 while (rs.next()) { 423 String pk = rs.getString(1); 424 pkC.addElement(pk); 425 } 426 } catch (Exception e) { 427 throw new javax.ejb.FinderException ("Failed to find bean from database (findInfo_beetwen)"); 428 } finally { 429 try{ 430 loadPstmt.close(); 431 conn.close(); 432 } catch(Exception e) { 433 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e); 435 } 436 } 437 if (entityContext != null) { 439 try { 440 entityContext.getTimerService(); 441 throw new FinderException ("Should not get TimerService from a finder method"); 442 } catch (IllegalStateException e) { 443 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown"); 444 } 445 try { 446 entityContext.getPrimaryKey(); 447 throw new FinderException ("Should not get PK from a finder method"); 448 } catch (IllegalStateException e) { 449 logger.log(BasicLevel.DEBUG, "IllegalStateException has been thrown"); 450 } 451 } 452 return(pkC.elements()); 453 } 454 455 private Connection getConnection() throws java.sql.SQLException { 456 if (dataSource == null) { 457 Context initialContext = null; 459 try { 460 initialContext = new InitialContext (); 461 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/BMP"); 462 } catch (Exception e) { 463 throw new javax.ejb.EJBException ("Pb with naming context "); 464 } 465 } 466 Connection ret = dataSource.getConnection(); 467 if (ret == null) { 468 throw new javax.ejb.EJBException ("dataSource.getConnection() returned null"); 469 } 470 return ret; 471 } 472 473 public void ejbSelect() { 475 } 476 477 } 478 | Popular Tags |