1 64 65 package com.jcorporate.expresso.ext.controller; 66 67 import com.jcorporate.expresso.core.ExpressoSchema; 68 import com.jcorporate.expresso.core.controller.Block; 69 import com.jcorporate.expresso.core.controller.Controller; 70 import com.jcorporate.expresso.core.controller.ControllerException; 71 import com.jcorporate.expresso.core.controller.ControllerInstallLog; 72 import com.jcorporate.expresso.core.controller.ControllerRequest; 73 import com.jcorporate.expresso.core.controller.ControllerResponse; 74 import com.jcorporate.expresso.core.controller.DBController; 75 import com.jcorporate.expresso.core.controller.ErrorCollection; 76 import com.jcorporate.expresso.core.controller.Input; 77 import com.jcorporate.expresso.core.controller.NonHandleableException; 78 import com.jcorporate.expresso.core.controller.Output; 79 import com.jcorporate.expresso.core.controller.State; 80 import com.jcorporate.expresso.core.controller.Transition; 81 import com.jcorporate.expresso.core.dataobjects.Securable; 82 import com.jcorporate.expresso.core.db.DBException; 83 import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException; 84 import com.jcorporate.expresso.core.dbobj.DBObject; 85 import com.jcorporate.expresso.core.dbobj.Schema; 86 import com.jcorporate.expresso.core.dbobj.SchemaFactory; 87 import com.jcorporate.expresso.core.job.Job; 88 import com.jcorporate.expresso.core.misc.ConfigManager; 89 import com.jcorporate.expresso.core.misc.StringUtil; 90 import com.jcorporate.expresso.core.utility.DBTool; 91 import com.jcorporate.expresso.services.dbobj.SchemaList; 92 import com.jcorporate.expresso.services.test.SchemaCreator; 93 import com.jcorporate.expresso.services.test.SchemaDeleter; 94 import org.apache.log4j.Logger; 95 96 import java.util.ArrayList ; 97 import java.util.Enumeration ; 98 import java.util.Hashtable ; 99 import java.util.Iterator ; 100 import java.util.Vector ; 101 102 103 120 121 public class ComponentManager extends DBController { 122 private static Logger log = Logger.getLogger("expresso.ext.controller.ComponentManager"); 123 124 public ComponentManager() { 125 State s = new State("list", "List Components"); 126 this.addState(s); 127 128 s = new State("promptAdd", "Add Component"); 129 this.addState(s); 130 131 s = new State("processAdd", "Process Add"); 132 s.addRequiredParameter("componentClass"); 133 this.addState(s); 134 135 s = new State("componentDetails", "Show Component Details"); 136 s.addRequiredParameter("componentClass"); 137 this.addState(s); 138 139 s = new State("promptDelete", "Prompt Delete"); 140 this.addState(s); 141 142 s = new State("processDelete", "Process Schema Deletion"); 143 this.addState(s); 144 145 s = new State("verifyInstall", "Verify Installation Of All Components"); 146 this.addState(s); 147 148 this.setInitialState("list"); 149 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 150 } 151 152 157 public String getTitle() { 158 return "Expresso Component Manager"; 159 } 160 161 172 protected ControllerResponse runListState(ControllerRequest request, 173 ControllerResponse response) 174 throws ControllerException, NonHandleableException { 175 176 try { 177 SchemaList sl = new SchemaList(request); 178 ArrayList installedComponents = sl.searchAndRetrieveList(SchemaList.FLD_SCHEMA_CLASS); 179 180 response.setTitle("Currently Installed Components"); 181 Block installedBlock = new Block("installedComponents"); 182 response.add(installedBlock); 183 installedBlock.setAttribute("header-row", "Mark|Component|Version"); 184 for (Iterator i = installedComponents.iterator(); i.hasNext();) { 185 Block oneRow = new Block("oneRow"); 186 installedBlock.add(oneRow); 187 SchemaList oneItem = (SchemaList) i.next(); 188 String className = oneItem.getField(SchemaList.FLD_SCHEMA_CLASS); 189 Input mark = new Input("mark_" + className, "Mark"); 190 mark.setAttribute("checkBox", "Y"); 191 oneRow.add(mark); 192 193 oneRow.add(new Output("className", className)); 194 oneRow.add(new Output("name", oneItem.getField(SchemaList.FLD_DESCRIP))); 195 oneRow.add(new Output("version", oneItem.getField(SchemaList.FLD_VERSION_NUMBER))); 196 197 Transition details = new Transition("details", 198 oneItem.getField(SchemaList.FLD_DESCRIP), 199 ComponentManager.class, 200 "componentDetails"); 201 details.addParam("componentClass", className); 202 oneRow.add(details); 203 } 204 205 Transition add = new Transition(); 206 add.setControllerObject(ComponentManager.class); 207 add.setName("add"); 208 add.setState("promptAdd"); 209 add.setLabel("Add New Component"); 210 response.add(add); 211 212 213 Transition delete = new Transition(); 214 delete.setControllerObject(ComponentManager.class); 215 delete.setName("delete"); 216 delete.setState("promptDelete"); 217 delete.setLabel("Delete Components"); 218 response.add(delete); 219 220 221 return response; 222 } catch (DBException ex) { 223 throw new ControllerException("Database Exception listing installed components", ex); 224 } 225 } 226 227 228 237 protected ControllerResponse runPromptAddState(ControllerRequest request, 238 ControllerResponse response) 239 throws ControllerException, NonHandleableException { 240 241 response.setTitle("Add Expresso Component"); 242 response.add(new Output("instructions", 243 "Enter the classname of the component you wish to install and press 'Install Component'")); 244 245 Input className = new Input("componentClass", "Component Class Name"); 246 String defaultVal = request.getParameter("componentClass"); 247 if (defaultVal != null && defaultVal.length() > 0) { 248 className.setDefaultValue(defaultVal); 249 } 250 251 className.setMaxLength(256); 252 className.setDisplayLength(50); 253 response.add(className); 254 255 Transition add = new Transition(); 256 add.setControllerObject(ComponentManager.class); 257 add.setName("processAdd"); 258 add.setState("processAdd"); 259 add.setLabel("Install Component"); 260 response.add(add); 261 262 263 addBackLink(response); 264 return response; 265 } 266 267 276 protected ControllerResponse runProcessAddState(ControllerRequest request, 277 ControllerResponse response) 278 throws ControllerException, NonHandleableException { 279 280 response.setTitle("Component Installation Results"); 281 String className = request.getParameter("componentClass"); 282 ErrorCollection ec = checkClassName(request, className); 283 284 if (isSchemaInstalled(request, response, className)) { 285 ec.addError("Class: " + className + " is already installed"); 286 } 287 288 if (ec.getErrorCount() > 0) { 289 transition("promptAdd", request, response); 290 response.saveErrors(ec); 291 292 return response; 293 } 294 295 SchemaCreator sc = new SchemaCreator(); 296 sc.setDataContext(request.getDataContext()); 297 SchemaFactory sf = SchemaFactory.getInstance(); 298 sf.setDataContext(request.getDataContext()); 299 sf.setRequestingUser(request.getUid()); 300 Block installBlock = new Block("installBlock"); 301 response.add(installBlock); 302 303 ControllerInstallLog installLog = new ControllerInstallLog(response); 304 installLog.setParentBlock(installBlock); 305 306 try { 307 SchemaCreator.ensureSchemaExists(sf.getSchema(className), installLog); 308 } catch (Throwable ex) { 309 ec.addError("Error processing schema addition"); 310 log.error("Error creating schema: ", ex); 312 installLog.error("Error creating schema: ", ex); 313 } 314 315 if (ec.getErrorCount() > 0) { 316 response.saveErrors(ec); 317 } 318 319 if (ec.getErrorCount() == 0) { 320 response.add(new Output("results", "Installation of schema: " + 321 className + " successfull")); 322 } else { 323 response.add(new Output("results", "Installation of schema: " + 324 className + " failed")); 325 } 326 addBackLink(response); 327 return response; 328 } 329 330 341 protected ControllerResponse runComponentDetailsState(ControllerRequest request, 342 ControllerResponse response) 343 throws ControllerException, NonHandleableException { 344 345 response.setTitle("Component Details"); 346 String className = request.getParameter("componentClass"); 347 ErrorCollection ec = checkClassName(request, className); 348 349 if (ec.getErrorCount() > 0) { 350 transition("list", request, response); 351 } 352 353 try { 354 SchemaList sl = new SchemaList(request); 355 sl.setField(SchemaList.FLD_SCHEMA_CLASS, className); 356 sl.retrieve(); 357 358 Block basicInformation = new Block("basicInfo"); 359 SchemaFactory sf = SchemaFactory.getInstance(); 360 Schema oneSchema = sf.getSchema(className); 361 362 basicInformation.add(new Output("schemaClass", oneSchema.getClass().getName())); 363 basicInformation.add(new Output("description", sl.getField(SchemaList.FLD_DESCRIP))); 364 basicInformation.add(new Output("schemaVersion", oneSchema.getVersion())); 365 basicInformation.add(new Output("componentCode", sl.getField(SchemaList.FLD_COMPONENT_CODE))); 366 response.add(basicInformation); 367 368 Block dbobjects = new Block("dbobjects"); 369 for (Enumeration e = oneSchema.getMembers(); e.hasMoreElements();) { 370 DBObject oneObj = (DBObject) e.nextElement(); 371 dbobjects.add(new Output("onedbobj", oneObj.getClass().getName())); 372 } 373 response.add(dbobjects); 374 375 Block controllers = new Block("controllers"); 376 for (Iterator e = oneSchema.getControllerList().iterator(); e.hasNext();) { 377 Controller oneObj = (Controller) e.next(); 378 controllers.add(new Output("oneController", oneObj.getClass().getName())); 379 } 380 response.add(controllers); 381 382 383 Block jobs = new Block("jobs"); 384 for (Enumeration e = oneSchema.getJobs(); e.hasMoreElements();) { 385 Job oneObj = (Job) e.nextElement(); 386 jobs.add(new Output("onejob", oneObj.getClass().getName())); 387 } 388 response.add(jobs); 389 390 } catch (DBException ex) { 391 log.error("Error getting schema info: ", ex); 392 ec.addError("Error getting schema information: " + ex.getMessage()); 393 } 394 395 396 addBackLink(response); 397 return response; 398 } 399 400 409 protected ControllerResponse runPromptDeleteState(ControllerRequest request, 410 ControllerResponse response) 411 throws ControllerException, NonHandleableException { 412 response.setTitle("Confirm Delete"); 413 Block schemas = new Block("deleteSchemas"); 414 ArrayList al = parseMarkedClassNames(request, response); 415 for (Iterator i = al.iterator(); i.hasNext();) { 416 String oneClassName = (String ) i.next(); 417 Input input = new Input(); 418 input.setLabel(oneClassName); 419 input.setName("mark_" + oneClassName); 420 schemas.add(input); 421 } 422 response.add(schemas); 423 424 Transition delete = new Transition("delete", 425 "Delete Listed Components", 426 ComponentManager.class, 427 "processDelete"); 428 response.add(delete); 429 430 addBackLink(response); 431 return response; 432 } 433 434 435 443 protected void runVerifyInstallState(ControllerRequest request, 444 ControllerResponse response) 445 throws ControllerException, NonHandleableException { 446 String dbContext = StringUtil.notNull(request.getParameter("dbContext")); 447 448 try { 449 ExpressoSchema jc = (ExpressoSchema) SchemaFactory.getInstance() 450 .getSchema(com.jcorporate.expresso 451 .core.ExpressoSchema.class.getName()); 452 453 if (jc == null) { 454 throw new ControllerException("Unable to construct Expresso Schema. Check logs for details"); 455 } 456 457 response.setTitle("Verification Result"); 458 Vector allSchemas = new Vector (5); 459 allSchemas.addElement(jc); 460 461 SchemaList sl = null; 462 SchemaList oneSchema = null; 463 boolean noOtherSchemas = true; 464 465 try { 466 sl = new SchemaList(Securable.SYSTEM_ACCOUNT); 467 sl.setDataContext(dbContext); 468 sl.search(); 469 470 if (sl.getFoundCount() > 0) { 471 noOtherSchemas = false; 472 } 473 } catch (DBException de) { 474 noOtherSchemas = true; 475 } 476 if (!noOtherSchemas) { 477 for (Iterator e = sl.searchAndRetrieveList().iterator(); 478 e.hasNext();) { 479 oneSchema = (SchemaList) e.next(); 480 481 boolean schemaWarning = false; 482 483 Schema mySchema2 = SchemaFactory.getInstance() 484 .getSchema(oneSchema.getField("SchemaClass")); 485 486 if (mySchema2 == null) { 487 schemaWarning = true; 488 } else { 489 allSchemas.addElement(mySchema2); 490 } 491 492 if (schemaWarning) { 493 response.add(new Output("Warning: Unable to process schema " + oneSchema.getField( 494 "SchemaClass") + 495 " - see log for details")); 496 } 497 } 498 499 } 500 501 response.add(new Output(response.getString("Processing_initializations"))); 502 ControllerInstallLog installLog = new ControllerInstallLog(response); 503 504 DBTool.createTables(installLog, allSchemas, dbContext); 505 DBTool.compareTables(installLog, allSchemas, dbContext); 506 507 DBTool.setupSecurity(installLog, allSchemas, dbContext); 511 DBTool.populateTables(installLog, allSchemas, dbContext); 512 513 DBTool.otherSetups(installLog, allSchemas, dbContext); 514 515 response.add(new Output(response.getString("All_functions_complete"))); 516 517 ConfigManager.initializeAllDBObjects(); 519 ConfigManager.mapOtherDBs(); 520 addBackLink(response); 521 } catch (Exception de) { 522 log.error("Error performing database verification", de); 523 throw new ControllerException("There was an error verifying database configuration", de); 524 } 525 526 527 } 528 529 protected ArrayList parseMarkedClassNames(ControllerRequest request, 530 ControllerResponse response) throws ControllerException { 531 ArrayList returnList = new ArrayList (); 532 533 Hashtable allParams = request.getParameters(); 534 535 for (Iterator i = allParams.keySet().iterator(); i.hasNext();) { 536 String oneParam = (String ) i.next(); 537 if (oneParam.startsWith("mark_")) { 538 String oneClassName = oneParam.substring(5); 539 returnList.add(oneClassName); 540 } 541 } 542 543 return returnList; 544 } 545 546 555 protected ControllerResponse runProcessDeleteState(ControllerRequest request, 556 ControllerResponse response) 557 throws ControllerException, NonHandleableException { 558 559 response.setTitle("Component Deletion Results"); 560 561 ArrayList classNames = this.parseMarkedClassNames(request, response); 562 563 ErrorCollection allErrors = new ErrorCollection(); 564 for (Iterator i = classNames.iterator(); i.hasNext();) { 565 String className = (String ) i.next(); 566 ErrorCollection ec = checkClassName(request, className); 567 568 if (allErrors.getErrorCount() > 0) { 569 for (int j = 0; j < ec.getErrorStrings().length; j++) { 570 String s = ec.getErrorStrings()[j]; 571 allErrors.addError(s); 572 } 573 } 574 } 575 576 if (allErrors.getErrorCount() > 0) { 577 response.saveErrors(allErrors); 578 transition("promptDelete", request, response); 579 return response; 580 } 581 582 583 SchemaFactory sf = SchemaFactory.getInstance(); 584 585 for (Iterator i = classNames.iterator(); i.hasNext();) { 586 String className = (String ) i.next(); 587 Schema s = sf.getSchema(className); 588 589 SchemaDeleter.deleteSchema(request.getDataContext(), s); 590 } 591 592 Block deletedSchemas = new Block("deletedSchemas"); 593 for (Iterator i = classNames.iterator(); i.hasNext();) { 594 String className = (String ) i.next(); 595 deletedSchemas.add(new Output("Deletion of schema: " + className + 596 " successfull")); 597 } 598 response.add(deletedSchemas); 599 addBackLink(response); 600 return response; 601 } 602 603 610 private void addBackLink(ControllerResponse response) 611 throws ControllerException { 612 Transition backLink = new Transition(); 613 backLink.setState("list"); 614 backLink.setName("backLink"); 615 backLink.setLabel("List Components"); 616 backLink.setControllerObject(ComponentManager.class); 617 response.add(backLink); 618 } 619 620 631 private ErrorCollection checkClassName(ControllerRequest request, 632 String className) 633 throws ControllerException { 634 635 636 ErrorCollection ec = request.getErrorCollection(); 637 if (ec == null) { 638 ec = new ErrorCollection(); 639 } 640 641 try { 642 if (className == null || className.length() == 0) { 643 log.error("Error: component classname was blank."); 644 ec.addError("Error: component classname was blank."); 645 } else if (className.equalsIgnoreCase(com.jcorporate 646 .expresso.core.ExpressoSchema.class.getName())) { 647 ec.addError("You cannot add or delete ExpressoSchema"); 648 } else { 649 ClassLoader cl = this.getClass().getClassLoader(); 650 if (cl == null) { 651 Class.forName(className).newInstance(); 652 } else { 653 cl.loadClass(className).newInstance(); 654 } 655 656 657 } 658 } catch (java.lang.NoClassDefFoundError ex) { 659 log.error("Error attempting to load class. " + className, ex); 660 ec.addError("Can't find class: " + className + " doesn't appear to " + 661 "be in the system's classpath, or there could be a typo in what you've entered"); 662 663 } catch (ClassCastException ex) { 664 log.error("Error attempting to load class. " + className, ex); 665 ec.addError("Class " + className + " is a valid class, but does not" + 666 " appear to derive from com.jcorporate.expresso.core.dbobj.Schema"); 667 } catch (Exception e) { 668 log.error("Error attempting to load class: " + className, e); 669 ec.addError("There was an error loading the class specified: " + className); 670 } 671 672 return ec; 673 674 } 675 676 686 boolean isSchemaInstalled(ControllerRequest request, 687 ControllerResponse response, 688 String className) { 689 try { 690 SchemaList sl = new SchemaList(request); 691 sl.setField(SchemaList.FLD_SCHEMA_CLASS, className); 692 try { 693 sl.retrieve(); 694 return true; 695 } catch (DBRecordNotFoundException ex) { 696 log.error("Error attempting to load class. " + className, ex); 697 return false; 698 } 699 } catch (DBException ex) { 700 log.error("Error attempting to load class. " + className, ex); 701 return false; 702 } 703 704 } 705 706 } | Popular Tags |