1 21 22 package com.izforge.izpack.util.os; 23 24 import java.io.File ; 25 import java.io.UnsupportedEncodingException ; 26 27 import com.izforge.izpack.util.Librarian; 28 import com.izforge.izpack.util.NativeLibraryClient; 29 30 31 53 54 public class ShellLink implements NativeLibraryClient 55 { 56 57 67 73 public static final int HIDE = 0; 74 75 78 public static final int NORMAL = 1; 79 80 86 public static final int MINIMIZED = 2; 87 88 89 public static final int MAXIMIZED = 3; 90 91 95 public static final int MINNOACTIVE = 7; 96 97 private static final int MIN_SHOW = 0; 98 99 private static final int MAX_SHOW = 7; 100 101 106 public static final int DESKTOP = 1; 107 108 109 public static final int PROGRAM_MENU = 2; 110 111 112 public static final int START_MENU = 3; 113 114 115 public static final int STARTUP = 4; 116 117 private static final int MIN_TYPE = 1; 118 119 private static final int MAX_TYPE = 4; 120 121 125 private static final int SL_OK = 1; 126 127 128 private static final int SL_ERROR = -1; 129 130 133 private static final int SL_INITIALIZED = -2; 134 135 138 private static final int SL_NOT_INITIALIZED = -3; 139 140 144 private static final int SL_OUT_OF_HANDLES = -4; 145 146 150 private static final int SL_NO_IPERSIST = -5; 151 152 156 private static final int SL_NO_SAVE = -6; 157 158 162 private static final int SL_WRONG_DATA_TYPE = -7; 163 164 private static final int UNINITIALIZED = -1; 168 169 170 private static final String LINK_EXTENSION = ".lnk"; 171 172 173 public static final int CURRENT_USER = 0; 174 175 176 public static final int ALL_USERS = 1; 177 178 185 private int nativeHandle = UNINITIALIZED; 186 187 192 private String currentUserLinkPath; 193 194 199 private String allUsersLinkPath; 200 201 private String groupName = ""; 202 203 private String linkName = ""; 204 205 210 private String linkFileName = ""; 211 212 216 private String linkDirectory = ""; 217 218 private String arguments = ""; 219 220 private String description = ""; 221 222 private String iconPath = ""; 223 224 private String targetPath = ""; 225 226 private String workingDirectory = ""; 227 228 244 private String dummyString = ""; 245 246 private int hotkey = 0; 247 248 private int iconIndex = 0; 249 250 private int showCommand = NORMAL; 251 252 private int linkType = DESKTOP; 253 254 private int userType = CURRENT_USER; 255 256 private boolean initializeSucceeded = false; 257 258 private native int initializeCOM(); 264 265 private native int releaseCOM(); 266 267 private native int getInterface(); 268 269 private native int releaseInterface(); 270 271 private native int GetArguments(); 272 273 private native int GetDescription(); 274 275 private native int GetHotkey(); 276 277 private native int GetIconLocation(); 278 279 private native int GetPath(); 280 281 private native int GetShowCommand(); 282 283 private native int GetWorkingDirectory(); 284 285 private native int Resolve(); 286 287 private native int SetArguments(); 288 289 private native int SetDescription(); 290 291 private native int SetHotkey(); 292 293 private native int SetIconLocation(); 294 295 private native int SetPath(); 296 297 private native int SetShowCommand(); 298 299 private native int SetWorkingDirectory(); 300 301 private native int saveLink(String name); 302 303 private native int loadLink(String name); 304 305 private native int GetFullLinkPath(int usertype, int linktype); 306 307 311 private native void FreeLibrary(String name); 312 313 340 public ShellLink(int type, String name) throws Exception , IllegalArgumentException 341 { 342 if ((type < MIN_TYPE) || (type > MAX_TYPE)) { throw (new IllegalArgumentException ( 343 "the type parameter used an illegal value")); } 344 if (name == null) { throw (new IllegalArgumentException ("the name parameter was null")); } 345 346 linkName = name; 347 linkType = type; 348 349 initialize(); 351 setAllLinkPaths(); 353 } 354 355 356 368 public ShellLink(String name, int userType) throws Exception , IllegalArgumentException 369 { 370 if (name == null) { throw (new IllegalArgumentException ("the name parameter was null")); } 371 372 this.userType = userType; 373 374 initialize(); 376 int pathEnd = name.lastIndexOf(File.separator); 378 int nameStart = pathEnd + 1; 379 int nameEnd = name.lastIndexOf('.'); 380 if (nameEnd < 0) { throw (new Exception ("illegal file name")); } 381 linkName = name.substring(nameStart, nameEnd); 382 383 if (userType == CURRENT_USER) 384 { 385 currentUserLinkPath = name.substring(0, pathEnd); 386 } 387 else 388 { 389 allUsersLinkPath = name.substring(0, pathEnd); 390 } 391 392 linkFileName = fullLinkName(userType); 393 if (loadLink(linkFileName) != SL_OK) { throw (new Exception ( 394 "reading of the file did not succeed")); } 395 396 get(); 398 } 399 400 401 422 public ShellLink(int type, int userType, String group, String name) throws Exception , 423 IllegalArgumentException 424 { 425 if ((type < MIN_TYPE) || (type > MAX_TYPE)) { throw (new IllegalArgumentException ( 426 "the type parameter used an illegal value")); } 427 if (name == null) { throw (new IllegalArgumentException ("the name parameter was null")); } 428 429 this.userType = userType; 430 431 initialize(); 433 setAllLinkPaths(); 435 436 if (group != null) 437 { 438 groupName = group; 439 } 440 linkName = name; 441 442 linkFileName = fullLinkName(userType); 444 if (loadLink(linkFileName) != SL_OK) { throw (new Exception ( 445 "reading of the file did not succeed")); } 446 447 get(); 449 } 450 451 452 457 private void initialize() throws Exception 458 { 459 try 460 { 461 Librarian.getInstance().loadLibrary("ShellLink", this); 462 } 463 catch (UnsatisfiedLinkError exception) 464 { 465 throw (new Exception ("could not locate native library")); 466 } 467 468 try 469 { 470 if (initializeCOM() != SL_OK) 471 { 472 throw (new Exception ("could not initialize COM")); 473 } 474 else 475 { 476 initializeSucceeded = true; 477 } 478 } 479 catch (Throwable exception) 480 { 481 throw (new Exception ("unidentified problem initializing COM\n" + exception.toString())); 482 } 483 484 int successCode = getInterface(); 485 if (successCode != SL_OK) 486 { 487 releaseCOM(); 488 initializeSucceeded = false; 489 490 if (successCode == SL_OUT_OF_HANDLES) 491 { 492 throw (new Exception ( 493 "could not get an instance of IShellLink, no more handles available")); 494 } 495 else 496 { 497 throw (new Exception ( 498 "could not get an instance of IShellLink, failed to co-create instance")); 499 } 500 } 501 } 502 503 504 507 protected void finalize() throws Throwable 508 { 509 releaseInterface(); 510 511 if (initializeSucceeded) 512 { 513 releaseCOM(); 514 initializeSucceeded = false; 515 } 516 super.finalize(); 517 } 518 519 520 534 public void freeLibrary(String name) 535 { 536 int result = releaseInterface(); 537 538 if (initializeSucceeded) 539 { 540 result = releaseCOM(); 541 initializeSucceeded = false; 542 } 543 544 FreeLibrary(name); 545 } 546 547 548 558 private String fullLinkPath(int userType) 559 { 560 StringBuffer path = new StringBuffer (); 561 562 if (userType == CURRENT_USER) 566 { 567 path.append(currentUserLinkPath); 568 } 569 else 570 { 571 path.append(allUsersLinkPath); 572 } 573 574 if ((groupName != null) && (groupName.length() > 0)) 575 { 576 path.append(File.separator); 577 path.append(groupName); 578 } 579 580 return (path.toString()); 581 } 582 583 584 594 private String fullLinkName(int userType) 595 { 596 StringBuffer name = new StringBuffer (); 597 598 name.append(fullLinkPath(userType)); 599 600 name.append(File.separator); 601 name.append(linkName); 602 name.append(LINK_EXTENSION); 603 604 return (name.toString()); 605 } 606 607 608 613 private void set() throws Exception 614 { 615 if (SetArguments() != SL_OK) { throw (new Exception ("could not set arguments")); } 616 if (SetDescription() != SL_OK) { throw (new Exception ("could not set description")); } 617 if (SetHotkey() != SL_OK) { throw (new Exception ("could not set hotkey")); } 618 if (SetIconLocation() != SL_OK) { throw (new Exception ("could not set icon location")); } 619 if (SetPath() != SL_OK) { throw (new Exception ("could not set target path")); } 620 if (SetShowCommand() != SL_OK) { throw (new Exception ("could not set show command")); } 621 if (SetWorkingDirectory() != SL_OK) { throw (new Exception ( 622 "could not set working directory")); } 623 624 } 625 626 627 633 private void get() throws Exception 634 { 635 if (GetArguments() != SL_OK) { throw (new Exception ("could not get arguments")); } 636 if (GetDescription() != SL_OK) { throw (new Exception ("could not get description")); } 637 if (GetHotkey() != SL_OK) { throw (new Exception ("could not get hotkey")); } 638 if (GetIconLocation() != SL_OK) { throw (new Exception ("could not get icon location")); } 639 if (GetPath() != SL_OK) { throw (new Exception ("could not get target ath")); } 640 if (GetShowCommand() != SL_OK) { throw (new Exception ("could not get show command")); } 641 if (GetWorkingDirectory() != SL_OK) { throw (new Exception ( 642 "could not get working directory")); } 643 } 644 645 646 651 public void setProgramGroup(String groupName) 652 { 653 this.groupName = groupName; 654 } 655 656 657 664 public void setArguments(String arguments) 665 { 666 this.arguments = arguments; 667 } 668 669 670 677 public void setDescription(String description) 678 { 679 this.description = description; 680 } 681 682 683 691 public void setHotkey(int hotkey) 692 { 693 this.hotkey = hotkey; 694 } 695 696 697 706 public void setIconLocation(String path, int index) 707 { 708 this.iconPath = path; 709 this.iconIndex = index; 710 } 711 712 713 720 public void setTargetPath(String path) 721 { 722 this.targetPath = path; 723 } 724 725 726 749 public void setShowCommand(int show) 750 { 751 if ((show < MIN_SHOW) || (show > MAX_SHOW)) { throw (new IllegalArgumentException ( 752 "illegal value for show command " + show)); } 753 754 this.showCommand = show; 755 } 756 757 758 765 public void setWorkingDirectory(String dir) 766 { 767 this.workingDirectory = dir; 768 } 769 770 771 777 public void setLinkName(String name) 778 { 779 linkName = name; 780 } 781 782 783 797 public void setLinkType(int type) throws IllegalArgumentException , UnsupportedEncodingException 798 { 799 if ((type < MIN_TYPE) || (type > MAX_TYPE)) { throw (new IllegalArgumentException ( 800 "illegal value for type")); } 801 802 linkType = type; 803 804 setAllLinkPaths(); 806 } 807 808 820 public int getLinkType() 821 { 822 return linkType; 823 } 824 825 826 836 public void setUserType(int type) throws IllegalArgumentException 837 { 838 if ((type == CURRENT_USER) || (type == ALL_USERS)) 839 { 840 userType = type; 841 } 842 else 843 { 844 throw (new IllegalArgumentException (type + " is not a recognized user type")); 845 } 846 } 847 848 849 855 public int getUserType() 856 { 857 return userType; 858 } 859 860 861 870 public String getLinkPath(int userType) 871 { 872 String result = null; 873 if (userType == CURRENT_USER) 874 { 875 result = currentUserLinkPath; 876 } 877 else 878 { 879 result = allUsersLinkPath; 880 } 881 return result; 882 } 883 884 885 892 public String getArguments() 893 { 894 return (arguments); 895 } 896 897 898 905 public String getDescription() 906 { 907 return (description); 908 } 909 910 911 918 public int getHotkey() 919 { 920 return (hotkey); 921 } 922 923 924 932 public String getIconLocation() 933 { 934 return (iconPath); 935 } 936 937 938 945 public int getIconIndex() 946 { 947 return (iconIndex); 948 } 949 950 951 958 public String getTargetPath() 959 { 960 return (targetPath); 961 } 962 963 964 971 public int getShowCommand() 972 { 973 return (showCommand); 974 } 975 976 977 984 public String getWorkingDirectory() 985 { 986 return (workingDirectory); 987 } 988 989 990 997 public String getFileName() 998 { 999 return (linkFileName); 1000 } 1001 1002 1003 1013 public String getDirectoryCreated() 1014 { 1015 return (linkDirectory); 1016 } 1017 1018 1019 1024 public String getLinkName() 1025 { 1026 return (linkName); 1027 } 1028 1029 1030 1035 public String getcurrentUserLinkPath() 1036 { 1037 return currentUserLinkPath; 1038 } 1039 1040 1041 1046 public String getallUsersLinkPath() 1047 { 1048 return allUsersLinkPath; 1049 } 1050 1051 1052 1057 public void save() throws Exception 1058 { 1059 set(); 1061 1062 int result = Resolve(); 1064 1065 if (result != SL_OK) { throw (new Exception ("cannot resolve target")); } 1066 1067 File directory = new File (fullLinkPath(userType)); 1069 1070 if (!directory.exists()) 1071 { 1072 directory.mkdirs(); 1073 linkDirectory = directory.getPath(); 1074 } 1075 else 1076 { 1077 linkDirectory = ""; 1078 } 1079 1080 String saveTo = fullLinkName(userType); 1082 1083 result = saveLink(saveTo); 1084 1085 if (result == SL_NO_IPERSIST) 1086 { 1087 throw (new Exception ("could not get handle for IPesist")); 1088 } 1089 else if (result == SL_NO_SAVE) { throw (new Exception ("the save operation failed")); } 1090 1091 linkFileName = saveTo; 1092 } 1093 1094 1095 1103 public void save(String name) throws Exception 1104 { 1105 if (name == null) { throw (new IllegalArgumentException ("name was null")); } 1106 1107 set(); 1109 1110 if (Resolve() != SL_OK) { throw (new Exception ("cannot resolve target")); } 1112 1113 File directory = new File (name.substring(0, name.lastIndexOf(File.separatorChar))); 1115 if (!directory.exists()) 1116 { 1117 directory.mkdirs(); 1118 linkDirectory = directory.getPath(); 1119 } 1120 else 1121 { 1122 linkDirectory = null; 1123 } 1124 1125 if (saveLink(name) != SL_OK) { throw (new Exception ("the save operation failed")); } 1127 1128 linkFileName = name; 1129 } 1130 1131 1132 1140 private void setAllLinkPaths() throws IllegalArgumentException 1141 { 1142 GetFullLinkPath(CURRENT_USER, linkType); 1144 GetFullLinkPath(ALL_USERS, linkType); 1145 1146 if (userType == CURRENT_USER && currentUserLinkPath.length() == 0) 1148 { 1149 userType = ALL_USERS; 1150 } 1151 else if (userType == ALL_USERS && allUsersLinkPath.length() == 0) 1152 { 1153 userType = CURRENT_USER; 1154 } 1155 1156 if ( allUsersLinkPath.length() == 0 && currentUserLinkPath.length() == 0) { throw (new IllegalArgumentException ( 1157 "linkType " + linkType + " is invalid.")); } 1158 } 1159 1160} 1161 1162 1163 | Popular Tags |