1 20 21 package org.jdesktop.jdic.packager.impl; 22 23 import java.util.ArrayList ; 24 import java.util.TreeMap ; 25 import java.util.Iterator ; 26 import java.io.IOException ; 27 28 31 public class WinMsiUtility { 32 33 36 private static final int ERROR_SUCCESS = WinMsiWrapper.ERROR_SUCCESS; 37 40 private static final int ERROR_FAIL = -1; 41 44 private static final int LICENSED_WELCOME_MSG_FIELD_INDEX = 2; 45 48 private static final int NON_LICENSED_WELCOME_MSG_FIELD_INDEX = 3; 49 50 58 public static void setSummaryInfoProperty(String msiFilePath, 59 int uiProperty, 60 String strValue) 61 throws IOException { 62 int hDatabase = 0, hSummaryInfo = 0; 64 try { 65 hDatabase = openDatabase(msiFilePath); 67 hSummaryInfo = openSummaryInfo(hDatabase); 69 WinMsiWrapper.winMsiSummaryInfoSetProperty(hSummaryInfo, 70 uiProperty, 71 strValue); 72 WinMsiWrapper.winMsiSummaryInfoPersist(hSummaryInfo); 73 WinMsiWrapper.winMsiDatabaseCommit(hDatabase); 74 } finally { 75 WinMsiWrapper.winMsiCloseHandle(hSummaryInfo); 76 WinMsiWrapper.winMsiCloseHandle(hDatabase); 77 WinMsiWrapper.winMsiCloseAllHandles(); 78 } 79 } 80 81 93 public static void winMsiSetProperty(String msiFilePath, 94 String tableName, 95 String keyFieldName, 96 int valueIndex, 97 boolean isBinary, 98 TreeMap map) throws IOException { 99 if (tableName == null) { 103 throw new IllegalArgumentException ("MSI table name can't be null"); 104 } 105 if (keyFieldName == null) { 107 throw new IllegalArgumentException ( 108 "MSI key field name can't be null"); 109 } 110 if (valueIndex <= 0) { 112 throw new IllegalArgumentException ("MSI value index can't be null"); 113 } 114 if (map == null) { 116 throw new IllegalArgumentException ("map data can't be null!"); 117 } 118 if (map.size() == 0) { 120 return; 121 } 122 int hDatabase = 0; 126 int hView = 0; 127 int hRecord = 0; 128 boolean recordsReplaced = false; 130 try { 131 hDatabase = openDatabase(msiFilePath); 133 Iterator it = map.keySet().iterator(); 135 String keyName = null; 136 String newKeyValue = null; 137 while (it.hasNext()) { 138 keyName = (String ) it.next(); 139 newKeyValue = (String ) map.get(keyName); 140 if (isBinary) { 141 WinUtility.checkFileValid(newKeyValue); 143 } 144 String fieldNames = "*"; 145 String criterial = " where " 146 + keyFieldName + " = '" + keyName + "'"; 147 hView = openView(hDatabase, tableName, fieldNames, criterial); 148 hRecord = getRecord(hView); 149 if (hRecord != 0) { 150 setRecordProperty(hView, hRecord, valueIndex, 152 isBinary, newKeyValue); 153 recordsReplaced = true; 154 } 155 } 156 if (recordsReplaced) { 160 WinMsiWrapper.winMsiDatabaseCommit(hDatabase); 161 } 162 } finally { 163 closeView(hView); 164 WinMsiWrapper.winMsiCloseHandle(hDatabase); 165 WinMsiWrapper.winMsiCloseAllHandles(); 166 } 167 } 168 169 178 public static void generateTransform(String database, 179 String databaseReference, 180 String transformFile) 181 throws IOException { 182 int hDatabase = 0; 183 int hDatabaseReference = 0; 184 try { 185 hDatabase = openDatabase(database); 186 hDatabaseReference = openDatabase(databaseReference); 187 WinMsiWrapper.winMsiDatabaseGenerateTransform(hDatabase, 188 hDatabaseReference, 189 transformFile); 190 WinMsiWrapper.winMsiCreateTransformSummaryInfo(hDatabase, 191 hDatabaseReference, 192 transformFile); 193 } finally { 194 WinMsiWrapper.winMsiCloseHandle(hDatabase); 195 WinMsiWrapper.winMsiCloseHandle(hDatabaseReference); 196 } 197 } 198 199 207 public static void runSql(String msiFilePath, ArrayList sqlStrings) 208 throws IOException { 209 int hDatabase = 0; 210 int hView = 0; 211 try { 212 hDatabase = openDatabase(msiFilePath); 213 int[] result = new int[] {0, 0}; 214 String sqlString; 215 for (Iterator i = sqlStrings.iterator(); i.hasNext();) { 216 sqlString = (String ) i.next(); 217 result = WinMsiWrapper.winMsiDatabaseOpenView(hDatabase, 218 sqlString); 219 hView = result[1]; 220 WinMsiWrapper.winMsiViewExecute(hView, 0); 221 } 222 WinMsiWrapper.winMsiDatabaseCommit(hDatabase); 223 } finally { 224 closeView(hView); 225 WinMsiWrapper.winMsiCloseHandle(hDatabase); 226 WinMsiWrapper.winMsiCloseAllHandles(); 227 } 228 } 229 230 245 public static void addBinaryRecord(String msiFilePath, 246 String tableName, 247 String [] fieldNames, 248 String [] fieldProperties, 249 String [] fieldValues) 250 throws IOException { 251 int hMsiDatabase = 0; 252 int hView = 0; 253 int hRecord = 0; 254 try { 255 hMsiDatabase = openDatabase(msiFilePath); 256 String fieldsName = fieldNames[0]; 257 for (int i = 1; i < fieldNames.length; i++) { 258 fieldsName += ", " + fieldNames[i]; 259 } 260 hView = openView(hMsiDatabase, tableName, fieldsName, ""); 261 hRecord = WinMsiWrapper.winMsiCreateRecord(fieldNames.length); 262 for (int i = 0; i < fieldNames.length; i++) { 263 if ((fieldProperties[i].compareToIgnoreCase("String")) == 0) { 264 WinMsiWrapper.winMsiRecordSetString(hRecord, i + 1, 265 fieldValues[i]); 266 WinMsiWrapper.winMsiViewExecute(hView, hRecord); 267 } 268 if ((fieldProperties[i].compareToIgnoreCase("Stream")) == 0) { 269 WinMsiWrapper.winMsiRecordSetStream(hRecord, i + 1, 270 fieldValues[i]); 271 WinMsiWrapper.winMsiViewModify( 272 hView, 273 WinMsiWrapper.MSIMODIFY_ASSIGN, 274 hRecord); 275 } 276 } 277 WinMsiWrapper.winMsiDatabaseCommit(hMsiDatabase); 278 } finally { 279 WinMsiWrapper.winMsiCloseHandle(hRecord); 280 closeView(hView); 281 WinMsiWrapper.winMsiCloseHandle(hMsiDatabase); 282 } 283 } 284 292 public static void incorporateMST(String msiFilePath, 293 String mstFilePath, 294 String fieldName) 295 throws IOException { 296 String [] fieldNames = new String [] {"Name", "Data"}; 297 String tableName = "_Storages"; 298 String [] fieldProperties = new String [] {"String", "Stream"}; 299 String [] fieldValues = new String [] {fieldName, mstFilePath}; 300 addBinaryRecord(msiFilePath, tableName, fieldNames, 301 fieldProperties, fieldValues); 302 } 303 304 311 public static void importTableFromFile(String msiFilePath, 312 String folderPath, 313 String txtTableName) 314 throws IOException { 315 int hDatabase = 0; 316 try { 317 hDatabase = openDatabase(msiFilePath); 318 WinMsiWrapper.winMsiDatabaseImport(hDatabase, 319 folderPath, 320 txtTableName); 321 WinMsiWrapper.winMsiDatabaseCommit(hDatabase); 322 } finally { 323 WinMsiWrapper.winMsiCloseHandle(hDatabase); 324 } 325 } 326 327 333 public static void applyMstToMsi(String msiFilePath, 334 String mstFilePath) 335 throws IOException { 336 int[] result = new int[] {0, 0}; 337 try { 338 result = WinMsiWrapper.winMsiOpenDatabase( 339 msiFilePath, 340 WinMsiWrapper.MSIDBOPEN_TRANSACT); 341 int hDatabase = result[1]; 343 WinMsiWrapper.winMsiDatabaseApplyTransform(hDatabase, mstFilePath); 344 WinMsiWrapper.winMsiDatabaseCommit(hDatabase); 345 WinMsiWrapper.winMsiCloseHandle(hDatabase); 346 } catch (IOException e) { 347 System.out.println(e); 348 } 349 } 350 351 360 private static void setRecordProperty(int hView, 361 int hRecord, 362 int valueIndex, 363 boolean isBinary, 364 String newValue) 365 throws IOException { 366 try { 367 if (isBinary) { 368 WinMsiWrapper.winMsiRecordSetStream(hRecord, 369 valueIndex, 370 newValue); 371 } else { 372 WinMsiWrapper.winMsiRecordSetString(hRecord, 373 valueIndex, 374 newValue); 375 } 376 WinMsiWrapper.winMsiViewModify(hView, 377 WinMsiWrapper.MSIMODIFY_REPLACE, 378 hRecord); 379 } finally { 380 WinMsiWrapper.winMsiCloseHandle(hRecord); 381 } 382 } 383 384 390 private static int openDatabase(String msiFilePath) throws IOException { 391 int[] result = new int[] {ERROR_FAIL, 0}; 392 result = WinMsiWrapper.winMsiOpenDatabase( 393 msiFilePath, 394 WinMsiWrapper.MSIDBOPEN_TRANSACT); 395 return result[1]; 397 } 398 399 405 private static int openSummaryInfo(int hDatabase) throws IOException { 406 int[] result = new int[] {ERROR_FAIL, 0}; 407 result = WinMsiWrapper.winMsiGetSummaryInformation(hDatabase); 408 return result[1]; 410 } 411 412 421 private static int openView(int hDatabase, 422 String tableName, 423 String fieldNames, 424 String criterial) 425 throws IOException { 426 int[] result = new int[] {ERROR_FAIL, 0}; 427 String sqlSelectStr = "select " + fieldNames + " from " + tableName 429 + " " + criterial; 430 result = WinMsiWrapper.winMsiDatabaseOpenView(hDatabase, sqlSelectStr); 431 int hView = result[1]; 432 WinMsiWrapper.winMsiViewExecute(hView, 0); 433 return hView; 434 } 435 436 441 private static void closeView(int hView) throws IOException { 442 WinMsiWrapper.winMsiViewClose(hView); 443 WinMsiWrapper.winMsiCloseHandle(hView); 444 } 445 446 452 private static int getRecord(int hView) throws IOException { 453 int[] result; 454 result = WinMsiWrapper.winMsiViewFetch(hView); 455 if (result[0] == WinMsiWrapper.ERROR_NO_MORE_ITEMS) { 456 return 0; 458 } else { 459 return result[1]; 461 } 462 } 463 464 472 public static String getWelcomeMsg(String msiFilePath, 473 String locale, 474 boolean isShowLicense) 475 throws IOException { 476 int hDatabase = 0; 477 int hView = 0; 478 int hRecord = 0; 479 try { 480 hDatabase = openDatabase(msiFilePath); 481 String tableName = 482 MsiPackageGenerator.LOCALIZED_WELCOME_MSG_TABLE_NAME; 483 String fieldNames = "*"; 484 String criterial = " where " 485 + "Locale = '" 486 + locale 487 + "'"; 488 hView = openView(hDatabase, tableName, fieldNames, criterial); 489 hRecord = getRecord(hView); 490 String welcomeMsg = null; 491 if (isShowLicense) { 492 welcomeMsg = WinMsiWrapper.winMsiRecordGetString( 493 hRecord, 494 LICENSED_WELCOME_MSG_FIELD_INDEX); 495 } else { 496 welcomeMsg = WinMsiWrapper.winMsiRecordGetString( 497 hRecord, 498 NON_LICENSED_WELCOME_MSG_FIELD_INDEX); 499 } 500 return welcomeMsg; 501 } finally { 502 WinMsiWrapper.winMsiCloseHandle(hRecord); 503 closeView(hView); 504 WinMsiWrapper.winMsiCloseHandle(hDatabase); 505 } 506 } 507 508 513 public static String genUUID() throws IOException { 514 String oneUUID = WinMsiWrapper.generateUUID(); 515 if (oneUUID != null) { 516 return oneUUID.toUpperCase(); 517 } else { 518 throw new IOException ("Could not generate the UUID."); 519 } 520 } 521 } 522 | Popular Tags |