1 20 21 package org.jdesktop.jdic.packager.impl; 22 23 import java.io.IOException ; 24 27 public class WinMsiWrapper { 28 29 static { 30 System.loadLibrary("WinMsiWrapper"); 31 } 32 33 37 public static final int MSIDBOPEN_CREATE = 3; 38 41 public static final int MSIDBOPEN_DIRECT = 2; 42 45 public static final int MSIDBOPEN_READONLY = 0; 46 49 public static final int MSIDBOPEN_TRANSACT = 1; 50 51 57 public static final int MSIMODIFY_SEEK = -1; 58 62 public static final int MSIMODIFY_REFRESH = 0; 63 66 public static final int MSIMODIFY_INSERT = 1; 67 70 public static final int MSIMODIFY_UPDATE = 2; 71 74 public static final int MSIMODIFY_ASSIGN = 3; 75 78 public static final int MSIMODIFY_REPLACE = 4; 79 82 public static final int MSIMODIFY_MERGE = 5; 83 86 public static final int MSIMODIFY_DELETE = 6; 87 90 public static final int MSIMODIFY_INSERT_TEMPORARY = 7; 91 94 public static final int MSIMODIFY_VALIDATE = 8; 95 98 public static final int MSIMODIFY_VALIDATE_NEW = 9; 99 102 public static final int MSIMODIFY_VALIDATE_FIELD = 10; 103 106 public static final int MSIMODIFY_VALIDATE_DELETE = 11; 107 108 112 public static final int ERROR_SUCCESS = 0; 113 116 public static final int ERROR_ACCESS_DENIED = 5; 117 120 public static final int ERROR_INVALID_HANDLE = 6; 121 124 public static final int ERROR_INVALID_DATA = 13; 125 128 public static final int ERROR_INVALID_PARAMETER = 87; 129 132 public static final int ERROR_OPEN_FAILED = 110; 133 136 public static final int ERROR_BAD_PATHNAME = 161; 137 140 public static final int ERROR_NO_MORE_ITEMS = 259; 141 144 public static final int ERROR_INVALID_HANDLE_STATE = 1609; 145 148 public static final int ERROR_BAD_QUERY_SYNTAX = 1615; 149 152 public static final int ERROR_CREATE_FAILED = 1631; 153 156 public static final int ERROR_FUNCTION_FAILED = 1627; 157 158 170 private static native int[] msiOpenDatabase(byte[] databasePath, 171 int persist); 172 173 181 private static native int[] msiDatabaseOpenView(int hDatabase, 182 byte[] szQuery); 183 184 190 private static native int msiViewExecute(int hView, int hRecord); 191 192 198 private static native int[] msiViewFetch(int hView); 199 200 206 private static native byte[] msiRecordGetString(int hRecord, int iField); 207 208 215 private static native int msiRecordSetString(int hRecord, 216 int iField, 217 byte[] szValue); 218 219 226 private static native int msiViewModify(int hView, 227 int eModifyMode, 228 int hRecord); 229 230 237 private static native int msiRecordGetFieldCount(int hRecord); 238 239 245 private static native int msiCreateRecord(int numRecords); 246 247 252 private static native int msiViewClose(int hView); 253 254 259 private static native int msiDatabaseCommit(int hDatabase); 260 261 266 private static native int msiCloseHandle(int hAny); 267 268 275 private static native int msiRecordSetStream(int hRecord, 276 int iField, 277 byte[] szFilePath); 278 279 285 private static native byte[] msiRecordReadStream(int hRecord, int iField); 286 287 292 private static native int[] msiGetSummaryInformation(int hDatabase); 293 294 301 private static native int msiSummaryInfoSetProperty(int hSummaryInfo, 302 int uiProperty, 303 byte[] szValue); 304 305 310 private static native int msiSummaryInfoPersist(int hSummaryInfo); 311 312 318 private static native int msiCloseAllHandles(); 319 320 328 private static native int msiDatabaseGenerateTransform( 329 int hDatabase, 330 int hDatabaseReference, 331 byte[] szTransformFile); 332 333 341 private static native int msiCreateTransformSummaryInfo( 342 int hDatabase, 343 int hDatabaseReference, 344 byte[] szTransformFile); 345 346 353 private static native int msiDatabaseImport(int hDatabase, 354 byte[] folderPath, 355 byte[] txtTableName); 356 357 364 private static native int msiDatabaseApplyTransform( 365 int hDatabase, 366 byte[] transformFile, 367 int iErrorConditions); 368 369 373 private static native byte[] genUUID(); 374 375 383 private static native int updateResourceString( 384 byte[] appFilePath, 385 byte[] contentStr, 386 int resID); 387 388 396 private static native int updateResourceData(byte[] appFilePath, 397 byte[] dataFilePath, 398 int resID); 399 400 405 private static byte[] stringToByteArray(String str) { 406 if (str == null) { 407 return null; 408 } 409 410 byte[] srcByte = str.getBytes(); 411 int srcLength = srcByte.length; 412 byte[] result = new byte[srcLength + 1]; 413 414 System.arraycopy(srcByte, 0, result, 0, srcLength); 415 result[srcLength] = 0; 416 417 return result; 418 } 419 420 425 private static String byteArrayToString(byte[] array) { 426 if (array != null) { 427 String temString = new String (array); 428 429 if (temString != null) { 430 return temString; 431 } 432 } 433 return null; 434 } 435 436 447 public static int[] winMsiOpenDatabase(String databasePath, 448 int persistMode) throws IOException { 449 int[] result = msiOpenDatabase(stringToByteArray(databasePath), 450 persistMode); 451 if (result[0] == ERROR_SUCCESS) { 452 return result; 453 } else { 454 throw new IOException ("MSI Open database failed!"); 455 } 456 } 457 458 467 public static int[] winMsiDatabaseOpenView(int hDatabase, 468 String szQuery) 469 throws IOException { 470 int[] result = msiDatabaseOpenView(hDatabase, 471 stringToByteArray(szQuery)); 472 if (result[0] == ERROR_SUCCESS) { 473 return result; 474 } else { 475 throw new IOException ("MSI Database Open View Failed!"); 476 } 477 } 478 479 485 public static void winMsiViewExecute(int hView, int hRecord) 486 throws IOException { 487 if (msiViewExecute(hView, hRecord) != ERROR_SUCCESS) { 488 throw new IOException ("MSI View Execuation Failed!"); 489 } 490 } 491 492 499 public static int[] winMsiViewFetch(int hView) throws IOException { 500 int[] result = msiViewFetch(hView); 501 if (result[0] == ERROR_SUCCESS) { 502 return result; 503 } else { 504 throw new IOException ("MSI View Fetch failed!"); 505 } 506 } 507 508 514 public static String winMsiRecordGetString(int hRecord, int iField) { 515 byte[] recordBytes = msiRecordGetString(hRecord, iField); 516 if (recordBytes != null) { 517 return byteArrayToString(recordBytes); 518 } else { 519 return null; 520 } 521 } 522 523 530 public static void winMsiRecordSetString(int hRecord, 531 int iField, 532 String valueStr) 533 throws IOException { 534 if (msiRecordSetString(hRecord, iField, stringToByteArray(valueStr)) 535 != ERROR_SUCCESS) { 536 throw new IOException ("MSI Record Set String Failed!"); 537 } 538 } 539 540 547 public static void winMsiViewModify(int hView, int eModifyMode, int hRecord) 548 throws IOException { 549 if (msiViewModify(hView, eModifyMode, hRecord) != ERROR_SUCCESS) { 550 throw new IOException ("MSI View Modification Failed!"); 551 } 552 } 553 554 562 public static int winMsiRecordGetFieldCount(int hRecord) 563 throws IOException { 564 int nFields = msiRecordGetFieldCount(hRecord); 565 if (nFields != -1) { 566 return nFields; 567 } else { 568 throw new IOException ("MSI Record Get Field Count Failed!"); 569 } 570 } 571 572 577 public static void winMsiViewClose(int hView) throws IOException { 578 if (msiViewClose(hView) != ERROR_SUCCESS) { 579 throw new IOException ("MSI View Close Failed!"); 580 } 581 } 582 583 588 public static void winMsiDatabaseCommit(int hDatabase) throws IOException { 589 if (msiDatabaseCommit(hDatabase) != ERROR_SUCCESS) { 590 throw new IOException ("MSI Database Commit Failed!"); 591 } 592 } 593 594 600 public static void winMsiDatabaseApplyTransform(int hDatabase, 601 String transformFile) 602 throws IOException { 603 if (ERROR_SUCCESS 604 != msiDatabaseApplyTransform(hDatabase, 605 stringToByteArray(transformFile), 0)) { 606 throw new IOException ("MSI Database apply tranform failed!"); 607 } 608 } 609 610 615 public static void winMsiCloseHandle(int hAny) throws IOException { 616 if (msiCloseHandle(hAny) != ERROR_SUCCESS) { 617 throw new IOException ("MSI Close Handle Failed!"); 618 } 619 } 620 621 628 public static void winMsiRecordSetStream(int hRecord, 629 int iField, 630 String filePathStr) 631 throws IOException { 632 if (msiRecordSetStream(hRecord, iField, stringToByteArray(filePathStr)) 633 != ERROR_SUCCESS) { 634 throw new IOException ("MSI Record Set Stream Failed!"); 635 } 636 } 637 638 644 public static byte[] winMsiRecordReadStream(int hRecord, int iField) { 645 return msiRecordReadStream(hRecord, iField); 646 } 647 648 653 public static int[] winMsiGetSummaryInformation(int hDatabase) { 654 return msiGetSummaryInformation(hDatabase); 655 } 656 657 664 public static void winMsiSummaryInfoSetProperty(int hSummaryInfo, 665 int uiProperty, 666 String strValue) 667 throws IOException { 668 if (msiSummaryInfoSetProperty(hSummaryInfo, 669 uiProperty, 670 stringToByteArray(strValue)) 671 != ERROR_SUCCESS) { 672 throw new IOException ("MSI SummaryInfo Set Property Failed!"); 673 } 674 } 675 676 681 public static void winMsiSummaryInfoPersist(int hSummaryInfo) 682 throws IOException { 683 if (msiSummaryInfoPersist(hSummaryInfo) != ERROR_SUCCESS) { 684 throw new IOException ("MSI Summaryinfo Persist Failed!"); 685 } 686 } 687 688 693 public static int winMsiCloseAllHandles() { 694 return msiCloseAllHandles(); 695 } 696 697 701 public static String generateUUID() { 702 byte[] newUUID = genUUID(); 703 if (newUUID != null) { 704 return byteArrayToString(newUUID); 705 } else { 706 return null; 707 } 708 } 709 710 717 public static void winMsiDatabaseGenerateTransform(int hDatabase, 718 int hDatabaseReference, 719 String transformFile) 720 throws IOException { 721 int result = msiDatabaseGenerateTransform( 722 hDatabase, 723 hDatabaseReference, 724 stringToByteArray(transformFile)); 725 if (result != ERROR_SUCCESS) { 726 throw new IOException ("MSI Database Generate Transform Failed!"); 727 } 728 } 729 730 738 public static void winMsiCreateTransformSummaryInfo( 739 int hDatabase, 740 int hDatabaseReference, 741 String transformFile) 742 throws IOException { 743 int result = msiCreateTransformSummaryInfo( 744 hDatabase, 745 hDatabaseReference, 746 stringToByteArray(transformFile)); 747 if (result != ERROR_SUCCESS) { 748 throw new IOException ("MSI Database Generate Transform Failed!"); 749 } 750 } 751 752 759 public static void winMsiDatabaseImport( 760 int hDatabase, 761 String folderPath, 762 String txtFileName) 763 throws IOException { 764 int result = msiDatabaseImport(hDatabase, 765 stringToByteArray(folderPath), 766 stringToByteArray(txtFileName)); 767 if (ERROR_SUCCESS != result) { 768 throw new IOException ("MSI Databse import failed!"); 769 } 770 } 771 772 780 public static void winUpdateResourceString( 781 String appFilePath, 782 String contentStr, 783 int resID) 784 throws IOException { 785 int result = updateResourceString(stringToByteArray(appFilePath), 786 stringToByteArray(contentStr), 787 resID); 788 if (result != ERROR_SUCCESS) { 789 throw new IOException ("Windows Update Resource String Failed!"); 790 } 791 } 792 793 800 public static void winUpdateResourceData( 801 String appFilePath, 802 String dataFilePath, 803 int resID) 804 throws IOException { 805 int result = updateResourceData(stringToByteArray(appFilePath), 806 stringToByteArray(dataFilePath), 807 resID); 808 if (result != ERROR_SUCCESS) { 809 throw new IOException ( 810 "Windows Update Resource Binary Data Failed!"); 811 } 812 } 813 814 820 public static int winMsiCreateRecord(int numFields) throws IOException { 821 int msiRecordHandle = msiCreateRecord(numFields); 822 if (msiRecordHandle != -1) { 823 return msiRecordHandle; 824 } else { 825 throw new IOException ("MSI Create Record Failed!"); 826 } 827 } 828 } 829 | Popular Tags |