1 64 65 70 package com.jcorporate.expresso.core.utility; 71 72 import com.jcorporate.expresso.core.controller.Controller; 73 import com.jcorporate.expresso.core.controller.ControllerElement; 74 import com.jcorporate.expresso.core.controller.ControllerException; 75 import com.jcorporate.expresso.core.controller.ControllerRequest; 76 import com.jcorporate.expresso.core.controller.ControllerResponse; 77 import com.jcorporate.expresso.core.controller.DBController; 78 import com.jcorporate.expresso.core.controller.Input; 79 import com.jcorporate.expresso.core.controller.NonHandleableException; 80 import com.jcorporate.expresso.core.controller.Output; 81 import com.jcorporate.expresso.core.controller.State; 82 import com.jcorporate.expresso.core.controller.Transition; 83 import com.jcorporate.expresso.core.controller.session.SimplePersistentSession; 84 import com.jcorporate.expresso.core.dbobj.ValidValue; 85 import com.jcorporate.expresso.core.misc.ConfigManager; 86 import com.jcorporate.expresso.core.misc.StringUtil; 87 import com.jcorporate.expresso.core.registry.MutableRequestRegistry; 88 import com.jcorporate.expresso.core.security.SuperUser; 89 import com.jcorporate.expresso.kernel.LogManager; 90 import com.jcorporate.expresso.services.html.Button; 91 import com.jcorporate.expresso.services.html.Cell; 92 import com.jcorporate.expresso.services.html.CheckBox; 93 import com.jcorporate.expresso.services.html.DropDown; 94 import com.jcorporate.expresso.services.html.Form; 95 import com.jcorporate.expresso.services.html.HiddenField; 96 import com.jcorporate.expresso.services.html.HtmlException; 97 import com.jcorporate.expresso.services.html.Row; 98 import com.jcorporate.expresso.services.html.Table; 99 import com.jcorporate.expresso.services.html.Text; 100 import com.jcorporate.expresso.services.html.TextField; 101 102 import java.io.BufferedReader ; 103 import java.io.File ; 104 import java.io.IOException ; 105 import java.io.InputStreamReader ; 106 import java.util.Enumeration ; 107 import java.util.Hashtable ; 108 import java.util.StringTokenizer ; 109 import java.util.Vector ; 110 111 112 117 public class ControllerRun { 118 protected static final String thisClass = ControllerRun.class.getName(); 119 private static SimplePersistentSession mySession = new SimplePersistentSession(); 120 121 125 private void add(Vector v, Table t) 126 throws HtmlException { 127 for (Enumeration e = v.elements(); e.hasMoreElements();) { 128 ControllerElement o = (ControllerElement) e.nextElement(); 129 130 if (o instanceof Output) { 131 addOutput((Output) o, t); 132 } else if (o instanceof Input) { 133 addInput((Input) o, t); 134 } 135 } 136 137 } 138 139 140 145 private void addActions(Vector h, Row r, Form f) 146 throws HtmlException { 147 Transition a = null; 148 Cell oneCell = null; 149 Button oneButton = null; 150 StringBuffer paramString = null; 151 String paramName = null; 152 Hashtable params = null; 153 HiddenField hidden = null; 154 155 for (Enumeration e = h.elements(); e.hasMoreElements();) { 156 a = (Transition) e.nextElement(); 157 params = a.getParams(); 158 paramString = new StringBuffer (); 159 160 for (Enumeration p = params.keys(); p.hasMoreElements();) { 161 paramName = (String ) p.nextElement(); 162 paramString.append("&"); 163 paramString.append(paramName); 164 paramString.append("="); 165 paramString.append((String ) params.get(paramName)); 166 } 167 168 hidden = new HiddenField(a.getName() + "_params", 169 a.getControllerObject() + 170 paramString.toString()); 171 f.add(hidden); 172 } 173 for (Enumeration e = h.elements(); e.hasMoreElements();) { 174 a = (Transition) e.nextElement(); 175 oneCell = new Cell(); 176 oneButton = new Button("button_" + a.getName(), a.getLabel()); 177 oneCell.add(oneButton); 178 r.add(oneCell); 179 } 180 } 181 182 183 187 private void addInput(Input i, Table t) 188 throws HtmlException { 189 Row oneRow = new Row(); 190 t.add(oneRow); 191 192 Cell oneCell = null; 193 String l = i.getLabel(); 194 195 if (l != null) { 196 oneCell = new Cell(); 197 oneCell.add(new Text(l, "bold")); 198 oneRow.add(oneCell); 199 } 200 201 oneCell = new Cell(); 202 203 String defaultValue = i.getDefaultValue(); 204 205 if (defaultValue == null) { 206 defaultValue = (""); 207 } 208 209 210 Vector validValues = i.getValidValues(); 211 ValidValue oneValue = null; 212 213 if (validValues != null) { 214 DropDown d = new DropDown(i.getName()); 215 216 for (Enumeration vv = validValues.elements(); 217 vv.hasMoreElements();) { 218 oneValue = (ValidValue) vv.nextElement(); 219 d.addOption(oneValue.getValue(), oneValue.getDescription()); 220 } 221 222 d.setCurrentValue(defaultValue); 223 oneCell.add(d); 224 } else if (i.getType().equalsIgnoreCase("boolean")) { 225 226 227 if (defaultValue.equalsIgnoreCase("Y")) { 228 oneCell.add(new CheckBox(i.getName(), "Y", true)); 229 } else { 230 oneCell.add(new CheckBox(i.getName(), "Y", false)); 231 } 232 } else { 233 234 235 oneCell.add(new TextField(i.getName(), defaultValue, 236 i.getMaxLength(), i.getDisplayLength())); 237 } 238 239 oneRow.add(oneCell); 240 241 if (i.getNested() != null) { 242 Vector v = i.getNested(); 243 244 for (Enumeration ve = v.elements(); ve.hasMoreElements();) { 245 addInput((Input) ve.nextElement(), t); 246 } 247 } 248 } 249 250 251 257 private void addOutput(Output o, Table t) 258 throws HtmlException { 259 Row oneRow = new Row(); 260 t.add(oneRow); 261 262 Cell oneCell = null; 263 String l = o.getLabel(); 264 265 if (l == null) { 266 l = (""); 267 } 268 if (!l.equals("")) { 269 oneCell = new Cell(); 270 271 if (o.getStyle() != null) { 272 oneCell.add(new Text(l, o.getStyle())); 273 } else { 274 oneCell.add(new Text(l)); 275 } 276 277 oneRow.add(oneCell); 278 } 279 280 String c = o.getContent(); 281 282 if (c != null) { 283 oneCell = new Cell(); 284 oneCell.add(new Text(c)); 285 oneRow.add(oneCell); 286 } 287 if (o.getNested() != null) { 288 Vector v = o.getNested(); 289 290 if (v.size() > 0) { 291 Table t2 = new Table("Nested Output"); 292 t2.setBorder(1); 293 294 Row r = new Row(); 295 t.add(r); 296 297 Cell nestedCell = new Cell(); 298 nestedCell.add(t2); 299 r.add(nestedCell); 300 301 for (Enumeration ve = v.elements(); ve.hasMoreElements();) { 302 addOutput((Output) ve.nextElement(), t2); 303 } 304 } 305 306 } 307 308 } 309 310 311 314 private static void displayTransition(Transition a) { 315 System.out.println(" " + a.getName()); 316 } 317 318 321 private static void displayInput(Input i) { 322 System.out.println(" " + i.getName()); 323 324 ValidValue oneValue = null; 325 Vector v = i.getValidValues(); 326 327 if (v != null) { 328 for (Enumeration e = v.elements(); e.hasMoreElements();) { 329 oneValue = (ValidValue) e.nextElement(); 330 System.out.println(" " + oneValue.getValue() + "/" + 331 oneValue.getDescription()); 332 } 333 } 334 } 335 336 340 private static void displayOutput(Output o, int nestLevel) { 341 Output nested = null; 342 343 for (int i = 0; i <= nestLevel; i++) { 344 System.out.print(" "); 345 } 346 347 System.out.println(o.getName() + ":" + o.getContent()); 348 349 for (Enumeration e = o.getNested().elements(); e.hasMoreElements();) { 350 nested = (Output) e.nextElement(); 351 displayOutput(nested, nestLevel + 1); 352 } 353 } 354 355 360 public static void doController(String conName, Hashtable commandArgs) 361 throws IOException , ControllerException, 362 HtmlException, NonHandleableException { 363 Controller con = ConfigManager.getControllerFactory().getController(conName); 364 ControllerRequest params = new ControllerRequest(); 365 params.setUid(1); 366 params.setSession(mySession); 367 368 Hashtable states = con.getStates(); 369 String thisStateName = null; 370 State oneState = null; 371 String command = (""); 372 String newState = null; 373 374 375 String oneKey = null; 376 String oneValue = null; 377 378 for (Enumeration eg = commandArgs.keys(); eg.hasMoreElements();) { 379 oneKey = (String ) eg.nextElement(); 380 oneValue = (String ) commandArgs.get(oneKey); 381 382 if (oneValue != null) { 383 if ((!oneKey.equals("webAppDir")) && 384 (!oneKey.equals("configDir")) && 385 (!oneKey.equals(Controller.STATE_PARAM_KEY))) { 386 params.setParameter(oneKey, oneValue); 387 } 388 } 389 390 } 391 392 if (con instanceof DBController) { 393 String dbName = StringUtil.notNull((String ) commandArgs.get("db")); 394 params.setDataContext(dbName); 395 } 396 397 boolean interactiveMode = true; 398 String initialState = (String ) commandArgs.get(Controller.STATE_PARAM_KEY); 399 400 401 if (initialState == null) { 402 initialState = con.getInitialState(); 403 } else { 404 interactiveMode = false; 405 } 406 if (initialState != null) { 407 newState(con, params, initialState); 408 } 409 410 Hashtable commands = new Hashtable (3); 411 412 while ((!command.equals("0")) && interactiveMode) { 413 int i = 0; 414 System.out.println("---------------------------------"); 415 System.out.println("0. Exit Controller"); 416 417 for (Enumeration hs = states.keys(); hs.hasMoreElements();) { 418 i++; 419 thisStateName = (String ) hs.nextElement(); 420 commands.put(("" + i), thisStateName); 421 oneState = (State) states.get(thisStateName); 422 System.out.println(i + ". State:" + thisStateName + 423 ", Description:" + 424 oneState.getDescription()); 425 426 String param = null; 427 Vector p = oneState.getParameters(); 428 429 if (p.size() > 0) { 430 System.out.print("\tParameters required:"); 431 } 432 for (Enumeration ea = p.elements(); ea.hasMoreElements();) { 433 param = (String ) ea.nextElement(); 434 System.out.print(" " + param); 435 } 436 if (p.size() > 0) { 437 System.out.println(""); 438 } 439 } 440 441 442 System.out.print("Enter state, or 0 to quit:"); 443 444 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 445 command = br.readLine(); 446 447 if (command.equals("0")) { 448 System.exit(0); 449 } 450 451 newState = (String ) commands.get(command); 452 453 if (newState == null) { 454 System.out.println("\nInvalid entry\n"); 455 } else { 456 newState(con, params, newState); 457 } 458 } 459 460 461 System.exit(0); 462 } 463 464 465 471 public static void main(String [] args) { 472 System.out.println("ControllerRun"); 473 474 Hashtable commandArgs = new Hashtable (10); 475 String paramCode = null; 476 String paramValue = null; 477 478 for (int i = 0; i < args.length; i++) { 479 StringTokenizer stk = new StringTokenizer (args[i], "="); 480 481 if (!stk.hasMoreTokens()) { 482 usage(); 483 } 484 485 paramCode = stk.nextToken(); 486 487 if (!stk.hasMoreTokens()) { 488 usage(); 489 } 490 491 paramValue = stk.nextToken(); 492 commandArgs.put(paramCode, paramValue); 493 } 494 495 try { 496 497 String logDir = (String ) commandArgs.get("logDir"); 498 String logConfig = (String ) commandArgs.get("logConfig"); 499 if (logConfig == null) { 500 logConfig = (String ) commandArgs.get("configDir") + "/expressoLogging.xml"; 501 } 502 503 LogManager lm = new LogManager(logConfig, logDir); 504 505 ConfigManager.setWebAppDir(getAppDir(commandArgs)); 507 508 ConfigManager.load(getConfigDir(commandArgs)); 510 511 ConfigManager.dbInitialize(); 513 514 String dbName = (String ) commandArgs.get("db"); 516 new MutableRequestRegistry(dbName, 517 SuperUser.SUPER_USER); 518 519 String conName = (String ) commandArgs.get("controller"); 520 System.out.println("Running Controller " + conName); 521 doController(conName, commandArgs); 522 } catch (Exception de) { 523 System.out.println("ControllerRun Error:" + de.getMessage()); 524 de.printStackTrace(); 525 System.exit(1); 526 } 527 } 528 529 530 private static String getConfigDir(Hashtable commandArgs) 531 throws IOException { 532 String configDir = StringUtil.notNull((String ) commandArgs.get("configDir")); 533 534 if (configDir.equals("")) { 535 boolean noDir = true; 536 System.out.println("No configDir=xxx command line argument was found."); 537 538 while (noDir) { 539 System.out.print("Please enter Expresso configuration directory:"); 540 541 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 542 configDir = br.readLine(); 543 544 if (StringUtil.notNull(configDir).equals("")) { 545 System.out.println("You must enter a directory name, not blank"); 546 continue; 547 } 548 549 File theDir = new File (configDir); 550 551 if (!theDir.isDirectory()) { 552 System.out.println(configDir + 553 " is not a valid directory. "); 554 continue; 555 } else { 556 noDir = false; 557 } 558 } 559 } 560 561 return configDir; 562 } 563 564 565 private static String getAppDir(Hashtable commandArgs) 566 throws IOException { 567 String webAppDir = StringUtil.notNull((String ) commandArgs.get("webAppDir")); 568 569 if (webAppDir.equals("")) { 570 boolean noDir = true; 571 System.out.println("No webAppDir=xxx command line argument was found."); 572 573 while (noDir) { 574 System.out.print("Please enter Expresso web application root directory:"); 575 576 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 577 webAppDir = br.readLine(); 578 579 if (StringUtil.notNull(webAppDir).equals("")) { 580 System.out.println("You must enter a directory name, not blank"); 581 continue; 582 } 583 584 File theDir = new File (webAppDir); 585 586 if (!theDir.isDirectory()) { 587 System.out.println(webAppDir + 588 " is not a valid directory. "); 589 continue; 590 } else { 591 noDir = false; 592 } 593 } 594 } 595 596 return webAppDir; 597 } 598 599 600 604 private static void newState(Controller con, ControllerRequest cparams, 605 String newState) 606 throws ControllerException, IOException , 607 HtmlException, NonHandleableException { 608 String myName = ("com.jcorporate.expresso.core.utility." + 609 "ControllerRun.newState(Controller, String)"); 610 State thisState = con.getState(newState); 611 612 613 String param = null; 614 String newValue = null; 615 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 616 617 for (Enumeration ea = thisState.getParameters().elements(); 618 ea.hasMoreElements();) { 619 param = (String ) ea.nextElement(); 620 621 if (StringUtil.notNull(cparams.getParameter(param)).equals("")) { 622 System.out.print("Value for " + param + ":"); 623 newValue = br.readLine(); 624 cparams.setParameter(param, newValue); 625 } 626 } 627 628 System.out.println("Transitioning to state " + newState + "..."); 629 630 ControllerResponse myResponse = con.newState(newState, cparams); 631 632 if (myResponse.isCustomResponse()) { 633 throw new ControllerException("Controller returned a 'custom' " + 634 "response to the client. This controller/state cannot be " + 635 "run in this environment."); 636 } 637 638 639 Vector inputs = myResponse.getInputs(); 640 641 if (inputs == null) { 642 inputs = new Vector (1); 643 } 644 645 Vector outputs = myResponse.getOutputs(); 646 647 if (outputs == null) { 648 outputs = new Vector (1); 649 } 650 651 Vector actions = myResponse.getTransitions(); 652 653 if (actions == null) { 654 actions = new Vector (1); 655 } 656 if ((inputs.size() == 0) && (outputs.size() == 0) && 657 (actions.size() == 0)) { 658 throw new ControllerException(myName + ":No input, output " + 659 "or actions defined for this Controller in this state"); 660 } 661 if (outputs.size() != 0) { 662 System.out.println(outputs.size() + " Outputs:"); 663 664 for (Enumeration oe = outputs.elements(); oe.hasMoreElements();) { 665 displayOutput((Output) oe.nextElement(), 0); 666 } 667 } else { 668 System.out.println("No outputs"); 669 } 670 if (inputs.size() != 0) { 671 System.out.println(inputs.size() + " Inputs:"); 672 673 for (Enumeration ie = inputs.elements(); ie.hasMoreElements();) { 674 displayInput((Input) ie.nextElement()); 675 } 676 } else { 677 System.out.println("No Inputs"); 678 } 679 if (actions.size() != 0) { 680 System.out.println(actions.size() + " Actions:"); 681 682 for (Enumeration ae = actions.elements(); ae.hasMoreElements();) { 683 displayTransition((Transition) ae.nextElement()); 684 } 685 } else { 686 System.out.println("No actions"); 687 } 688 689 } 693 694 695 698 private static void usage() { 699 System.out.println("Usage:"); 700 System.exit(1); 701 } 702 703 } 704 | Popular Tags |