| 1 18 19 package org.objectweb.jac.core; 20 21 22 23 import java.io.*; 24 import java.io.File ; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.util.*; 28 import org.apache.log4j.ConsoleAppender; 29 import org.apache.log4j.FileAppender; 30 import org.apache.log4j.Level; 31 import org.apache.log4j.Logger; 32 import org.apache.log4j.PatternLayout; 33 import org.objectweb.jac.util.*; 34 35 85 86 public class Jac { 87 88 89 static Date start_time = new Date(); 90 91 93 public static String getFullJavaVersion() { 94 return System.getProperties().getProperty("java.version"); 95 } 96 97 100 public static String getMainJavaVersion() { 101 return System.getProperties().getProperty("java.version").substring(0,3); 102 } 103 104 108 109 public static Date getStartTime() { 110 return start_time; 111 } 112 113 114 private static boolean verbose = false; 115 116 117 private static boolean debug = false; 118 119 120 private static boolean gen = false; 121 122 124 private static boolean clean = false; 125 126 128 private static boolean write = false; 129 130 132 private static boolean server = false; 133 134 135 private static boolean client = false; 136 137 138 private static String clientHost = ""; 139 140 141 private static String serverHost = ""; 142 143 144 private static String master = ""; 145 146 149 private static boolean distributed = false; 150 151 152 private static String classRepository = ""; 153 154 156 private static boolean start = true; 157 158 159 public static JacLoader classLoader; 161 162 163 164 private static String jac_root = ""; 165 166 167 private static String launchedClass = ""; 168 169 170 private static String [] startSwingGUI = null; 171 172 173 private static String [] startWebGUI = null; 174 175 private static Hashtable logLevels = new Hashtable(); 176 177 private static String logFileName; 178 179 180 private static String remoteGuiServerName = null; 181 182 183 public static ClassLoader otherClasspath; 184 185 static final String version = "0.12"; 186 187 static Logger logger = Logger.getLogger("jac"); 188 static Logger perf = Logger.getLogger("perf"); 189 static Logger classpath = Logger.getLogger("classpath"); 190 static Logger urlloader = Logger.getLogger("urlloader"); 191 192 201 public static void main(String [] args) throws Throwable { 202 Logger root = Logger.getRootLogger(); 203 root.addAppender( 204 new ConsoleAppender( 205 new PatternLayout("%d %p %c: %x %m%n"))); 206 root.setLevel(Level.WARN); 207 Logger.getLogger("jac").setLevel(Level.INFO); 208 Logger.getLogger("gui").setLevel(Level.INFO); 209 Logger.getLogger("dist").setLevel(Level.INFO); 210 Logger.getLogger("props").setLevel(Level.ERROR); 211 logger.info("JAC version "+version); 212 213 args = parseOptions(args); 214 215 classLoader = new JacLoader(write, clean, otherClasspath); 216 217 if (remoteGuiServerName!=null) { 218 remoteInvoke(remoteGuiServerName,"launchGUI", new Object [0]); 219 System.exit(0); 220 } 221 222 226 Class daemon = null; 227 228 229 if (!master.equals("")) { 230 try { 231 ClassLoader cl = 232 (ClassLoader )Class.forName("org.objectweb.jac.core.dist.DistdClassLoader") 233 .newInstance (); 234 Class jac = cl.loadClass("org.objectweb.jac.core.Jac"); 235 jac.getMethod("setJacRoot", new Class [] {String .class}).invoke( 236 null,new Object [] {jac_root}); 237 238 daemon = cl.loadClass("org.objectweb.jac.core.dist.rmi.RMIDistd"); 239 daemon.getConstructor( new Class [] { String [].class } ) 241 .newInstance ( new Object [] { 242 new String [] {"-r",master,serverHost} } ); 243 244 Class bootstrap = 245 cl.loadClass("org.objectweb.jac.aspects.distribution.bootstrap.DistBootstrap"); 246 bootstrap.getMethod("main", new Class [] {String [].class}).invoke( 247 null,new Object [] {new String [0]}); 248 249 if (start) { 250 long _start_time = System.currentTimeMillis(); 251 252 Class acm = cl.loadClass("org.objectweb.jac.core.ACManager"); 255 acm.getMethod("main", new Class [] {String [].class}).invoke( 256 null,new Object [] {new String [0]}); 257 258 259 if (args.length > 0) { 260 Class appRep = cl.loadClass("org.objectweb.jac.core.ApplicationRepository"); 261 appRep.getMethod("main", new Class [] {String [].class}).invoke( 262 null,new Object [] {args}); 263 } 264 perf.info("application started in "+ 265 (System.currentTimeMillis()-_start_time)+"ms"); 266 } 272 273 } catch (Exception e) { 274 System.out.println ("Error: cannot launch Jac server."); 275 e.printStackTrace(); 276 while (true) {} 277 } 278 279 } else { 280 281 long _start_time = System.currentTimeMillis(); 282 if (distributed) { 283 daemon = classLoader.loadClass("org.objectweb.jac.core.dist.rmi.RMIDistd"); 284 daemon.getConstructor(new Class [] { String [].class }) 285 .newInstance ( new Object [] { new String [] {serverHost} } ); 286 } 287 if (client) { 288 daemon = classLoader.loadClass("org.objectweb.jac.core.dist.rmi.RMIDistd"); 289 daemon.getConstructor(new Class [] { String [].class }) 290 .newInstance ( new Object [] { new String [] { clientHost } } ); 291 } 292 293 307 308 if (start) { 309 310 classLoader.loadClass("org.objectweb.jac.util.Repository"); 311 if (distributed || client || server) { 312 313 classLoader.run("org.objectweb.jac.aspects.distribution.bootstrap.DistBootstrap", null); 314 } 315 321 classLoader.run("org.objectweb.jac.core.ACManager",null); 322 323 324 if (args.length > 0) { 325 classLoader.run("org.objectweb.jac.core.ApplicationRepository", args); 326 } 327 } 331 perf.info("application started in "+ 332 (System.currentTimeMillis()-_start_time)+"ms"); 333 334 } 335 } 336 337 338 343 private static URL [] parseClasspath(String path) 344 { 345 String [] tab = Strings.splitPath(path); 346 347 URL [] result = new URL [tab.length]; 348 349 try { 350 for (int i = 0; i < tab.length; i++) { 351 urlloader.info("adding "+new File (tab[i]).toURL()); 352 result[i] = new File (tab[i]).toURL(); 353 } 354 } catch (Exception e) { 355 e.printStackTrace(); 356 } 357 358 return result; 359 } 360 361 366 367 private static String [] parseOptions(String [] args) { 368 int i = 0; 369 String [] ret = args; 370 if (args.length == 0) { 371 start = false; 372 displayLaunchingHelp(); 373 return ret; 374 } 375 try { 376 parse: 377 while((i < args.length) && args[i].startsWith("-")) { 378 String current = 379 (args[i].length()>2) 380 ? args[i].substring(2) 381 : null; 382 switch (args[i].charAt(1)) { 383 case '-': 384 i++; 385 break parse; 386 case 'r': 387 System.out.println( "- JAC version "+version+" - " + 388 "Get new release at http://org.objectweb.jac.aopsys.com/ -" ); 389 break; 390 case 'v': 391 verbose = true; 392 break; 393 case 'd': 394 debug = true; 395 break; 396 case 'g': 397 gen = true; 398 start = false; 399 break; 400 case 'w': 401 write = true; 402 break; 403 case 'c': 404 clean = true; 405 break; 406 case 'C': i++; 408 String [] cp = Strings.splitPath(args[i]); 409 String newClassPath = ""; 410 411 for (int j = 0; j < cp.length; j++) 413 { 414 File file = new File (cp[j]); 415 cp[j] = file.getAbsolutePath(); 416 if (newClassPath.length() != 0) 417 newClassPath += System.getProperty("path.separator"); 418 newClassPath += cp[j]; 419 } 420 421 classpath.info("java.class.path="+System.getProperty("java.class.path")); 422 System.setProperty("java.class.path", 424 newClassPath 425 + System.getProperty("path.separator") 426 + System.getProperty("java.class.path")); 427 428 otherClasspath = 429 new URLClassLoader (parseClasspath(newClassPath)); 430 431 break; 432 case 'D': 433 distributed = true; 434 if( (i+1>=args.length) || args[i+1].startsWith("-") || 435 args[i+1].endsWith(".jac")) { 436 serverHost = "s0"; 437 } else { 438 i++; 439 serverHost = args[i]; 440 } 441 break; 442 case 'M': 443 i++; 444 master = args[i]; 445 break; 446 case 'V': 447 { 448 String category = current; 449 if (category==null) { 450 i++; 451 category = args[i]; 452 } 453 int index = category.indexOf(':'); 454 String serverName=null; 455 if (index!=-1) { 456 serverName = category.substring(0,index); 457 category = category.substring(index+1); 458 } 459 int equalIndex = category.indexOf('='); 460 Level level = Level.DEBUG; 461 if (equalIndex!=-1) { 462 try { 463 level = 464 level.toLevel( 465 Integer.parseInt(category.substring(equalIndex+1))); 466 } catch (NumberFormatException e) { 467 level = Level.toLevel(category.substring(equalIndex+1)); 468 } 469 category = category.substring(0,equalIndex); 470 } 471 if (serverName!=null) { 472 remoteSetTrace("",serverName,category,level.toInt()); 473 System.exit(0); 474 } else { 475 Logger.getLogger(category).setLevel(level); 476 } 477 } 478 break; 479 case 'L': 480 if (current==null) { 481 i++; 482 current = args[i]; 483 } 484 logFileName = current; 485 Logger.getRootLogger().addAppender( 486 new FileAppender( 487 new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN), 488 logFileName)); 489 break; 490 case 'h': 491 start = false; 492 displayLaunchingHelp(); 493 break; 494 case 'R': 495 i++; 496 Jac.setJacRoot(args[i]); 497 break; 498 case 'G': 499 { 500 if (current==null) { 501 i++; 502 current = args[i]; 503 } 504 String guis = current; 505 int index = guis.indexOf(':'); 506 String serverName = null; 507 if (index!=-1) { 508 serverName = guis.substring(0,index); 509 guis = guis.substring(index+1); 510 } 511 if (serverName==null) { 512 Jac.setStartSwingGUI(Strings.split(guis,",")); 513 } else { 514 index = serverName.indexOf('@'); 515 if (index!=-1) { 516 remoteStartSwingGUI(serverName.substring(0,index), 517 serverName.substring(index+1), 518 Strings.split(guis,",")); 519 System.exit(0); 520 } else { 521 logger.fatal("No application name specified "+ 522 "(appName@sever:guiName"); 523 System.exit(1); 524 } 525 } 526 } 527 break; 528 case 'x': 529 PrintStream stream = 530 new PrintStream(new FileOutputStream("out.txt")); 531 System.setOut(stream); 532 System.setErr(stream); 533 break; 534 case 'A': 535 i++; 536 remoteGuiServerName = args[i]; 537 538 start = false; 539 break; 540 case 'a': 541 { 542 i++; 543 String applicationName = args[i]; 544 i++; 545 String aspectName = args[i]; 546 i++; 547 String serverName = args[i]; 548 remoteReloadAspect(applicationName,serverName,aspectName); 549 start = false; 550 System.exit(0); 551 } 552 break; 553 case 'u': 554 { 555 i++; 556 String applicationName = args[i]; 557 i++; 558 String aspectName = args[i]; 559 i++; 560 String serverName = args[i]; 561 remoteUnweaveAspect(applicationName,serverName,aspectName); 562 start = false; 563 System.exit(0); 564 } 565 break; 566 case 'n': 567 { 568 i++; 569 String applicationName = args[i]; 570 i++; 571 String aspectName = args[i]; 572 i++; 573 String serverName = args[i]; 574 i++; 575 String confPath = args[i]; 576 remoteWeaveAspect(applicationName,serverName,aspectName,confPath); 577 start = false; 578 System.exit(0); 579 } 580 break; 581 case 'W': 582 if (client || server) { 583 System.out.println("Incompatible options "+ 584 "(-W, -S, and -C are incompatible)"); 585 displayLaunchingHelp(); 586 } 587 i++; 588 Jac.setStartWebGUI(Strings.split(args[i],",")); 589 break; 590 case 't': 591 String aspect = args[++i]; 592 String config = args[++i]; 593 try { 594 ACManager.main(new String [0]); 595 Logger.getLogger("aspects.config").setLevel(Level.DEBUG); 596 Logger root = Logger.getRootLogger(); 597 root.removeAllAppenders(); 598 root.addAppender( 599 new ConsoleAppender( 600 new PatternLayout("%p %m%n"))); 601 AspectComponent ac = 602 (AspectComponent)Class.forName( 603 ACManager.getACM().getACPathFromName(aspect)).newInstance(); 604 ac.configure(aspect,config); 605 } catch (Throwable e) { 606 e.printStackTrace(); 607 } 608 System.exit(0); 609 break; 610 default: 611 System.out.println("Wrong option: " + args[i]); 612 displayLaunchingHelp(); 613 } 614 if (args[i].startsWith("-S")) { i++; break; } 615 if (args[i].equals("-g")) { break; } 616 i++; 617 } 618 } catch (Exception e) { 619 e.printStackTrace(); 620 621 System.out.println("Error while arguments parsing"); 622 displayLaunchingHelp(); 623 } 624 625 if (i>0) { 630 ret = new String [args.length - i]; 631 System.arraycopy(args, i, ret, 0, args.length - i); 632 } 633 return ret; 634 } 635 636 |