1 7 package org.mmbase.applications.packaging.packagehandlers; 8 9 import java.io.BufferedInputStream ; 10 import java.io.InputStream ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.jar.JarEntry ; 15 import java.util.jar.JarFile ; 16 17 import org.mmbase.applications.packaging.InstallManager; 18 import org.mmbase.applications.packaging.PackageManager; 19 import org.mmbase.applications.packaging.Person; 20 import org.mmbase.applications.packaging.ShareManager; 21 import org.mmbase.applications.packaging.UninstallManager; 22 import org.mmbase.applications.packaging.bundlehandlers.BundleInterface; 23 import org.mmbase.applications.packaging.installhandlers.installStep; 24 import org.mmbase.applications.packaging.providerhandlers.DiskProvider; 25 import org.mmbase.applications.packaging.providerhandlers.ProviderInterface; 26 import org.mmbase.applications.packaging.util.ExtendedDocumentReader; 27 import org.mmbase.module.builders.Versions; 28 import org.mmbase.module.core.MMBase; 29 import org.mmbase.util.XMLEntityResolver; 30 import org.mmbase.util.logging.Logger; 31 import org.mmbase.util.logging.Logging; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.NamedNodeMap ; 34 import org.xml.sax.InputSource ; 35 36 41 public class BasicPackage implements PackageInterface { 42 private static Logger log = Logging.getLoggerInstance(BasicPackage.class); 43 44 private String name; 45 private String id; 46 private String maintainer; 47 private String version; 48 private String date; 49 private String type = "unknown/unknown"; 50 private String state = "not installed"; 51 private String path; 52 private String description = null; 53 private String releasenotes = ""; 54 private String installationnotes = ""; 55 private String licensename = ""; 56 private String licensetype = ""; 57 private String licenseversion = ""; 58 private String licensebody = ""; 59 private ProviderInterface provider; 60 private installStep bundlestep; 61 private boolean dependsfailed = false; 62 private BundleInterface parentbundle = null; 63 private ArrayList initiators, supporters, contacts, developers; 64 private float progressbar = 0; 65 private float progressstep = 1; 66 67 private ArrayList installsteps; 73 74 private long lastupdated; 75 76 79 public final static String DTD_PACKAGEDEPENDS_1_0 = "packagedepends_1_0.dtd"; 80 83 public final static String DTD_PACKAGE_1_0 = "package_1_0.dtd"; 84 85 88 public final static String PUBLIC_ID_PACKAGEDEPENDS_1_0 = "-//MMBase//DTD packagedepends config 1.0//EN"; 89 92 public final static String PUBLIC_ID_PACKAGE_1_0 = "-//MMBase//DTD package config 1.0//EN"; 93 94 95 96 100 public static void registerPublicIDs() { 101 XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGEDEPENDS_1_0, DTD_PACKAGEDEPENDS_1_0, ShareManager.class); 102 XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGE_1_0, DTD_PACKAGE_1_0, DiskProvider.class); 103 } 104 105 106 109 public BasicPackage() { } 110 111 112 124 public void init(org.w3c.dom.Node n, ProviderInterface provider, String name, String type, String maintainer, String version, String date, String path) { 125 this.name = name; 126 this.version = version; 127 this.date = date; 128 this.maintainer = maintainer; 129 this.provider = provider; 130 this.type = type; 131 this.id = name + "@" + maintainer + "_" + type; 132 this.id = this.id.replace(' ', '_'); 133 this.id = this.id.replace('/', '_'); 134 this.path = path; 135 if (n != null) { 136 addMetaInfo(n); 137 } 138 } 139 140 141 146 public String getId() { 147 if (id == null) { 148 return ""; 149 } 150 return id; 151 } 152 153 154 159 public String getName() { 160 if (name == null) { 161 return ""; 162 } 163 return name; 164 } 165 166 167 172 public String getDescription() { 173 if (description == null) { 174 delayedMetaInfo(); 175 } 176 return description; 177 } 178 179 180 185 public String getInstallationNotes() { 186 return installationnotes; 187 } 188 189 190 195 public String getReleaseNotes() { 196 return releasenotes; 197 } 198 199 200 205 public String getLicenseType() { 206 return licensetype; 207 } 208 209 210 215 public String getLicenseName() { 216 return licensename; 217 } 218 219 220 225 public String getLicenseVersion() { 226 return licenseversion; 227 } 228 229 230 235 public String getLicenseBody() { 236 return licensebody; 237 } 238 239 240 245 public String getVersion() { 246 if (version == null) { 247 return (""); 248 } 249 return version; 250 } 251 252 253 258 public String getCreationDate() { 259 if (date == null) { 260 return ""; 261 } 262 return date; 263 } 264 265 266 271 public String getMaintainer() { 272 if (maintainer == null) { 273 return ""; 274 } 275 return maintainer; 276 } 277 278 279 284 public String getState() { 285 if (InstallManager.isActive()) { 286 if (this == InstallManager.getInstallingPackage()) { 287 return "installing"; 288 } 289 } 290 291 if (UninstallManager.isActive()) { 292 if (this == UninstallManager.getUnInstallingPackage()) { 293 return "uninstalling"; 294 } 295 } 296 297 if (PackageManager.isInstalledVersion(this)) { 298 return "installed"; 299 } 300 301 if (PackageManager.upgradeAvailable(this)) { 302 return "upgrade available"; 303 } 304 305 if (state == null) { 306 return ""; 307 } 308 return state; 309 } 310 311 312 317 public String getType() { 318 if (type == null) { 319 return ""; 320 } 321 return type; 322 } 323 324 325 330 public ProviderInterface getProvider() { 331 return provider; 332 } 333 334 335 341 public boolean setState(String state) { 342 this.state = state; 343 return true; 344 } 345 346 347 352 public boolean install() { 353 log.error("this package doesn't implement the install() call"); 354 return false; 355 } 356 357 358 364 public boolean install(installStep step) { 365 bundlestep = step; 366 return install(); 367 } 368 369 370 375 public boolean uninstall() { 376 log.error("this package doesn't implement the uninstall() call"); 377 return false; 378 } 379 380 381 387 public boolean uninstall(installStep step) { 388 bundlestep = step; 389 return uninstall(); 390 } 391 392 393 398 public installStep getNextInstallStep() { 399 installStep step = null; 400 if (bundlestep != null) { 401 step = bundlestep.getNextInstallStep(); 402 } else { 403 step = new installStep(); 405 } 406 if (installsteps == null) { 407 installsteps = new ArrayList (); 408 installsteps.add(step); 409 return step; 410 } else { 411 installsteps.add(step); 412 return step; 413 } 414 } 415 416 417 422 public Iterator getInstallSteps() { 423 if (installsteps != null) { 424 return installsteps.iterator(); 425 } else { 426 return null; 427 } 428 } 429 430 431 443 449 public Iterator getInstallSteps(int logid) { 450 Iterator e = getInstallSteps(); 452 while (e.hasNext()) { 453 installStep step = (installStep) e.next(); 454 Object o = step.getInstallSteps(logid); 455 if (o != null) { 456 return (Iterator ) o; 457 } 458 } 459 return null; 460 } 461 462 463 466 public void clearInstallSteps() { 467 installsteps = null; 468 } 469 470 471 476 public JarFile getJarFile() { 477 if (provider != null) { 478 if (parentbundle != null) { 479 return parentbundle.getIncludedPackageJarFile(getId(), getVersion()); 480 } else { 481 return provider.getJarFile(getPath(), getId(), getVersion()); 482 } 483 } 484 return null; 485 } 486 487 488 493 public BufferedInputStream getJarStream() { 494 if (provider != null) { 495 return provider.getJarStream(getPath()); 496 } 497 return null; 498 } 499 500 501 506 public String getPath() { 507 return path; 508 } 509 510 511 516 public boolean updateRegistryInstalled() { 517 return PackageManager.updateRegistryInstalled(this); 518 } 519 520 521 526 public boolean updateRegistryUninstalled() { 527 return PackageManager.updateRegistryUninstalled(this); 528 } 529 530 531 534 public void signalUpdate() { 535 lastupdated = System.currentTimeMillis(); 536 } 537 538 539 544 public long lastSeen() { 545 return lastupdated; 546 } 547 548 549 556 public boolean dependsInstalled(JarFile jf, installStep step) { 557 Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions"); 558 dependsfailed = false; 559 if (versions == null) { 560 log.error("Versions builder not installed."); 561 return false; 562 } 563 try { 564 JarEntry je = jf.getJarEntry("depends.xml"); 565 if (je != null) { 566 InputStream input = jf.getInputStream(je); 567 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource (input), BasicPackage.class); 568 for (Iterator ns = reader.getChildElements("packagedepends", "package"); ns.hasNext(); ) { 569 Element n = (Element ) ns.next(); 570 String name = n.getAttribute("name"); 571 String type = n.getAttribute("type"); 572 String version = n.getAttribute("version"); 573 String versionmode = n.getAttribute("versionmode"); 574 String maintainer = n.getAttribute("maintainer"); 575 installStep substep = step.getNextInstallStep(); 577 substep.setUserFeedBack("checking package : " + name + " (" + type + ") version : " + version + " (" + versionmode + ") from : " + maintainer); 578 579 String id = name + "@" + maintainer + "_" + type; 580 id = id.replace(' ', '_'); 581 id = id.replace('/', '_'); 582 int installedversion = versions.getInstalledVersion(id, "package"); 583 if (installedversion == -1) { 586 substep.setUserFeedBack("depends failed package : " + name + " (" + type + ") version : " + version + " (" + versionmode + ") from : " + maintainer); 587 substep.setType(installStep.TYPE_ERROR); 588 dependsfailed = true; 589 return false; 590 } 591 } 592 } else { 593 return true; 594 } 595 } catch (Exception e) { 596 return false; 597 } 598 return true; 599 } 600 601 602 607 public boolean getDependsFailed() { 608 return dependsfailed; 609 } 610 611 612 617 public BundleInterface getParentBundle() { 618 return parentbundle; 619 } 620 621 622 627 public void setParentBundle(BundleInterface parent) { 628 parentbundle = parent; 629 } 630 631 632 635 private void delayedMetaInfo() { 636 JarFile jf = getJarFile(); 639 try { 641 JarEntry je = jf.getJarEntry("package.xml"); 642 if (je != null) { 643 InputStream input = jf.getInputStream(je); 644 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource (input), DiskProvider.class); 645 if (reader != null) { 646 Element e = reader.getElementByPath("package"); 647 addMetaInfo(e); 648 } 649 } 650 } catch (Exception e) { 651 e.printStackTrace(); 652 } 653 654 } 655 656 657 662 private void addMetaInfo(org.w3c.dom.Node n) { 663 org.w3c.dom.Node n2 = n.getFirstChild(); 664 while (n2 != null) { 665 String type = n2.getNodeName(); 666 if (type != null) { 667 if (type.equals("description")) { 668 org.w3c.dom.Node n3 = n2.getFirstChild(); 669 if (n3 != null) { 670 description = n3.getNodeValue(); 671 } 672 } else if (type.equals("releasenotes")) { 673 org.w3c.dom.Node n3 = n2.getFirstChild(); 674 if (n3 != null) { 675 releasenotes = n3.getNodeValue(); 676 } 677 } else if (type.equals("installationnotes")) { 678 org.w3c.dom.Node n3 = n2.getFirstChild(); 679 if (n3 != null) { 680 installationnotes = n3.getNodeValue(); 681 } 682 } else if (type.equals("license")) { 683 org.w3c.dom.Node n3 = n2.getFirstChild(); 684 if (n3 != null) { 685 licensebody = n3.getNodeValue(); 686 } 687 NamedNodeMap nm = n2.getAttributes(); 688 if (nm != null) { 689 org.w3c.dom.Node n4 = nm.getNamedItem("name"); 691 if (n4 != null) { 692 licensename = n4.getNodeValue(); 693 } 694 n4 = nm.getNamedItem("type"); 696 if (n4 != null) { 697 licensetype = n4.getNodeValue(); 698 } 699 n4 = nm.getNamedItem("version"); 701 if (n4 != null) { 702 licenseversion = n4.getNodeValue(); 703 } 704 } 705 } else if (type.equals("initiators")) { 706 initiators = decodeRelatedPeople(n2, "initiator"); 707 } else if (type.equals("supporters")) { 708 supporters = decodeRelatedPeople(n2, "supporter"); 709 } else if (type.equals("contacts")) { 710 contacts = decodeRelatedPeople(n2, "contact"); 711 } else if (type.equals("developers")) { 712 developers = decodeRelatedPeople(n2, "developer"); 713 } 714 } 715 n2 = n2.getNextSibling(); 716 } 717 } 718 719 720 726 public List getRelatedPeople(String type) { 727 if (type.equals("initiators")) { 728 return initiators; 729 } 730 if (type.equals("supporters")) { 731 return supporters; 732 } 733 if (type.equals("developers")) { 734 return developers; 735 } 736 if (type.equals("contacts")) { 737 return contacts; 738 } 739 return null; 740 } 741 742 743 750 private ArrayList decodeRelatedPeople(org.w3c.dom.Node n, String type) { 751 ArrayList list = new ArrayList (); 752 org.w3c.dom.Node n2 = n.getFirstChild(); 753 while (n2 != null) { 754 if (n2.getNodeName().equals(type)) { 755 Person p = new Person(); 756 NamedNodeMap nm = n2.getAttributes(); 757 if (nm != null) { 758 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 759 if (n3 != null) { 760 p.setName(n3.getNodeValue()); 761 } 762 n3 = nm.getNamedItem("company"); 763 if (n3 != null) { 764 p.setCompany(n3.getNodeValue()); 765 } 766 n3 = nm.getNamedItem("reason"); 767 if (n3 != null) { 768 p.setReason(n3.getNodeValue()); 769 } 770 n3 = nm.getNamedItem("mailto"); 771 if (n3 != null) { 772 p.setMailto(n3.getNodeValue()); 773 } 774 } 775 list.add(p); 776 } 777 n2 = n2.getNextSibling(); 778 } 779 return list; 780 } 781 782 783 788 public void setProgressBar(int stepcount) { 789 progressbar = 1; 790 progressstep = 100 / (float) stepcount; 791 } 792 793 794 797 public void increaseProgressBar() { 798 increaseProgressBar(1); 799 } 800 801 802 807 public void increaseProgressBar(int stepcount) { 808 progressbar += (stepcount * progressstep); 809 } 810 811 812 817 public int getProgressBarValue() { 818 return (int) progressbar; 819 } 820 821 public installStep getBundleStep() { 822 return bundlestep; 823 } 824 825 } 826 827 | Popular Tags |