1 18 19 package sync4j.syncclient.spap; 20 21 import java.sql.Timestamp ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 import java.net.URL ; 27 import java.net.MalformedURLException ; 28 29 import sync4j.syncclient.common.FileSystemTools; 30 import sync4j.syncclient.common.HttpTools; 31 import sync4j.syncclient.common.logging.Logger; 32 import sync4j.syncclient.common.VectorTools; 33 import sync4j.syncclient.common.ZipTools; 34 import sync4j.syncclient.spap.launcher.Launcher; 35 import sync4j.syncclient.spap.launcher.LauncherFactory; 36 import sync4j.syncclient.spap.installer.InstallationContext; 37 import sync4j.syncclient.spdm.DeviceManager; 38 import sync4j.syncclient.spdm.ManagementNode; 39 import sync4j.syncclient.spdm.SimpleDeviceManager; 40 41 42 67 public class AssetManager { 68 69 71 72 private static final String PROPERTY_INSTALLATION_DIRECTORY = "installationDirectory"; 73 74 75 private static final String PROPERTY_BIN_DIRECTORY = "binDirectory"; 76 77 78 private static final String PROPERTY_LIB_DIRECTORY = "libDirectory"; 79 80 81 private static final String PROPERTY_CHECK_PACKAGE_SIZE = "checkSize"; 82 83 84 private static final String ASSET_CONTENT_DIRECTORY = "assetContent"; 85 86 87 private static final String ASSET_FILE_DIRECTORY = "assetFile"; 88 89 90 private static final String CONFIG_CONTEXT_NODE = "spap/assetManager"; 91 92 93 95 98 private String installationDirectory = null; 99 100 106 public void setInstallationDirectory(String installationDirectory) { 107 this.installationDirectory = installationDirectory; 108 } 109 110 113 public String getInstallationDirectory() { 114 return installationDirectory; 115 } 116 117 120 private String binDirectory = null; 121 122 128 public void setBinDirectory(String binDirectory) { 129 this.binDirectory = binDirectory; 130 } 131 132 135 public String getBinDirectory() { 136 return binDirectory; 137 } 138 139 142 private String libDirectory = null; 143 144 150 public void setLibDirectory(String libDirectory) { 151 this.libDirectory = libDirectory; 152 } 153 154 157 public String getLibDirectory() { 158 return libDirectory; 159 } 160 161 164 private boolean checkPackageSize = false; 165 166 public void setCheckPackageSize(boolean checkPackageSize){ 167 this.checkPackageSize = checkPackageSize; 168 } 169 170 public boolean isCheckPackageSize() { 171 return this.checkPackageSize; 172 } 173 174 175 177 178 private static AssetManager instance = null; 179 180 181 private AssetDAO assetDAO = null; 182 183 private Logger logger = new Logger(); 184 185 187 194 private AssetManager() throws IllegalStateException { 195 DeviceManager dm = null; 196 ManagementNode managementNode = null; 197 dm = SimpleDeviceManager.getDeviceManager(); 198 Hashtable values = null; 199 200 try { 201 values = dm.getManagementTree("").getNodeValues(CONFIG_CONTEXT_NODE); 202 203 installationDirectory = (String )values.get(PROPERTY_INSTALLATION_DIRECTORY); 204 binDirectory = (String )values.get(PROPERTY_BIN_DIRECTORY ); 205 libDirectory = (String )values.get(PROPERTY_LIB_DIRECTORY ); 206 207 if (!new File (installationDirectory).isAbsolute()) { 212 installationDirectory = 213 new File ( 214 dm.getDevice().getBaseDirectory(), 215 installationDirectory 216 ).getAbsolutePath(); 217 } 218 if (!new File (binDirectory).isAbsolute()) { 219 binDirectory = new File ( 220 dm.getDevice().getBaseDirectory(), 221 binDirectory 222 ).getAbsolutePath(); 223 } 224 if (!new File (libDirectory).isAbsolute()) { 225 libDirectory = new File ( 226 dm.getDevice().getBaseDirectory(), 227 libDirectory 228 ).getAbsolutePath(); 229 } 230 232 checkPackageSize = Boolean.getBoolean((String )values.get(PROPERTY_CHECK_PACKAGE_SIZE)); 233 } 234 catch (Exception ex) { 235 throw new IllegalStateException ( 236 "Error in AssetManager config (" + ex.getMessage() + ")" 237 ); 238 } 239 assetDAO = new AssetDAO(); 240 } 241 242 244 251 public static AssetManager getAssetManager() throws IllegalStateException { 252 instance = new AssetManager(); 253 return instance; 254 } 255 256 257 258 290 public void installAsset(String idAsset) throws AssetManagementException { 291 292 if (logger.isLoggable(Logger.DEBUG)) { 293 logger.debug("InstallAsset: " + idAsset); 294 } 295 296 Asset asset = assetDAO.getAsset(idAsset); 297 298 if (logger.isLoggable(Logger.DEBUG)) { 299 logger.debug("Asset's state: " + asset.getState()); 300 } 301 302 AssetVersion assetNewVersion = asset.getNewVersion(); 303 304 if (assetNewVersion == null) { 305 throw new AssetManagementException(asset, "New version not found"); 306 } 307 308 String name = asset.getName().toLowerCase(); 309 String manufacturer = asset.getManufacturer().toLowerCase(); 310 String state = asset.getState(); 311 312 String workingDirectory = this.installationDirectory + "/" + manufacturer + "/" + name; 313 314 AssetVersion assetCurrentVersion = asset.getCurrentVersion(); 315 316 byte[] content = null; 317 318 boolean stateRecognized = false; 319 320 if (state.equals(Asset.STATE_UPDATE)) { 321 stateRecognized = true; 322 323 try { 325 verifyAndUninstallPreviousVersion(asset); 326 } 327 catch (Exception ex) { 328 throw new AssetManagementException(asset, "Error during uninstall previous version" , ex); 329 } 330 state = Asset.STATE_PREVIOUS_VERSION_UNINSTALLED; 331 assetDAO.setAssetState(asset, state); 332 } 333 334 if (state.equals(Asset.STATE_NEW) || 335 state.equals(Asset.STATE_PREVIOUS_VERSION_UNINSTALLED)) { 336 337 stateRecognized = true; 338 339 try { 341 content = downloadContentFileForNewVersion(asset); 342 } 343 catch (IOException ex) { 344 throw new AssetManagementException(asset, "Error downloading asset file '" + 345 assetNewVersion.getUrl() + "'", ex); 346 } 347 348 String logMsg = "Size content file: " + 349 assetNewVersion.getSizeContentFile() + 350 ", size downloaded file: " + 351 content.length ; 352 353 if (logger.isLoggable(Logger.DEBUG)) { 354 logger.debug(logMsg); 355 } 356 357 if (checkPackageSize && 359 (content.length != assetNewVersion.getSizeContentFile())) { 360 361 if (logger.isLoggable(Logger.DEBUG)) { 362 logger.debug("Error! Wrong size"); 363 } 364 365 throw new AssetManagementException(asset, 366 "Error downloading content file. Wrong size (" 367 + content.length + ")"); 368 369 370 } 371 372 if (logger.isLoggable(Logger.DEBUG)) { 373 logger.debug("Dimension is correct (or ignored)"); 374 } 375 376 try { 378 ZipTools.verifyZip(content); 379 if (logger.isLoggable(Logger.DEBUG)) { 380 logger.debug("Zip file is valid"); 381 } 382 } 383 catch (IOException ex) { 384 if (logger.isLoggable(Logger.DEBUG)) { 386 logger.debug("Zip file is not valid"); 387 } 388 389 throw new AssetManagementException(asset, "Zip file '" + 390 assetNewVersion.getUrl().toString() + 391 "' downloaded but not appared be a valid file", ex); 392 } 393 394 try { 396 if (logger.isLoggable(Logger.DEBUG)) { 397 logger.debug("Save the content file"); 398 } 399 FileSystemTools.createFile(workingDirectory + 400 File.separator + ASSET_FILE_DIRECTORY, 401 asset.getName() + "_" + assetNewVersion.getVersion() + ".zip", content); 402 403 } 404 catch (Exception ex) { 405 406 throw new AssetManagementException(asset, 407 "Zip file '" + assetNewVersion.getUrl() + 408 "' verified but fault saving process", ex); 409 } 410 411 state = Asset.STATE_FILE_DOWNLOAD; 412 assetDAO.setAssetState(asset, state); 413 414 } 415 416 if (state.equals(Asset.STATE_FILE_DOWNLOAD)) { 417 stateRecognized = true; 418 if (content == null) { 419 try { 420 if (logger.isLoggable(Logger.DEBUG)) { 421 logger.debug("Load the content file from local system"); 422 } 423 content = getAssetPackage(asset); 424 } 425 catch (Exception ex) { 426 throw new AssetManagementException(asset, 427 "Error loading content file '" + 428 asset.getName() + "_" + assetNewVersion.getVersion() + ".zip" + 429 "' from local system", ex); 430 } 431 432 } 433 434 435 String msgLog = "Delete directory '" + 437 workingDirectory + 438 File.separator + 439 ASSET_CONTENT_DIRECTORY + 440 "'" ; 441 442 if (logger.isLoggable(Logger.DEBUG)) { 443 logger.debug(msgLog); 444 } 445 446 try { 447 FileSystemTools.removeDirectoryTree(workingDirectory + File.separator + 448 ASSET_CONTENT_DIRECTORY); 449 } 450 catch (Exception ex) { 451 throw new AssetManagementException(asset, 452 "Error during the cancellation of the directory '" + 453 workingDirectory + File.separator + 454 ASSET_CONTENT_DIRECTORY + "'", ex); 455 } 456 457 458 msgLog = "Extract new version in '" + 460 workingDirectory + 461 File.separator + 462 ASSET_CONTENT_DIRECTORY + 463 "'" ; 464 if (logger.isLoggable(Logger.DEBUG)) { 465 logger.debug(msgLog); 466 } 467 468 try { 469 ZipTools.extract(workingDirectory + File.separator + 470 ASSET_CONTENT_DIRECTORY, content); 471 } 472 catch (Exception ex) { 473 474 throw new AssetManagementException(asset, 475 "Error during the extraction of the zip file '" + 476 assetNewVersion.getUrl() + "'", ex); 477 478 } 479 480 481 new File (workingDirectory + 483 File.separator + ASSET_FILE_DIRECTORY, 484 asset.getName() + "_" + assetNewVersion.getVersion() + ".zip").delete(); 485 486 487 state = Asset.STATE_FILE_EXTRACTED; 488 assetDAO.setAssetState(asset, state); 489 } 490 491 492 if (state.equals(Asset.STATE_FILE_EXTRACTED)) { 493 stateRecognized = true; 494 495 String installProgram = assetNewVersion.getInstallProgram(); 497 if (installProgram != null && !installProgram.equalsIgnoreCase("")) { 498 499 if (logger.isLoggable(Logger.DEBUG)) { 500 logger.debug("Launch the installation program (" + installProgram + ")"); 501 } 502 int exitState = -1; 503 504 try { 505 Launcher launcher = LauncherFactory.getLauncher(installProgram); 506 507 InstallationContext ctx = new InstallationContext(); 508 ctx.setWorkingDirectory( 509 workingDirectory + 510 File.separator + 511 ASSET_CONTENT_DIRECTORY 512 ); 513 ctx.setBinDirectory(binDirectory); 514 ctx.setLibDirectory(libDirectory); 515 ctx.setAsset(asset); 516 517 exitState = launcher.execute(installProgram, true, ctx); 518 519 } catch (AssetInstallationException e) { 520 throw e; 521 } catch (AssetManagementException e) { 522 assetDAO.setAssetAsNotValid(asset, e); 523 throw e; 524 } catch (Throwable t) { 525 assetDAO.setAssetAsNotValid(asset, t); 526 527 throw new AssetManagementException(asset, 528 "Error during the execution of the installation program", t); 529 } 530 531 if (exitState != 0) { 532 throw new AssetManagementException("Installation program exit with state: " +exitState); 533 } 534 535 } 536 537 if (logger.isLoggable(Logger.DEBUG)) { 538 logger.debug("Save dm information"); 539 } 540 541 AssetVersion newVersion = asset.getNewVersion(); 542 543 asset.setCurrentVersion(newVersion); 545 newVersion = null; 546 asset.setNewVersion(newVersion); 547 548 asset.setState(Asset.STATE_ASSET_INSTALLED); 549 assetDAO.setAsset(asset, asset.getLastUpdate() ); 550 } 551 552 553 if (!stateRecognized) { 554 555 throw new AssetManagementException(asset, 556 "Asset not required installation process. State is: '" 557 + state + "'"); 558 } 559 560 if (logger.isLoggable(Logger.DEBUG)) { 561 logger.debug("Installation process finished"); 562 } 563 } 564 565 566 581 public void removeAsset(String idAsset) throws AssetManagementException { 582 583 if (logger.isLoggable(Logger.DEBUG)) { 584 logger.debug("removeAsset: " + idAsset); 585 } 586 587 Asset asset = assetDAO.getAsset(idAsset); 588 AssetVersion currentVersion = null; 589 590 String state = asset.getState(); 591 currentVersion = asset.getCurrentVersion(); 592 593 if (!state.equals(Asset.STATE_DELETE)) { 594 throw new AssetManagementException(asset, "Asset cannot be deleted, state is not '" + 595 Asset.STATE_DELETE + "'"); 596 } 597 String name = asset.getName().toLowerCase(); 598 String manufacturer = asset.getManufacturer().toLowerCase(); 599 600 String workingDirectory = this.installationDirectory + "/" + manufacturer + "/" + name; 601 602 if (currentVersion != null) { 603 String uninstallProgram = currentVersion.getUninstallProgram(); 605 if (uninstallProgram != null && !uninstallProgram.equalsIgnoreCase("")) { 606 607 if (logger.isLoggable(Logger.DEBUG)) { 608 logger.debug("Launch the uninstall program (" + uninstallProgram + ")"); 609 } 610 611 try { 612 Launcher launcher = LauncherFactory.getLauncher(uninstallProgram); 613 614 InstallationContext ctx = new InstallationContext(); 615 ctx.setWorkingDirectory( 616 workingDirectory + 617 File.separator + 618 ASSET_CONTENT_DIRECTORY 619 ); 620 ctx.setBinDirectory(binDirectory); 621 ctx.setLibDirectory(libDirectory); 622 ctx.setAsset(asset); 623 624 launcher.execute(uninstallProgram, false, ctx); 625 } 626 catch (Exception ex) { 627 throw new AssetManagementException(asset, "Error during uninstall process", ex); 628 } 629 } else { 630 if (logger.isLoggable(Logger.DEBUG)) { 631 logger.debug("Uninstall program is null or empty"); 632 } 633 } 634 } else { 635 if (logger.isLoggable(Logger.DEBUG)) { 636 logger.debug("Current version not found"); 637 } 638 } 639 640 641 assetDAO.removeAsset(asset); 643 644 645 if (logger.isLoggable(Logger.DEBUG)) { 647 logger.debug("Remove the asset directory (" + workingDirectory + ")"); 648 } 649 650 try { 651 FileSystemTools.removeDirectoryTree(workingDirectory); 652 } 653 catch (Exception ex) { 654 } 656 657 File file = new File (this.installationDirectory + "/" + manufacturer); 659 file.delete(); 660 661 if (logger.isLoggable(Logger.DEBUG)) { 662 logger.debug("Asset removed"); 663 } 664 665 } 666 667 668 687 public Vector listAssetsForInstallation() { 688 689 String [] states = { 690 Asset.STATE_NEW, 691 Asset.STATE_UPDATE, 692 Asset.STATE_FILE_DOWNLOAD, 693 Asset.STATE_FILE_EXTRACTED, 694 Asset.STATE_PREVIOUS_VERSION_UNINSTALLED, 695 }; 696 697 return listAssets(states); 698 } 699 700 710 public Vector listAssetsForRemoving() { 711 return listAssets(Asset.STATE_DELETE); 712 } 713 714 720 public Vector listAssets(String [] states) { 721 Vector assetsList = new Vector (); 722 Vector temp = null; 723 if (states==null || states.length==0) { 724 assetsList = assetDAO.getAllAsset(); 725 } else { 726 int numState = states.length; 727 for (int i=0; i<numState; i++) { 728 temp = listAssets(states[i]); 729 VectorTools.add(assetsList,temp); 730 } 731 } 732 return assetsList; 733 } 734 735 736 743 public Vector listAssets(String state) { 744 Vector assetsList = null; 745 if (state==null || state.equals("")) { 746 assetsList = assetDAO.getAllAsset(); 747 } else { 748 assetsList = assetDAO.listAsset(state); 749 } 750 751 return assetsList; 752 } 753 754 755 762 public String getAssetState(String idAsset) throws AssetManagementException { 763 return assetDAO.getAssetState(idAsset); 764 } 765 766 774 public void setAssetState(Asset asset, String state) 775 throws AssetManagementException { 776 assetDAO.setAssetState(asset, state); 777 } 778 779 780 787 public Asset getAsset(String idAsset) throws AssetManagementException { 788 return assetDAO.getAsset(idAsset); 789 } 790 791 797 public void setAssetAsNotWanted(String idAsset) throws AssetManagementException { 798 Asset asset = assetDAO.getAsset(idAsset); 799 String state = asset.getState(); 800 801 if (!state.equals(Asset.STATE_NEW) && 802 !state.equals(Asset.STATE_UPDATE) && 803 !state.equals(Asset.STATE_FILE_DOWNLOAD) && 804 !state.equals(Asset.STATE_FILE_EXTRACTED) && 805 !state.equals(Asset.STATE_PREVIOUS_VERSION_UNINSTALLED)) { 806 807 throw new AssetManagementException(asset, "Impossible setting like NOT WISHED" + 808 " (state: " + state + ")"); 809 } 810 811 812 assetDAO.setAssetState(idAsset, Asset.STATE_NEW_VERSION_NOT_WANTED); 813 } 814 815 816 823 public void addAsset(Asset asset) throws AssetManagementException { 824 assetDAO.setAsset(asset, new Timestamp (System.currentTimeMillis())); 825 } 826 827 829 842 private void verifyAndUninstallPreviousVersion(Asset asset) 843 throws AssetInstallationException { 844 845 846 String name = asset.getName().toLowerCase(); 847 String manufacturer = asset.getManufacturer().toLowerCase(); 848 849 String workingDirectory = this.installationDirectory + "/" + manufacturer + "/" + name; 850 851 AssetVersion assetCurrentVersion = asset.getCurrentVersion(); 852 AssetVersion assetNewVersion = asset.getNewVersion(); 853 854 if (assetCurrentVersion != null) { 855 String uninstallProgram = assetCurrentVersion.getUninstallProgram(); 856 if (uninstallProgram != null && !uninstallProgram.equals("")) { 857 if (assetNewVersion.getNeedUninstallPrev().equalsIgnoreCase("Y")) { 858 859 String msgLog = "Uninstall previous version (Uninstall program: " + 860 uninstallProgram + 861 ")" ; 862 863 if (logger.isLoggable(Logger.DEBUG)) { 864 logger.debug(msgLog); 865 } 866 867 869 Launcher launcher = LauncherFactory.getLauncher(uninstallProgram); 870 871 InstallationContext ctx = new InstallationContext(); 872 ctx.setWorkingDirectory( 873 workingDirectory + 874 File.separator + 875 ASSET_CONTENT_DIRECTORY 876 ); 877 ctx.setBinDirectory(binDirectory); 878 ctx.setLibDirectory(libDirectory); 879 ctx.setAsset(asset); 880 881 int exitState = launcher.execute(uninstallProgram, false, ctx); 882 if (exitState != 0) { 883 throw new AssetInstallationException("Uninstallation program exit with state: " +exitState); 884 } 885 } 886 } 887 } 888 } 889 890 891 902 private byte[] downloadContentFileForNewVersion(Asset asset) throws IOException { 903 byte[] content = null; 904 AssetVersion newVersion = asset.getNewVersion(); 905 906 String name = asset.getName(); 907 String manufacturer = asset.getManufacturer(); 908 909 String version = newVersion.getVersion(); 910 String assetUrl = newVersion.getUrl(); 911 912 if (logger.isLoggable(Logger.DEBUG)) { 913 logger.debug("Download asset file: " + assetUrl.toString()); 914 } 915 916 if (HttpTools.isHTTPURL(assetUrl)) { 917 content = HttpTools.downloadPackage(assetUrl); 918 } else { 919 if (assetUrl.startsWith("file:/")) { 920 try { 921 assetUrl = new URL (assetUrl).getFile(); 922 } catch (MalformedURLException e) { 923 } 925 } 926 content = FileSystemTools.getFile(assetUrl); 927 } 928 929 return content; 930 } 931 932 939 private byte[] getAssetPackage(Asset asset) throws Exception { 940 941 AssetVersion assetNewVersion = asset.getNewVersion(); 942 943 if (assetNewVersion == null) { 944 throw new AssetManagementException(asset, "New version not found"); 945 } 946 947 String name = asset.getName().toLowerCase(); 948 String manufacturer = asset.getManufacturer().toLowerCase(); 949 950 String workingDirectory = this.installationDirectory + File.separator + 951 manufacturer + File.separator + name + 952 File.separator + ASSET_FILE_DIRECTORY; 953 954 955 String contentFile = workingDirectory + File.separator + 956 asset.getName() + "_" + assetNewVersion.getVersion() + ".zip"; 957 958 return FileSystemTools.getFile(contentFile); 959 960 } 961 962 963 } | Popular Tags |