1 6 7 package SOFA.SOFAnet.Core; 8 9 import SOFA.SOFAnet.Repository.*; 10 import SOFA.SOFAnet.TR.TRInterface; 11 import SOFA.SOFAnode.TR.Impl.*; 12 import SOFA.SOFAnode.TR.*; 13 import SOFA.SOFAnet.Transport.*; 14 import SOFA.SOFAnet.Search.SearchReplyItem; 15 import java.io.*; 16 import java.util.*; 17 18 23 public class LocalOps 24 { 25 private TRInterface tr; 26 private TransportInterface transport; 27 private Repository rep; 28 private NetOps netOps; 29 private ShareOps shareOps; 30 private SearchOps searchOps; 31 32 33 public LocalOps() 34 { 35 } 36 37 public void setRepository(Repository repository) 38 { 39 rep = repository; 40 } 41 42 public void setTRInterface(TRInterface tr) 43 { 44 this.tr = tr; 45 } 46 47 public void setTransportInterface(TransportInterface transport) 48 { 49 this.transport = transport; 50 } 51 52 public void setNetOps(NetOps netOps) 53 { 54 this.netOps = netOps; 55 } 56 57 public void setShareOps(ShareOps shareOps) 58 { 59 this.shareOps = shareOps; 60 } 61 62 public void setSearchOps(SearchOps searchOps) 63 { 64 this.searchOps = searchOps; 65 } 66 67 73 public boolean isBundleInstalled(String bundleName) 74 { 75 LocalInfos localInfos = rep.getLocalInfos(); 76 synchronized (localInfos) 77 { 78 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 79 if (localInfo == null) return false; 80 return localInfo.isInstalled(); 81 } 82 } 83 84 90 public void installBundle(String bundleName, boolean reinstall) throws CoreException 91 { 92 Reporter.startInfo("Installing bundle '" + bundleName + "'"); 93 94 BundleInfo bundleInfo = new BundleInfo(); 95 try 96 { 97 bundleInfo.fromBundleName(bundleName); 98 } 99 catch (BundleInfo.InvalidBundleNameException e) 100 { 101 throw new CoreException("Installation of bundle '" + bundleName + "' failed: Invalid name of bundle", e); 102 } 103 104 BinBundles binBundles = rep.getBinBundles(); 105 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 106 LocalInfos localInfos = rep.getLocalInfos(); 107 Licences licences = rep.getLicences(); 108 BinBundles shareClientCache = rep.getShareClientCache(); 109 110 synchronized (binBundles) { 111 synchronized (installedBundleContents) { 112 synchronized (localInfos) { 113 synchronized (licences) { 114 synchronized (shareClientCache) 115 { 116 BundleInfo binBundle = null; 117 118 119 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 121 if (localInfo != null && localInfo.areShareClientModes()) 122 { 123 if (localInfo.isShareClient()) 124 { 125 binBundle = shareClientCache.getBinBundle(bundleName); 127 if (binBundle == null) throw new CoreException("Installation of bundle '" + bundleName + "' failed: Cannot find binary version of bundle in share client cache"); 128 } 129 else throw new CoreException("Installation of bundle '" + bundleName + "' failed: Bundle has pre-share client active."); 130 } 131 else 132 { 133 binBundle = binBundles.getBinBundle(bundleName); 135 if (binBundle == null) throw new CoreException("Installation of bundle '" + bundleName + "' failed: Cannot find binary version of bundle"); 136 } 137 138 139 if (isBundleInstalled(bundleName)) 141 { 142 if (reinstall) uninstallBundle(bundleName, true, false, true); 143 else throw new CoreException("Installation of bundle '" + bundleName + "' failed: Bundle is already installed"); 144 } 145 146 if (localInfo != null) 148 { 149 151 Licence licence = localInfo.getLicence(); 152 if (!licence.isValid()) throw new CoreException("Installation of bundle '" + bundleName + "' failed: Licence is not valid (expired)"); 153 154 localInfo.setInstalled(true); 155 localInfo.saveToStorage(); 156 } 157 else 158 { 159 161 Licence licence = null; 163 Licence lic = licences.getLicence(bundleName); 164 165 if (lic != null) 166 { 167 if (!lic.isValid()) throw new CoreException("Installation of bundle '" + bundleName + "' failed: Licence is not valid (expired)"); 169 if (lic.getNumberOfCopies() == 0) throw new CoreException("Installation of bundle '" + bundleName + "' failed: No licence copy available in licence file"); 170 171 licence = (Licence)lic.clone(); 172 173 if (licence.getNumberOfCopies() == Licence.WITHOUT_COPIES) 175 { 176 } 178 else 179 { 180 licence.setNumberOfCopies(1); 181 lic.decreaseNumberOfCopies(1); 182 lic.saveToStorage(); 183 } 184 } 185 else licence = new Licence(); 186 187 localInfo = localInfos.addLocalInfo(bundleName, licence, LocalInfo.STATE_INSTALLED, null, null, false, ""); 188 } 189 190 191 BundleImpl bundle = new BundleImpl(); 193 try 194 { 195 bundle._read(new FileInputStream(binBundle.getFile())); 196 } 197 catch (IOException e) 198 { 199 throw new CoreException("Installation of bundle '" + bundleName + "' failed while decompressing bundle", e); 200 } 201 202 203 if (installedBundleContents.getBundleContent(bundleName) != null) 205 { 206 installedBundleContents.deleteBundleContent(bundleName); 208 } 209 210 installedBundleContents.addBundleContent(bundleName, bundle.getComponents()); 211 212 213 try 215 { 216 tr.storeBundle(bundle); 217 } 218 catch (TRException e) 219 { 220 throw new CoreException("Installation of bundle '" + bundleName + "' failed while storing bundle to TR", e); 221 } 222 223 bundle = null; 224 225 } } } } } 231 Reporter.stopInfo("Bundle '" + bundleName + "' successfully installed"); 232 } 233 234 242 public void uninstallBundle(String bundleName, boolean deleteComponents, boolean includingInterfaces, boolean force) throws CoreException 243 { 244 Reporter.startInfo("Uninstalling bundle '" + bundleName + "'"); 245 246 BundleInfo bundleInfo = new BundleInfo(); 247 try 248 { 249 bundleInfo.fromBundleName(bundleName); 250 } 251 catch (BundleInfo.InvalidBundleNameException e) 252 { 253 throw new CoreException("Uninstallation of bundle '" + bundleName + "' failed: Invalid name of bundle", e); 254 } 255 256 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 257 LocalInfos localInfos = rep.getLocalInfos(); 258 Licences licences = rep.getLicences(); 259 260 synchronized (installedBundleContents) { 261 synchronized (localInfos) { 262 synchronized (licences) 263 { 264 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 266 if (localInfo != null) 267 { 268 if (localInfo.isInMemory()) throw new CoreException("Uninstallation of bundle '" + bundleName + "' failed: Bundle in use (components loaded in memory)"); 269 270 if (!force && !localInfo.isInstalled()) throw new CoreException("Uninstallation of bundle '" + bundleName + "' failed: Bundle is not installed"); 271 272 localInfo.setInstalled(false); 273 if (localInfo.getState() == LocalInfo.STATE_NONE) 274 { 275 Licence licence = localInfo.getLicence(); 277 Licence lic = licences.getLicence(bundleName); 278 if (lic != null && licence.withCopies() && lic.withCopies()) 279 { 280 lic.increaseNumberOfCopies(licence.getNumberOfCopies()); 282 lic.saveToStorage(); 283 } 284 285 localInfos.deleteLocalInfo(bundleName); 286 } 287 else 288 { 289 localInfo.saveToStorage(); 290 } 291 } 292 else 293 { 294 if (force) Reporter.error("Incosistent repository - cannot find LocalInfo for " + bundleName); 295 else throw new CoreException("Uninstallation of bundle '" + bundleName + "' failed: Incosistent repository - cannot find LocalInfo"); 296 } 297 298 BundleContent installedBundleContent = installedBundleContents.getBundleContent(bundleName); 300 if (installedBundleContent != null) 301 { 302 if (deleteComponents) 303 { 304 try 306 { 307 List components = installedBundleContent.getComponents(); 308 tr.deleteComponents((ComponentInfo[])components.toArray(new ComponentInfoImpl[components.size()]), includingInterfaces, false); 309 } 310 catch (TRException e) 311 { 312 if (force) Reporter.error("Incosistent repository - deletion of bundle " + bundleName + " from TR failed"); 313 else throw new CoreException("Uninstallation of bundle '" + bundleName + "' failed while deleting bundle from TR", e); 314 } 315 } 316 317 installedBundleContents.deleteBundleContent(bundleName); 319 } 320 else 321 { 322 if (force) Reporter.error("Incosistent repository - cannot find BundleContent for " + bundleName); 323 else throw new CoreException("Uninstallation of bundle '" + bundleName + "' failed: Incosistent repository - cannot find BundleContent"); 324 } 325 326 } } } 330 Reporter.stopInfo("Bundle '" + bundleName + "' uninstalled"); 331 } 332 333 341 public void createBinaryBundle(String bundleName, boolean overwrite) throws CoreException 342 { 343 Reporter.startInfo("Creating binary bundle '" + bundleName + "'"); 344 345 BundleInfo bundleInfo = new BundleInfo(); 346 try 347 { 348 bundleInfo.fromBundleName(bundleName); 349 } 350 catch (BundleInfo.InvalidBundleNameException e) 351 { 352 throw new CoreException("Creation of binary bundle '" + bundleName + "' failed: Invalid name of bundle", e); 353 } 354 355 BinBundles binBundles = rep.getBinBundles(); 356 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 357 LocalInfos localInfos = rep.getLocalInfos(); 358 359 synchronized (binBundles) { 360 synchronized (installedBundleContents) { 361 synchronized (localInfos) 362 { 363 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 365 if (localInfo == null || !localInfo.isInstalled()) throw new CoreException("Creation of binary bundle '" + bundleName + "' failed: Bundle is not installed"); 366 if (localInfo != null && localInfo.areShareClientModes()) throw new CoreException("Creation of binary bundle '" + bundleName + "' failed: Creation of binary version of bundle provided by share client is forbidden"); 367 368 BundleContent installedBundleContent = installedBundleContents.getBundleContent(bundleName); 370 if (installedBundleContent == null) throw new CoreException("Creation of binary bundle '" + bundleName + "' failed: Cannot find installed version of bundle"); 371 372 BundleImpl bundle = null; 374 try 375 { 376 List components = installedBundleContent.getComponents(); 377 bundle = (BundleImpl) tr.getBundle(null, (ComponentInfo[])components.toArray(new ComponentInfoImpl[components.size()]), false); 378 } 379 catch (TRException e) 380 { 381 throw new CoreException("Creation of binary bundle '" + bundleName + "' failed while getting bundle from TR", e); 382 } 383 384 BundleInfo binBundle = binBundles.getBinBundle(bundleName); 386 if (binBundle != null) 387 { 388 if (!overwrite) throw new CoreException("Creation of binary bundle '" + bundleName + "' failed: Binary version of bundle already exists"); 389 } 390 else 391 { 392 binBundle = binBundles.addBinBundle(bundleName); 394 if (binBundle == null) throw new CoreException("Creation of binary bundle '" + bundleName + "' failed: Cannot create BinBundle structure"); 395 } 396 397 try 398 { 399 FileOutputStream os = new FileOutputStream(binBundle.getFile()); 400 bundle._write(os); 401 os.close(); 402 } 403 catch (Exception e) 404 { 405 binBundles.deleteBinBundle(bundleName); 406 throw new CoreException("Creation of binary bundle '" + bundleName + "' failed while saving binary bundle to file", e); 407 } 408 409 bundle = null; 410 411 } } } 415 String myNodeName = NodeInfo.getLocalNodeName(); 416 417 netOps.performInputTriggers(bundleName, myNodeName, InputTrigger.SOURCE_MADE, false); 418 419 Reporter.stopInfo("Binary version of bundle '" + bundleName + "' created"); 420 } 421 422 428 BundleData recreateBundleFromTR(String bundleName) 429 { 430 BundleInfo bundleInfo = new BundleInfo(); 431 try 432 { 433 bundleInfo.fromBundleName(bundleName); 434 } 435 catch (BundleInfo.InvalidBundleNameException e) 436 { 437 return null; 438 } 439 440 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 441 442 synchronized (installedBundleContents) 443 { 444 BundleContent installedBundleContent = installedBundleContents.getBundleContent(bundleName); 446 if (installedBundleContent == null) return null; 447 448 BundleImpl bundle = null; 450 try 451 { 452 List components = installedBundleContent.getComponents(); 453 bundle = (BundleImpl) tr.getBundle(null, (ComponentInfo[])components.toArray(new ComponentInfoImpl[components.size()]), false); 454 } 455 catch (TRException e) 456 { 457 return null; 458 } 459 460 File tempFile = new File(BundleData.generateFilename()); 461 462 try 463 { 464 FileOutputStream os = new FileOutputStream(tempFile); 465 bundle._write(os); 466 os.close(); 467 } 468 catch (Exception e) 469 { 470 bundle = null; 471 return null; 472 } 473 474 bundle = null; 475 476 return new BundleData(tempFile, false); 477 } } 479 480 485 public void deleteBinaryBundle(String bundleName) throws CoreException 486 { 487 Reporter.startInfo("Deleting binary bundle '" + bundleName + "'"); 488 489 BundleInfo bundleInfo = new BundleInfo(); 490 try 491 { 492 bundleInfo.fromBundleName(bundleName); 493 } 494 catch (BundleInfo.InvalidBundleNameException e) 495 { 496 throw new CoreException("Deletion of binary bundle '" + bundleName + "' failed: Invalid name of bundle", e); 497 } 498 499 BinBundles binBundles = rep.getBinBundles(); 500 501 synchronized (binBundles) 502 { 503 BundleInfo binBundle = binBundles.getBinBundle(bundleName); 505 if (binBundle == null) 506 { 507 throw new CoreException("Deletion of binary bundle '" + bundleName + "' failed: Binary version of bundle is not present"); 508 } 509 510 try 511 { 512 boolean deleted = binBundle.getFile().delete(); 513 if (!deleted) throw new CoreException("Deletion of binary bundle '" + bundleName + "' failed: The fail was not deleted"); 514 } 515 catch (Exception e) 516 { 517 throw new CoreException("Deletion of binary bundle '" + bundleName + "' failed while deleting the file", e); 518 } 519 520 binBundles.deleteBinBundle(bundleName); 521 522 523 } 525 Reporter.stopInfo("Binary version of bundle '" + bundleName + "' deleted"); 526 } 527 528 534 public void deleteBundle(String bundleName) throws CoreException 535 { 536 Reporter.startInfo("Deleting bundle '" + bundleName + "'"); 537 538 BinBundles binBundles = rep.getBinBundles(); 539 LocalInfos localInfos = rep.getLocalInfos(); 540 Licences licences = rep.getLicences(); 541 542 synchronized (binBundles) { 543 synchronized (localInfos) { 544 synchronized (licences) 545 { 546 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 547 if (localInfo != null) 548 { 549 throw new CoreException("Deletion of bundle '" + bundleName + "' failed: Bundle is installed and/or shared"); 550 } 551 552 binBundles.deleteBinBundle(bundleName); 553 licences.deleteLicence(bundleName); 554 } } } 558 Reporter.stopInfo("Bundle '" + bundleName + "' deleted (plus its potential licence file)"); 559 } 560 561 562 576 public String createBundle(String bundleName, ComponentInfo[] components, boolean subcomponents) throws CoreException 577 { 578 if (components == null || components.length == 0 || 580 bundleName.length() == 0 && components.length > 1 ) 581 { 582 throw new CoreException("Creation of bundle '" + bundleName + "' failed: Invalid parameters"); 583 } 584 585 boolean componentBundle = false; 586 587 if (bundleName.length() == 0) 588 { 589 componentBundle = true; 590 bundleName = BundleInfo.makeComponentBundleName(components[0].getName(), components[0].getImplementationVersion(), subcomponents); 591 } 592 593 Reporter.startInfo("Creating bundle '" + bundleName + "'"); 594 595 BundleInfo bundleInfo = new BundleInfo(); 596 try 597 { 598 bundleInfo.fromBundleName(bundleName); 599 } 600 catch (BundleInfo.InvalidBundleNameException e) 601 { 602 throw new CoreException("Creation of bundle '" + bundleName + "' failed: Invalid name of bundle", e); 603 } 604 605 if (componentBundle != bundleInfo.isComponent()) 606 { 607 throw new CoreException("Creation of bundle '" + bundleName + "' failed: Invalid parameters"); 608 } 609 610 BundleImpl bundle = null; 612 try 613 { 614 bundle = (BundleImpl) tr.getBundle(components, null, subcomponents); 615 } 616 catch (TRException e) 617 { 618 new CoreException("Creation of bundle '" + bundleName + "' failed while getting information about components from TR", e); 619 } 620 621 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 622 LocalInfos localInfos = rep.getLocalInfos(); 623 624 synchronized (installedBundleContents) { 625 synchronized (localInfos) 626 { 627 if (isBundleInstalled(bundleName)) 628 { 629 throw new CoreException("Creation of bundle '" + bundleName + "' failed: Bundle is already installed"); 630 } 631 632 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 634 if (localInfo != null) 635 { 636 if (localInfo.areShareClientModes()) new CoreException("Creation of bundle '" + bundleName + "' failed: Share client active for bundle of that name"); 637 localInfo.setInstalled(true); 638 localInfo.saveToStorage(); 639 } 640 else 641 { 642 localInfos.addLocalInfo(bundleName, new Licence(), LocalInfo.STATE_INSTALLED, null, null, false, ""); 643 } 644 645 installedBundleContents.deleteBundleContent(bundleName); 650 ComponentInfo[] bundleComponents = bundle.getComponents(); 651 ComponentInfoImpl[] myComponents = new ComponentInfoImpl[bundleComponents.length]; 652 for (int i = 0; i < bundleComponents.length; i++) 653 { 654 myComponents[i] = new ComponentInfoImpl(bundleComponents[i].getName(), bundleComponents[i].getImplementationVersion()); 655 } 656 657 installedBundleContents.addBundleContent(bundleName, myComponents); 658 659 } } 662 bundle = null; 663 664 Reporter.stopInfo("Bundle '" + bundleName + "' created"); 665 return bundleName; 666 } 667 668 675 public void createUserBundle(String bundleName, ComponentInfo[] components, boolean subcomponents) throws CoreException 676 { 677 if (bundleName.length() == 0) throw new CoreException("Creation of user bundle failed: Empty name of user bundle"); 678 createBundle(bundleName, components, subcomponents); 679 } 680 681 688 public String createSingleComponentBundle(ComponentInfo component) throws CoreException 689 { 690 ComponentInfo[] components = new ComponentInfo[1]; 691 components[0] = component; 692 return createBundle("", components, false); 693 } 694 695 703 public String createCompleteComponentBundle(ComponentInfo component) throws CoreException 704 { 705 ComponentInfo[] components = new ComponentInfo[1]; 706 components[0] = component; 707 return createBundle("", components, true); 708 } 709 710 721 BundleData createBundleFromTR(ComponentInfo component, boolean subcomponents) throws CoreException 722 { 723 BundleImpl listBundle = null; 725 try 726 { 727 listBundle = (BundleImpl) tr.list(); 728 } 729 catch (TRException e) 730 { 731 new CoreException("Creation of bundle of component '" + component.getName() + "[" + component.getImplementationVersion() + "]' failed while listing TR", e); 732 } 733 734 if (!listBundle.contains(component)) return null; 735 736 737 BundleImpl bundle = null; 739 ComponentInfo[] components = new ComponentInfo[1]; 740 components[0] = component; 741 try 742 { 743 bundle = (BundleImpl) tr.getBundle(null, components, subcomponents); 744 } 745 catch (TRException e) 746 { 747 new CoreException("Creation of bundle of component '" + component.getName() + "[" + component.getImplementationVersion() + "]' failed while getting component from TR", e); 748 } 749 750 File tempFile = new File(BundleData.generateFilename()); 752 753 try 754 { 755 FileOutputStream os = new FileOutputStream(tempFile); 756 bundle._write(os); 757 os.close(); 758 } 759 catch (Exception e) 760 { 761 bundle = null; 762 new CoreException("Creation of bundle of component '" + component.getName() + "[" + component.getImplementationVersion() + "]' failed while creating BundleData", e); 763 } 764 765 bundle = null; 766 767 return new BundleData(tempFile, false); 768 } 769 770 771 780 boolean testComponentPresenceInTR(ComponentInfo component, boolean subcomponents) 781 { 782 return tr.containsComponent(component, subcomponents); 783 } 784 785 791 ComponentInfo[] listComponentsPresentInTR() 792 { 793 return tr.list().getComponents(); 794 } 795 796 812 public int acquireComponent(String componentFullName) 813 { 814 String bundleName; 815 boolean shareManager = false; 816 NodeNameList shareClients = null; 817 818 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 819 CompBundleMap compBundleMap = installedBundleContents.getCompBundleMap(); 820 LocalInfos localInfos = rep.getLocalInfos(); 821 822 boolean secondRound = false; 823 for (;;) 824 { 825 synchronized (installedBundleContents) { 826 synchronized (localInfos) 827 { 828 bundleName = compBundleMap.findBundle(componentFullName); 830 if (bundleName == null) 831 { 832 Reporter.info("Load of component '" + componentFullName + "' allowed (no bundle info)"); 833 return 1; 834 } 835 836 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 838 if (localInfo == null || !localInfo.isInstalled()) 839 { 840 Reporter.warning("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' PROHIBITED: The bundle int not installed"); 841 return 3; 842 } 843 844 if (localInfo.isInMemory()) 846 { 847 localInfo.increaseInMemoryCounter(); 848 Reporter.info("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' allowed (bundle already in memory)"); 849 return 0; 850 } 851 852 854 Licence licence = localInfo.getLicence(); 855 if (!licence.isValid()) 857 { 858 Reporter.warning("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' PROHIBITED: Licence is not valid (expired)"); 859 return 3; 860 } 861 862 if (!licence.withCopies() || licence.getNumberOfCopies() > 0) 864 { 865 if (licence.withCopies()) licence.decreaseNumberOfCopies(1); 866 localInfo.setInMemoryCounter(1); 867 localInfo.saveToStorage(); 868 Reporter.info("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' allowed (bundle loaded to memory)"); 869 return 0; 870 } 871 872 874 if (!secondRound && licence.getType() == Licence.TYPE_ACTIVE && ((localInfo.isShareManager() && localInfo.getSMClients().getList().size() > 0) || localInfo.isShareClient())) 875 { 876 if (localInfo.isShareManager()) 878 { 879 shareManager = true; 880 shareClients = (NodeNameList)localInfo.getSMClients().clone(); 881 } 882 else 883 { 884 shareManager = false; 885 } 886 } 887 else 888 { 889 Reporter.warning("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' PROHIBITED: Licence copy not available"); 891 return 3; 892 } 893 894 } } 897 898 if (shareManager) 900 { 901 903 IOParams ioParams = new IOParams(); 904 ioParams.setBundleName(bundleName); 905 ioParams.setLicenceOnly(true); 906 907 shareOps.makeShareClientsReturnLicence(ioParams, bundleName, null, shareClients, null, "Returning of licence to share manager for bundle"); 908 909 int errCode = ioParams.getErrCode(); 910 911 if (errCode == 2) 912 { 913 Reporter.warning("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' PROHIBITED: Licence copy not available"); 914 return 3; 915 } 916 917 if (errCode != 0) 918 { 919 Reporter.error("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' PROHIBITED: Incosistent state of share manager"); 920 return 3; 921 } 922 923 } 924 else 925 { 926 928 try 929 { 930 shareOps.acquireSharedBundleLicence(bundleName); 931 } 932 catch (CoreException e) 933 { 934 Reporter.error(e); 935 Reporter.warning("Load of component '" + componentFullName + "' from bundle '" + bundleName + "' PROHIBITED: Acquisition of licence from share manager failed"); 936 return 3; 937 } 938 } 939 940 secondRound = true; 941 } 942 } 943 944 956 public int releaseComponent(String componentFullName) 957 { 958 BundleContents installedBundleContents = rep.getInstalledBundleContents(); 959 CompBundleMap compBundleMap = installedBundleContents.getCompBundleMap(); 960 LocalInfos localInfos = rep.getLocalInfos(); 961 962 synchronized (installedBundleContents) { 963 synchronized (localInfos) 964 { 965 String bundleName = compBundleMap.findBundle(componentFullName); 967 if (bundleName == null) 968 { 969 Reporter.info("Component '" + componentFullName + "' released (no bundle info)"); 970 return 0; 971 } 972 973 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 975 if (localInfo == null || !localInfo.isInstalled() || !localInfo.isInMemory()) 976 { 977 Reporter.error("Component '" + componentFullName + "' from bundle '" + bundleName + "' released, but SOFAnet repository in incosistent state"); 978 return 1; 979 } 980 981 localInfo.decreaseInMemoryCounter(); 982 if (!localInfo.isInMemory()) 983 { 984 Licence licence = localInfo.getLicence(); 986 if (licence.withCopies()) licence.increaseNumberOfCopies(1); 987 localInfo.saveToStorage(); 988 Reporter.info("Component '" + componentFullName + "' from bundle '" + bundleName + "' released (bundle released from memory)"); 989 } 990 else 991 { 992 Reporter.info("Component '" + componentFullName + "' from bundle '" + bundleName + "' released (bundle remains in memory)"); 993 } 994 995 return 0; 996 997 } } } 1000 1001 1029 public int obtainMissingComponent(String componentFullName) throws CoreException 1030 { 1031 Reporter.startInfo("Obtaining missing component '" + componentFullName + "' using SOFAnet"); 1032 1033 BinBundles binBundles = rep.getBinBundles(); 1034 LocalInfos localInfos = rep.getLocalInfos(); 1035 BundleOffers bundleOffers = rep.getBundleOffers(); 1036 CompBundleMap offersCompBundleMap = bundleOffers.getCompBundleMap(); 1037 BinBundles shareClientCache = rep.getShareClientCache(); 1038 BundleContents sharedBundleContents = rep.getSharedBundleContents(); 1039 CompBundleMap sharedCompBundleMap = sharedBundleContents.getCompBundleMap(); 1040 1041 String bundleName = ""; 1042 boolean returnLicenceAndBundleFromShareClient = false; 1043 NodeNameList shareClients = null; 1044 boolean acquireSharedBundle = false; 1045 1046 class InstallException extends Exception {} 1047 1048 try 1049 { 1050 synchronized (binBundles) { 1052 synchronized (localInfos) { 1053 synchronized (shareClientCache) { 1054 synchronized (sharedBundleContents) 1055 { 1056 1060 bundleName = sharedCompBundleMap.findBundle(componentFullName); 1061 if (bundleName != null) 1062 { 1063 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 1065 if (localInfo == null || !localInfo.isShareManager() && !localInfo.isShareClient()) 1066 { 1067 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Missing share manager/share client state of the bundle"); 1068 return 2; 1069 } 1070 1071 if (localInfo.isInstalled()) 1072 { 1073 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Bundle already marked as installed"); 1074 return 1; 1075 } 1076 1077 if (localInfo.isShareManager()) 1078 { 1079 1082 BundleInfo binBundle = binBundles.getBinBundle(bundleName); 1084 if (binBundle != null) 1085 { 1086 throw new InstallException(); 1088 } 1089 1090 shareClients = (NodeNameList)localInfo.getSMClients().clone(); 1091 if (shareClients.getList().size() > 0) 1092 { 1093 returnLicenceAndBundleFromShareClient = true; 1094 } 1095 else 1096 { 1097 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: No share client to return licence&bundle back to share manager"); 1098 return 4; 1099 } 1100 } 1101 else 1102 { 1103 1105 Licence licence = localInfo.getLicence(); 1106 if (!licence.withCopies() || licence.getType() != Licence.TYPE_PHYSICAL || licence.getNumberOfCopies() > 0) 1107 { 1108 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: The share client of bundle active but wrong licence present"); 1109 return 2; 1110 } 1111 1112 acquireSharedBundle = true; 1113 } 1114 } 1115 else 1116 { 1117 Iterator it = binBundles.map.values().iterator(); 1119 while (it.hasNext()) 1120 { 1121 BundleInfo bundleInfo = (BundleInfo)it.next(); 1122 1123 if (bundleInfo.isComponentBundleOf(componentFullName)) 1124 { 1125 bundleName = bundleInfo.getName(); 1127 throw new InstallException(); 1128 } 1129 } 1130 1131 } 1133 } } } } 1138 if (returnLicenceAndBundleFromShareClient) 1139 { 1140 1142 IOParams ioParams = new IOParams(); 1143 ioParams.setBundleName(bundleName); 1144 ioParams.setLicenceOnly(false); 1145 1146 shareOps.makeShareClientsReturnLicence(ioParams, bundleName, null, shareClients, null, "Returning of licence to share manager for bundle"); 1147 1148 int errCode = ioParams.getErrCode(); 1149 1150 if (errCode == 2) 1151 { 1152 Reporter.warning("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Cannot return licence&bundle back from share client"); 1153 return 4; 1154 } 1155 1156 if (errCode != 0) 1157 { 1158 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Incosistent state of share manager"); 1159 return 2; 1160 } 1161 1162 ioParams.setBundleName(bundleName); 1163 ioParams.setNodeName(ioParams.getAddressNodeName()); 1164 ioParams.setPriority(IOParams.PRIORITY_BLOCKING); 1165 1166 ioParams.setAddress(false); 1167 ioParams.setAddressNodeName(""); 1168 1169 try 1170 { 1171 transport.acquireShared(ioParams); 1172 } 1173 catch (TransportException e) 1174 { 1175 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed while calling acquireShared", e); 1176 return 4; 1177 } 1178 1179 if (ioParams.isAddress() || ioParams.getErrCode() != 0) 1180 { 1181 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Unexpected result of acquireShared"); 1182 return 4; 1183 } 1184 1185 BundleData bundleData = ioParams.getBundleData(); 1186 1187 synchronized (binBundles) 1189 { 1190 BundleInfo binBundle = binBundles.getBinBundle(bundleName); 1191 if (binBundle == null) 1192 { 1193 binBundle = binBundles.addBinBundle(bundleName); 1194 if (binBundle != null) 1195 { 1196 if (!bundleData.copyToFile(binBundle.getFile(), true)) 1197 { 1198 bundleData.delete(); 1199 binBundles.deleteBinBundle(bundleName); 1200 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed while copying bundle to binary bundles"); 1201 return 5; 1202 } 1203 } 1204 else 1205 { 1206 Reporter.warning("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Cannot add bundle to binary bundles, bundle already present"); 1207 } 1208 } 1209 else 1210 { 1211 Reporter.warning("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Binary bundle already present"); 1212 } 1213 } 1215 bundleData.delete(); 1216 bundleData = null; 1217 1218 throw new InstallException(); 1219 } 1220 1221 1222 if (acquireSharedBundle) 1223 { 1224 boolean install = false; 1226 try 1227 { 1228 shareOps.acquireSharedBundle(bundleName, null, false); 1229 install = true; 1230 } 1231 catch (CoreException e) 1232 { 1233 if (e.getMessage().endsWith("Not a share manager or access not allowed")) 1234 { 1235 install = false; 1236 } 1239 else 1240 { 1241 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "': Acquisition of shared bundle failed", e); 1242 return 6; 1243 } 1244 } 1245 1246 if (install) throw new InstallException(); 1247 else 1248 { 1249 try 1251 { 1252 shareOps.stopShareClientOfBundle(bundleName, true, false); 1253 } 1254 catch (CoreException e) 1255 { 1256 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "': Cannot stop obsolete share client", e); 1257 return 6; 1258 } 1259 1260 acquireSharedBundle = false; 1261 } 1262 } 1263 1264 1265 1267 boolean tryPullBundle = false; 1268 String offeringNodeName = ""; 1269 1270 synchronized (binBundles) { 1271 synchronized (bundleOffers) 1272 { 1273 bundleName = offersCompBundleMap.findBundle(componentFullName); 1274 if (bundleName != null) 1275 { 1276 BundleOffer bundleOffer = bundleOffers.getBundleOffer(bundleName); 1277 1278 if (bundleOffer == null) 1279 { 1280 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed: Missing bundle offer (incosistency of component-bundle map of offers and offers themselves)"); 1281 return 2; 1282 } 1283 1284 1286 BundleInfo binBundle = binBundles.getBinBundle(bundleName); 1288 if (binBundle != null) 1289 { 1290 throw new InstallException(); 1292 } 1293 1294 tryPullBundle = true; 1295 offeringNodeName = bundleOffer.getNodeName(); 1296 } 1297 } } 1300 if (tryPullBundle) 1301 { 1302 1304 boolean canPull = false; 1305 try 1306 { 1307 canPull = netOps.testPull(bundleName, offeringNodeName, ""); 1308 } 1309 catch (CoreException e) 1310 { 1311 Reporter.warning("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "': Test of pull acceptance failed", e); 1312 } 1313 1314 if (canPull) 1315 { 1316 try 1317 { 1318 netOps.synchroPullBundle(bundleName, offeringNodeName, ""); 1319 } 1320 catch (CoreException e) 1321 { 1322 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "': Pull of bundle failed", e); 1323 return 6; 1324 } 1325 1326 throw new InstallException(); 1328 } 1329 } 1330 1331 1332 1334 SearchReplyItem replyItem = null; 1335 try 1336 { 1337 replyItem = searchOps.searchForBundleWithComponent(componentFullName); 1338 } 1339 catch (CoreException e) 1340 { 1341 Reporter.error("Obtaining of component '" + componentFullName + "failed: Searching failed", e); 1342 return 8; 1343 } 1344 1345 if (replyItem != null) 1346 { 1347 1349 bundleName = replyItem.getBundleName(); 1350 1351 try 1352 { 1353 shareOps.acquireSharedBundle(bundleName, replyItem.getNodeName(), true); 1354 } 1355 catch (CoreException e) 1356 { 1357 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "': Acquisition of shared bundle failed", e); 1358 return 6; 1359 } 1360 1361 throw new InstallException(); 1362 } 1363 1364 1366 } 1367 catch (InstallException ie) 1368 { 1369 try 1371 { 1372 installBundle(bundleName, true); 1373 } 1374 catch (CoreException e) 1375 { 1376 Reporter.error("Obtaining of component '" + componentFullName + "' of bundle '" + bundleName + "' failed", e); 1377 return 3; 1378 } 1379 1380 Reporter.stopInfo("Component '" + componentFullName + "' found in bundle '" + bundleName + "'. Bundle obtained and installed."); 1381 return 0; } 1383 1384 Reporter.stopInfo("Missing component '" + componentFullName + "' not obtained."); 1385 return 9; } 1387} 1388 | Popular Tags |