1 21 22 51 package com.izforge.izpack.util.os; 52 53 import com.izforge.izpack.util.Debug; 54 import com.izforge.izpack.util.FileExecutor; 55 import com.izforge.izpack.util.OsVersion; 56 import com.izforge.izpack.util.StringTool; 57 import com.izforge.izpack.util.os.unix.ShellScript; 58 import com.izforge.izpack.util.os.unix.UnixHelper; 59 import com.izforge.izpack.util.os.unix.UnixUser; 60 import com.izforge.izpack.util.os.unix.UnixUsers; 61 62 import java.io.BufferedReader ; 63 import java.io.BufferedWriter ; 64 import java.io.File ; 65 import java.io.FileReader ; 66 import java.io.FileWriter ; 67 import java.io.IOException ; 68 69 import java.util.ArrayList ; 70 import java.util.Enumeration ; 71 import java.util.Properties ; 72 import java.util.StringTokenizer ; 73 import java.util.Vector ; 74 75 80 public class Unix_Shortcut extends Shortcut implements Unix_ShortcutConstants 81 { 82 83 86 private static String version = "$Id: Unix_Shortcut.java 1708 2007-01-13 18:31:26Z jponge $"; 87 88 89 private static String rev = "$Revision: 1708 $"; 90 91 92 private static String DESKTOP_EXT = ".desktop"; 93 94 95 private static String template = ""; 96 97 98 private final static String N = "\n"; 99 100 101 private final static String H = "#"; 102 103 104 private final static String S = " "; 105 106 107 private final static String C = H + S; 108 109 110 private final static String QM = "\""; 111 112 private static ShellScript rootScript = null; 113 private static ShellScript uninstallScript = null; 114 private static ArrayList users = UnixUsers.getUsersWithValidShellsExistingHomesAndDesktops(); 115 117 120 private String createdDirectory; 121 122 123 private int itsUserType; 124 125 126 private String itsGroupName; 127 128 129 private String itsName; 130 131 132 private String itsFileName; 133 134 135 private String itsApplnkFolder = "applnk"; 136 137 138 private Properties props; 139 140 143 private Boolean forAll = Boolean.FALSE; 144 145 146 public StringBuffer hlp; 147 148 150 155 public Unix_Shortcut() 156 { 157 hlp = new StringBuffer (); 158 159 String userLanguage = System.getProperty("user.language", "en"); 160 161 hlp.append("[Desktop Entry]" + N); 162 163 165 hlp.append("Categories=" + $Categories + N); 166 167 hlp.append("Comment=" + $Comment + N); 168 hlp.append("Comment[").append(userLanguage).append("]=" + $Comment + N); 169 hlp.append("Encoding=" + $Encoding + N); 170 171 hlp.append("TryExec=" + $TryExec + N); 172 173 hlp.append("Exec=" + $E_QUOT + $Exec + $E_QUOT + S + $Arguments + N); 174 hlp.append("GenericName=" + $GenericName + N); 175 176 hlp.append("GenericName[").append(userLanguage).append("]=" + $GenericName + N); 177 hlp.append("Icon=" + $Icon + N); 178 hlp.append("MimeType=" + $MimeType + N); 179 hlp.append("Name=" + $Name + N); 180 hlp.append("Name[").append(userLanguage).append("]=" + $Name + N); 181 182 hlp.append("Path=" + $P_QUOT + $Path + $P_QUOT + N); 183 hlp.append("ServiceTypes=" + $ServiceTypes + N); 184 hlp.append("SwallowExec=" + $SwallowExec + N); 185 hlp.append("SwallowTitle=" + $SwallowTitle + N); 186 hlp.append("Terminal=" + $Terminal + N); 187 188 hlp.append("TerminalOptions=" + $Options_For_Terminal + N); 189 hlp.append("Type=" + $Type + N); 190 hlp.append("URL=" + $URL + N); 191 hlp.append("X-KDE-SubstituteUID=" + $X_KDE_SubstituteUID + N); 192 hlp.append("X-KDE-Username=" + $X_KDE_Username + N); 193 hlp.append(N); 194 hlp.append(C + "created by" + S).append(getClass().getName()).append(S).append(rev).append( 195 N); 196 hlp.append(C).append(version); 197 198 template = hlp.toString(); 199 200 props = new Properties (); 201 202 initProps(); 203 204 if( rootScript==null ) 205 rootScript = new ShellScript(); 206 if( uninstallScript == null) 207 uninstallScript = new ShellScript(); 208 } 209 210 212 217 private void initProps() 218 { 219 String [] propsArray = { $Comment, $$LANG_Comment, $Encoding, $Exec, $Arguments, 220 $GenericName, $$LANG_GenericName, $MimeType, $Name, $$LANG_Name, $Path, 221 $ServiceTypes, $SwallowExec, $SwallowTitle, $Terminal, $Options_For_Terminal, 222 $Type, $X_KDE_SubstituteUID, $X_KDE_Username, $Icon, $URL, $E_QUOT, $P_QUOT, 223 $Categories, $TryExec}; 224 225 for (int i = 0; i < propsArray.length; i++) 226 { 227 props.put(propsArray[i], ""); 228 } 229 } 230 231 236 public void initialize(int aType, String aName) throws Exception 237 { 238 this.itsName = aName; 239 props.put($Name, aName); 240 } 241 242 247 public boolean supported() 248 { 249 return true; 250 } 251 252 257 public String getDirectoryCreated() 258 { 259 return this.createdDirectory; } 261 262 267 public String getFileName() 268 { 269 return (this.itsFileName); 270 } 271 272 277 public Vector getProgramGroups(int userType) 278 { 279 Vector groups = new Vector (); 280 281 File kdeShareApplnk = getKdeShareApplnkFolder(userType); 282 283 try 284 { 285 File [] listing = kdeShareApplnk.listFiles(); 286 287 for (int i = 0; i < listing.length; i++) 288 { 289 if (listing[i].isDirectory()) 290 { 291 groups.add(listing[i].getName()); 292 } 293 } 294 } 295 catch (Exception e) 296 { 297 } 299 300 return (groups); 301 } 302 303 308 public String getProgramsFolder(int current_user) 309 { 310 String result = ""; 311 312 result = getKdeShareApplnkFolder(current_user).toString(); 314 315 return result; 316 } 317 318 326 private File getKdeShareApplnkFolder(int userType) 327 { 328 File kdeBase = getKdeBase(userType); 329 330 File result = new File (kdeBase + File.separator + "share" + File.separator 331 + getKdeApplinkFolderName()); 332 333 return result; 334 } 335 336 342 private String getKdeApplinkFolderName() 343 { 344 String applinkFolderName = "applnk"; 345 346 if (OsVersion.IS_REDHAT_LINUX) 347 { 348 applinkFolderName = "applnk-redhat"; 349 } 350 351 if (OsVersion.IS_MANDRAKE_LINUX) 352 { 353 applinkFolderName = "applnk-mdk"; 354 } 355 356 return applinkFolderName; 357 } 358 359 366 private File getKdeBase(int userType) 367 { 368 File result = null; 369 370 if (userType == Shortcut.ALL_USERS) 371 { 372 FileExecutor fe = new FileExecutor(); 373 374 String [] execOut = new String [2]; 375 376 int execResult = fe.executeCommand(new String [] { "/usr/bin/env", "kde-config", 377 "--prefix"}, execOut); 378 379 result = new File (execOut[0].trim()); 380 } 381 else 382 { 383 result = new File (System.getProperty("user.home") + File.separator + ".kde"); 384 } 385 386 return result; 387 } 388 389 396 public boolean multipleUsers() 397 { 398 return (true); 400 } 401 402 407 public void save() throws Exception 408 { 409 String FS = File.separator; 410 String chmod = UnixHelper.getCustomCommand("chmod"); 411 String chown = UnixHelper.getCustomCommand("chown"); 412 String rm = UnixHelper.getRmCommand(); 413 String copy = UnixHelper.getCpCommand(); 414 String su = UnixHelper.getSuCommand(); 415 416 String myHome=System.getProperty("user.home"); 417 418 String target = null; 419 420 String shortCutDef = this.replace(); 421 422 boolean rootUser4All = this.getUserType() == Shortcut.ALL_USERS; 423 boolean create4All = this.getCreateForAll().booleanValue(); 424 425 if ("".equals(this.itsGroupName) && (this.getLinkType() == Shortcut.DESKTOP)) 427 { 428 429 target = myHome + FS + "Desktop" + FS + this.itsName 430 + DESKTOP_EXT; 431 this.itsFileName = target; 432 433 File writtenDesktopFile = writeShortCut(target, shortCutDef); 435 436 if (rootUser4All && create4All) 438 { 439 File dest = null; 440 441 File tempFile = File.createTempFile(this.getClass().getName(), Long.toString(System 443 .currentTimeMillis()) 444 + ".tmp"); 445 446 copyTo(writtenDesktopFile, tempFile); 447 448 450 FileExecutor.getExecOutput(new String [] { chmod, "uga+rwx", tempFile.toString()}); 451 452 455 458 for (int idx = 0; idx < users.size(); idx++) 459 { 460 UnixUser user = ((UnixUser) users.get(idx)); 461 462 if( user.getHome().equals(myHome) ) 463 { 464 Debug.log( "need not to copy for itself: " + user.getHome() + "==" + myHome ); 465 continue; 466 } 467 try 468 { 469 dest = new File (user.getHome() + FS + "Desktop" + FS 471 + writtenDesktopFile.getName()); 472 482 484 rootScript.append(su); 485 rootScript.append(S); 486 rootScript.append(user.getName()); 487 rootScript.append(S); 488 rootScript.append("-c"); 489 rootScript.append(S); 490 rootScript.append('"'); 491 rootScript.append(copy); 492 rootScript.append(S); 493 rootScript.append(tempFile.toString()); 494 rootScript.append(S); 495 rootScript.append(StringTool.replace(dest.toString(), " ", "\\ ")); 496 rootScript.appendln('"'); 497 498 rootScript.append('\n'); 499 500 502 rootScript.append( chown ); 503 rootScript.append( S ); 504 rootScript.append( user.getName() ); 505 rootScript.append( S ); 506 rootScript.appendln( StringTool.replace(dest.toString(), " ", "\\ ") ); 507 rootScript.append('\n'); 508 rootScript.append('\n'); 509 510 512 uninstallScript.append(su); 513 uninstallScript.append(S); 514 uninstallScript.append(user.getName()); 515 uninstallScript.append(S); 516 uninstallScript.append("-c"); 517 uninstallScript.append(S); 518 uninstallScript.append('"'); 519 uninstallScript.append(rm); 520 uninstallScript.append(S); 521 uninstallScript.append(StringTool.replace(dest.toString(), " ", "\\ ")); 522 uninstallScript.appendln('"'); 523 uninstallScript.appendln(); 524 } 526 catch (Exception rex) 527 { 528 System.out.println("Error while su Copy: " + rex.getLocalizedMessage()+"\n\n"); 529 rex.printStackTrace(); 530 531 532 } 536 } 537 538 539 rootScript.append( rm ); 540 rootScript.append( S ); 541 rootScript.appendln( tempFile.toString()); 542 rootScript.appendln(); 543 } 544 } 545 546 else 548 { 549 File kdeHomeShareApplnk = getKdeShareApplnkFolder(this.getUserType()); 550 target = kdeHomeShareApplnk.toString() + FS + this.itsGroupName + FS + this.itsName 551 + DESKTOP_EXT; 552 this.itsFileName = target; 553 writeShortCut(target, shortCutDef); 554 555 if (rootUser4All) 556 { 557 if (create4All) 558 { 559 561 File theIcon = new File (this.getIconLocation()); 562 File commonIcon = new File ("/usr/share/pixmaps/" + theIcon.getName()); 563 564 try 565 { 566 copyTo(theIcon, commonIcon); 567 uninstaller.addFile(commonIcon.toString()); 568 } 569 catch (Exception cnc) 570 { 571 Debug.log("Could Not Copy: " + theIcon + " to " + commonIcon + "( " 572 + cnc.getMessage() + " )"); 573 } 574 575 577 String commonTarget = "/usr/share/applications/" + this.itsName + DESKTOP_EXT; 578 this.itsFileName = target; 579 File writtenFile = writeShortCut(commonTarget, shortCutDef); 580 581 uninstaller.addFile(writtenFile.toString()); 582 583 } 584 else 585 { 586 } 588 } 589 590 } 591 } 592 593 594 599 public void execPostAction() 600 { 601 Debug.log("Call of Impl. execPostAction Method in " + this.getClass().getName() ); 602 603 String pseudoUnique = this.getClass().getName()+ Long.toString(System.currentTimeMillis()); 604 605 String scriptFilename = null; 606 607 try 608 { 609 scriptFilename = File.createTempFile( pseudoUnique, ".sh" ).toString(); 610 } 611 catch (IOException e) 612 { 613 scriptFilename = System.getProperty("java.io.tmpdir", "/tmp") + "/" + pseudoUnique + ".sh"; 614 e.printStackTrace(); 615 } 616 617 rootScript.write(scriptFilename); 618 rootScript.exec(); 619 620 Debug.log(rootScript); 621 622 623 Debug.log(uninstallScript); 624 625 uninstaller.addRootUninstallScript( uninstallScript.getContentAsString() ); 626 } 627 628 636 public static void copyTo(File inFile, File outFile) throws IOException 637 { 638 char[] cbuff = new char[32768]; 639 BufferedReader reader = new BufferedReader (new FileReader (inFile)); 640 BufferedWriter writer = new BufferedWriter (new FileWriter (outFile)); 641 642 int readedBytes = 0; 643 long absWrittenBytes = 0; 644 645 while ((readedBytes = reader.read(cbuff, 0, cbuff.length)) != -1) 646 { 647 writer.write(cbuff, 0, readedBytes); 648 absWrittenBytes += readedBytes; 649 } 650 651 reader.close(); 652 writer.close(); 653 } 654 655 663 private File writeShortCut(String target, String shortCutDef) 664 { 665 File targetPath = new File (target.substring(0, target.lastIndexOf(File.separatorChar))); 666 667 if (!targetPath.exists()) 668 { 669 targetPath.mkdirs(); 670 this.createdDirectory = targetPath.toString(); 671 } 672 673 File targetFileName = new File (target); 674 File backupFile = new File (targetPath + File.separator + "." + targetFileName.getName() 675 + System.currentTimeMillis()); 676 677 if (targetFileName.exists()) 678 { 679 try 680 { 681 copyTo(targetFileName, backupFile); targetFileName.delete(); 684 } 685 catch (IOException e3) 686 { 687 System.out.println("cannot create backup file " + backupFile + " of " 688 + targetFileName); } 690 } 691 692 FileWriter fileWriter = null; 693 694 try 695 { 696 fileWriter = new FileWriter (targetFileName); 697 } 698 catch (IOException e1) 699 { 700 System.out.println(e1.getMessage()); 701 } 702 703 try 704 { 705 fileWriter.write(shortCutDef); 706 } 707 catch (IOException e) 708 { 709 e.printStackTrace(); 710 } 711 712 try 713 { 714 fileWriter.close(); 715 } 716 catch (IOException e2) 717 { 718 e2.printStackTrace(); 719 } 720 721 return targetFileName; 722 } 723 724 729 public void setArguments(String args) 730 { 731 props.put($Arguments, args); 732 } 733 734 739 public void setDescription(String description) 740 { 741 props.put($Comment, description); 742 } 743 744 749 public void setIconLocation(String path, int index) 750 { 751 props.put($Icon, path); 752 } 753 754 759 public void setLinkName(String aName) 760 { 761 this.itsName = aName; 762 props.put($Name, aName); 763 } 764 765 770 public void setLinkType(int aType) throws IllegalArgumentException 771 { 772 } 773 774 779 public void setProgramGroup(String aGroupName) 780 { 781 this.itsGroupName = aGroupName; 782 } 783 784 789 public void setShowCommand(int show) 790 { 791 } 792 793 798 public void setTargetPath(String aPath) 799 { 800 StringTokenizer whiteSpaceTester = new StringTokenizer (aPath); 801 802 if (whiteSpaceTester.countTokens() > 1) 803 { 804 props.put($E_QUOT, QM); 805 } 806 807 props.put($Exec, aPath); 808 } 809 810 815 public void setUserType(int aUserType) 816 { 817 this.itsUserType = aUserType; 818 } 819 820 825 public void setWorkingDirectory(String aDirectory) 826 { 827 StringTokenizer whiteSpaceTester = new StringTokenizer (aDirectory); 828 829 if (whiteSpaceTester.countTokens() > 1) 830 { 831 props.put($P_QUOT, QM); 832 } 833 834 props.put($Path, aDirectory); 835 } 836 837 842 public String toString() 843 { 844 return this.itsName + N + template; 845 } 846 847 852 public String replace() 853 { 854 String result = template; 855 Enumeration enumeration = props.keys(); 856 857 while (enumeration.hasMoreElements()) 858 { 859 String key = (String ) enumeration.nextElement(); 860 861 result = StringTool.replace(result, key, props.getProperty(key)); 862 } 863 864 return result; 865 } 866 867 872 public static void main(String [] args) 873 { 874 Unix_Shortcut aSample = new Unix_Shortcut(); 875 876 try 877 { 878 aSample.initialize(APPLICATIONS, "Start Tomcat"); 879 } 880 catch (Exception exc) 881 { 882 System.err.println("Could not init Unix_Shourtcut"); 883 } 884 885 aSample.replace(); 886 System.out.println(aSample); 887 888 File targetFileName = new File (System.getProperty("user.home") + File.separator 889 + "Start Tomcat" + DESKTOP_EXT); 890 FileWriter fileWriter = null; 891 892 try 893 { 894 fileWriter = new FileWriter (targetFileName); 895 } 896 catch (IOException e1) 897 { 898 e1.printStackTrace(); 899 } 900 901 try 902 { 903 fileWriter.write(template); 904 } 905 catch (IOException e) 906 { 907 e.printStackTrace(); 908 } 909 910 try 911 { 912 fileWriter.close(); 913 } 914 catch (IOException e2) 915 { 916 e2.printStackTrace(); 917 } 918 } 919 920 925 public void setEncoding(String aEncoding) 926 { 927 props.put($Encoding, aEncoding); 928 } 929 930 935 public void setKdeSubstUID(String trueFalseOrNothing) 936 { 937 props.put($X_KDE_SubstituteUID, trueFalseOrNothing); 938 } 939 940 945 public void setKdeUserName(String aUserName) 946 { 947 props.put($X_KDE_Username, aUserName); 948 } 949 950 955 public void setMimetype(String aMimetype) 956 { 957 props.put($MimeType, aMimetype); 958 } 959 960 965 public void setTerminal(String trueFalseOrNothing) 966 { 967 props.put($Terminal, trueFalseOrNothing); 968 } 969 970 975 public void setTerminalOptions(String someTerminalOptions) 976 { 977 props.put($Options_For_Terminal, someTerminalOptions); 978 } 979 980 985 public void setType(String aType) 986 { 987 props.put($Type, aType); 988 } 989 990 995 public void setURL(String anUrl) 996 { 997 props.put($URL, anUrl); 998 } 999 1000 1005 public int getUserType() 1006 { 1007 return itsUserType; 1008 } 1009 1010 1015 public void setCategories(String theCategories) 1016 { 1017 props.put($Categories, theCategories); 1018 } 1019 1020 1025 public void setTryExec(String aTryExec) 1026 { 1027 props.put($TryExec, aTryExec); 1028 } 1029 1030} 1031 | Popular Tags |