1 25 26 package com.izforge.izpack.compiler; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.Set ; 40 import java.util.jar.JarInputStream ; 41 import java.util.zip.ZipEntry ; 42 43 import com.izforge.izpack.CustomData; 44 import com.izforge.izpack.GUIPrefs; 45 import com.izforge.izpack.Info; 46 import com.izforge.izpack.Pack; 47 import com.izforge.izpack.Panel; 48 import com.izforge.izpack.compressor.PackCompressor; 49 import com.izforge.izpack.util.Debug; 50 import com.izforge.izpack.util.VariableSubstitutor; 51 52 64 public class Compiler extends Thread 65 { 66 67 public final static String IZPACK_VERSION = "3.10.0"; 68 69 70 public static String IZPACK_HOME = "."; 71 72 73 protected String basedir; 74 75 76 protected String kind; 77 78 79 protected String output; 80 81 82 private IPackager packager = null; 83 84 85 private boolean compileFailed = true; 86 87 88 private Properties properties; 89 90 91 private VariableSubstitutor propertySubstitutor; 92 93 private String compr_format; 94 private int compr_level; 95 private PackagerListener packagerlistener; 96 97 101 public static void setIzpackHome(String izHome) 102 { 103 IZPACK_HOME = izHome; 104 } 105 106 114 public Compiler(String basedir, String kind, String output) throws CompilerException 115 { 116 this(basedir,kind,output,"default"); 117 } 118 119 128 public Compiler(String basedir, String kind, String output, String compr_format) throws CompilerException 129 { 130 this(basedir,kind,output, compr_format, -1); 131 } 132 133 143 public Compiler(String basedir, String kind, String output, 144 String compr_format, int compr_level) throws CompilerException 145 { 146 this.basedir = basedir; 148 this.kind = kind; 149 this.output = output; 150 151 properties = new Properties (System.getProperties()); 153 propertySubstitutor = new VariableSubstitutor(properties); 154 155 setProperty("izpack.version", IZPACK_VERSION); 157 setProperty("basedir", basedir); 158 159 this.compr_format = compr_format; 160 this.compr_level = compr_level; 161 } 162 163 168 public void initPackager(String classname) throws CompilerException{ 169 try { 170 packager = PackagerFactory.getPackager(classname); 171 packager.initPackCompressor(this.compr_format, this.compr_level); 172 PackCompressor compressor = packager.getCompressor(); 173 if (compressor != null){ 174 compressor.setCompiler(this); 175 } 176 if (this.packagerlistener != null){ 177 packager.setPackagerListener(this.packagerlistener); 178 } 179 } 180 catch (Exception e){ 181 Debug.trace(e); 182 throw new CompilerException("Error loading packager class: " + classname); 183 } 184 } 185 186 190 public PackagerListener getPackagerListener() 191 { 192 return packager.getPackagerListener(); 193 } 194 199 public void setPackagerListener(PackagerListener listener) 200 { 201 if (packager != null){ 202 packager.setPackagerListener(listener); 203 } 204 else { 205 this.packagerlistener = listener; 206 } 207 } 208 209 213 public String getKind() 214 { 215 return kind; 216 } 217 221 public Properties getVariables() 222 { 223 return packager.getVariables(); 224 } 225 226 227 public void compile() 228 { 229 start(); 230 } 231 232 233 public void run() 234 { 235 try 236 { 237 createInstaller(); } 240 catch (CompilerException ce) 241 { 242 System.out.println(ce.getMessage() + "\n"); 243 } 244 catch (Exception e) 245 { 246 if (Debug.stackTracing()) 247 { 248 e.printStackTrace(); 249 } 250 else 251 { 252 System.out.println("ERROR: " + e.getMessage()); 253 } 254 } 255 } 256 257 262 public void createInstaller() throws Exception 263 { 264 if( packager.getCompressor().getContainerPaths() != null ) 266 { 267 String [] containerPaths = packager.getCompressor().getContainerPaths(); 268 String [][] decoderClassNames = packager.getCompressor().getDecoderClassNames(); 269 for( int i = 0; i < containerPaths.length; ++i) 270 { 271 URL compressorURL = null; 272 if( containerPaths[i] != null ) 273 compressorURL = findIzPackResource(containerPaths[i],"pack compression Jar file"); 274 if( decoderClassNames[i] != null && decoderClassNames[i].length > 0) 275 addJarContent(compressorURL, Arrays.asList(decoderClassNames[i])); 276 } 277 278 279 } 280 281 packager.createInstaller(new File (output)); 283 this.compileFailed = false; 284 } 285 286 290 public boolean wasSuccessful() 291 { 292 return !this.compileFailed; 293 } 294 295 300 public String replaceProperties(String value) 301 { 302 return propertySubstitutor.substitute(value, "at"); 303 } 304 305 309 public void setGUIPrefs(GUIPrefs prefs) 310 { 311 packager.setGUIPrefs(prefs); 312 } 313 318 public void setInfo(Info info) throws Exception 319 { 320 packager.setInfo(info); 321 } 322 323 327 public IPackager getPackager() 328 { 329 return packager; 330 } 331 335 public Properties getProperties() 336 { 337 return properties; 338 } 339 340 346 public String getProperty(String name) 347 { 348 return properties.getProperty(name); 349 } 350 351 358 public boolean setProperty(String name, String value) 359 { 360 if (System.getProperties().containsKey(name)) { 361 return false; 362 } 363 properties.put(name, value); 364 return true; 365 } 366 367 375 public boolean addProperty(String name, String value) 376 { 377 String old = properties.getProperty(name); 378 if (old == null) 379 { 380 properties.put(name, value); 381 return true; 382 } 383 return false; 384 } 385 386 390 public void addJarContent(URL content) 391 { 392 packager.addJarContent(content); 393 } 394 395 404 public void addJarContent(URL content, List files) 405 { 406 packager.addJarContent(content, files); 407 } 408 409 415 public void addCustomJar(CustomData ca, URL url) 416 { 417 packager.addCustomJar(ca, url); 418 } 419 425 public void addLangPack(String iso3, URL iso3xmlURL, URL iso3FlagURL) 426 { 427 packager.addLangPack(iso3, iso3xmlURL, iso3FlagURL); 428 } 429 435 public void addNativeLibrary(String name, URL url) throws Exception 436 { 437 packager.addNativeLibrary(name, url); 438 } 439 443 public void addNativeUninstallerLibrary(CustomData data) 444 { 445 packager.addNativeUninstallerLibrary(data); 446 } 447 451 public void addPack(PackInfo pack) 452 { 453 packager.addPack(pack); 454 } 455 460 public void addPanelJar(Panel panel, URL url) 461 { 462 packager.addPanelJar(panel, url); 463 } 464 469 public void addResource(String name, URL url) 470 { 471 packager.addResource(name, url); 472 } 473 474 480 public void checkDependencies() throws CompilerException 481 { 482 checkDependencies(packager.getPacksList()); 483 } 484 485 489 public void checkExcludes() throws CompilerException 490 { 491 checkExcludes(packager.getPacksList()); 492 } 493 494 499 public void checkExcludes(List packs) throws CompilerException 500 { 501 for(int q=0; q<packs.size(); q++) 502 { 503 PackInfo packinfo1 = (PackInfo) packs.get(q); 504 Pack pack1 = packinfo1.getPack(); 505 for(int w = 0; w < q; w++) 506 { 507 508 PackInfo packinfo2 = (PackInfo) packs.get(w); 509 Pack pack2 = packinfo2.getPack(); 510 if(pack1.excludeGroup != null && pack2.excludeGroup != null) 511 { 512 if(pack1.excludeGroup.equals(pack2.excludeGroup)) 513 { 514 if(pack1.preselected && pack2.preselected) 515 { 516 parseError("Packs "+pack1.name+" and "+pack2.name+ 517 " belong to the same excludeGroup "+pack1.excludeGroup+ 518 " and are both preselected. This is not allowed."); 519 } 520 } 521 } 522 } 523 524 } 525 } 526 533 public void checkDependencies(List packs) throws CompilerException 534 { 535 Map names = new HashMap (); 538 for (int i = 0; i < packs.size(); i++) 539 { 540 PackInfo pack = (PackInfo) packs.get(i); 541 names.put(pack.getPack().name, pack); 542 } 543 int result = dfs(packs, names); 544 if (result == -2) 546 parseError("Circular dependency detected"); 547 else if (result == -1) parseError("A dependency doesn't exist"); 548 } 549 550 559 private int dfs(List packs, Map names) 560 { 561 Map edges = new HashMap (); 562 for (int i = 0; i < packs.size(); i++) 563 { 564 PackInfo pack = (PackInfo) packs.get(i); 565 if (pack.colour == PackInfo.WHITE) 566 { 567 if (dfsVisit(pack, names, edges) != 0) return -1; 568 } 569 570 } 571 return checkBackEdges(edges); 572 } 573 574 579 private int checkBackEdges(Map edges) 580 { 581 Set keys = edges.keySet(); 582 for (Iterator iterator = keys.iterator(); iterator.hasNext();) 583 { 584 final Object key = iterator.next(); 585 int color = ((Integer ) edges.get(key)).intValue(); 586 if (color == PackInfo.GREY) { return -2; } 587 } 588 return 0; 589 590 } 591 592 595 private class Edge 596 { 597 598 PackInfo u; 599 600 PackInfo v; 601 602 Edge(PackInfo u, PackInfo v) 603 { 604 this.u = u; 605 this.v = v; 606 } 607 } 608 609 private int dfsVisit(PackInfo u, Map names, Map edges) 610 { 611 u.colour = PackInfo.GREY; 612 List deps = u.getDependencies(); 613 if (deps != null) 614 { 615 for (int i = 0; i < deps.size(); i++) 616 { 617 String name = (String ) deps.get(i); 618 PackInfo v = (PackInfo) names.get(name); 619 if (v == null) 620 { 621 System.out.println("Failed to find dependency: "+name); 622 return -1; 623 } 624 Edge edge = new Edge(u, v); 625 if (edges.get(edge) == null) edges.put(edge, new Integer (v.colour)); 626 627 if (v.colour == PackInfo.WHITE) 628 { 629 630 final int result = dfsVisit(v, names, edges); 631 if (result != 0) return result; 632 } 633 } 634 } 635 u.colour = PackInfo.BLACK; 636 return 0; 637 } 638 639 650 public URL findIzPackResource(String path, String desc) 651 throws CompilerException 652 { 653 URL url = getClass().getResource("/" + path); 654 if (url == null) 655 { 656 File resource = new File (path); 657 if (!resource.isAbsolute()) resource = new File (IZPACK_HOME, path); 658 659 if (!resource.exists()) parseError(desc + " not found: " + resource); 661 662 try 663 { 664 url = resource.toURL(); 665 } 666 catch (MalformedURLException how) 667 { 668 parseError(desc + "(" + resource + ")", how); 669 } 670 } 671 672 return url; 673 } 674 675 682 public void parseError(String message) throws CompilerException 683 { 684 this.compileFailed = true; 685 throw new CompilerException(message); 686 } 687 695 public void parseError(String message, Throwable how) throws CompilerException 696 { 697 this.compileFailed = true; 698 throw new CompilerException(message, how); 699 } 700 701 707 public static void main(String [] args) 708 { 709 CompilerConfig.main(args); 710 } 711 712 715 727 public void addCustomListener(int type, String className, String jarPath, List constraints) throws Exception 728 { 729 jarPath = replaceProperties(jarPath); 730 URL url = findIzPackResource(jarPath, "CustomAction jar file"); 731 List filePaths = getContainedFilePaths(url); 732 String fullClassName = getFullClassName(url, className); 733 CustomData ca = new CustomData(fullClassName, filePaths, constraints, type); 734 packager.addCustomJar(ca, url); 735 } 736 737 745 private List getContainedFilePaths(URL url) throws Exception 746 { 747 JarInputStream jis = new JarInputStream (url.openStream()); 748 ZipEntry zentry = null; 749 ArrayList fullNames = new ArrayList (); 750 while ((zentry = jis.getNextEntry()) != null) 751 { 752 String name = zentry.getName(); 753 if (!zentry.isDirectory()) fullNames.add(name); 755 } 756 jis.close(); 757 return (fullNames); 758 } 759 760 769 private String getFullClassName(URL url, String className) throws Exception 770 { 771 JarInputStream jis = new JarInputStream (url.openStream()); 772 ZipEntry zentry = null; 773 while ((zentry = jis.getNextEntry()) != null) 774 { 775 String name = zentry.getName(); 776 int lastPos = name.lastIndexOf(".class"); 777 if (lastPos < 0) 778 { 779 continue; } 781 name = name.replace('/', '.'); 782 int pos = -1; 783 if (className != null) 784 { 785 pos = name.indexOf(className); 786 } 787 if (name.length() == pos + className.length() + 6) { 790 jis.close(); 791 return (name.substring(0, lastPos)); 792 } 793 } 794 jis.close(); 795 return (null); 796 } 797 798 801 806 static class CmdlinePackagerListener implements PackagerListener 807 { 808 809 814 public void packagerMsg(String info) 815 { 816 packagerMsg(info, MSG_INFO); 817 } 818 819 825 public void packagerMsg(String info, int priority) 826 { 827 final String prefix; 828 switch (priority) 829 { 830 case MSG_DEBUG: 831 prefix = "[ DEBUG ] "; 832 break; 833 case MSG_ERR: 834 prefix = "[ ERROR ] "; 835 break; 836 case MSG_WARN: 837 prefix = "[ WARNING ] "; 838 break; 839 case MSG_INFO: 840 case MSG_VERBOSE: 841 default: prefix = ""; 843 } 844 845 System.out.println(prefix + info); 846 } 847 848 849 public void packagerStart() 850 { 851 System.out.println("[ Begin ]"); 852 System.out.println(); 853 } 854 855 856 public void packagerStop() 857 { 858 System.out.println(); 859 System.out.println("[ End ]"); 860 } 861 } 862 863 } 864 | Popular Tags |