| 1 7 package org.mmbase.applications.packaging.providerhandlers; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.InputStream ; 15 import java.util.jar.JarEntry ; 16 import java.util.jar.JarFile ; 17 18 import org.mmbase.applications.packaging.BundleManager; 19 import org.mmbase.applications.packaging.PackageManager; 20 import org.mmbase.applications.packaging.bundlehandlers.BundleInterface; 21 import org.mmbase.applications.packaging.packagehandlers.PackageInterface; 22 import org.mmbase.applications.packaging.util.ExtendedDocumentReader; 23 import org.mmbase.util.XMLEntityResolver; 24 import org.mmbase.util.logging.Logger; 25 import org.mmbase.util.logging.Logging; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.NamedNodeMap ; 28 import org.xml.sax.InputSource ; 29 30 36 public class DiskProvider extends BasicProvider implements ProviderInterface { 37 private static Logger log = Logging.getLoggerInstance(DiskProvider.class); 38 39 42 public final static String DTD_PACKAGE_1_0 = "package_1_0.dtd"; 43 46 public final static String DTD_BUNDLE_1_0 = "bundle_1_0.dtd"; 47 48 51 public final static String PUBLIC_ID_PACKAGE_1_0 = "-//MMBase//DTD package config 1.0//EN"; 52 55 public final static String PUBLIC_ID_BUNDLE_1_0 = "-//MMBase//DTD bundle config 1.0//EN"; 56 57 58 62 public static void registerPublicIDs() { 63 XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGE_1_0, DTD_PACKAGE_1_0, DiskProvider.class); 64 XMLEntityResolver.registerPublicID(PUBLIC_ID_BUNDLE_1_0, DTD_BUNDLE_1_0, DiskProvider.class); 65 } 66 67 68 71 public DiskProvider() { } 72 73 74 82 public void init(org.w3c.dom.Node n, String name, String method, String maintainer) { 83 super.init(n, name, method, maintainer); 84 org.w3c.dom.Node n2 = xmlnode.getFirstChild(); 85 while (n2 != null) { 86 if (n2.getNodeName().equals("path")) { 87 org.w3c.dom.Node n3 = n2.getFirstChild(); 88 if (n3 != null) { 89 path = n3.getNodeValue(); 90 } 91 } 92 if (n2.getNodeName().equals("description")) { 93 org.w3c.dom.Node n3 = n2.getFirstChild(); 94 if (n3 != null) { 95 description = n3.getNodeValue(); 96 } 97 } 98 n2 = n2.getNextSibling(); 99 } 100 101 baseScore = 5000; 102 getPackages(); 103 } 104 105 106 114 public void init(String name, String method, String maintainer, String path) { 115 super.init(name, method, maintainer, path); 116 this.path = path; 117 baseScore = 5000; 118 } 119 120 121 124 public void getPackages() { 125 signalUpdate(); 126 127 String realpath = path; 128 if (realpath.indexOf("~import") == 0) { 129 realpath = getImportPath(); 130 } 131 if (realpath.indexOf("~build") == 0) { 132 realpath = PackageManager.getConfigPath() + "/packaging/build/"; 133 } 134 File appDir = new File (realpath); 135 136 if (!appDir.isDirectory()) { 137 setState("down"); 139 return; 140 } 141 142 String files[] = appDir.list(); 143 144 for (int i = 0; i < files.length; i++) { 145 String filename = files[i]; 146 if (filename.endsWith(".mmp") && filename.indexOf(".") != 0) { 147 148 try { 150 JarFile jarFile = new JarFile (realpath + filename); 151 JarEntry je = jarFile.getJarEntry("package.xml"); 152 if (je != null) { 153 InputStream input = jarFile.getInputStream(je); 154 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource (input), DiskProvider.class); 155 if (reader != null) { 156 String name = reader.getElementAttributeValue("package", "name"); 157 String type = reader.getElementAttributeValue("package", "type"); 158 String maintainer = reader.getElementAttributeValue("package", "maintainer"); 159 String version = reader.getElementAttributeValue("package", "version"); 160 String date = reader.getElementAttributeValue("package", "creation-date"); 161 Element e = reader.getElementByPath("package"); 162 PackageInterface pack = PackageManager.foundPackage(this, e, name, type, maintainer, version, date, realpath + filename); 163 } 164 } 165 } catch (Exception e) { 166 } 168 } else if (filename.endsWith(".mmb")) { 169 170 try { 172 JarFile jarFile = new JarFile (realpath + filename); 173 JarEntry je = jarFile.getJarEntry("bundle.xml"); 174 if (je != null) { 175 InputStream input = jarFile.getInputStream(je); 176 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource (input), DiskProvider.class); 177 if (reader != null) { 178 String name = reader.getElementAttributeValue("bundle", "name"); 179 String type = reader.getElementAttributeValue("bundle", "type"); 180 String maintainer = reader.getElementAttributeValue("bundle", "maintainer"); 181 String version = reader.getElementAttributeValue("bundle", "version"); 182 String date = reader.getElementAttributeValue("bundle", "creation-date"); 183 Element e = reader.getElementByPath("bundle"); 184 BundleInterface bun = BundleManager.foundBundle(this, e, name, type, maintainer, version, date, realpath + filename); 185 findIncludedPackages(e, realpath, date, realpath + filename, bun); 187 } 188 } 189 } catch (Exception e) { 190 } 192 } 193 } 194 setState("up"); 195 } 196 197 207 public JarFile getIncludedPackageJarFile(String path, String id, String version, String packageid, String packageversion) { 208 try { 210 JarFile jarFile = new JarFile (path); 211 JarEntry je = jarFile.getJarEntry(packageid + "_" + packageversion + ".mmp"); 212 try { 213 BufferedInputStream in = new BufferedInputStream (jarFile.getInputStream(je)); 214 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (getImportPath() + ".temp_" + packageid + "_" + packageversion + ".mmp")); 215 int val; 216 while ((val = in.read()) != -1) { 217 out.write(val); 218 } 219 out.close(); 220 } catch (Exception e) { 221 log.error("can't load : " + path); 222 e.printStackTrace(); 223 } 224 JarFile tmpjarfile = new JarFile (getImportPath() + ".temp_" + packageid + "_" + packageversion + ".mmp"); 225 return tmpjarfile; 226 } catch (Exception e) { 227 log.error("can't load : " + path); 228 e.printStackTrace(); 229 } 230 return null; 231 } 232 233 234 242 public JarFile getJarFile(String path, String id, String version) { 243 String realpath = getImportPath(); 244 try { 245 JarFile jarFile = new JarFile (path); 246 if (path.endsWith("mmb") && id.indexOf("_bundle_") == -1) { 247 JarEntry je = jarFile.getJarEntry(id + "_" + version + ".mmp"); 248 try { 249 InputStream in = jarFile.getInputStream(je); 250 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (realpath + "/.temp_" + id + "_" + version + ".mmp")); 251 int val; 252 while ((val = in.read()) != -1) { 253 out.write(val); 254 } 255 out.close(); 256 } catch (Exception e) { 257 log.error("can't load : " + path); 258 e.printStackTrace(); 259 } 260 JarFile tmpjarfile = new JarFile (realpath + "/.temp_" + id + "_" + version + ".mmp"); 261 return tmpjarfile; 262 } 263 return jarFile; 264 } catch (Exception e) { 265 log.error("can't load : " + path); 266 e.printStackTrace(); 267 } 268 return null; 269 } 270 271 272 278 public BufferedInputStream getJarStream(String path) { 279 try { 280 BufferedInputStream in = new BufferedInputStream (new FileInputStream (path)); 281 return in; 282 } catch (Exception e) { 283 log.error("can't load : " + path); 284 e.printStackTrace(); 285 } 286 return null; 287 } 288 289 290 295 public boolean close() { 296 return super.close(); 297 } 298 299 300 309 private void findIncludedPackages(org.w3c.dom.Node n, String realpath, String date, String filename, BundleInterface bun) { 310 org.w3c.dom.Node n2 = n.getFirstChild(); 311 while (n2 != null) { 312 String name = n2.getNodeName(); 313 if (name.equals("neededpackages")) { 315 org.w3c.dom.Node n3 = n2.getFirstChild(); 316 while (n3 != null) { 317 name = n3.getNodeName(); 318 NamedNodeMap nm = n3.getAttributes(); 319 if (nm != null) { 320 String maintainer = null; 321 String type = null; 322 String version = null; 323 boolean included = false; 324 325 org.w3c.dom.Node n5 = nm.getNamedItem("name"); 327 if (n5 != null) { 328 name = n5.getNodeValue(); 329 } 330 331 n5 = nm.getNamedItem("type"); 333 if (n5 != null) { 334 type = n5.getNodeValue(); 335 } 336 337 n5 = nm.getNamedItem("maintainer"); 339 if (n5 != null) { 340 maintainer = n5.getNodeValue(); 341 } 342 343 n5 = nm.getNamedItem("version"); 345 if (n5 != null) { 346 version = n5.getNodeValue(); 347 } 348 349 n5 = nm.getNamedItem("included"); 351 if (n5 != null) { 352 if (n5.getNodeValue().equals("true")) { 353 included = true; 354 } 355 } 356 357 if (included) { 359 PackageInterface pack = PackageManager.foundPackage(this, null, name, type, maintainer, version, date, filename); 360 if (pack != null) { 362 pack.setParentBundle(bun); 363 } 364 } 365 } 366 n3 = n3.getNextSibling(); 367 } 368 } 369 n2 = n2.getNextSibling(); 370 } 371 } 372 373 374 379 public String getImportPath() { 380 String path = PackageManager.getConfigPath() + File.separator + "packaging" + File.separator + "import" + File.separator; 381 File dir = new File (path); 382 if (!dir.exists()) { 383 dir.mkdir(); 384 } 385 return path; 386 } 387 388 } 389 390 | Popular Tags |