1 22 package org.jboss.test.cmp2.audit.beans; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.ResultSetMetaData ; 28 import java.sql.Timestamp ; 29 import java.sql.Types ; 30 import java.util.Date ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.SessionBean ; 35 import javax.ejb.SessionContext ; 36 import javax.naming.InitialContext ; 37 import javax.security.auth.login.LoginContext ; 38 import javax.sql.DataSource ; 39 40 import org.jboss.test.cmp2.audit.interfaces.ApplicationCallbackHandler; 41 import org.jboss.test.cmp2.audit.interfaces.Audit; 42 import org.jboss.test.cmp2.audit.interfaces.AuditHome; 43 import org.jboss.test.cmp2.audit.interfaces.AuditMapped; 44 import org.jboss.test.cmp2.audit.interfaces.AuditMappedHome; 45 46 52 public class AuditSessionBean 53 implements SessionBean 54 { 55 private static final int FULL = 1; 56 private static final int CREATE = 2; 57 private static final int UPDATE = 3; 58 private static final int CREATE_CHANGED_NAMES = 4; 59 private static final int UPDATE_CHANGED_NAMES = 5; 60 private static final int CREATE_MAPPED = 6; 61 private static final int UPDATE_MAPPED = 7; 62 63 private static String QUERY_FULL = "select audit_created_by, audit_created_time, audit_updated_by, audit_updated_time" 64 + " from cmp2_audit where id = ?"; 65 private static String QUERY_CREATE = "select audit_created_by, audit_created_time" 66 + " from cmp2_audit where id = ?"; 67 private static String QUERY_UPDATE = "select audit_updated_by, audit_updated_time" 68 + " from cmp2_audit where id = ?"; 69 private static String QUERY_CREATE_CHANGED_NAMES = "select createdby, createdtime" 70 + " from cmp2_audit_changednames where id = ?"; 71 private static String QUERY_UPDATE_CHANGED_NAMES = "select updatedby, updatedtime" 72 + " from cmp2_audit_changednames where id = ?"; 73 private static String QUERY_CREATE_MAPPED = "select createdby, createdtime" 74 + " from cmp2_audit_mapped where id = ?"; 75 private static String QUERY_UPDATE_MAPPED = "select updatedby, updatedtime" 76 + " from cmp2_audit_mapped where id = ?"; 77 78 public void createAudit(String id) 79 { 80 try 81 { 82 LoginContext login = ApplicationCallbackHandler.login("audituser1", "user1"); 83 try 84 { 85 AuditHome home = getAuditEJB(); 86 home.create(id); 87 } 88 finally 89 { 90 if (login != null) 91 login.logout(); 92 } 93 } 94 catch (Exception e) 95 { 96 throw new EJBException (e); 97 } 98 } 99 100 public void updateAudit(String id, String stringValue) 101 { 102 try 103 { 104 LoginContext login = ApplicationCallbackHandler.login("audituser2", "user2"); 105 try 106 { 107 AuditHome home = getAuditEJB(); 108 Audit audit = home.findByPrimaryKey(id); 109 audit.setStringValue(stringValue); 110 } 111 finally 112 { 113 if (login != null) 114 login.logout(); 115 } 116 } 117 catch (Exception e) 118 { 119 throw new EJBException (e); 120 } 121 } 122 123 public void updateAuditWithClear(String id, String stringValue) 124 { 125 try 126 { 127 LoginContext login = ApplicationCallbackHandler.login("old-client-login", "audituser2", "user2"); 128 try 129 { 130 AuditHome home = getAuditEJB(); 131 Audit audit = home.findByPrimaryKey(id); 132 audit.setStringValue(stringValue); 133 } 134 finally 135 { 136 if (login != null) 137 login.logout(); 138 } 139 } 140 catch (Exception e) 141 { 142 throw new EJBException (e); 143 } 144 } 145 146 public void createAuditChangedNames(String id) 147 { 148 try 149 { 150 LoginContext login = ApplicationCallbackHandler.login("audituser1", "user1"); 151 try 152 { 153 AuditHome home = getAuditChangedNamesEJB(); 154 home.create(id); 155 } 156 finally 157 { 158 if (login != null) 159 login.logout(); 160 } 161 } 162 catch (Exception e) 163 { 164 throw new EJBException (e); 165 } 166 } 167 168 public void updateAuditChangedNames(String id, String stringValue) 169 { 170 try 171 { 172 LoginContext login = ApplicationCallbackHandler.login("audituser2", "user2"); 173 try 174 { 175 AuditHome home = getAuditChangedNamesEJB(); 176 Audit audit = home.findByPrimaryKey(id); 177 audit.setStringValue(stringValue); 178 } 179 finally 180 { 181 if (login != null) 182 login.logout(); 183 } 184 } 185 catch (Exception e) 186 { 187 throw new EJBException (e); 188 } 189 } 190 191 public void createAuditMapped(String id) 192 { 193 try 194 { 195 LoginContext login = ApplicationCallbackHandler.login("audituser1", "user1"); 196 try 197 { 198 AuditMappedHome home = getAuditMappedEJB(); 199 home.create(id); 200 } 201 finally 202 { 203 if (login != null) 204 login.logout(); 205 } 206 } 207 catch (Exception e) 208 { 209 throw new EJBException (e); 210 } 211 } 212 213 public void updateAuditMapped(String id, String stringValue) 214 { 215 try 216 { 217 LoginContext login = ApplicationCallbackHandler.login("audituser2", "user2"); 218 try 219 { 220 AuditMappedHome home = getAuditMappedEJB(); 221 AuditMapped audit = home.findByPrimaryKey(id); 222 audit.setStringValue(stringValue); 223 } 224 finally 225 { 226 if (login != null) 227 login.logout(); 228 } 229 } 230 catch (Exception e) 231 { 232 throw new EJBException (e); 233 } 234 } 235 236 public void createAuditMappedChangedFields(String id, String user, long time) 237 { 238 try 239 { 240 LoginContext login = ApplicationCallbackHandler.login("audituser1", "user1"); 241 try 242 { 243 AuditMappedHome home = getAuditMappedEJB(); 244 AuditMapped audit = home.create(id); 245 audit.setCreatedBy(user); 246 audit.setCreatedTime(new Date (time)); 247 } 248 finally 249 { 250 if (login != null) 251 login.logout(); 252 } 253 } 254 catch (Exception e) 255 { 256 throw new EJBException (e); 257 } 258 } 259 260 public void updateAuditMappedChangedFields(String id, String stringValue, String user, long time) 261 { 262 try 263 { 264 LoginContext login = ApplicationCallbackHandler.login("audituser2", "user2"); 265 try 266 { 267 AuditMappedHome home = getAuditMappedEJB(); 268 AuditMapped audit = home.findByPrimaryKey(id); 269 audit.setStringValue(stringValue); 270 audit.setUpdatedBy(user); 271 audit.setUpdatedTime(new Date (time)); 272 } 273 finally 274 { 275 if (login != null) 276 login.logout(); 277 } 278 } 279 catch (Exception e) 280 { 281 throw new EJBException (e); 282 } 283 } 284 285 public String fullAuditCheck(String id, String user, long beginTime, long endTime) 286 { 287 try 288 { 289 AuditData auditData = getAuditData(id, FULL); 290 if (user.equals(auditData.createdBy) == false) 291 return "Expected created by to be set to " + user + " during the test but got " + auditData.createdBy; 292 if (auditData.createdTime < beginTime || auditData.createdTime > endTime) 293 return "Expected created time to be set between " + 294 beginTime + "-" + endTime + " during the test but got " + auditData.createdTime; 295 if (user.equals(auditData.updatedBy) == false) 296 return "Expected updated by to be set to " + user + " during the test but got " + auditData.updatedBy; 297 if (auditData.updatedTime < beginTime || auditData.updatedTime > endTime) 298 return "Expected updated time to be set between " + 299 beginTime + "-" + endTime + " during the test but got " + auditData.updatedTime; 300 return null; 301 } 302 catch (Exception e) 303 { 304 throw new EJBException (e); 305 } 306 } 307 308 public String createAuditCheck(String id, String user, long beginTime, long endTime) 309 { 310 return createCheck(id, CREATE, user, beginTime, endTime); 311 } 312 313 public String updateAuditCheck(String id, String user, long beginTime, long endTime) 314 { 315 return updateCheck(id, UPDATE, user, beginTime, endTime); 316 } 317 318 public String createAuditChangedNamesCheck(String id, String user, long beginTime, long endTime) 319 { 320 return createCheck(id, CREATE_CHANGED_NAMES, user, beginTime, endTime); 321 } 322 323 public String updateAuditChangedNamesCheck(String id, String user, long beginTime, long endTime) 324 { 325 return updateCheck(id, UPDATE_CHANGED_NAMES, user, beginTime, endTime); 326 } 327 328 public String createAuditMappedCheck(String id, String user, long beginTime, long endTime) 329 { 330 String failure = createCheck(id, CREATE_MAPPED, user, beginTime, endTime); 331 if (failure != null) 332 return failure; 333 334 try 335 { 336 LoginContext login = ApplicationCallbackHandler.login("audituser1", "user1"); 337 try 338 { 339 AuditMappedHome home = getAuditMappedEJB(); 340 AuditMapped audit = home.findByPrimaryKey(id); 341 if (user.equals(audit.getCreatedBy()) == false) 342 return "Expected getter to return user from test"; 343 long time = audit.getCreatedTime().getTime(); 344 if (time < beginTime || time > endTime) 345 return "Expected getter to return time from test"; 346 347 return null; 348 } 349 finally 350 { 351 if (login != null) 352 login.logout(); 353 } 354 } 355 catch (Exception e) 356 { 357 throw new EJBException (e); 358 } 359 } 360 361 public String updateAuditMappedCheck(String id, String user, long beginTime, long endTime) 362 { 363 String failure = updateCheck(id, UPDATE_MAPPED, user, beginTime, endTime); 364 if (failure != null) 365 return failure; 366 367 try 368 { 369 LoginContext login = ApplicationCallbackHandler.login("audituser1", "user1"); 370 try 371 { 372 AuditMappedHome home = getAuditMappedEJB(); 373 AuditMapped audit = home.findByPrimaryKey(id); 374 if (user.equals(audit.getUpdatedBy()) == false) 375 return "Expected getter to return user from test"; 376 long time = audit.getUpdatedTime().getTime(); 377 if (time < beginTime || time > endTime) 378 return "Expected getter to return time from test"; 379 380 return null; 381 } 382 finally 383 { 384 if (login != null) 385 login.logout(); 386 } 387 } 388 catch (Exception e) 389 { 390 throw new EJBException (e); 391 } 392 } 393 394 public String createCheck(String id, int type, String user, long beginTime, long endTime) 395 { 396 try 397 { 398 AuditData auditData = getAuditData(id, type); 399 if (user.equals(auditData.createdBy) == false) 400 return "Expected created by to be set to " + user + " during the test but got " + auditData.createdBy; 401 if (auditData.createdTime < beginTime || auditData.createdTime > endTime) 402 return "Expected created time to be set between " + 403 beginTime + "-" + endTime + " during the test but got " + auditData.createdTime; 404 return null; 405 } 406 catch (Exception e) 407 { 408 throw new EJBException (e); 409 } 410 } 411 412 public String updateCheck(String id, int type, String user, long beginTime, long endTime) 413 { 414 try 415 { 416 AuditData auditData = getAuditData(id, type); 417 if (user.equals(auditData.updatedBy) == false) 418 return "Expected updated by to be set to " + user + " during the test but got " + auditData.updatedBy; 419 if (auditData.updatedTime < beginTime || auditData.updatedTime > endTime) 420 return "Expected updated time to be set between " + 421 beginTime + "-" + endTime + " during the test but got " + auditData.updatedTime; 422 return null; 423 } 424 catch (Exception e) 425 { 426 throw new EJBException (e); 427 } 428 } 429 430 public void ejbCreate() 431 throws CreateException 432 { 433 } 434 435 public void setSessionContext(SessionContext ctx) 436 { 437 } 438 439 public void ejbActivate() 440 { 441 } 442 443 public void ejbPassivate() 444 { 445 } 446 447 public void ejbRemove() 448 { 449 } 450 451 private AuditData getAuditData(String id, int type) 452 throws Exception 453 { 454 Connection c = getDataSource().getConnection(); 455 PreparedStatement s = null; 456 try 457 { 458 switch (type) 459 { 460 case FULL: 461 s = c.prepareStatement(QUERY_FULL); 462 break; 463 case CREATE: 464 s = c.prepareStatement(QUERY_CREATE); 465 break; 466 case UPDATE: 467 s = c.prepareStatement(QUERY_UPDATE); 468 break; 469 case CREATE_CHANGED_NAMES: 470 s = c.prepareStatement(QUERY_CREATE_CHANGED_NAMES); 471 break; 472 case UPDATE_CHANGED_NAMES: 473 s = c.prepareStatement(QUERY_UPDATE_CHANGED_NAMES); 474 break; 475 case CREATE_MAPPED: 476 s = c.prepareStatement(QUERY_CREATE_MAPPED); 477 break; 478 case UPDATE_MAPPED: 479 s = c.prepareStatement(QUERY_UPDATE_MAPPED); 480 break; 481 } 488 s.setString(1, id); 489 ResultSet r = s.executeQuery(); 490 r.next(); 491 492 switch (type) 493 { 494 case FULL: 495 return new AuditData(r.getString(1), getTimestamp(r, 2), 496 r.getString(3), getTimestamp(r, 4)); 497 case CREATE: 498 case CREATE_CHANGED_NAMES: 499 case CREATE_MAPPED: 500 return new AuditData(r.getString(1), getTimestamp(r, 2), null, 0); 501 case UPDATE: 502 case UPDATE_CHANGED_NAMES: 503 case UPDATE_MAPPED: 504 return new AuditData(null, 0, r.getString(1), getTimestamp(r, 2)); 505 } 506 507 return null; 508 } 509 finally 510 { 511 if (s != null) 512 s.close(); 513 if (c != null) 514 c.close(); 515 } 516 } 517 518 private AuditHome getAuditEJB() 519 throws Exception 520 { 521 return (AuditHome) new InitialContext ().lookup("java:comp/env/ejb/AuditEJB"); 522 } 523 524 private AuditHome getAuditChangedNamesEJB() 525 throws Exception 526 { 527 return (AuditHome) new InitialContext ().lookup("java:comp/env/ejb/AuditChangedNamesEJB"); 528 } 529 530 private AuditMappedHome getAuditMappedEJB() 531 throws Exception 532 { 533 return (AuditMappedHome) new InitialContext ().lookup("java:comp/env/ejb/AuditMappedEJB"); 534 } 535 536 private DataSource getDataSource() 537 throws Exception 538 { 539 return (DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/DataSource"); 540 } 541 542 private static long getTimestamp(ResultSet r, int index) 543 throws Exception 544 { 545 ResultSetMetaData metaData = r.getMetaData(); 546 switch (metaData.getColumnType(index)) 547 { 548 case Types.DATE: 549 return r.getDate(index).getTime(); 550 case Types.TIMESTAMP: 551 Timestamp timestamp = r.getTimestamp(index); 552 long time = timestamp.getTime(); 553 if (time % 1000 == 0) 554 time += timestamp.getNanos() / 1000000; 555 return time; 556 } 557 return -1; 558 } 559 560 public static class AuditData 561 { 562 public String createdBy; 563 public long createdTime; 564 public String updatedBy; 565 public long updatedTime; 566 567 public AuditData(String createdBy, long createdTime, String updatedBy, long updatedTime) 568 { 569 this.createdBy = createdBy; 570 this.createdTime = createdTime; 571 this.updatedBy = updatedBy; 572 this.updatedTime = updatedTime; 573 } 574 } 575 } 576 | Popular Tags |