1 10 11 package org.mmbase.applications.packaging; 12 13 import java.io.File ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 ; 32 import org.w3c.dom.NamedNodeMap ; 33 34 39 public class PackageManager { 40 private static Logger log = Logging.getLoggerInstance(PackageManager.class); 41 42 private static HashMap packages = new HashMap (); 44 45 private static boolean state = false; 47 private static HashMap packagehandlers; 48 49 public static final String DTD_PACKAGEHANDLERS_1_0 = "packagehandlers_1_0.dtd"; 50 public static final String PUBLIC_ID_PACKAGEHANDLERS_1_0 = "-//MMBase//DTD packagehandlers config 1.0//EN"; 51 52 56 public static void registerPublicIDs() { 57 XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGEHANDLERS_1_0, DTD_PACKAGEHANDLERS_1_0, PackageManager.class); 58 } 59 63 public static synchronized void init() { 64 readPackageHandlers(); 65 state = true; 66 } 67 68 public static boolean isRunning() { 69 return state; 70 } 71 72 76 public static Iterator getPackages() { 77 return packages.values().iterator(); 78 } 79 80 84 public static Iterator getPackageVersions(String id) { 85 Object 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 97 public static Iterator getPackageVersionNumbers(String id) { 98 Object o = packages.get(id); 99 if (o != null) { 100 PackageContainer pc = (PackageContainer)o; 101 return pc.getVersionNumbers(); 102 } 103 return null; 104 } 105 106 110 public static PackageInterface getPackage(String id) { 111 Object o = packages.get(id); 112 if (o != null) { 113 return (PackageInterface)o; 114 } 115 return null; 116 } 117 118 119 123 public static PackageInterface getPackage(String id,String wv,String wp) { 124 Object 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 143 public static PackageInterface getPackage(String id,String wv) { 144 Object 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 160 public static PackageInterface foundPackage(ProviderInterface provider,org.w3c.dom.Element n,String name,String type,String maintainer,String version,String date,String path) { 161 162 String id = name+"@"+maintainer+"_"+type; 164 id = id.replace(' ','_'); 165 id = id.replace('/','_'); 166 167 PackageContainer pc = (PackageContainer)packages.get(id); 169 170 boolean found = false; 171 if (pc != null) { 172 found = pc.contains(version,provider); 175 } 176 177 if (!found) { 178 181 String classname = (String )packagehandlers.get(type); 183 if (classname != null) { 184 try { 185 Class 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 packages.put(id,pc); 192 } else { 193 pc.addPackage(newpackage); 194 } 195 ((BasicPackage)newpackage).signalUpdate(); 196 return newpackage; 197 } catch(Exception 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 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 id) throws SearchQueryException { 213 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 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 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 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 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 Iterator e = ((HashMap )packages.clone()).values().iterator(); 295 while (e.hasNext()) { 296 PackageContainer pc = (PackageContainer)e.next(); 297 Iterator e2 = pc.getVersions(); 298 while (e2.hasNext()) { 299 PackageVersionContainer pvc = (PackageVersionContainer)e2.next(); 300 Iterator 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 (); 325 String filename = getConfigPath()+File.separator+"packaging"+File.separator+"packagehandlers.xml"; 326 327 File file = new File (filename); 328 if(file.exists()) { 329 330 ExtendedDocumentReader reader = new ExtendedDocumentReader(filename,PackageManager.class); 331 if(reader != null) { 332 for(Iterator ns = reader.getChildElements("packagehandlers","packagehandler");ns.hasNext(); ) { 333 Element n = (Element )ns.next(); 334 NamedNodeMap nm = n.getAttributes(); 335 if (nm != null) { 336 String type = null; 337 String classname = null; 338 339 org.w3c.dom.Node n2 = nm.getNamedItem("type"); 341 if (n2 != null) { 342 type = n2.getNodeValue(); 343 } 344 345 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 getPackageHandlers() { 360 return packagehandlers; 361 } 362 363 public static String getConfigPath() { 364 List files = ResourceLoader.getConfigurationRoot().getFiles(""); 365 if (files.size() == 0) { 366 return null; 367 } else { 368 return ((File ) files.get(0)).getAbsolutePath(); 369 } 370 } 371 372 } 373 | Popular Tags |