KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import org.mmbase.applications.packaging.PackageManager;
15 import org.mmbase.applications.packaging.ProviderManager;
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 /**
20  * @author danielockeloen
21  */

22 public class ProviderFileWriter {
23
24     private static Logger log = Logging.getLoggerInstance(ProviderFileWriter.class);
25
26
27     /**
28      * Description of the Method
29      *
30      * @return Description of the Return Value
31      */

32     public static boolean write() {
33         String JavaDoc body =
34                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
35                 "<!DOCTYPE providers PUBLIC \"-//MMBase/DTD providers config 1.0//EN\" \"http://www.mmbase.org/dtd/providers_1_0.dtd\">\n";
36         body += "<providers>\n";
37
38         body += writeProviders();
39
40         body += "</providers>\n";
41
42         String JavaDoc filename = PackageManager.getConfigPath() + File.separator + "packaging" + File.separator + "providers.xml";
43         saveFile(filename, body);
44         return true;
45     }
46
47
48     /**
49      * Description of the Method
50      *
51      * @return Description of the Return Value
52      */

53     private static String JavaDoc writeProviders() {
54         String JavaDoc result = "";
55         Iterator JavaDoc e = ProviderManager.getProviders();
56         while (e.hasNext()) {
57             ProviderInterface p = (ProviderInterface) e.next();
58             String JavaDoc method = p.getMethod();
59             result += "\t<provider name=\"" + p.getName() + "\" maintainer=\"" + p.getMaintainer() + "\" ";
60             result += "method=\"" + method + "\" ";
61             result += ">\n";
62             if (method.equals("http")) {
63                 HttpProvider p2 = (HttpProvider) p;
64                 result += "\t\t<path>" + p2.getPath() + "</path>\n";
65                 result += "\t\t<account>" + p2.getAccount() + "</account>\n";
66                 result += "\t\t<password>" + p2.getPassword() + "</password>\n";
67                 result += "\t\t<description>" + p2.getDescription() + "</description>\n";
68             } else if (method.equals("disk")) {
69                 result += "\t\t<path>" + p.getPath() + "</path>\n";
70                 result += "\t\t<description>" + p.getDescription() + "</description>\n";
71             }
72
73             result += "\t</provider>\n";
74         }
75         return result;
76     }
77
78
79     /**
80      * Description of the Method
81      *
82      * @param filename Description of the Parameter
83      * @param value Description of the Parameter
84      * @return Description of the Return Value
85      */

86     static boolean saveFile(String JavaDoc filename, String JavaDoc value) {
87         File JavaDoc sfile = new File JavaDoc(filename);
88         try {
89             DataOutputStream JavaDoc scan = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(sfile));
90             scan.writeBytes(value);
91             scan.flush();
92             scan.close();
93         } catch (Exception JavaDoc e) {
94             log.error(Logging.stackTrace(e));
95         }
96         return true;
97     }
98
99 }
100
101
Popular Tags