1 23 package com.sun.enterprise.tools.verifier; 24 25 import java.io.File ; 26 import java.util.logging.Handler ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import java.util.logging.ConsoleHandler ; 30 31 import com.sun.enterprise.logging.LogDomains; 32 import com.sun.enterprise.logging.VerifierFormatter; 33 import com.sun.enterprise.tools.verifier.util.VerifierConstants; 34 import com.sun.enterprise.util.LocalStringManagerImpl; 35 36 43 public class Initializer { 44 45 private LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager(); 46 private Logger logger = LogDomains.getLogger( 47 LogDomains.AVK_VERIFIER_LOGGER); 48 49 private FrameworkContext frameworkContext = null; 50 private String _FOStr = "Failures only"; private String _WFOStr = "Warning and Failures only"; private String _AOStr = "All"; 54 58 public Initializer(String [] args) { 59 this.frameworkContext = new FrameworkContext(); 60 parseArgs(args); 61 } 62 63 67 public FrameworkContext getFrameworkContext() { 68 return frameworkContext; 69 } 70 71 76 private void parseArgs(String [] argv) { 77 int c; 78 String arg; 79 boolean isVerboseSet = false; 80 LongOption[] longopts = new LongOption[20]; 81 StringBuffer sb = new StringBuffer (); 83 longopts[0] = new LongOption("help", false, null, 'h'); longopts[1] = new LongOption("timestamp", false, null, 't'); longopts[2] = new LongOption("verbose", false, null, 'v'); longopts[3] = new LongOption("version", false, null, 'V'); longopts[4] = new LongOption("destdir", true, sb, 'd'); longopts[5] = new LongOption("reportlevel", true, sb, 'r'); longopts[6] = new LongOption("gui", false, null, 'u'); longopts[7] = new LongOption("app", false, null, 'a'); longopts[8] = new LongOption("appclient", false, null, 'A'); longopts[9] = new LongOption("connector", false, null, 'c'); longopts[10] = new LongOption("ejb", false, null, 'e'); longopts[11] = new LongOption("web", false, null, 'w'); longopts[12] = new LongOption("webservices", false, null, 's'); longopts[13] = new LongOption("webservicesclient", false, null, 'l'); longopts[14] = new LongOption("persistence", false, null, 'P'); longopts[15] = new LongOption("configdir", true, null, 'C'); longopts[16] = new LongOption("portability", false, null, 'p'); longopts[17] = new LongOption("domain", true, null, 'D'); longopts[18] = new LongOption("extDir", true, null, 'E'); longopts[19] = new LongOption("mode", true, null, 'm'); 104 String [] levels = new String [6]; 106 levels[0] = "a"; levels[1] = "w"; levels[2] = "f"; levels[3] = "all"; levels[4] = "warnings"; levels[5] = "failures"; 113 114 CLIPParser parser = null; 115 parser = 116 new CLIPParser("verifier", argv, "-:vtVhud:r:aAcewslC:pPm:D:E:", longopts); 118 119 while ((c = parser.getopt()) != -1) 120 switch (c) { 121 case 0: 122 arg = parser.getOptarg(); 123 processValidLongOpt( 124 (char) (new Integer (sb.toString())).intValue(), 125 arg, levels); 126 break; 127 case 'v': 128 logger.setLevel(Level.FINEST); 129 isVerboseSet = true; 130 Handler [] handler = Logger.getLogger("").getHandlers(); 131 for (int i = 0; i < handler.length; i++) { 132 handler[i].setLevel(Level.FINEST); 133 } 134 logger.log(Level.FINE, 135 getClass().getName() + ".debug.verboseFlag"); break; 137 138 case 't': 139 frameworkContext.setUseTimeStamp(true); 140 break; 141 142 case 'h': 143 logger.log(Level.FINE, 144 getClass().getName() + ".debug.helpMessage"); usage(); 146 break; 147 148 case 'V': 149 logger.log(Level.INFO, getClass().getName() + ".Version"); 150 System.exit(0); 151 break; 152 153 case 'u': 154 logger.log(Level.FINE, 155 getClass().getName() + ".debug.startGUI"); frameworkContext.setUsingGui(true); 157 break; 158 159 case 'r': 160 arg = parser.getOptarg(); 161 if (arg == null) { 162 logger.log(Level.SEVERE, 163 getClass().getName() + ".reqargs", new Object []{"verifier", "-r"}); usage(); 166 167 } else { 168 boolean validLevel = false; 169 for (int i = 0; i < levels.length; i++) { 170 if (arg.equals(levels[i])) { 171 validLevel = true; 172 break; 173 } 174 } 175 if (!validLevel) { 176 logger.log(Level.SEVERE, getClass().getName() + 177 ".invalidreplevel", new Object []{"verifier", arg}); usage(); 180 } else { 181 setReportingLevel(arg.charAt(0)); 182 } 183 } 184 break; 185 186 case 'd': 187 arg = parser.getOptarg(); 188 if (arg == null) { 189 logger.log(Level.SEVERE, getClass().getName() + 190 ".reqargs", new Object []{"verifier", "-d"}); usage(); 193 } else if (!verifyAndSetResultDir(arg)) { 194 usage(); 195 } 196 break; 197 198 case ':': 199 logger.log(Level.SEVERE, getClass().getName() + ".reqsarg", 201 new Object []{"verifier", new Character ( 203 (char) parser.getOptopt()).toString()}); 204 usage(); 205 break; 206 case 'a': 207 logger.log(Level.FINE, getClass().getName() + ".debug.app"); 208 frameworkContext.setApp(true); 209 frameworkContext.setPartition(true); 210 break; 211 212 case 'A': 213 logger.log(Level.FINE, 214 getClass().getName() + ".debug.appclient"); frameworkContext.setAppClient(true); 216 frameworkContext.setPartition(true); 217 break; 218 219 case 'c': 220 logger.log(Level.FINE, 221 getClass().getName() + ".debug.connector"); frameworkContext.setConnector(true); 223 frameworkContext.setPartition(true); 224 break; 225 226 case 'e': 227 logger.log(Level.FINE, getClass().getName() + ".debug.ejb"); 228 frameworkContext.setEjb(true); 229 frameworkContext.setPartition(true); 230 break; 231 232 case 'w': 233 logger.log(Level.FINE, getClass().getName() + ".debug.web"); 234 frameworkContext.setWeb(true); 235 frameworkContext.setPartition(true); 236 break; 237 238 case 's': 239 logger.log(Level.FINE, 240 getClass().getName() + ".debug.webservices"); frameworkContext.setWebServices(true); 242 frameworkContext.setPartition(true); 243 break; 244 245 case 'l': 246 logger.log(Level.FINE, 247 getClass().getName() + ".debug.webservicesclient"); frameworkContext.setWebServicesClient(true); 249 frameworkContext.setPartition(true); 250 break; 251 252 case 'P': 253 frameworkContext.setPersistenceUnits(true); 254 frameworkContext.setPartition(true); 255 break; 256 257 case '?': 258 char x = (char) parser.getOptopt(); 259 if (x == '?') { 260 usage(); 261 } 262 logger.log(Level.SEVERE, 263 getClass().getName() + ".invalidarg", new Object []{"verifier", new Character (x).toString()}); 266 usage(); 267 break; 268 269 case 'C': 270 arg = parser.getOptarg(); 271 if (arg == null || !(new File (arg).exists())) { 272 logger.log(Level.SEVERE, getClass().getName() + 273 ".reqargs", new Object []{"verifier", "-C"}); usage(); 276 } 277 frameworkContext.setConfigDirStr(arg); 278 break; 279 280 case 'p': 281 frameworkContext.setPortabilityMode(true); 282 break; 283 284 case 'm': 285 arg = parser.getOptarg(); 286 if (arg != null && 287 (arg.equals(SpecVersionMapper.JavaEEVersion_1_2) || 288 arg.equals(SpecVersionMapper.JavaEEVersion_1_3) || 289 arg.equals(SpecVersionMapper.JavaEEVersion_1_4) || 290 arg.equals(SpecVersionMapper.JavaEEVersion_5))) { 291 frameworkContext.setJavaEEVersion(arg); 292 } else { 293 logger.log(Level.SEVERE, 294 getClass().getName() + ".invalidjavaeeversion", new Object []{"verifier", arg}); usage(); 297 } 298 logger.log(Level.INFO, getClass().getName() + ".specversion", 299 new Object []{frameworkContext.getJavaEEVersion()}); 300 break; 301 302 case 'D': 303 arg = parser.getOptarg(); 304 if (arg == null) { 305 logger.log(Level.SEVERE, getClass().getName() + 306 ".reqargs", new Object []{"verifier", "-D"}); usage(); 309 } 310 if(!new File (arg).exists()) { 311 logger.log(Level.SEVERE, getClass().getName() + 312 ".log.noDirExists", new Object []{arg}); 314 usage(); 315 } 316 frameworkContext.setDomainDir(arg); 317 break; 318 319 case 'E': 320 arg = parser.getOptarg(); 321 if (arg == null) { 322 logger.log(Level.SEVERE, getClass().getName() + 323 ".reqargs", new Object []{"verifier", "-E"}); usage(); 326 } 327 frameworkContext.setExtDir(arg); 328 break; 329 330 default: 331 logger.log(Level.SEVERE, 332 getClass().getName() + ".invalidarg", new Object []{"verifier", new Character ((char) c).toString()}); 335 usage(); 336 break; 337 } 338 339 int i = parser.getOptind(); 340 if (i < argv.length) { 341 File jarFile = new File (argv[i]); 342 if (!jarFile.exists()) { 343 logger.log(Level.SEVERE, 344 getClass().getName() + ".invalidArchive", new Object []{argv[i]}); 346 usage(); 347 } 348 frameworkContext.setJarFileName(jarFile.getPath()); 349 logger.log(Level.FINE, getClass().getName() + ".debug.jarFileName", 350 new Object []{frameworkContext.getJarFileName()}); 351 352 i++; 353 for (; i < argv.length; i++) 354 logger.log(Level.INFO, getClass().getName() + ".extraargs", 355 new Object []{"verifier", argv[i]}); 357 } else { 358 if (!frameworkContext.isUsingGui()) { 359 logger.log(Level.SEVERE, getClass().getName() + ".jarmissing", 360 new Object []{"verifier"}); usage(); 362 } 363 } 364 if(!isVerboseSet) 365 setFormatter(); 366 logger.log(Level.FINE, getClass().getName() + ".debug.endParseArgs"); 367 368 } 369 370 376 private void processValidLongOpt(int c, String arg, String [] levels) { 377 378 switch (c) { 379 case 'v': 380 logger.setLevel(Level.FINEST); 381 Handler [] handler = Logger.getLogger("").getHandlers(); 382 for (int i = 0; i < handler.length; i++) { 383 handler[i].setLevel(Level.FINEST); 384 } 385 logger.log(Level.FINE, 386 getClass().getName() + ".debug.verboseFlag"); break; 388 389 case 't': 390 frameworkContext.setUseTimeStamp(true); 391 break; 392 393 case 'h': 394 logger.log(Level.FINE, 395 getClass().getName() + ".debug.helpMessage"); usage(); 397 break; 398 399 case 'V': 400 logger.log(Level.INFO, getClass().getName() + ".Version"); 401 System.exit(0); 402 break; 403 404 case 'u': 405 logger.log(Level.FINE, 406 getClass().getName() + ".debug.startGUI"); frameworkContext.setUsingGui(true); 408 break; 409 410 case 'r': 411 if (arg == null) { 412 logger.log(Level.SEVERE, getClass().getName() + ".reqargs", 413 new Object []{"verifier", "-r"}); usage(); 415 } else { 416 boolean validLevel = false; 417 for (int i = 0; i < levels.length; i++) { 418 if (arg.equals(levels[i])) { 419 validLevel = true; 420 break; 421 } 422 } 423 if (!validLevel) { 424 logger.log(Level.SEVERE, getClass().getName() + 425 ".invalidreplevel", new Object []{"verifier", arg}); usage(); 428 } else { 429 setReportingLevel(arg.charAt(0)); 430 } 431 } 432 break; 433 434 case 'd': 435 if (arg == null) { 436 logger.log(Level.SEVERE, getClass().getName() + ".reqargs", 437 new Object []{"verifier", "-d"}); usage(); 439 } else if (!verifyAndSetResultDir(arg)) { 440 usage(); 441 } 442 break; 443 444 default: 445 logger.log(Level.SEVERE, getClass().getName() + ".invalidarg", 447 new Object []{"verifier", new Character ((char) c).toString()}); 449 usage(); 450 break; 451 } 452 } 453 454 459 private void setReportingLevel(char c) { 460 461 boolean setWarningLevelOnce = false; 462 switch (c) { 463 case 'w': 464 if (!setWarningLevelOnce) { 465 logger.log(Level.FINE, 466 getClass().getName() + 467 ".debug.displayWarningFailures"); frameworkContext.setReportLevel(VerifierConstants.WARN); 469 setWarningLevelOnce = true; 470 } else { 471 logger.log(Level.FINE, 472 getClass().getName() + ".debug.reportLevel", new Object []{ 474 getReportLevelString( 475 frameworkContext.getReportLevel())}); 476 } 477 break; 478 479 case 'f': 480 if (!setWarningLevelOnce) { 481 logger.log(Level.FINE, 482 getClass().getName() + ".debug.displayFailures"); frameworkContext.setReportLevel(VerifierConstants.FAIL); 484 setWarningLevelOnce = true; 485 } else { 486 logger.log(Level.FINE, 487 getClass().getName() + ".debug.reportLevel", new Object []{ 489 getReportLevelString( 490 frameworkContext.getReportLevel())}); 491 } 492 break; 493 494 case 'a': 495 if (!setWarningLevelOnce) { 496 logger.log(Level.FINE, 497 getClass().getName() + ".debug.displayAll"); frameworkContext.setReportLevel(VerifierConstants.ALL); 499 setWarningLevelOnce = true; 500 } else { 501 logger.log(Level.FINE, 502 getClass().getName() + ".debug.reportLevel", new Object []{ 504 getReportLevelString( 505 frameworkContext.getReportLevel())}); 506 } 507 break; 508 509 default: 510 logger.log(Level.SEVERE, 512 getClass().getName() + ".invalidreplevel", new Object []{"verifier", new Character (c).toString()}); usage(); 515 } 516 } 517 518 521 private void usage() { 522 if (!frameworkContext.isUsingGui()) { 523 logger.log(Level.INFO, getUsageString()); 524 System.exit(1); 526 } 527 } 528 529 533 private String getUsageString() { 534 String usage = "\n" + (smh.getLocalString 536 (getClass().getName() + ".usageLine1", "usage: VERIFIER [optional_params] <jarFile>")) + "\n\n" + (smh.getLocalString 540 (getClass().getName() + ".usageLine2", "where :")) + "\n\n" + (smh.getLocalString 544 (getClass().getName() + ".usageLine3", " [optional_params]: Must be: ")) + "\n\n" + (smh.getLocalString 548 (getClass().getName() + ".usageLine5", " -d|--destdir <destination dir> : Verifier " + "results are put in the specified existing directory")) + "\n" + (smh.getLocalString 553 (getClass().getName() + ".usageLine35", " -D|--domain : Absolute path of the domain directory. Domain directory will be ignored if verifier is run with -p option. The default domain directory is <AS_INSTALL_DIR>/domains/domain1")) + "\n" + (smh.getLocalString 557 (getClass().getName() + ".usageLine36", " -p|--portability : Verifier will be run in portability mode with this option. Verifier runs in appserver mode by default. In the default mode verifier additionally checks correct usage of Sun application server features.")) + "\n" + (smh.getLocalString 561 (getClass().getName() + ".usageLine16", " -h|--help|-? : display verifier help")) + "\n" + (smh.getLocalString 565 (getClass().getName() + ".usageLine34", " -t|--timestamp : verifer results are generated with timestamp appended to it")) + "\n\n" + (smh.getLocalString 569 (getClass().getName() + ".usageLine7", " -u|--gui : use Verifier GUI")) + "\n\n" + (smh.getLocalString 573 (getClass().getName() + ".usageLine4", " -v|--verbose : Display more execution information ")) + "\n" + (smh.getLocalString 577 (getClass().getName() + ".usageLine17", " -V|--version : display verifier tool version")) + "\n" + (smh.getLocalString 581 (getClass().getName() + ".usageLine8", " (Note: Overrides default behavior, ")) + "\n" + (smh.getLocalString 585 (getClass().getName() + ".usageLine9", " required jarFile not needed)")) + "\n\n" + (smh.getLocalString 589 (getClass().getName() + ".usageLine10", " -r|--reportlevel : result reporting level")) + "\n" + (smh.getLocalString 593 (getClass().getName() + ".usageLine11", " level= a|all : set output report level to " + "display all results")) + "\n" + (smh.getLocalString 598 (getClass().getName() + ".usageLine14", " f|failures : set output report level " + "to display only failure")) + "\n" + (smh.getLocalString 603 (getClass().getName() + ".usageLine15", " results")) + "\n" + (smh.getLocalString 607 (getClass().getName() + ".usageLine12", " w|warnings : set output report level " + "to display only warning and")) + "\n" + (smh.getLocalString 612 (getClass().getName() + ".usageLine13", " failure results")) + "\n" + (smh.getLocalString 616 (getClass().getName() + ".usageLine27", " -a|--app : run only the Application tests")) + "\n" + (smh.getLocalString 620 (getClass().getName() + ".usageLine28", " -A|--appclient : run only the Application Client tests")) + "\n" + (smh.getLocalString 624 (getClass().getName() + ".usageLine29", " -c|--connector : run only the Connector tests")) + "\n" + (smh.getLocalString 628 (getClass().getName() + ".usageLine30", " -e|--ejb : run only the EJB tests")) + "\n" + (smh.getLocalString 632 (getClass().getName() + ".usageLine31", " -w|--web : run only the Web tests")) + "\n" + (smh.getLocalString 636 (getClass().getName() + ".usageLine32", " -s|--webservices : run only the WebServices tests")) + "\n" + (smh.getLocalString 640 (getClass().getName() + ".usageLine33", " -l|--webservicesclient : run only the WebServicesClient tests")) + "\n" + (smh.getLocalString 644 (getClass().getName() + ".usageLine37", " -P|--persistence : run only the EJB 3.0 persistence tests")) + "\n\n\n" + (smh.getLocalString 648 (getClass().getName() + ".usageLine19", "Note: The default mode is non-verbose mode and the " + "default reportlevel is to display only warning and failure results.\n")) + "\n\n" + 653 (smh.getLocalString 654 (getClass().getName() + ".usageLine20", 655 "<jarFile>: ear/war/jar/rar file to perform static " + 656 "verification on ")) 657 + "\n\n" + 658 (smh.getLocalString 659 (getClass().getName() + ".usageLine21", 660 "Below is an example which runs verifier in verbose mode")) 661 + "\n" + 662 (smh.getLocalString 663 (getClass().getName() + ".usageLine22", 664 "and writes all the results of static verification of ")) 665 + "\n" + 666 (smh.getLocalString 667 (getClass().getName() + ".usageLine23", 668 "file sample.ear to the destination directory /verifierresults")) 669 + "\n\n" + 670 (smh.getLocalString 671 (getClass().getName() + ".usageLine24", 672 " verifier -v -ra -d /verifierresults sample.ear")) 673 + "\n\n" + 674 (smh.getLocalString 675 (getClass().getName() + ".usageLine25", 676 " The results get generated in both text and xml format.")) 677 + "\n" + 678 (smh.getLocalString 679 (getClass().getName() + ".usageLine26", 680 " Two result files, sample.ear.txt and sample.ear.xml, " + 681 "will be created.")) 682 + "\n\n"; 683 684 return usage; 685 } 686 687 692 private boolean verifyAndSetResultDir(String name) { 693 694 File outputDir = new File (name); 695 if (outputDir.exists()) { 696 if (outputDir.isDirectory()) { 697 if (outputDir.canWrite()) { 698 frameworkContext.setOutputDirName(name); 699 return true; 700 } else { 701 logger.log(Level.SEVERE, getClass().getName() + 702 ".log.noPermissions", new Object []{name}); return false; 704 } 705 } else { 706 logger.log(Level.SEVERE, getClass().getName() + 707 ".log.notADir", new Object []{name}); return false; 709 710 } 711 } else { 712 logger.log(Level.SEVERE, getClass().getName() + 713 ".log.noDirExists", new Object []{name}); return false; 715 } 716 } 717 718 723 private void setFormatter() { 724 Handler [] handlers = logger.getParent().getHandlers(); 725 for (int i = 0; i < handlers.length; i++) 726 if(handlers[i] instanceof ConsoleHandler ) 727 handlers[i].setFormatter(new VerifierFormatter()); 728 } 729 730 734 private String getReportLevelString(int rl) { 735 String rls = ""; 736 if (rl == VerifierConstants.FAIL) { 737 rls = _FOStr; 738 } else if (rl == VerifierConstants.WARN) { 739 rls = _WFOStr; 740 } else if (rl == VerifierConstants.ALL) { 741 rls = _AOStr; 742 } 743 return rls; 744 } 745 746 } 747 | Popular Tags |