KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > sharehandlers > ShareFileWriter


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.sharehandlers;
8
9 import java.io.DataOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import org.mmbase.applications.packaging.PackageManager;
16 import org.mmbase.applications.packaging.ShareManager;
17 import org.mmbase.applications.packaging.bundlehandlers.BundleContainer;
18 import org.mmbase.applications.packaging.packagehandlers.PackageContainer;
19 import org.mmbase.util.logging.Logger;
20 import org.mmbase.util.logging.Logging;
21
22 /**
23  * @author danielockeloen
24  */

25 public class ShareFileWriter {
26
27     private static Logger log = Logging.getLoggerInstance(ShareFileWriter.class);
28
29
30     /**
31      * Description of the Method
32      *
33      * @return Description of the Return Value
34      */

35     public static boolean write() {
36         String JavaDoc body =
37                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
38                 "<!DOCTYPE shared PUBLIC \"-//MMBase/DTD shared config 1.0//EN\" \"http://www.mmbase.org/dtd/shared_1_0.dtd\">\n";
39         body += "<shared>\n";
40
41         body += writeSharedBundles();
42         body += writeSharedPackages();
43         body += writeUsers();
44         body += writeGroups();
45         body += writeSettings();
46         body += writeProvidingPaths();
47
48         body += "</shared>\n";
49
50         String JavaDoc filename = PackageManager.getConfigPath() + File.separator + "packaging" + File.separator + "sharing.xml";
51         saveFile(filename, body);
52         return true;
53     }
54
55
56     /**
57      * Description of the Method
58      *
59      * @return Description of the Return Value
60      */

61     private static String JavaDoc writeSharedPackages() {
62         String JavaDoc result = "\t<packages>\n";
63         Iterator JavaDoc e = ShareManager.getSharedPackages();
64         while (e.hasNext()) {
65             PackageContainer p = (PackageContainer) e.next();
66             ShareInfo shareinfo = p.getShareInfo();
67             result += "\t\t<package name=\"" + p.getName() + "\" maintainer=\"" + p.getMaintainer() + "\" ";
68             result += "type=\"" + p.getType() + "\" ";
69             result += "versions=\"best\" ";
70             result += "active=\"" + shareinfo.isActive() + "\" ";
71             result += ">\n";
72             Iterator JavaDoc e2 = shareinfo.getShareUsers();
73             while (e2.hasNext()) {
74                 ShareUser su = (ShareUser) e2.next();
75                 result += "\t\t\t<login user=\"" + su.getName() + "\" />\n";
76             }
77             e2 = shareinfo.getShareGroups();
78             while (e2.hasNext()) {
79                 ShareGroup sg = (ShareGroup) e2.next();
80                 result += "\t\t\t<login group=\"" + sg.getName() + "\" />\n";
81             }
82             result += "\t\t</package>\n";
83         }
84         result += "\t</packages>\n";
85         return result;
86     }
87
88
89     /**
90      * Description of the Method
91      *
92      * @return Description of the Return Value
93      */

94     private static String JavaDoc writeSharedBundles() {
95         String JavaDoc result = "\t<bundles>\n";
96         Iterator JavaDoc e = ShareManager.getSharedBundles();
97         while (e.hasNext()) {
98             BundleContainer b = (BundleContainer) e.next();
99             ShareInfo shareinfo = b.getShareInfo();
100             result += "\t\t<bundle name=\"" + b.getName() + "\" maintainer=\"" + b.getMaintainer() + "\" ";
101             result += "type=\"" + b.getType() + "\" ";
102             result += "versions=\"best\" ";
103             result += "active=\"" + shareinfo.isActive() + "\" ";
104             result += ">\n";
105             Iterator JavaDoc e2 = shareinfo.getShareUsers();
106             while (e2.hasNext()) {
107                 ShareUser su = (ShareUser) e2.next();
108                 result += "\t\t\t<login user=\"" + su.getName() + "\" />\n";
109             }
110             e2 = shareinfo.getShareGroups();
111             while (e2.hasNext()) {
112                 ShareGroup sg = (ShareGroup) e2.next();
113                 result += "\t\t\t<login group=\"" + sg.getName() + "\" />\n";
114             }
115             result += "\t\t</bundle>\n";
116         }
117         result += "\t</bundles>\n";
118         return result;
119     }
120
121
122     /**
123      * Description of the Method
124      *
125      * @return Description of the Return Value
126      */

127     private static String JavaDoc writeUsers() {
128         String JavaDoc result = "\t<users>\n";
129         Iterator JavaDoc e = ShareManager.getShareUsers();
130         while (e.hasNext()) {
131             ShareUser su = (ShareUser) e.next();
132             result += "\t\t<user name=\"" + su.getName() + "\" password=\"" + su.getPassword() + "\" ";
133             if (su.getMethod() != null) {
134                 result += "method=\"" + su.getMethod() + "\" ";
135             }
136             if (su.getHost() != null) {
137                 result += "ip=\"" + su.getHost() + "\" ";
138             }
139             result += "/>\n";
140         }
141         result += "\t</users>\n";
142         return result;
143     }
144
145
146     /**
147      * Description of the Method
148      *
149      * @return Description of the Return Value
150      */

151     private static String JavaDoc writeProvidingPaths() {
152         String JavaDoc result = "\t<providingpaths>\n";
153         HashMap JavaDoc has = ShareManager.getProvidingPaths();
154         Iterator JavaDoc e = has.keySet().iterator();
155         while (e.hasNext()) {
156             String JavaDoc key = (String JavaDoc) e.next();
157             String JavaDoc path = (String JavaDoc) has.get(key);
158             result += "\t\t<providingpath method=\"" + key + "\" path=\"" + path + "\" />\n";
159         }
160         result += "\t</providingpaths>\n";
161         return result;
162     }
163
164
165     /**
166      * Description of the Method
167      *
168      * @return Description of the Return Value
169      */

170     private static String JavaDoc writeGroups() {
171         String JavaDoc result = "\t<groups>\n";
172         Iterator JavaDoc e = ShareManager.getShareGroups();
173         while (e.hasNext()) {
174             ShareGroup sg = (ShareGroup) e.next();
175             result += "\t\t<group name=\"" + sg.getName() + "\">\n";
176             Iterator JavaDoc e2 = sg.getMembers();
177             while (e2.hasNext()) {
178                 ShareUser su = (ShareUser) e2.next();
179                 result += "\t\t\t<member user=\"" + su.getName() + "\" />\n";
180             }
181             result += "\t\t</group>\n";
182         }
183         result += "\t</groups>\n";
184         return result;
185     }
186
187
188     /**
189      * Description of the Method
190      *
191      * @return Description of the Return Value
192      */

193     private static String JavaDoc writeSettings() {
194         String JavaDoc result = "\t<settings>\n";
195         result += "\t\t<providername>" + ShareManager.getProviderName() + "</providername>\n";
196         result += "\t\t<callbackurl>" + ShareManager.getCallbackUrl() + "</callbackurl>\n";
197         result += "\t</settings>\n";
198         return result;
199     }
200
201
202     /**
203      * Description of the Method
204      *
205      * @param filename Description of the Parameter
206      * @param value Description of the Parameter
207      * @return Description of the Return Value
208      */

209     static boolean saveFile(String JavaDoc filename, String JavaDoc value) {
210         File JavaDoc sfile = new File JavaDoc(filename);
211         try {
212             DataOutputStream JavaDoc scan = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(sfile));
213             scan.writeBytes(value);
214             scan.flush();
215             scan.close();
216         } catch (Exception JavaDoc e) {
217             log.error(Logging.stackTrace(e));
218         }
219         //log.info("FILE="+value);
220
return true;
221     }
222
223 }
224
225
Popular Tags