| 1 10 11 package org.mmbase.applications.packaging.bundlehandlers; 12 13 import java.io.BufferedInputStream ; 14 import java.io.BufferedOutputStream ; 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.jar.JarEntry ; 23 import java.util.jar.JarFile ; 24 25 import org.mmbase.applications.packaging.BundleManager; 26 import org.mmbase.applications.packaging.InstallManager; 27 import org.mmbase.applications.packaging.PackageManager; 28 import org.mmbase.applications.packaging.Person; 29 import org.mmbase.applications.packaging.UninstallManager; 30 import org.mmbase.applications.packaging.installhandlers.installStep; 31 import org.mmbase.applications.packaging.packagehandlers.PackageInterface; 32 import org.mmbase.applications.packaging.providerhandlers.ProviderInterface; 33 import org.mmbase.module.core.MMBaseContext; 34 import org.mmbase.util.logging.Logger; 35 import org.mmbase.util.logging.Logging; 36 import org.w3c.dom.NamedNodeMap ; 37 38 43 public class BasicBundle implements BundleInterface { 44 private static Logger log = Logging.getLoggerInstance(BasicBundle.class); 45 46 private String name; 47 private String id; 48 private String maintainer; 49 private String version; 50 private String date; 51 private String type = "unknown/unknown"; 52 private String state = "not installed"; 53 private String path; 54 private String description = ""; 55 private String releasenotes = ""; 56 private String installationnotes = ""; 57 private String licensename = ""; 58 private String licensetype = ""; 59 private String licenseversion = ""; 60 private String licensebody = ""; 61 private ProviderInterface provider; 62 63 private HashMap neededpackages = new HashMap (); 64 private ArrayList initiators,supporters,contacts,developers,screenshots,starturls; 65 private float progressbar = 0; 66 private float progressstep = 1; 67 private PackageInterface pkg = null; 68 69 private ArrayList installsteps; 75 76 private long lastupdated; 77 78 public BasicBundle() { 79 } 80 81 public BasicBundle(org.w3c.dom.Node n,ProviderInterface provider,String name,String type,String maintainer,String version, String date,String path) { 82 this.name = name; 83 this.version = version; 84 this.date = date; 85 this.maintainer = maintainer; 86 this.provider = provider; 87 this.type = type; 88 this.id = name+"@"+maintainer+"_"+type; 89 this.id = this.id.replace(' ','_'); 90 this.id = this.id.replace('/','_'); 91 this.path = path; 92 93 decodeNeededPackages(n); 94 if (n != null) { 95 addMetaInfo(n); 96 } 97 } 98 99 public String getId() { 100 if (id==null) return ""; 101 return id; 102 } 103 104 public String getName() { 105 if (name == null) return""; 106 return name; 107 } 108 109 public Iterator getNeededPackages() { 110 return neededpackages.values().iterator(); 111 } 112 113 public List getScreenshots() { 114 return screenshots; 115 } 116 117 public List getStarturls() { 118 return starturls; 119 } 120 121 public String getVersion() { 122 if (version == null) return(""); 123 return version; 124 } 125 126 public String getCreationDate() { 127 if (date == null) return ""; 128 return date; 129 } 130 131 public String getMaintainer() { 132 if (maintainer == null) return ""; 133 return maintainer; 134 } 135 136 public String getState() { 137 if (InstallManager.isActive()) { 138 if (this == InstallManager.getInstallingBundle()) { 139 return "installing"; 140 } 141 } 142 143 if (UninstallManager.isActive()) { 144 if (this == UninstallManager.getUnInstallingBundle()) { 145 return "uninstalling"; 146 } 147 } 148 149 if (BundleManager.isInstalledVersion(this)) { 150 return "installed"; 151 } 152 153 if (BundleManager.upgradeAvailable(this)) { 154 return "upgrade available"; 155 } 156 157 if (state == null) return ""; 158 return state; 159 } 160 161 public String getType() { 162 if (type==null) return ""; 163 return type; 164 } 165 166 public ProviderInterface getProvider() { 167 return provider; 168 } 169 170 public boolean setState(String state) { 171 this.state = state; 172 return true; 173 } 174 175 176 public boolean install() { 177 installStep step = getNextInstallStep(); 179 step.setUserFeedBack("bundle/basic installer started"); 180 181 setProgressBar(1000); step = getNextInstallStep(); 183 step.setUserFeedBack("getting the mmb bundle... "); 184 provider.setInstallStep(step); 185 JarFile jf = getJarFile(); 186 provider.setInstallStep(null); 187 if (jf != null) { 188 step.setUserFeedBack("getting the mmb bundle...done "); 189 increaseProgressBar(100); 191 int pbs = 0; 192 Iterator d = getNeededPackages(); 194 while (d.hasNext()) { 195 pbs++; 196 d.next(); 197 } 198 int pss = 800/pbs; 200 boolean changed = true; while (changed) { 202 Iterator e = getNeededPackages(); 203 changed = false; 204 while (e.hasNext()) { 205 Object o=e.next(); 206 211 HashMap np = (HashMap )o; 212 String tmp=(String )np.get("id"); 213 pkg = PackageManager.getPackage(tmp); 214 if (pkg != null) { 215 String state = pkg.getState(); 216 String name = pkg.getName(); 217 if (!state.equals("installed")) { 218 step = getNextInstallStep(); 219 step.setUserFeedBack("calling package installer "+name+".."); 220 boolean ins = pkg.install(step); 221 if (ins) { 222 step.setUserFeedBack("calling package installer "+name+"...done"); 223 increaseProgressBar(pss); 224 changed = true; 225 } else { 226 if (pkg.getDependsFailed()) { 227 step.setUserFeedBack("calling package installer "+name+"...skipped"); 228 removeInstallStep(step); 229 } else { 230 step.setUserFeedBack("calling package installer "+name+"...failed"); 231 return false; 232 } 233 } 234 } 235 } else { 236 log.error("Missing package on +"+np.get("id")); 237 } 238 } 239 } 240 241 step = getNextInstallStep(); 242 step.setUserFeedBack("updating mmbase registry .."); 243 updateRegistryInstalled(); 244 increaseProgressBar(100); 245 step.setUserFeedBack("updating mmbase registry ... done"); 246 } else { 247 step.setUserFeedBack("getting the mmb bundle...failed (server down or removed disk ? )"); 248 step.setType(installStep.TYPE_ERROR); 249 try { 250 Thread.sleep(2000); 251 } catch(Exception ee) {} 252 } 253 254 step=getNextInstallStep(); 255 step.setUserFeedBack("bundle/basic installer ended"); 256 return true; 257 } 258 259 public boolean uninstall() { 260 try { 261 installStep step = getNextInstallStep(); 263 step.setUserFeedBack("bundle/basic uninstaller started"); 264 265 Iterator e = getNeededPackages(); 266 while (e.hasNext()) { 267 HashMap np = (HashMap )e.next(); 268 PackageInterface pkg = PackageManager.getPackage((String )np.get("id")); 269 if (pkg != null) { 270 String name = pkg.getName(); 271 step=getNextInstallStep(); 272 step.setUserFeedBack("calling package uninstaller "+name+".."); 273 if (pkg.uninstall(step)) { 274 step.setUserFeedBack("calling package uninstaller "+name+"...done"); 275 } else { 276 step.setUserFeedBack("calling package uninstaller "+name+"...failed"); 277 } 278 } else { 279 log.error("Missing package on +"+np.get("id")); 280 } 281 } 282 283 step = getNextInstallStep(); 285 step.setUserFeedBack("updating mmbase registry .."); 286 updateRegistryUninstalled(); 287 step.setUserFeedBack("updating mmbase registry ... done"); 288 289 step = getNextInstallStep(); 291 step.setUserFeedBack("bundle/basic installer ended"); 292 293 } catch (Exception e) { 294 log.error("install crash on : "+this); 295 return false; 296 } 297 return true; 298 } 299 300 public installStep getNextInstallStep() { 301 installStep step=new installStep(); 303 if (installsteps==null) { 304 installsteps=new ArrayList (); 305 installsteps.add(step); 306 return step; 307 } else { 308 installsteps.add(step); 309 return step; 310 } 311 } 312 313 public boolean removeInstallStep(installStep step) { 314 if (installsteps.contains(step)) { 315 installsteps.remove(step); 316 return true; 317 } else { 318 return false; 319 } 320 } 321 322 public Iterator getInstallSteps() { 323 if (installsteps != null) { 324 return installsteps.iterator(); 325 } else { 326 return null; 327 } 328 } 329 330 public Iterator getInstallSteps(int logid) { 331 Iterator e = getInstallSteps(); 333 while (e.hasNext()) { 334 installStep step = (installStep)e.next(); 335 Object o = step.getInstallSteps(logid); 336 if (o != null) { 337 return (Iterator )o; 338 } 339 } 340 return null; 341 } 342 343 public void clearInstallSteps() { 344 installsteps = null; 345 } 346 347 public JarFile getJarFile() { 348 if (provider != null) { 349 JarFile jf=provider.getJarFile(getPath(),getId(),getVersion()); 350 return jf; 351 } 352 return null; 353 } 354 355 public JarFile getIncludedPackageJarFile(String packageid,String packageversion) { 356 if (provider != null) { 357 return provider.getIncludedPackageJarFile(getPath(),getId(),getVersion(),packageid,packageversion); 358 } 359 return null; 360 } 361 362 public BufferedInputStream getJarStream() { 363 if (provider!=null) { 364 return provider.getJarStream(getPath()); 365 } 366 return null; 367 } 368 369 public String getPath() { 370 return path; 371 } 372 373 374 public boolean updateRegistryInstalled() { 375 return BundleManager.updateRegistryInstalled(this); 376 } 377 378 public boolean updateRegistryUninstalled() { 379 return BundleManager.updateRegistryUninstalled(this); 380 } 381 382 public void signalUpdate() { 383 lastupdated=System.currentTimeMillis(); 384 } 385 386 public long lastSeen() { 387 return lastupdated; 388 } 389 390 private void decodeNeededPackages(org.w3c.dom.Node n) { 391 org.w3c.dom.Node n2 = n.getFirstChild(); 392 while (n2 != null) { 393 String name = n2.getNodeName(); 394 if (name.equals("neededpackages")) { 396 org.w3c.dom.Node n3 = n2.getFirstChild(); 397 while (n3 != null) { 398 name = n3.getNodeName(); 399 NamedNodeMap nm = n3.getAttributes(); 400 if (nm != null) { 401 String maintainer = null; 402 String type = null; 403 String version = null; 404 405 org.w3c.dom.Node n5 = nm.getNamedItem("name"); 407 if (n5 != null) { 408 name = n5.getNodeValue(); 409 } 410 411 n5 = nm.getNamedItem("type"); 413 if (n5 != null) { 414 type = n5.getNodeValue(); 415 } 416 417 n5 = nm.getNamedItem("maintainer"); 419 if (n5 != null) { 420 maintainer = n5.getNodeValue(); 421 } 422 423 n5 = nm.getNamedItem("version"); 425 if (n5 != null) { 426 version = n5.getNodeValue(); 427 } 428 429 String wid = name+"@"+maintainer+"_"+type; 430 wid = wid.replace(' ','_'); 431 wid = wid.replace('/','_'); 432 433 HashMap h = new HashMap (); 434 h.put("name",name); 435 h.put("id",wid); 436 h.put("type",type); 437 h.put("maintainer",maintainer); 438 h.put("version",version); 439 neededpackages.put(wid,h); 440 } 441 n3 = n3.getNextSibling(); 442 } 443 } 444 if (name.equals("includedpackages")) { 446 org.w3c.dom.Node n3 = n2.getFirstChild(); 447 while (n3 != null) { 448 name = n3.getNodeName(); 449 NamedNodeMap nm = n3.getAttributes(); 450 if (nm != null) { 451 String maintainer = null; 452 String type = null; 453 String version = null; 454 boolean packed = false; 455 456 org.w3c.dom.Node n5 = nm.getNamedItem("name"); 458 if (n5 != null) { 459 name = n5.getNodeValue(); 460 } 461 462 n5 = nm.getNamedItem("type"); 464 if (n5 != null) { 465 type = n5.getNodeValue(); 466 } 467 468 n5 = nm.getNamedItem("maintainer"); 470 if (n5 != null) { 471 maintainer = n5.getNodeValue(); 472 } 473 474 n5 = nm.getNamedItem("version"); 476 if (n5 != null) { 477 version = n5.getNodeValue(); 478 } 479 480 n5 = nm.getNamedItem("packed"); 482 if (n5 != null) { 483 if (n5.getNodeValue().equals("true")) packed = true; 484 } 485 486 String wid = name+"@"+maintainer+"_"+type; 487 wid = wid.replace(' ','_'); 488 wid = wid.replace('/','_'); 489 490 HashMap h = new HashMap (); 491 h.put("name",name); 492 h.put("id",wid); 493 h.put("type",type); 494 h.put("maintainer",maintainer); 495 h.put("version",version); 496 neededpackages.put(wid,h); 497 498 } 500 n3 = n3.getNextSibling(); 501 } 502 } 503 n2 = n2.getNextSibling(); 504 } 505 } 506 507 private void addMetaInfo(org.w3c.dom.Node n) { 508 org.w3c.dom.Node n2 = n.getFirstChild(); 509 while (n2 != null) { 510 if (n2.getNodeName().equals("description")) { 511 org.w3c.dom.Node n3 = n2.getFirstChild(); 512 if (n3!=null) { 513 description = n3.getNodeValue(); 514 } 515 } else if (n2.getNodeName().equals("releasenotes")) { 516 org.w3c.dom.Node n3 = n2.getFirstChild(); 517 if (n3!=null) { 518 releasenotes = n3.getNodeValue(); 519 } 520 } else if (n2.getNodeName().equals("installationnotes")) { 521 org.w3c.dom.Node n3 = n2.getFirstChild(); 522 if (n3!=null) { 523 installationnotes = n3.getNodeValue(); 524 } 525 } else if (n2.getNodeName().equals("license")) { 526 org.w3c.dom.Node n3 = n2.getFirstChild(); 527 if (n3 != null) { 528 licensebody = n3.getNodeValue(); 529 } 530 NamedNodeMap nm = n2.getAttributes(); 531 if (nm != null) { 532 org.w3c.dom.Node n4 = nm.getNamedItem("name"); 534 if (n4 != null) { 535 licensename = n4.getNodeValue(); 536 } 537 n4 = nm.getNamedItem("type"); 539 if (n4 != null) { 540 licensetype = n4.getNodeValue(); 541 } 542 n4 = nm.getNamedItem("version"); 544 if (n4 != null) { 545 licenseversion = n4.getNodeValue(); 546 } 547 } 548 } else if (n2.getNodeName().equals("screenshots")) { 549 screenshots = decodeRelatedScreenshots(n2); 550 } else if (n2.getNodeName().equals("starturls")) { 551 starturls = decodeRelatedStarturls(n2); 552 } else if (n2.getNodeName().equals("initiators")) { 553 initiators = decodeRelatedPeople(n2,"initiator"); 554 } else if (n2.getNodeName().equals("supporters")) { 555 supporters = decodeRelatedPeople(n2,"supporter"); 556 } else if (n2.getNodeName().equals("contacts")) { 557 contacts = decodeRelatedPeople(n2,"contact"); 558 } else if (n2.getNodeName().equals("developers")) { 559 developers = decodeRelatedPeople(n2,"developer"); 560 } 561 n2 = n2.getNextSibling(); 562 } 563 } 564 565 public List getRelatedPeople(String type) { 566 if (type.equals("initiators")) return initiators; 567 if (type.equals("supporters")) return supporters; 568 if (type.equals("developers")) return developers; 569 if (type.equals("contacts")) return contacts; 570 return null; 571 } 572 573 574 private ArrayList decodeRelatedPeople(org.w3c.dom.Node n,String type) { 575 ArrayList list = new ArrayList (); 576 org.w3c.dom.Node n2 = n.getFirstChild(); 577 while (n2 != null) { 578 if (n2.getNodeName().equals(type)) { 579 Person p = new Person(); 580 NamedNodeMap nm = n2.getAttributes(); 581 if (nm != null) { 582 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 583 if (n3 != null) p.setName(n3.getNodeValue()); 584 n3 = nm.getNamedItem("company"); 585 if (n3 != null) p.setCompany(n3.getNodeValue()); 586 n3 = nm.getNamedItem("reason"); 587 if (n3 != null) p.setReason(n3.getNodeValue()); 588 n3 = nm.getNamedItem("mailto"); 589 if (n3 != null) p.setMailto(n3.getNodeValue()); 590 } 591 list.add(p); 592 } 593 n2 = n2.getNextSibling(); 594 } 595 return list; 596 } 597 598 599 private ArrayList decodeRelatedScreenshots(org.w3c.dom.Node n) { 600 ArrayList list = new ArrayList (); 601 org.w3c.dom.Node n2 = n.getFirstChild(); 602 while (n2 != null) { 603 if (n2.getNodeName().equals("screenshot")) { 604 NamedNodeMap nm = n2.getAttributes(); 605 String ufile=""; 606 if (nm != null) { 607 String name=""; 608 String file=""; 609 String description=""; 610 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 611 if (n3 != null) name=n3.getNodeValue(); 612 n3 = nm.getNamedItem("file"); 613 if (n3 != null) file=n3.getNodeValue(); 614 n3=n2.getFirstChild(); 615 if (n3!=null) description=n3.getNodeValue(); 616 617 if (provider!=null) { 623 String method=provider.getMethod(); 624 if (method.equals("disk")) { 625 String htmldir=MMBaseContext.getHtmlRoot() + File.separator+"mmbase"+File.separator+"packagemanager"+File.separator+"images"+File.separator; 626 ufile=File.separator+"mmbase"+File.separator+"packagemanager"+File.separator+"images"+File.separator+getId()+File.separator+file; 627 File d = new File (htmldir+getId()); 628 if (!d.exists()) { 629 d.mkdir(); 630 } 631 d = new File (htmldir+getId()+File.separator+"screenshots"); 632 if (!d.exists()) { 633 d.mkdir(); 634 } 635 JarFile jf = getJarFile(); 636 if (jf!=null) { 637 try { 638 JarEntry je = jf.getJarEntry(file); 639 BufferedInputStream in = new BufferedInputStream (jf.getInputStream(je)); 640 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (htmldir+getId()+File.separator+file)); 641 int val; 642 while ((val = in.read()) != -1) { 643 out.write(val); 644 } 645 out.close(); 646 } catch (IOException f) { 647 f.printStackTrace(); 648 } 649 } 650 } else if (method.equals("http")) { 651 ufile=file; 652 } 653 } 654 655 list.add(name); 656 list.add(ufile); 657 list.add(description); 658 } 659 } 660 n2 = n2.getNextSibling(); 661 } 662 return list; 663 } 664 665 666 private ArrayList decodeRelatedStarturls(org.w3c.dom.Node n) { 667 ArrayList list = new ArrayList (); 668 org.w3c.dom.Node n2 = n.getFirstChild(); 669 while (n2 != null) { 670 if (n2.getNodeName().equals("url")) { 671 NamedNodeMap nm = n2.getAttributes(); 672 if (nm != null) { 673 String name=""; 674 String link=""; 675 String description=""; 676 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 677 if (n3 != null) name=n3.getNodeValue(); 678 n3 = nm.getNamedItem("link"); 679 if (n3 != null) link=n3.getNodeValue(); 680 n3=n2.getFirstChild(); 681 if (n3!=null) description=n3.getNodeValue(); 682 list.add(name); 683 list.add(link); 684 list.add(description); 685 } 686 } 687 n2 = n2.getNextSibling(); 688 } 689 return list; 690 } 691 692 693 public String getDescription() { 694 return description; 695 } 696 697 public String getInstallationNotes() { 698 return installationnotes; 699 } 700 701 public String getReleaseNotes() { 702 return releasenotes; 703 } 704 705 public String getLicenseType() { 706 return licensetype; 707 } 708 709 public String getLicenseName() { 710 return licensename; 711 } 712 713 public String getLicenseVersion() { 714 return licenseversion; 715 } 716 717 public String getLicenseBody() { 718 return licensebody; 719 } 720 721 public void setProgressBar(int stepcount) { 722 progressbar=1; 723 progressstep=100/(float)stepcount; 724 } 725 726 public void increaseProgressBar() { 727 increaseProgressBar(1); 728 } 729 730 public void increaseProgressBar(int stepcount) { 731 progressbar += (stepcount*progressstep); 732 } 733 734 public int getProgressBarValue() { 735 return (int)progressbar; 736 } 737 738 public int getPackageProgressBarValue() { 739 if (pkg!=null) { 740 return pkg.getProgressBarValue(); 741 } 742 return 0; 743 } 744 } 745 | Popular Tags |