1 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 29 public class BundleManager { 30 31 private static Logger log = Logging.getLoggerInstance(BundleManager.class); 33 34 private static boolean state = false; 36 37 private static HashMap bundles = new HashMap(); 39 40 43 public static synchronized void init() { 44 state = true; 45 } 46 47 48 51 public static boolean isRunning() { 52 return state; 53 } 54 55 59 public static Iterator getBundles() { 60 return bundles.values().iterator(); 61 } 62 63 68 public static BundleInterface getBundle(String id) { 69 Object 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 83 public static BundleInterface getBundle(String id,String wv,String wp) { 84 Object 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 id,String wv) { 101 Object 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 n,String name,String type,String maintainer,String version,String date,String path) { 115 String id = name+"@"+maintainer+"_"+type; 117 id = id.replace(' ','_'); 118 id = id.replace('/','_'); 119 120 BundleContainer bc = (BundleContainer)bundles.get(id); 122 123 boolean found = false; 124 if (bc != null) { 125 found = bc.contains(version,provider); 128 } 129 130 if (!found) { 131 BundleInterface newbundle = null; 134 135 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 bundles.put(id,bc); 143 } else { 144 bc.addBundle(newbundle); 145 } 146 ((BasicBundle)newbundle).signalUpdate(); 147 } else { 148 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 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 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 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 id) throws SearchQueryException { 207 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 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 236 public static Iterator getBundleVersions(String id) { 237 Object 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 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 |