KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > PackageManager


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

10
11 package org.mmbase.applications.packaging;
12
13 import java.io.File JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.mmbase.applications.packaging.packagehandlers.BasicPackage;
19 import org.mmbase.applications.packaging.packagehandlers.PackageContainer;
20 import org.mmbase.applications.packaging.packagehandlers.PackageInterface;
21 import org.mmbase.applications.packaging.packagehandlers.PackageVersionContainer;
22 import org.mmbase.applications.packaging.providerhandlers.ProviderInterface;
23 import org.mmbase.applications.packaging.util.ExtendedDocumentReader;
24 import org.mmbase.module.builders.Versions;
25 import org.mmbase.module.core.MMBase;
26 import org.mmbase.storage.search.SearchQueryException;
27 import org.mmbase.util.ResourceLoader;
28 import org.mmbase.util.XMLEntityResolver;
29 import org.mmbase.util.logging.Logger;
30 import org.mmbase.util.logging.Logging;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33
34 /**
35  * package manager, access point for all packages available to this cloud
36  *
37  * @author Daniel Ockeloen (MMBased)
38  */

39 public class PackageManager {
40     private static Logger log = Logging.getLoggerInstance(PackageManager.class);
41
42     // Contains all packages key=packagename/maintainer value=reference to application
43
private static HashMap JavaDoc packages = new HashMap JavaDoc();
44
45     // state of this manager
46
private static boolean state = false;
47     private static HashMap JavaDoc packagehandlers;
48
49     public static final String JavaDoc DTD_PACKAGEHANDLERS_1_0 = "packagehandlers_1_0.dtd";
50     public static final String JavaDoc PUBLIC_ID_PACKAGEHANDLERS_1_0 = "-//MMBase//DTD packagehandlers config 1.0//EN";
51
52     /**
53      * Register the Public Ids for DTDs used by XMLBasicReader
54      * This method is called by XMLEntityResolver.
55      */

56     public static void registerPublicIDs() {
57         XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGEHANDLERS_1_0, DTD_PACKAGEHANDLERS_1_0, PackageManager.class);
58     }
59     /**
60     * init(), starts the package manager mostly start the
61     * package discovery system.
62     */

63     public static synchronized void init() {
64         readPackageHandlers();
65         state = true;
66     }
67
68     public static boolean isRunning() {
69         return state;
70     }
71
72     /**
73      * return all packages based on the input query
74      * @return all packages
75      */

76     public static Iterator JavaDoc getPackages() {
77         return packages.values().iterator();
78     }
79     
80     /**
81      * return all packages based
82      * @return all packages
83      */

84     public static Iterator JavaDoc getPackageVersions(String JavaDoc id) {
85         Object JavaDoc o = packages.get(id);
86         if (o != null) {
87             PackageContainer pc = (PackageContainer)o;
88             return pc.getVersions();
89         }
90         return null;
91     }
92
93
94     /**
95      * return a list of version numbers of this package
96      */

97     public static Iterator JavaDoc getPackageVersionNumbers(String JavaDoc id) {
98         Object JavaDoc o = packages.get(id);
99         if (o != null) {
100             PackageContainer pc = (PackageContainer)o;
101             return pc.getVersionNumbers();
102         }
103         return null;
104     }
105
106     /**
107      * return all packages based on the input query
108      * @return all packages
109      */

110     public static PackageInterface getPackage(String JavaDoc id) {
111         Object JavaDoc o = packages.get(id);
112         if (o != null) {
113             return (PackageInterface)o;
114         }
115         return null;
116     }
117
118
119     /**
120      * return all packages based on the input query
121      * @return all packages
122      */

123     public static PackageInterface getPackage(String JavaDoc id,String JavaDoc wv,String JavaDoc wp) {
124         Object JavaDoc o = packages.get(id);
125         if (o != null) {
126             PackageContainer pc = (PackageContainer)o;
127             ProviderInterface provider = ProviderManager.get(wp);
128             if (provider != null) {
129                 PackageInterface p = pc.getVersion(wv,provider);
130                 if (p != null) {
131                     return p;
132                 }
133             }
134         }
135         return null;
136     }
137
138
139     /**
140      * return all packages based on the input query
141      * @return all packages
142      */

143     public static PackageInterface getPackage(String JavaDoc id,String JavaDoc wv) {
144         Object JavaDoc o = packages.get(id);
145         if (o != null) {
146             PackageContainer pc = (PackageContainer)o;
147             PackageInterface p = pc.getPackageByScore(wv);
148             if (p != null) {
149                 return p;
150             }
151         }
152         return null;
153     }
154
155     /**
156      * called by Providers with found packages
157      * they are checked and if new put into the
158      * package pool.
159      */

160     public static PackageInterface foundPackage(ProviderInterface provider,org.w3c.dom.Element JavaDoc n,String JavaDoc name,String JavaDoc type,String JavaDoc maintainer,String JavaDoc version,String JavaDoc date,String JavaDoc path) {
161
162         // create its id (name+maintainer)
163
String JavaDoc id = name+"@"+maintainer+"_"+type;
164         id = id.replace(' ','_');
165         id = id.replace('/','_');
166     
167         // check if we allready have a package container for this
168
PackageContainer pc = (PackageContainer)packages.get(id);
169
170         boolean found = false;
171         if (pc != null) {
172             // we allready have a container check if we allready
173
// have this one
174
found = pc.contains(version,provider);
175         }
176
177         if (!found) {
178             // so we don't have this package refernce yet, then
179
// create and store it
180

181             // try to create this handler
182
String JavaDoc classname = (String JavaDoc)packagehandlers.get(type);
183             if (classname != null) {
184                 try {
185                     Class JavaDoc newclass = Class.forName(classname);
186                     PackageInterface newpackage = (PackageInterface)newclass.newInstance();
187                     newpackage.init(n,provider,name,type,maintainer,version,date,path);
188                     if (pc == null) {
189                         pc = new PackageContainer(newpackage);
190                         // since this is a new container store it
191
packages.put(id,pc);
192                     } else {
193                         pc.addPackage(newpackage);
194                     }
195                     ((BasicPackage)newpackage).signalUpdate();
196                     return newpackage;
197                 } catch(Exception JavaDoc e) {
198                     log.error("Can't create packagehandler : "+classname,e);
199                 }
200             } else {
201                 log.error("package type : "+type+" not supported (no handler)");
202             }
203         } else {
204             // get the package to update its available time
205
BasicPackage oldp = (BasicPackage)pc.getVersion(version,provider);
206             if (oldp != null) oldp.signalUpdate();
207         }
208         return null;
209     }
210
211
212     public static int getInstalledVersion(String JavaDoc id) throws SearchQueryException {
213         // Get the versions builder
214
Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions");
215         if(versions == null) {
216             log.error("Versions builder not installed.");
217             return -1;
218         } else {
219             return versions.getInstalledVersion(id,"package");
220         }
221     }
222
223     public static boolean isInstalledVersion(PackageInterface p) {
224         try {
225             int newversion = Integer.parseInt(p.getVersion());
226             if (getInstalledVersion(p.getId()) == newversion) {
227                 return true;
228             }
229         } catch(Exception JavaDoc e) {
230             log.debug("error while checking if a version"+ ((p != null)?p.getVersion():"(p == null")+" of the package "+ p +" is installed:" + e.getMessage() , e);
231             return false;
232         }
233         return false;
234     }
235
236
237     public static boolean upgradeAvailable(PackageInterface p) {
238         try {
239             int newversion = Integer.parseInt(p.getVersion());
240             int oldversion = getInstalledVersion(p.getId());
241         if (oldversion!=-1 && newversion > oldversion) {
242         return true;
243         }
244         } catch(Exception JavaDoc e) {
245             log.debug("error while checking if a version"+ ((p != null)?p.getVersion():"(p == null")+" of the package "+ p +" is installed:" + e.getMessage() , e);
246             return false;
247         }
248         return false;
249     }
250
251
252
253     public static boolean updateRegistryInstalled(PackageInterface p) {
254         try {
255             Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions");
256             if (versions == null) {
257                 log.error("Versions builder not installed.");
258                 return false;
259             }
260             int newversion = Integer.parseInt(p.getVersion());
261             int oldversion = getInstalledVersion(p.getId());
262             if (oldversion == -1) {
263                 versions.setInstalledVersion(p.getId(),"package",p.getMaintainer(),newversion);
264             } else if (oldversion != newversion) {
265                 versions.updateInstalledVersion(p.getId(),"package",p.getMaintainer(),newversion);
266             }
267             return true;
268         } catch(Exception JavaDoc e) {
269             log.debug("error while updating versions for version "+ ((p != null)?p.getVersion():"(p == null")+" of the package "+ p + ":" + e.getMessage() , e);
270             return false;
271         }
272     }
273
274
275     public static boolean updateRegistryUninstalled(PackageInterface p) {
276         try {
277             Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions");
278             if(versions == null) {
279                 log.error("Versions builder not installed.");
280                 return false;
281             }
282             versions.updateInstalledVersion(p.getId(),"package",p.getMaintainer(),0);
283             return true;
284         } catch(Exception JavaDoc e) {
285             log.debug("error while updating the installed status to 0 of the package "+ p + ":" + e.getMessage() , e);
286             return false;
287         }
288     }
289    
290     public static boolean removeOfflinePackages(ProviderInterface wantedprov) {
291         // this checks all the packages if they are still found at their
292
// providers, this is done by checking the last provider update
293
// against the last package update
294
Iterator JavaDoc e = ((HashMap JavaDoc)packages.clone()).values().iterator();
295         while (e.hasNext()) {
296             PackageContainer pc = (PackageContainer)e.next();
297             Iterator JavaDoc e2 = pc.getVersions();
298             while (e2.hasNext()) {
299                 PackageVersionContainer pvc = (PackageVersionContainer)e2.next();
300                 Iterator JavaDoc e3 = pvc.getPackages();
301                 while (e3.hasNext()) {
302                     BasicPackage p = (BasicPackage)e3.next();
303                     ProviderInterface prov = p.getProvider();
304                     if (wantedprov == prov) {
305                         long providertime = p.getProvider().lastSeen();
306                         long packagetime = p.lastSeen();
307                         if (providertime > packagetime) {
308                             pvc.removePackage(p);
309                             if (pvc.getPackageCount() == 0) {
310                                 pc.removePackage(p);
311                                 if (pc.getPackageCount() == 0) {
312                                     packages.remove(pc.getId());
313                                 }
314                             }
315                         }
316                     }
317                 }
318             }
319         }
320         return true;
321     }
322
323     public static void readPackageHandlers() {
324         packagehandlers = new HashMap JavaDoc();
325         String JavaDoc filename = getConfigPath()+File.separator+"packaging"+File.separator+"packagehandlers.xml";
326
327         File JavaDoc file = new File JavaDoc(filename);
328         if(file.exists()) {
329
330             ExtendedDocumentReader reader = new ExtendedDocumentReader(filename,PackageManager.class);
331             if(reader != null) {
332                 for(Iterator JavaDoc ns = reader.getChildElements("packagehandlers","packagehandler");ns.hasNext(); ) {
333                     Element JavaDoc n = (Element JavaDoc)ns.next();
334                     NamedNodeMap JavaDoc nm = n.getAttributes();
335                     if (nm != null) {
336                         String JavaDoc type = null;
337                         String JavaDoc classname = null;
338
339                         // decode type
340
org.w3c.dom.Node JavaDoc n2 = nm.getNamedItem("type");
341                         if (n2 != null) {
342                             type = n2.getNodeValue();
343                         }
344
345                         // decode the class
346
n2 = nm.getNamedItem("class");
347                         if (n2 != null) {
348                             classname = n2.getNodeValue();
349                         }
350                         packagehandlers.put(type,classname);
351                     }
352                 }
353             }
354         } else {
355             log.error("missing packagehandler file : "+filename);
356         }
357     }
358
359     public static HashMap JavaDoc getPackageHandlers() {
360         return packagehandlers;
361     }
362
363     public static String JavaDoc getConfigPath() {
364         List JavaDoc files = ResourceLoader.getConfigurationRoot().getFiles("");
365         if (files.size() == 0) {
366             return null;
367         } else {
368             return ((File JavaDoc) files.get(0)).getAbsolutePath();
369         }
370     }
371
372 }
373
Popular Tags