KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > providerhandlers > DiskProvider


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

7 package org.mmbase.applications.packaging.providerhandlers;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.jar.JarEntry JavaDoc;
16 import java.util.jar.JarFile JavaDoc;
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 JavaDoc;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29
30 /**
31  * DiskProvider, Handler for Disk Providers. gets packages and bundles from
32  * the provider and feeds them to the package and bundle managers.
33  *
34  * @author Daniel Ockeloen (MMBased)
35  */

36 public class DiskProvider extends BasicProvider implements ProviderInterface {
37     private static Logger log = Logging.getLoggerInstance(DiskProvider.class);
38
39     /**
40      * DTD resource filename of the package DTD version 1.0
41      */

42     public final static String JavaDoc DTD_PACKAGE_1_0 = "package_1_0.dtd";
43     /**
44      * Description of the Field
45      */

46     public final static String JavaDoc DTD_BUNDLE_1_0 = "bundle_1_0.dtd";
47
48     /**
49      * Public ID of the package DTD version 1.0
50      */

51     public final static String JavaDoc PUBLIC_ID_PACKAGE_1_0 = "-//MMBase//DTD package config 1.0//EN";
52     /**
53      * Description of the Field
54      */

55     public final static String JavaDoc PUBLIC_ID_BUNDLE_1_0 = "-//MMBase//DTD bundle config 1.0//EN";
56
57
58     /**
59      * Register the Public Ids for DTDs used by DatabaseReader
60      * This method is called by XMLEntityResolver.
61      */

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     /**
69      *Constructor for the DiskProvider object
70      */

71     public DiskProvider() { }
72
73
74     /**
75      * Description of the Method
76      *
77      * @param n Description of the Parameter
78      * @param name Description of the Parameter
79      * @param method Description of the Parameter
80      * @param maintainer Description of the Parameter
81      */

82     public void init(org.w3c.dom.Node JavaDoc n, String JavaDoc name, String JavaDoc method, String JavaDoc maintainer) {
83         super.init(n, name, method, maintainer);
84         org.w3c.dom.Node JavaDoc n2 = xmlnode.getFirstChild();
85         while (n2 != null) {
86             if (n2.getNodeName().equals("path")) {
87                 org.w3c.dom.Node JavaDoc n3 = n2.getFirstChild();
88                 if (n3 != null) {
89                     path = n3.getNodeValue();
90                 }
91             }
92             if (n2.getNodeName().equals("description")) {
93                 org.w3c.dom.Node JavaDoc 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     /**
107      * Description of the Method
108      *
109      * @param name Description of the Parameter
110      * @param method Description of the Parameter
111      * @param maintainer Description of the Parameter
112      * @param path Description of the Parameter
113      */

114     public void init(String JavaDoc name, String JavaDoc method, String JavaDoc maintainer, String JavaDoc path) {
115         super.init(name, method, maintainer, path);
116         this.path = path;
117         baseScore = 5000;
118     }
119
120
121     /**
122      * Gets the packages attribute of the DiskProvider object
123      */

124     public void getPackages() {
125         signalUpdate();
126
127         String JavaDoc 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 JavaDoc appDir = new File JavaDoc(realpath);
135
136         if (!appDir.isDirectory()) {
137             //log.error("DiskProvider the place where *.mmp files should be is not a directory : path="+realpath+" ignoring this Provider");
138
setState("down");
139             return;
140         }
141
142         String JavaDoc files[] = appDir.list();
143
144         for (int i = 0; i < files.length; i++) {
145             String JavaDoc filename = files[i];
146             if (filename.endsWith(".mmp") && filename.indexOf(".") != 0) {
147
148                 // open the jar to read the input xml
149
try {
150                     JarFile JavaDoc jarFile = new JarFile JavaDoc(realpath + filename);
151                     JarEntry JavaDoc je = jarFile.getJarEntry("package.xml");
152                     if (je != null) {
153                         InputStream JavaDoc input = jarFile.getInputStream(je);
154                         ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource JavaDoc(input), DiskProvider.class);
155                         if (reader != null) {
156                             String JavaDoc name = reader.getElementAttributeValue("package", "name");
157                             String JavaDoc type = reader.getElementAttributeValue("package", "type");
158                             String JavaDoc maintainer = reader.getElementAttributeValue("package", "maintainer");
159                             String JavaDoc version = reader.getElementAttributeValue("package", "version");
160                             String JavaDoc date = reader.getElementAttributeValue("package", "creation-date");
161                             Element JavaDoc e = reader.getElementByPath("package");
162                             PackageInterface pack = PackageManager.foundPackage(this, e, name, type, maintainer, version, date, realpath + filename);
163                         }
164                     }
165                 } catch (Exception JavaDoc e) {
166                     //log.error("Can't open jar file "+realpath+filename);
167
}
168             } else if (filename.endsWith(".mmb")) {
169
170                 // open the jar to read the input xml
171
try {
172                     JarFile JavaDoc jarFile = new JarFile JavaDoc(realpath + filename);
173                     JarEntry JavaDoc je = jarFile.getJarEntry("bundle.xml");
174                     if (je != null) {
175                         InputStream JavaDoc input = jarFile.getInputStream(je);
176                         ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource JavaDoc(input), DiskProvider.class);
177                         if (reader != null) {
178                             String JavaDoc name = reader.getElementAttributeValue("bundle", "name");
179                             String JavaDoc type = reader.getElementAttributeValue("bundle", "type");
180                             String JavaDoc maintainer = reader.getElementAttributeValue("bundle", "maintainer");
181                             String JavaDoc version = reader.getElementAttributeValue("bundle", "version");
182                             String JavaDoc date = reader.getElementAttributeValue("bundle", "creation-date");
183                             Element JavaDoc e = reader.getElementByPath("bundle");
184                             BundleInterface bun = BundleManager.foundBundle(this, e, name, type, maintainer, version, date, realpath + filename);
185                             // check for included packages in the bundle
186
findIncludedPackages(e, realpath, date, realpath + filename, bun);
187                         }
188                     }
189                 } catch (Exception JavaDoc e) {
190                     //log.error("Can't open jar file "+realpath+filename);
191
}
192             }
193         }
194         setState("up");
195     }
196
197     /**
198      * Gets the includedPackageJarFile attribute of the DiskProvider object
199      *
200      * @param path Description of the Parameter
201      * @param id Description of the Parameter
202      * @param version Description of the Parameter
203      * @param packageid Description of the Parameter
204      * @param packageversion Description of the Parameter
205      * @return The includedPackageJarFile value
206      */

207     public JarFile JavaDoc getIncludedPackageJarFile(String JavaDoc path, String JavaDoc id, String JavaDoc version, String JavaDoc packageid, String JavaDoc packageversion) {
208         // it should now be in our import dir for us to get the package from
209
try {
210             JarFile JavaDoc jarFile = new JarFile JavaDoc(path);
211             JarEntry JavaDoc je = jarFile.getJarEntry(packageid + "_" + packageversion + ".mmp");
212             try {
213                 BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(jarFile.getInputStream(je));
214                 BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc e) {
221                 log.error("can't load : " + path);
222                 e.printStackTrace();
223             }
224             JarFile JavaDoc tmpjarfile = new JarFile JavaDoc(getImportPath() + ".temp_" + packageid + "_" + packageversion + ".mmp");
225             return tmpjarfile;
226         } catch (Exception JavaDoc e) {
227             log.error("can't load : " + path);
228             e.printStackTrace();
229         }
230         return null;
231     }
232
233
234     /**
235      * Gets the jarFile attribute of the DiskProvider object
236      *
237      * @param path Description of the Parameter
238      * @param id Description of the Parameter
239      * @param version Description of the Parameter
240      * @return The jarFile value
241      */

242     public JarFile JavaDoc getJarFile(String JavaDoc path, String JavaDoc id, String JavaDoc version) {
243         String JavaDoc realpath = getImportPath();
244         try {
245             JarFile JavaDoc jarFile = new JarFile JavaDoc(path);
246             if (path.endsWith("mmb") && id.indexOf("_bundle_") == -1) {
247                 JarEntry JavaDoc je = jarFile.getJarEntry(id + "_" + version + ".mmp");
248                 try {
249                     InputStream JavaDoc in = jarFile.getInputStream(je);
250                     BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc e) {
257                     log.error("can't load : " + path);
258                     e.printStackTrace();
259                 }
260                 JarFile JavaDoc tmpjarfile = new JarFile JavaDoc(realpath + "/.temp_" + id + "_" + version + ".mmp");
261                 return tmpjarfile;
262             }
263             return jarFile;
264         } catch (Exception JavaDoc e) {
265             log.error("can't load : " + path);
266             e.printStackTrace();
267         }
268         return null;
269     }
270
271
272     /**
273      * Gets the jarStream attribute of the DiskProvider object
274      *
275      * @param path Description of the Parameter
276      * @return The jarStream value
277      */

278     public BufferedInputStream JavaDoc getJarStream(String JavaDoc path) {
279         try {
280             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(path));
281             return in;
282         } catch (Exception JavaDoc e) {
283             log.error("can't load : " + path);
284             e.printStackTrace();
285         }
286         return null;
287     }
288
289
290     /**
291      * Description of the Method
292      *
293      * @return Description of the Return Value
294      */

295     public boolean close() {
296         return super.close();
297     }
298
299
300     /**
301      * Description of the Method
302      *
303      * @param n Description of the Parameter
304      * @param realpath Description of the Parameter
305      * @param date Description of the Parameter
306      * @param filename Description of the Parameter
307      * @param bun Description of the Parameter
308      */

309     private void findIncludedPackages(org.w3c.dom.Node JavaDoc n, String JavaDoc realpath, String JavaDoc date, String JavaDoc filename, BundleInterface bun) {
310         org.w3c.dom.Node JavaDoc n2 = n.getFirstChild();
311         while (n2 != null) {
312             String JavaDoc name = n2.getNodeName();
313             // this should me one way defined (remote or local)
314
if (name.equals("neededpackages")) {
315                 org.w3c.dom.Node JavaDoc n3 = n2.getFirstChild();
316                 while (n3 != null) {
317                     name = n3.getNodeName();
318                     NamedNodeMap JavaDoc nm = n3.getAttributes();
319                     if (nm != null) {
320                         String JavaDoc maintainer = null;
321                         String JavaDoc type = null;
322                         String JavaDoc version = null;
323                         boolean included = false;
324
325                         // decode name
326
org.w3c.dom.Node JavaDoc n5 = nm.getNamedItem("name");
327                         if (n5 != null) {
328                             name = n5.getNodeValue();
329                         }
330
331                         // decode the type
332
n5 = nm.getNamedItem("type");
333                         if (n5 != null) {
334                             type = n5.getNodeValue();
335                         }
336
337                         // decode the maintainer
338
n5 = nm.getNamedItem("maintainer");
339                         if (n5 != null) {
340                             maintainer = n5.getNodeValue();
341                         }
342
343                         // decode the version
344
n5 = nm.getNamedItem("version");
345                         if (n5 != null) {
346                             version = n5.getNodeValue();
347                         }
348
349                         // decode the included
350
n5 = nm.getNamedItem("included");
351                         if (n5 != null) {
352                             if (n5.getNodeValue().equals("true")) {
353                                 included = true;
354                             }
355                         }
356
357                         // done
358
if (included) {
359                             PackageInterface pack = PackageManager.foundPackage(this, null, name, type, maintainer, version, date, filename);
360                             // returns a package if new one
361
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     /**
375      * Gets the importPath attribute of the DiskProvider object
376      *
377      * @return The importPath value
378      */

379     public String JavaDoc getImportPath() {
380         String JavaDoc path = PackageManager.getConfigPath() + File.separator + "packaging" + File.separator + "import" + File.separator;
381         File JavaDoc dir = new File JavaDoc(path);
382         if (!dir.exists()) {
383             dir.mkdir();
384         }
385         return path;
386     }
387
388 }
389
390
Popular Tags