KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > packagehandlers > BasicPackage


1 /*
2  * This software is OSI Certified Open Source Software.
3  * OSI Certified is a certification mark of the Open Source Initiative.
4  * The license (Mozilla version 1.0) can be read at the MMBase site.
5  * See http://www.MMBase.org/license
6  */

7 package org.mmbase.applications.packaging.packagehandlers;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.jar.JarEntry JavaDoc;
15 import java.util.jar.JarFile JavaDoc;
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 JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35
36 /**
37  * BasicPackage, base class for packages
38  *
39  * @author Daniel Ockeloen (MMBased)
40  */

41 public class BasicPackage implements PackageInterface {
42     private static Logger log = Logging.getLoggerInstance(BasicPackage.class);
43
44     private String JavaDoc name;
45     private String JavaDoc id;
46     private String JavaDoc maintainer;
47     private String JavaDoc version;
48     private String JavaDoc date;
49     private String JavaDoc type = "unknown/unknown";
50     private String JavaDoc state = "not installed";
51     private String JavaDoc path;
52     private String JavaDoc description = null;
53     private String JavaDoc releasenotes = "";
54     private String JavaDoc installationnotes = "";
55     private String JavaDoc licensename = "";
56     private String JavaDoc licensetype = "";
57     private String JavaDoc licenseversion = "";
58     private String JavaDoc licensebody = "";
59     private ProviderInterface provider;
60     private installStep bundlestep;
61     private boolean dependsfailed = false;
62     private BundleInterface parentbundle = null;
63     private ArrayList JavaDoc initiators, supporters, contacts, developers;
64     private float progressbar = 0;
65     private float progressstep = 1;
66
67     // the install manager keeps track of what happend during a install
68
// of a package or bundle. These are called steps because they not
69
// only can provide log info but also possible fixed, feedback, stats
70
// etc etc. Each step in itself can have steps again providing for things
71
// like three style logging and feedback
72
private ArrayList JavaDoc installsteps;
73
74     private long lastupdated;
75
76     /**
77      * DTD resource filename of the packagedepends DTD version 1.0
78      */

79     public final static String JavaDoc DTD_PACKAGEDEPENDS_1_0 = "packagedepends_1_0.dtd";
80     /**
81      * Description of the Field
82      */

83     public final static String JavaDoc DTD_PACKAGE_1_0 = "package_1_0.dtd";
84
85     /**
86      * Public ID of the packagedepends DTD version 1.0
87      */

88     public final static String JavaDoc PUBLIC_ID_PACKAGEDEPENDS_1_0 = "-//MMBase//DTD packagedepends config 1.0//EN";
89     /**
90      * Description of the Field
91      */

92     public final static String JavaDoc PUBLIC_ID_PACKAGE_1_0 = "-//MMBase//DTD package config 1.0//EN";
93
94
95
96     /**
97      * Register the Public Ids for DTDs used by DatabaseReader
98      * This method is called by XMLEntityResolver.
99      */

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     /**
107      *Constructor for the BasicPackage object
108      */

109     public BasicPackage() { }
110
111
112     /**
113      * Description of the Method
114      *
115      * @param n Description of the Parameter
116      * @param provider Description of the Parameter
117      * @param name Description of the Parameter
118      * @param type Description of the Parameter
119      * @param maintainer Description of the Parameter
120      * @param version Description of the Parameter
121      * @param date Description of the Parameter
122      * @param path Description of the Parameter
123      */

124     public void init(org.w3c.dom.Node JavaDoc n, ProviderInterface provider, String JavaDoc name, String JavaDoc type, String JavaDoc maintainer, String JavaDoc version, String JavaDoc date, String JavaDoc 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     /**
142      * Gets the id attribute of the BasicPackage object
143      *
144      * @return The id value
145      */

146     public String JavaDoc getId() {
147         if (id == null) {
148             return "";
149         }
150         return id;
151     }
152
153
154     /**
155      * Gets the name attribute of the BasicPackage object
156      *
157      * @return The name value
158      */

159     public String JavaDoc getName() {
160         if (name == null) {
161             return "";
162         }
163         return name;
164     }
165
166
167     /**
168      * Gets the description attribute of the BasicPackage object
169      *
170      * @return The description value
171      */

172     public String JavaDoc getDescription() {
173         if (description == null) {
174             delayedMetaInfo();
175         }
176         return description;
177     }
178
179
180     /**
181      * Gets the installationNotes attribute of the BasicPackage object
182      *
183      * @return The installationNotes value
184      */

185     public String JavaDoc getInstallationNotes() {
186         return installationnotes;
187     }
188
189
190     /**
191      * Gets the releaseNotes attribute of the BasicPackage object
192      *
193      * @return The releaseNotes value
194      */

195     public String JavaDoc getReleaseNotes() {
196         return releasenotes;
197     }
198
199
200     /**
201      * Gets the licenseType attribute of the BasicPackage object
202      *
203      * @return The licenseType value
204      */

205     public String JavaDoc getLicenseType() {
206         return licensetype;
207     }
208
209
210     /**
211      * Gets the licenseName attribute of the BasicPackage object
212      *
213      * @return The licenseName value
214      */

215     public String JavaDoc getLicenseName() {
216         return licensename;
217     }
218
219
220     /**
221      * Gets the licenseVersion attribute of the BasicPackage object
222      *
223      * @return The licenseVersion value
224      */

225     public String JavaDoc getLicenseVersion() {
226         return licenseversion;
227     }
228
229
230     /**
231      * Gets the licenseBody attribute of the BasicPackage object
232      *
233      * @return The licenseBody value
234      */

235     public String JavaDoc getLicenseBody() {
236         return licensebody;
237     }
238
239
240     /**
241      * Gets the version attribute of the BasicPackage object
242      *
243      * @return The version value
244      */

245     public String JavaDoc getVersion() {
246         if (version == null) {
247             return ("");
248         }
249         return version;
250     }
251
252
253     /**
254      * Gets the creationDate attribute of the BasicPackage object
255      *
256      * @return The creationDate value
257      */

258     public String JavaDoc getCreationDate() {
259         if (date == null) {
260             return "";
261         }
262         return date;
263     }
264
265
266     /**
267      * Gets the maintainer attribute of the BasicPackage object
268      *
269      * @return The maintainer value
270      */

271     public String JavaDoc getMaintainer() {
272         if (maintainer == null) {
273             return "";
274         }
275         return maintainer;
276     }
277
278
279     /**
280      * Gets the state attribute of the BasicPackage object
281      *
282      * @return The state value
283      */

284     public String JavaDoc 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     /**
313      * Gets the type attribute of the BasicPackage object
314      *
315      * @return The type value
316      */

317     public String JavaDoc getType() {
318         if (type == null) {
319             return "";
320         }
321         return type;
322     }
323
324
325     /**
326      * Gets the provider attribute of the BasicPackage object
327      *
328      * @return The provider value
329      */

330     public ProviderInterface getProvider() {
331         return provider;
332     }
333
334
335     /**
336      * Sets the state attribute of the BasicPackage object
337      *
338      * @param state The new state value
339      * @return Description of the Return Value
340      */

341     public boolean setState(String JavaDoc state) {
342         this.state = state;
343         return true;
344     }
345
346
347     /**
348      * Description of the Method
349      *
350      * @return Description of the Return Value
351      */

352     public boolean install() {
353         log.error("this package doesn't implement the install() call");
354         return false;
355     }
356
357
358     /**
359      * Description of the Method
360      *
361      * @param step Description of the Parameter
362      * @return Description of the Return Value
363      */

364     public boolean install(installStep step) {
365         bundlestep = step;
366         return install();
367     }
368
369
370     /**
371      * Description of the Method
372      *
373      * @return Description of the Return Value
374      */

375     public boolean uninstall() {
376         log.error("this package doesn't implement the uninstall() call");
377         return false;
378     }
379
380
381     /**
382      * Description of the Method
383      *
384      * @param step Description of the Parameter
385      * @return Description of the Return Value
386      */

387     public boolean uninstall(installStep step) {
388         bundlestep = step;
389         return uninstall();
390     }
391
392
393     /**
394      * Gets the nextInstallStep attribute of the BasicPackage object
395      *
396      * @return The nextInstallStep value
397      */

398     public installStep getNextInstallStep() {
399         installStep step = null;
400         if (bundlestep != null) {
401             step = bundlestep.getNextInstallStep();
402         } else {
403             // create new step
404
step = new installStep();
405         }
406         if (installsteps == null) {
407             installsteps = new ArrayList JavaDoc();
408             installsteps.add(step);
409             return step;
410         } else {
411             installsteps.add(step);
412             return step;
413         }
414     }
415
416
417     /**
418      * Gets the installSteps attribute of the BasicPackage object
419      *
420      * @return The installSteps value
421      */

422     public Iterator JavaDoc getInstallSteps() {
423         if (installsteps != null) {
424             return installsteps.iterator();
425         } else {
426             return null;
427         }
428     }
429
430
431     /*
432      * public Enumeration getInstallSteps(int logid) {
433      * Enumeration e=getInstallSteps();
434      * while (e.hasMoreElements()) {
435      * installStep step=(installStep)e.nextElement();
436      * if (step.getId()==logid) {
437      * return step.getInstallSteps();
438      * }
439      * }
440      * return null;
441      * }
442      */

443     /**
444      * Gets the installSteps attribute of the BasicPackage object
445      *
446      * @param logid Description of the Parameter
447      * @return The installSteps value
448      */

449     public Iterator JavaDoc getInstallSteps(int logid) {
450         // well maybe its one of my subs ?
451
Iterator JavaDoc e = getInstallSteps();
452         while (e.hasNext()) {
453             installStep step = (installStep) e.next();
454             Object JavaDoc o = step.getInstallSteps(logid);
455             if (o != null) {
456                 return (Iterator JavaDoc) o;
457             }
458         }
459         return null;
460     }
461
462
463     /**
464      * Description of the Method
465      */

466     public void clearInstallSteps() {
467         installsteps = null;
468     }
469
470
471     /**
472      * Gets the jarFile attribute of the BasicPackage object
473      *
474      * @return The jarFile value
475      */

476     public JarFile JavaDoc 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     /**
489      * Gets the jarStream attribute of the BasicPackage object
490      *
491      * @return The jarStream value
492      */

493     public BufferedInputStream JavaDoc getJarStream() {
494         if (provider != null) {
495             return provider.getJarStream(getPath());
496         }
497         return null;
498     }
499
500
501     /**
502      * Gets the path attribute of the BasicPackage object
503      *
504      * @return The path value
505      */

506     public String JavaDoc getPath() {
507         return path;
508     }
509
510
511     /**
512      * Description of the Method
513      *
514      * @return Description of the Return Value
515      */

516     public boolean updateRegistryInstalled() {
517         return PackageManager.updateRegistryInstalled(this);
518     }
519
520
521     /**
522      * Description of the Method
523      *
524      * @return Description of the Return Value
525      */

526     public boolean updateRegistryUninstalled() {
527         return PackageManager.updateRegistryUninstalled(this);
528     }
529
530
531     /**
532      * Description of the Method
533      */

534     public void signalUpdate() {
535         lastupdated = System.currentTimeMillis();
536     }
537
538
539     /**
540      * Description of the Method
541      *
542      * @return Description of the Return Value
543      */

544     public long lastSeen() {
545         return lastupdated;
546     }
547
548
549     /**
550      * Description of the Method
551      *
552      * @param jf Description of the Parameter
553      * @param step Description of the Parameter
554      * @return Description of the Return Value
555      */

556     public boolean dependsInstalled(JarFile JavaDoc 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 JavaDoc je = jf.getJarEntry("depends.xml");
565             if (je != null) {
566                 InputStream JavaDoc input = jf.getInputStream(je);
567                 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource JavaDoc(input), BasicPackage.class);
568                 for (Iterator JavaDoc ns = reader.getChildElements("packagedepends", "package"); ns.hasNext(); ) {
569                     Element JavaDoc n = (Element JavaDoc) ns.next();
570                     String JavaDoc name = n.getAttribute("name");
571                     String JavaDoc type = n.getAttribute("type");
572                     String JavaDoc version = n.getAttribute("version");
573                     String JavaDoc versionmode = n.getAttribute("versionmode");
574                     String JavaDoc maintainer = n.getAttribute("maintainer");
575                     //log.info("depends name "+name+" "+type+" "+version+" "+versionmode+" "+maintainer);
576
installStep substep = step.getNextInstallStep();
577                     substep.setUserFeedBack("checking package : " + name + " (" + type + ") version : " + version + " (" + versionmode + ") from : " + maintainer);
578
579                     String JavaDoc id = name + "@" + maintainer + "_" + type;
580                     id = id.replace(' ', '_');
581                     id = id.replace('/', '_');
582                     int installedversion = versions.getInstalledVersion(id, "package");
583                     // if not installed at all then well for sure
584
// a negative
585
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 JavaDoc e) {
596             return false;
597         }
598         return true;
599     }
600
601
602     /**
603      * Gets the dependsFailed attribute of the BasicPackage object
604      *
605      * @return The dependsFailed value
606      */

607     public boolean getDependsFailed() {
608         return dependsfailed;
609     }
610
611
612     /**
613      * Gets the parentBundle attribute of the BasicPackage object
614      *
615      * @return The parentBundle value
616      */

617     public BundleInterface getParentBundle() {
618         return parentbundle;
619     }
620
621
622     /**
623      * Sets the parentBundle attribute of the BasicPackage object
624      *
625      * @param parent The new parentBundle value
626      */

627     public void setParentBundle(BundleInterface parent) {
628         parentbundle = parent;
629     }
630
631
632     /**
633      * Description of the Method
634      */

635     private void delayedMetaInfo() {
636         // needs to be smarter now i need to unzip in anyway :(
637
// to get the package.xml to get the meta info
638
JarFile JavaDoc jf = getJarFile();
639         // open the jar to read the input xml
640
try {
641             JarEntry JavaDoc je = jf.getJarEntry("package.xml");
642             if (je != null) {
643                 InputStream JavaDoc input = jf.getInputStream(je);
644                 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource JavaDoc(input), DiskProvider.class);
645                 if (reader != null) {
646                     Element JavaDoc e = reader.getElementByPath("package");
647                     addMetaInfo(e);
648                 }
649             }
650         } catch (Exception JavaDoc e) {
651             e.printStackTrace();
652         }
653
654     }
655
656
657     /**
658      * Adds a feature to the MetaInfo attribute of the BasicPackage object
659      *
660      * @param n The feature to be added to the MetaInfo attribute
661      */

662     private void addMetaInfo(org.w3c.dom.Node JavaDoc n) {
663         org.w3c.dom.Node JavaDoc n2 = n.getFirstChild();
664         while (n2 != null) {
665             String JavaDoc type = n2.getNodeName();
666             if (type != null) {
667                 if (type.equals("description")) {
668                     org.w3c.dom.Node JavaDoc n3 = n2.getFirstChild();
669                     if (n3 != null) {
670                         description = n3.getNodeValue();
671                     }
672                 } else if (type.equals("releasenotes")) {
673                     org.w3c.dom.Node JavaDoc n3 = n2.getFirstChild();
674                     if (n3 != null) {
675                         releasenotes = n3.getNodeValue();
676                     }
677                 } else if (type.equals("installationnotes")) {
678                     org.w3c.dom.Node JavaDoc n3 = n2.getFirstChild();
679                     if (n3 != null) {
680                         installationnotes = n3.getNodeValue();
681                     }
682                 } else if (type.equals("license")) {
683                     org.w3c.dom.Node JavaDoc n3 = n2.getFirstChild();
684                     if (n3 != null) {
685                         licensebody = n3.getNodeValue();
686                     }
687                     NamedNodeMap JavaDoc nm = n2.getAttributes();
688                     if (nm != null) {
689                         // decode name
690
org.w3c.dom.Node JavaDoc n4 = nm.getNamedItem("name");
691                         if (n4 != null) {
692                             licensename = n4.getNodeValue();
693                         }
694                         // decode type
695
n4 = nm.getNamedItem("type");
696                         if (n4 != null) {
697                             licensetype = n4.getNodeValue();
698                         }
699                         // decode version
700
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     /**
721      * Gets the relatedPeople attribute of the BasicPackage object
722      *
723      * @param type Description of the Parameter
724      * @return The relatedPeople value
725      */

726     public List JavaDoc getRelatedPeople(String JavaDoc 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     /**
744      * Description of the Method
745      *
746      * @param n Description of the Parameter
747      * @param type Description of the Parameter
748      * @return Description of the Return Value
749      */

750     private ArrayList JavaDoc decodeRelatedPeople(org.w3c.dom.Node JavaDoc n, String JavaDoc type) {
751         ArrayList JavaDoc list = new ArrayList JavaDoc();
752         org.w3c.dom.Node JavaDoc n2 = n.getFirstChild();
753         while (n2 != null) {
754             if (n2.getNodeName().equals(type)) {
755                 Person p = new Person();
756                 NamedNodeMap JavaDoc nm = n2.getAttributes();
757                 if (nm != null) {
758                     org.w3c.dom.Node JavaDoc 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     /**
784      * Sets the progressBar attribute of the BasicPackage object
785      *
786      * @param stepcount The new progressBar value
787      */

788     public void setProgressBar(int stepcount) {
789         progressbar = 1;
790         progressstep = 100 / (float) stepcount;
791     }
792
793
794     /**
795      * Description of the Method
796      */

797     public void increaseProgressBar() {
798         increaseProgressBar(1);
799     }
800
801
802     /**
803      * Description of the Method
804      *
805      * @param stepcount Description of the Parameter
806      */

807     public void increaseProgressBar(int stepcount) {
808         progressbar += (stepcount * progressstep);
809     }
810
811
812     /**
813      * Gets the progressBarValue attribute of the BasicPackage object
814      *
815      * @return The progressBarValue value
816      */

817     public int getProgressBarValue() {
818         return (int) progressbar;
819     }
820
821     public installStep getBundleStep() {
822     return bundlestep;
823     }
824
825 }
826
827
Popular Tags