KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.mmbase.module.core.*;
14 import org.mmbase.util.logging.*;
15 import org.mmbase.applications.packaging.providerhandlers.*;
16 import org.mmbase.applications.packaging.bundlehandlers.*;
17 import org.mmbase.module.builders.Versions;
18 import org.mmbase.storage.search.SearchQueryException;
19
20 import java.util.*;
21
22 /**
23  * Bundle manager, keeps track of all the bundles (of packages) available to this
24  * this MMBase. Since bundles themselfs are also packages it can become a little
25  * confusing thats why we have to managers (bundle and packages).
26  *
27  * @author Daniel Ockeloen (MMBased)
28  */

29 public class BundleManager {
30
31     // create a logger for this class
32
private static Logger log = Logging.getLoggerInstance(BundleManager.class);
33
34     // state if this manager is running or not.
35
private static boolean state = false;
36     
37     // Contains all bundles key=bundlename/maintainer value=reference to bundle
38
private static HashMap bundles = new HashMap();
39
40     /**
41     * init this manager
42     */

43     public static synchronized void init() {
44         state = true;
45     }
46
47
48     /**
49     * is this manager running
50     */

51     public static boolean isRunning() {
52         return state;
53     }
54
55     /**
56      * get all the bundles available to this MMBase
57      * @return bundle list
58      */

59     public static Iterator getBundles() {
60         return bundles.values().iterator();
61     }
62
63     /**
64      * get a bundle based on its id
65      *
66      * return Bundle (interface) or null if not found
67      */

68     public static BundleInterface getBundle(String JavaDoc id) {
69         Object JavaDoc o = bundles.get(id);
70         if (o != null) {
71             return (BundleInterface)o;
72         }
73         log.debug("bundle with id = "+id+" not found");
74         return null;
75     }
76
77
78     /**
79     * get a bundle (interface) based on it and wanted version/provider
80     *
81     * return Bundle (interface) or null if not found
82     */

83     public static BundleInterface getBundle(String JavaDoc id,String JavaDoc wv,String JavaDoc wp) {
84         Object JavaDoc o = bundles.get(id);
85         if (o != null) {
86             BundleContainer bc = (BundleContainer)o;
87             ProviderInterface provider = ProviderManager.get(wp);
88             if (provider != null) {
89                 BundleInterface b = bc.getVersion(wv,provider);
90                 if (b != null) {
91                     return b;
92                 }
93             }
94         }
95         log.debug("bundle with id = "+id+" not found");
96         return null;
97     }
98
99
100     public static BundleInterface getBundle(String JavaDoc id,String JavaDoc wv) {
101         Object JavaDoc o = bundles.get(id);
102         if (o != null) {
103             BundleContainer pc = (BundleContainer)o;
104             BundleInterface p = pc.getBundleByScore(wv);
105             if (p != null) {
106                 return p;
107             }
108         }
109         log.debug("bundle with id = "+id+" not found");
110         return null;
111     }
112
113
114     public static BundleInterface foundBundle(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) {
115         // create its id (name+maintainer)
116
String JavaDoc id = name+"@"+maintainer+"_"+type;
117         id = id.replace(' ','_');
118         id = id.replace('/','_');
119     
120         // check if we allready have a bundle container for this
121
BundleContainer bc = (BundleContainer)bundles.get(id);
122
123         boolean found = false;
124         if (bc != null) {
125             // we allready have a container check if we allready
126
// have this one
127
found = bc.contains(version,provider);
128         }
129
130         if (!found) {
131             // so we don't have this bundle refernce yet, then
132
// create and store it, should be a config file
133
BundleInterface newbundle = null;
134
135             // hardcoded handlers, need to be loaded using a xml def.
136
if (type.equals("bundle/basic")) {
137                 newbundle = new BasicBundle(n,provider,name,type,maintainer,version,date,path);
138             }
139             if (bc == null) {
140                 bc = new BundleContainer(newbundle);
141                 // since this is a new container store it
142
bundles.put(id,bc);
143             } else {
144                 bc.addBundle(newbundle);
145             }
146             ((BasicBundle)newbundle).signalUpdate();
147         } else {
148             // get the package to update its available time
149
BasicBundle oldb = (BasicBundle)bc.getVersion(version,provider);
150             if (oldb != null) oldb.signalUpdate();
151         }
152         return bc;
153     }
154
155     
156     public static boolean updateRegistryInstalled(BundleInterface b) {
157         try {
158             Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions");
159             if(versions == null) {
160                 log.error("Versions builder not installed.");
161                 return false;
162             }
163             int newversion = Integer.parseInt(b.getVersion());
164             int oldversion = getInstalledVersion(b.getId());
165             if (oldversion == -1) {
166                 versions.setInstalledVersion(b.getId(),"bundle",b.getMaintainer(),newversion);
167             } else if (oldversion != newversion) {
168                 versions.updateInstalledVersion(b.getId(),"bundle",b.getMaintainer(),newversion);
169             }
170             return true;
171         } catch(Exception JavaDoc e) {
172             log.debug("error while updating " + b + ":" + e.getMessage() , e);
173             return false;
174         }
175     }
176
177
178     public static boolean isInstalledVersion(BundleInterface b) {
179         try {
180             int newversion = Integer.parseInt(b.getVersion());
181             if (getInstalledVersion(b.getId()) == newversion) {
182                 return true;
183             }
184         } catch(Exception JavaDoc e) {
185             log.debug("error while checking if a version of bundle: " + b + " is installed:" + e.getMessage() , e);
186             return false;
187         }
188         return false;
189     }
190
191
192     public static boolean upgradeAvailable(BundleInterface b) {
193         try {
194             int newversion = Integer.parseInt(b.getVersion());
195             int oldversion = getInstalledVersion(b.getId());
196         if (oldversion!=-1 && oldversion!=0 && newversion > oldversion) {
197         return true;
198         }
199         } catch(Exception JavaDoc e) {
200             log.debug("error while checking if a version of bundle: " + b + " is installed:" + e.getMessage() , e);
201             return false;
202         }
203         return false;
204     }
205
206     public static int getInstalledVersion(String JavaDoc id) throws SearchQueryException {
207         // Get the versions builder
208
Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions");
209         if(versions==null) {
210             log.error("Versions builder not installed.");
211             return -1;
212         } else {
213             return versions.getInstalledVersion(id,"bundle");
214         }
215     }
216
217     public static boolean updateRegistryUninstalled(BundleInterface b) {
218         try {
219             Versions versions = (Versions) MMBase.getMMBase().getMMObject("versions");
220             if(versions == null) {
221                 log.error("Versions builder not installed.");
222                 return false;
223             }
224             versions.updateInstalledVersion(b.getId(),"bundle",b.getMaintainer(),0);
225             return true;
226         } catch(Exception JavaDoc e) {
227             log.debug("error while updateing the versions builder to mark " + b + " as uninstalled:" + e.getMessage() , e);
228             return false;
229         }
230     }
231
232
233     /**
234      * return all bundles versions of this id
235      */

236     public static Iterator getBundleVersions(String JavaDoc id) {
237         Object JavaDoc o = bundles.get(id);
238         if (o != null) {
239             BundleContainer bc = (BundleContainer)o;
240             return bc.getVersions();
241         }
242         return null;
243     }
244
245
246     public static boolean removeOfflineBundles(ProviderInterface wantedprov) {
247         // this checks all the bundles if they are still found at their
248
// providers, this is done by checking the last provider update
249
// against the last bundle update
250
HashMap mm = (HashMap)bundles.clone();
251         Iterator e = mm.values().iterator();
252         while (e.hasNext()) {
253             BundleContainer pc = (BundleContainer)e.next();
254             Iterator e2 = pc.getVersions();
255             while (e2.hasNext()) {
256                BundleVersionContainer pvc = (BundleVersionContainer)e2.next();
257                Iterator e3 = pvc.getBundles();
258                while (e3.hasNext()) {
259                    BasicBundle p = (BasicBundle)e3.next();
260                    ProviderInterface prov = p.getProvider();
261                    if (wantedprov == prov) {
262                        long providertime = p.getProvider().lastSeen();
263                        long packagetime = p.lastSeen();
264                        if (providertime > packagetime) {
265                            pvc.removeBundle(p);
266                            if (pvc.getBundleCount() == 0) {
267                                pc.removeBundle(p);
268                                if (pc.getBundleCount() == 0) {
269                                    bundles.remove(pc.getId());
270                                }
271                            }
272                        }
273                    }
274                }
275            }
276         }
277         return true;
278     }
279
280
281 }
282
Popular Tags