1 24 package org.objectweb.speedo.j2eedo.bo; 25 26 import java.util.Collection ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 31 import javax.jdo.JDOException; 32 import javax.jdo.JDOFatalException; 33 import javax.jdo.PersistenceManager; 34 import javax.jdo.Query; 35 36 import org.objectweb.speedo.Alea; 37 import org.objectweb.speedo.j2eedo.common.PMHolder; 38 import org.objectweb.speedo.j2eedo.database.Address; 39 import org.objectweb.speedo.j2eedo.database.DatabaseObjectInterface; 40 import org.objectweb.speedo.j2eedo.database.Department; 41 import org.objectweb.speedo.j2eedo.database.Employee; 42 import org.objectweb.speedo.j2eedo.database.Project; 43 import org.objectweb.util.monolog.Monolog; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.util.monolog.api.Logger; 46 import org.objectweb.util.monolog.api.LoggerFactory; 47 48 62 public class DatabaseImpl { 63 69 final public static String PARAMETER_PING = "ping"; 70 82 final public static String PARAMETER_NEW_DEPARTMENT = "newDept"; 83 95 final public static String PARAMETER_NEW_PROJECT = "newProj"; 96 104 final public static String PARAMETER_NEW_EMPLOYEE = "newEmp"; 105 110 final public static String PARAMETER_SET_BOSS = "setDeptBoss"; 111 116 final public static String PARAMETER_REM_EMPLOYEE = "delEmployee"; 117 122 final public static String PARAMETER_REM_PROJECT = "delProject"; 123 128 final public static String PARAMETER_SPLIT_PROJECT = "splitProject"; 129 134 final public static String PARAMETER_SPLIT_DEPARTMENT = "splitDepartment"; 135 140 final public static String PARAMETER_MERGE_DEPARTMENT = "mergeDept"; 141 146 final public static String PARAMETER_INCREASE_SALARY = "incSalary"; 147 152 final public static String PARAMETER_GET_DEPARTMENT = "getDepartment"; 153 158 final public static String PARAMETER_GET_PROJECT = "getProject"; 159 164 final public static String PARAMETER_GET_EMPLOYEE = "getEmployee"; 165 179 final public static String PARAMETER_QUERY_PROJECTS = "queryProjects"; 180 192 final public static String PARAMETER_QUERY_EMPLOYEES = "queryEmployees"; 193 194 final public static String PARAMETER_EVICTALL = "evictall"; 195 199 final public static String actionArray[] = 200 { 201 DatabaseImpl.PARAMETER_NEW_DEPARTMENT, 202 DatabaseImpl.PARAMETER_NEW_PROJECT, 203 DatabaseImpl.PARAMETER_NEW_EMPLOYEE, 204 DatabaseImpl.PARAMETER_SET_BOSS, 205 DatabaseImpl.PARAMETER_REM_EMPLOYEE, 206 DatabaseImpl.PARAMETER_REM_PROJECT, 207 DatabaseImpl.PARAMETER_SPLIT_PROJECT, 208 DatabaseImpl.PARAMETER_SPLIT_DEPARTMENT, 209 DatabaseImpl.PARAMETER_MERGE_DEPARTMENT, 210 DatabaseImpl.PARAMETER_INCREASE_SALARY, 211 DatabaseImpl.PARAMETER_GET_DEPARTMENT, 212 DatabaseImpl.PARAMETER_GET_PROJECT, 213 DatabaseImpl.PARAMETER_GET_EMPLOYEE, 214 DatabaseImpl.PARAMETER_QUERY_PROJECTS, 215 DatabaseImpl.PARAMETER_QUERY_EMPLOYEES, 216 DatabaseImpl.PARAMETER_PING }; 217 218 final public static int READ = 1; 219 final public static int WRITE = 0; 220 221 227 final public static int actionWeightArray[][] = { { 4, 0 }, { 230 5, 3 }, { 232 1, 1 }, { 234 4, 2 }, { 236 1, 1 }, { 238 4, 2 }, { 240 4, 3 }, { 242 4, 4 }, { 244 5, 2 }, { 246 3, 2 }, { 248 0, 5 }, { 250 0, 3 }, { 252 0, 1 }, { 254 0, 3 }, { 256 0, 2 }, { 258 0, 0 } }; 260 261 private String action; 262 private boolean performCommit; 263 private Hashtable needTransactionArray; 264 protected StringBuffer outStr; 265 private PMHolder persistenceManagerHolder; 266 267 270 public PersistenceManager pm; 271 private PollsSynchronizations pollsSync; 272 273 private DepartmentFactory departmentFactory = null; 274 private EmployeeFactory employeeFactory = null; 275 private ProjectFactory projectFactory = null; 276 277 285 public final static Vector poolOfDepartmentId = new Vector (); 286 294 public final static Vector poolOfProjectId = new Vector (); 295 303 public final static Vector poolOfEmployeeId = new Vector (); 304 305 private static boolean resetPools = true; 306 307 static Logger logger = null; 308 static { 309 LoggerFactory lf = Monolog.initialize(); 310 logger = lf.getLogger(DatabaseImpl.class.getName()); 311 } 312 313 private final static int PROJECT_INIT_SIZE = 100; 314 private final static int DEPARTMENT_INIT_SIZE = 30; 315 316 323 public DatabaseImpl(PMHolder pmHolder) { 324 super(); 325 this.persistenceManagerHolder = pmHolder; 326 this.action = ""; 327 this.performCommit = false; 328 this.outStr = new StringBuffer (); 329 this.departmentFactory = new DepartmentFactory(); 330 this.employeeFactory = new EmployeeFactory(); 331 this.projectFactory = new ProjectFactory(); 332 this.needTransactionArray = new Hashtable (); 333 int writeWeight; 335 for (int i = 0; i < DatabaseImpl.actionWeightArray.length; i++) { 336 String _action = DatabaseImpl.actionArray[i]; 337 writeWeight = DatabaseImpl.actionWeightArray[i][DatabaseImpl.WRITE]; 338 if (0 == writeWeight) 339 needTransactionArray.put(_action, new Boolean (false)); 340 else 341 needTransactionArray.put(_action, new Boolean (true)); 342 } 343 } 344 345 355 public String doAction(String parameter) throws JDOException, Exception { 356 this.action = parameter; 357 this.performCommit = false; 358 return doAction(); 359 } 360 361 374 public String doAction(String parameter, boolean withTransaction) 375 throws JDOException, Exception { 376 this.action = parameter; 377 this.performCommit = withTransaction; 378 return doAction(); 379 } 380 381 396 private String doAction() throws JDOException, Exception { 397 this.outStr = new StringBuffer (); 398 boolean needTrans = 400 (this.performCommit) 401 ? ((Boolean ) this.needTransactionArray.get(this.action)) 402 .booleanValue() 403 : false; 404 try { 405 if (PARAMETER_PING.equalsIgnoreCase(action)) { 406 return "Alive..."; 407 } 408 this.pm = this.persistenceManagerHolder.getPersistenceManager(); 409 if (PARAMETER_EVICTALL.equalsIgnoreCase(action)) { 410 logger.log(BasicLevel.WARN, "Flushing cache ..."); 411 this.pm.evictAll(); 412 resetPolls(this.pm, this.outStr); 413 return "Cache flushed !"; 414 } 415 DatabaseImpl.initPolls(this.pm); 416 417 if (!"".equalsIgnoreCase(this.action) 419 && ((Boolean ) this.needTransactionArray.get(this.action)) 420 .booleanValue()) { 421 this.pollsSync = new PollsSynchronizations(); 422 this.pm.setUserObject(pollsSync); 423 this.pm.currentTransaction().setSynchronization(pollsSync); 424 } 425 426 if (needTrans) { 427 logger.log( 428 BasicLevel.DEBUG, 429 "Start a new transaction " 430 + this.pm.currentTransaction().toString()); 431 this.pm.currentTransaction().begin(); 432 } 433 logger.log(BasicLevel.DEBUG, "do action " + this.action); 434 if (PARAMETER_NEW_DEPARTMENT.equalsIgnoreCase(action)) { 435 this.outStr.append("\nCreate new deparment with its employees"); 436 this.departmentFactory.setOutStr(this.outStr); 437 this.departmentFactory.setPm(this.pm); 438 this.departmentFactory.newDepartmentWithEmployees( 439 this.pollsSync); 440 } else if (PARAMETER_NEW_PROJECT.equalsIgnoreCase(action)) { 441 this.outStr.append("\nCreate new project with its members"); 442 this.projectFactory.setOutStr(this.outStr); 443 this.projectFactory.setPm(this.pm); 444 this.projectFactory.newProjectWithEmployees(this.pollsSync); 445 } else if (PARAMETER_SET_BOSS.equalsIgnoreCase(action)) { 446 this.outStr.append( 447 "\nUpdate the boss for a department's employees"); 448 this.departmentFactory.setOutStr(this.outStr); 449 this.departmentFactory.setPm(this.pm); 450 this.departmentFactory.setManagerForADepartment(); 451 } else if (PARAMETER_NEW_EMPLOYEE.equalsIgnoreCase(action)) { 452 this.outStr.append("\nCreate an employee"); 453 this.employeeFactory.setOutStr(this.outStr); 454 this.employeeFactory.setPm(this.pm); 455 this.employeeFactory.newEmployee(this.pollsSync); 456 } else if (PARAMETER_REM_EMPLOYEE.equalsIgnoreCase(action)) { 457 this.outStr.append("\nDelete an employee"); 458 this.employeeFactory.setOutStr(this.outStr); 459 this.employeeFactory.setPm(this.pm); 460 this.employeeFactory.deleteEmployee(this.pollsSync); 461 } else if (PARAMETER_REM_PROJECT.equalsIgnoreCase(action)) { 462 this.outStr.append("\nDelete a project"); 463 this.projectFactory.setOutStr(this.outStr); 464 this.projectFactory.setPm(this.pm); 465 this.projectFactory.deleteProject(this.pollsSync); 466 } else if (PARAMETER_SPLIT_PROJECT.equalsIgnoreCase(action)) { 467 this.outStr.append("\nSplit a project"); 468 this.projectFactory.setOutStr(this.outStr); 469 this.projectFactory.setPm(this.pm); 470 this.projectFactory.splitProject(this.pollsSync); 471 } else if (PARAMETER_MERGE_DEPARTMENT.equalsIgnoreCase(action)) { 472 this.outStr.append("\nMerge two departments"); 473 this.departmentFactory.setOutStr(this.outStr); 474 this.departmentFactory.setPm(this.pm); 475 this.departmentFactory.mergeDepartment(this.pollsSync); 476 } else if (PARAMETER_SPLIT_DEPARTMENT.equalsIgnoreCase(action)) { 477 this.outStr.append("\nSplit a department"); 478 this.departmentFactory.setOutStr(this.outStr); 479 this.departmentFactory.setPm(this.pm); 480 this.departmentFactory.splitDepartment(this.pollsSync); 481 } else if (PARAMETER_INCREASE_SALARY.equalsIgnoreCase(action)) { 482 this.outStr.append("\nIncrease salary"); 483 this.employeeFactory.setOutStr(this.outStr); 484 this.employeeFactory.setPm(this.pm); 485 this.employeeFactory.increaseSalary(); 486 } else if (PARAMETER_GET_DEPARTMENT.equalsIgnoreCase(action)) { 487 this.outStr.append("\nGet a department"); 488 this.departmentFactory.setOutStr(this.outStr); 489 this.departmentFactory.setPm(this.pm); 490 this.departmentFactory.getDepartment(); 491 } else if (PARAMETER_GET_PROJECT.equalsIgnoreCase(action)) { 492 this.outStr.append("\nGet a project"); 493 this.projectFactory.setOutStr(this.outStr); 494 this.projectFactory.setPm(this.pm); 495 this.projectFactory.getProject(); 496 } else if (PARAMETER_GET_EMPLOYEE.equalsIgnoreCase(action)) { 497 this.outStr.append("\nGet an employee"); 498 this.employeeFactory.setOutStr(this.outStr); 499 this.employeeFactory.setPm(this.pm); 500 this.employeeFactory.getEmployee(); 501 } else if (PARAMETER_QUERY_PROJECTS.equalsIgnoreCase(action)) { 502 this.outStr.append("\nGet some projects"); 503 this.projectFactory.setOutStr(this.outStr); 504 this.projectFactory.setPm(this.pm); 505 this.projectFactory.getProjects(); 506 } else if (PARAMETER_QUERY_EMPLOYEES.equalsIgnoreCase(action)) { 507 this.outStr.append("\nGet some persons"); 508 this.employeeFactory.setOutStr(this.outStr); 509 this.employeeFactory.setPm(this.pm); 510 this.employeeFactory.getEmployees(); 511 } else { 512 logger.log(BasicLevel.DEBUG, "Resets and shows pools"); 513 resetPolls(this.pm, new StringBuffer ()); 514 } 515 logger.log(BasicLevel.DEBUG, "End of action : " + action); 516 if (needTrans) { 518 logger.log( 519 BasicLevel.DEBUG, 520 "Valid the current transaction " 521 + this.pm.currentTransaction().toString()); 522 this.pm.currentTransaction().setSynchronization(this.pollsSync); 523 this.pm.currentTransaction().commit(); 524 } 525 } catch (JDOFatalException e) { 526 if (logger.isLoggable(BasicLevel.DEBUG)) { 527 logger.log(BasicLevel.DEBUG, "The action : '" + action 528 + "' fails : Deadlock detected. Action canceled: ", e); 529 } else { 530 logger.log(BasicLevel.WARN, "The action : '" + action 531 + "' fails : Deadlock detected. Action canceled." + e.getMessage()); 532 } 533 if (needTrans) 534 this.pm.currentTransaction().rollback(); 535 536 } catch (JDOException e) { 541 logger.log( 542 BasicLevel.INFO, 543 "The action : '" 544 + action 545 + "' fails : it throws a JDO exception.", 546 e); 547 throw e; 548 } catch (Exception e) { 549 logger.log( 550 BasicLevel.WARN, 551 "The action : '" + action + "' fails : it throws an exception.", 552 e); 553 throw e; 554 } finally { 555 this.persistenceManagerHolder.closePersistenceManager(); 556 } 557 return this.outStr.toString(); 558 } 559 560 564 private synchronized static void resetPolls(PersistenceManager pm, StringBuffer outStr) 565 throws JDOException, Exception { 566 DatabaseImpl.poolOfDepartmentId.clear(); 567 DatabaseImpl.poolOfEmployeeId.clear(); 568 DatabaseImpl.poolOfProjectId.clear(); 569 DatabaseImpl.resetPools = true; 570 DatabaseImpl.initPolls(pm); 571 if (outStr != null) { 572 outStr.append("\nDo nothing and dump static poll contents"); 573 outStr.append("\nDepartments:"); 574 outStr.append(DatabaseImpl.poolOfDepartmentId.toString()); 575 outStr.append("\nEmployees:"); 576 outStr.append(DatabaseImpl.poolOfEmployeeId.toString()); 577 outStr.append("\nProjects:"); 578 outStr.append(DatabaseImpl.poolOfProjectId.toString()); 579 } 580 } 581 582 private static synchronized void initPolls(PersistenceManager pm) 583 throws JDOException, Exception { 584 if (!DatabaseImpl.resetPools) 585 return; 586 try { 587 if (DatabaseImpl.poolOfDepartmentId.size() == 0) { 588 DatabaseImpl.initIdPoll( 589 DatabaseImpl.poolOfDepartmentId, 590 Department.class, 591 pm); 592 logger.log( 593 BasicLevel.DEBUG, 594 "Initialize the static pool of Departments Id : " 595 + DatabaseImpl.poolOfDepartmentId.toString()); 596 } 597 if (DatabaseImpl.poolOfProjectId.size() == 0) { 598 DatabaseImpl.initIdPoll( 599 DatabaseImpl.poolOfProjectId, 600 Project.class, 601 pm); 602 logger.log( 603 BasicLevel.DEBUG, 604 "Initialize the static pool of Projects Id : " 605 + DatabaseImpl.poolOfProjectId.toString()); 606 } 607 if (DatabaseImpl.poolOfEmployeeId.size() == 0) { 608 DatabaseImpl.initIdPoll( 609 DatabaseImpl.poolOfEmployeeId, 610 Employee.class, 611 pm); 612 logger.log( 613 BasicLevel.DEBUG, 614 "Initialize the static pool of Employees Id : " 615 + DatabaseImpl.poolOfEmployeeId.toString()); 616 } 617 } finally { 618 DatabaseImpl.resetPools = false; 619 } 620 } 621 622 private static void initIdPoll( 623 Collection poll, 624 Class c, 625 PersistenceManager pm) 626 throws JDOException, Exception { 627 Query query = pm.newQuery(c); 628 logger.log( 629 BasicLevel.DEBUG, 630 "(Re)Initialize static pool id for classe : " + c.getName()); 631 Collection col = (Collection ) query.execute(); 632 Iterator it = col.iterator(); 633 while (it.hasNext()) { 634 Object o = it.next(); 635 try { 636 poll.add(new Long (((DatabaseObjectInterface) o).getId())); 637 } catch (Exception e) { 638 throw e; 639 } 640 } 641 query.close(col); 642 } 643 644 final private static long getIdFromPool(Vector poll) { 645 while (DatabaseImpl.resetPools) { 647 try { 648 logger.log( 649 BasicLevel.DEBUG, 650 "sleep until the end off static pool id reset..."); 651 Thread.sleep(10); 652 } catch (InterruptedException e) { 653 } 654 } 655 int alea = Alea.rand(1, poll.size()); 656 Iterator it = poll.iterator(); 657 long current = -1; 658 while (it.hasNext() && alea > 0) { 659 current = ((Long ) it.next()).longValue(); 660 alea--; 661 if (!it.hasNext()) 662 it = poll.iterator(); 663 } 664 return current; 665 } 666 667 public static void initTestData(PMHolder pmHolder) 668 throws JDOException, Exception { 669 String str; 670 PersistenceManager pm = pmHolder.getPersistenceManager(); 671 DatabaseImpl departmentImpl = null; 672 departmentImpl = new DatabaseImpl(pmHolder); 673 pm.evictAll(); 674 Iterator objIter = null; 675 DatabaseImpl.poolOfDepartmentId.clear(); 676 DatabaseImpl.poolOfEmployeeId.clear(); 677 DatabaseImpl.poolOfProjectId.clear(); 678 Class [] classes = new Class [] { 680 Employee.class, 681 Address.class, 682 Project.class, 683 Department.class 684 }; 685 pm.currentTransaction().begin(); 686 for (int i = 0; i < classes.length; i++) { 687 logger.log(BasicLevel.DEBUG, "Removing " + classes[i].getName() + " ..."); 688 pm.deletePersistentAll((Collection ) pm.newQuery(classes[i]).execute()); 689 logger.log(BasicLevel.INFO, "All " + classes[i].getName() + " have been removed."); 690 } 691 pm.currentTransaction().commit(); 692 693 pm.evictAll(); 694 695 pm.currentTransaction().begin(); 696 logger.log(BasicLevel.INFO, "Init departments and employees data."); 697 for (int i = 0; i < DEPARTMENT_INIT_SIZE; i++) { 698 str = departmentImpl.doAction(DatabaseImpl.PARAMETER_NEW_DEPARTMENT); 699 logger.log(BasicLevel.DEBUG, str); 700 } 701 DatabaseImpl.resetPools = true; 702 logger.log(BasicLevel.INFO, "Init projects data."); 703 for (int i = 0; i < PROJECT_INIT_SIZE; i++) { 704 str = departmentImpl.doAction(DatabaseImpl.PARAMETER_NEW_PROJECT); 705 logger.log(BasicLevel.DEBUG, str); 706 } 707 DatabaseImpl.resetPools = true; 708 pm.currentTransaction().commit(); 709 logger.log(BasicLevel.INFO, "Initial data set."); 710 resetPolls(pm, null); 711 pm.evictAll(); 712 logger.log(BasicLevel.DEBUG, "Remove all cache entries."); 713 pmHolder.closePersistenceManager(); 714 logger.log(BasicLevel.DEBUG, "Close the persistenceManager."); 715 } 716 717 722 public static long getDepartmentIdFromPool() { 723 return DatabaseImpl.getIdFromPool(DatabaseImpl.poolOfDepartmentId); 725 } 727 728 733 public static long getEmployeeIdFromPool() { 734 return DatabaseImpl.getIdFromPool(DatabaseImpl.poolOfEmployeeId); 736 } 738 739 744 public static long getProjectIdFromPool() { 745 return DatabaseImpl.getIdFromPool(DatabaseImpl.poolOfProjectId); 747 } 749 } | Popular Tags |