KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > packagehandlers > FunctionSetPackage


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.packagehandlers;
8
9 import java.io.BufferedOutputStream JavaDoc;
10 import java.io.DataOutputStream JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.jar.JarEntry JavaDoc;
17 import java.util.jar.JarFile JavaDoc;
18
19 import org.mmbase.applications.packaging.PackageManager;
20 import org.mmbase.applications.packaging.installhandlers.installStep;
21 import org.mmbase.applications.packaging.util.ExtendedDocumentReader;
22 import org.mmbase.util.XMLEntityResolver;
23 import org.mmbase.util.logging.Logger;
24 import org.mmbase.util.logging.Logging;
25 import org.w3c.dom.Element JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27
28 /**
29  * FunctionSetPackage, Handler for html packages
30  *
31  * @author Daniel Ockeloen (MMBased)
32  */

33 public class FunctionSetPackage extends BasicPackage implements PackageInterface {
34
35     private static Logger log = Logging.getLoggerInstance(DisplayHtmlPackage.class);
36
37     /**
38      * DTD resource filename of the functionsets DTD version 1.0
39      */

40     public final static String JavaDoc DTD_FUNCTIONSETS_1_0 = "functionsets_1_0.dtd";
41
42     /**
43      * Public ID of the functionsets DTD version 1.0
44      */

45     public final static String JavaDoc PUBLIC_ID_FUNCTIONSETS_1_0 = "-//MMBase//DTD functionsets config 1.0//EN";
46
47
48     /**
49      * Register the Public Ids for DTDs used by DatabaseReader
50      * This method is called by XMLEntityResolver.
51      */

52     public static void registerPublicIDs() {
53         XMLEntityResolver.registerPublicID(PUBLIC_ID_FUNCTIONSETS_1_0, DTD_FUNCTIONSETS_1_0, FunctionSetPackage.class);
54     }
55
56
57     /**
58      *Constructor for the FunctionSetPackage object
59      */

60     public FunctionSetPackage() { }
61
62
63     /**
64      * Description of the Method
65      *
66      * @return Description of the Return Value
67      */

68     public boolean install() {
69         boolean result = true;
70         // needs to be set to false on a error
71
try {
72
73             // step1
74
installStep step = getNextInstallStep();
75             step.setUserFeedBack("function/set installer started");
76
77             // step 2
78
step = getNextInstallStep();
79             step.setUserFeedBack("receiving package ..");
80             JarFile JavaDoc jf = getJarFile();
81             if (jf != null) {
82                 step.setUserFeedBack("receiving package ... done (" + jf + ")");
83
84                 // step 3
85
step = getNextInstallStep();
86                 step.setUserFeedBack("checking dependencies ..");
87                 if (dependsInstalled(jf, step)) {
88
89                     step.setUserFeedBack("checking dependencies ... done");
90
91                     // step 4
92
step = getNextInstallStep();
93                     step.setUserFeedBack("installing functionsets ..");
94                     installFunctionSets(jf, step);
95                     step.setUserFeedBack("installing functionsets ... done");
96
97                     // step 5
98
step = getNextInstallStep();
99                     step.setUserFeedBack("updating mmbase registry ..");
100                     updateRegistryInstalled();
101                     step.setUserFeedBack("updating mmbase registry ... done");
102                 } else {
103                     step.setUserFeedBack("checking dependencies ... failed");
104                     setState("failed");
105                     result = false;
106                 }
107             } else {
108                 step.setUserFeedBack("getting the mmp package...failed (server down or removed disk ? )");
109                 step.setType(installStep.TYPE_ERROR);
110                 try {
111                     Thread.sleep(2000);
112                 } catch(Exception JavaDoc ee) {}
113             }
114
115             // step 6
116
step = getNextInstallStep();
117             step.setUserFeedBack("function/set installer ended");
118
119         } catch (Exception JavaDoc e) {
120             log.error("install crash on : " + this);
121             result = false;
122         }
123         return result;
124     }
125
126
127     /**
128      * Description of the Method
129      *
130      * @return Description of the Return Value
131      */

132     public boolean uninstall() {
133         try {
134
135             // step1
136
installStep step = getNextInstallStep();
137             step.setUserFeedBack("function/set uninstaller started");
138
139             // step 3
140
step = getNextInstallStep();
141             step.setUserFeedBack("updating mmbase registry ..");
142             updateRegistryUninstalled();
143             step.setUserFeedBack("updating mmbase registry ... done");
144
145             // step 4
146
step = getNextInstallStep();
147             step.setUserFeedBack("function/set installer ended");
148
149         } catch (Exception JavaDoc e) {
150             log.error("install crash on : " + this);
151         }
152         return true;
153     }
154
155
156     /**
157      * Description of the Method
158      *
159      * @param jf Description of the Parameter
160      * @param step Description of the Parameter
161      * @return Description of the Return Value
162      */

163     private boolean installFunctionSets(JarFile JavaDoc jf, installStep step) {
164         JarEntry JavaDoc je = jf.getJarEntry("functionsets.xml");
165         if (je == null) {
166             // temp extra until i remove the file ref !
167
je = jf.getJarEntry("functionsetfile.xml");
168         }
169         if (je != null) {
170             try {
171                 InputStream JavaDoc input = jf.getInputStream(je);
172                 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource JavaDoc(input), FunctionSetPackage.class);
173                 for (Iterator JavaDoc ns = reader.getChildElements("functionsets", "functionset"); ns.hasNext(); ) {
174                     Element JavaDoc n = (Element JavaDoc) ns.next();
175                     String JavaDoc name = n.getAttribute("name");
176                     String JavaDoc file = n.getAttribute("file");
177                     if (file != null) {
178                         if (!installFunctionSet(jf, step, name, file)) {
179                             return false;
180                         }
181                     }
182                 }
183             } catch (Exception JavaDoc e) {
184                 installStep substep = step.getNextInstallStep();
185                 substep.setType(installStep.TYPE_ERROR);
186                 substep.setUserFeedBack("can't open functionsetfile.xml");
187                 return false;
188             }
189         }
190         return true;
191     }
192
193
194     /**
195      * Description of the Method
196      *
197      * @param jf Description of the Parameter
198      * @param step Description of the Parameter
199      * @param name Description of the Parameter
200      * @param file Description of the Parameter
201      * @return Description of the Return Value
202      */

203     private boolean installFunctionSet(JarFile JavaDoc jf, installStep step, String JavaDoc name, String JavaDoc file) {
204         String JavaDoc functiondir = PackageManager.getConfigPath() + File.separator + "functions" + File.separator;
205         installStep substep = step.getNextInstallStep();
206
207         JarEntry JavaDoc je = jf.getJarEntry(file);
208         if (je != null) {
209             substep.setUserFeedBack("creating fuction file : " + functiondir + file + ".. ");
210             try {
211                 InputStream JavaDoc in = jf.getInputStream(je);
212                 BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(functiondir + "sets" + File.separator + file));
213                 int val;
214                 while ((val = in.read()) != -1) {
215                     out.write(val);
216                 }
217                 out.close();
218                 substep.setUserFeedBack("creating file : " + functiondir + file + ".. done");
219             } catch (IOException JavaDoc f) {
220                 substep.setUserFeedBack("creating file : " + functiondir + file + ".. failed");
221                 substep.setType(installStep.TYPE_ERROR);
222                 f.printStackTrace();
223                 return false;
224             }
225         }
226         // so the functionset file is installed/updated then update the
227
// functionsetfile. the update/write should be done my the function
228
// manager but until functions are clear in 1.7 ill do it like this.
229
ExtendedDocumentReader reader = new ExtendedDocumentReader(functiondir + "functionsets.xml", FunctionSetPackage.class);
230         String JavaDoc body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
231         body += "<!DOCTYPE functionsets PUBLIC \"//MMBase - functionsets//\" \"http://www.mmbase.org/dtd/functionsets_1_0.dtd\">\n";
232         body += "<functionsets>\n";
233         boolean found = false;
234         for (Iterator JavaDoc ns = reader.getChildElements("functionsets", "functionset"); ns.hasNext(); ) {
235             Element JavaDoc n = (Element JavaDoc) ns.next();
236             String JavaDoc oldname = n.getAttribute("name");
237             String JavaDoc oldfile = n.getAttribute("file");
238             body += "\t<functionset name=\"" + oldname + "\" file=\"" + oldfile + "\" />\n";
239             if (name.equals(oldname) && file.equals(oldfile.substring(5))) {
240                 found = true;
241             }
242         }
243
244         if (!found) {
245             body += "\t<functionset name=\"" + name + "\" file=\"sets/" + file + "\" />\n";
246             body += "</functionsets>\n";
247             saveFile(functiondir + "functionsets.xml", body);
248         }
249
250         return true;
251     }
252
253
254     /**
255      * Description of the Method
256      *
257      * @param filename Description of the Parameter
258      * @param value Description of the Parameter
259      * @return Description of the Return Value
260      */

261     static boolean saveFile(String JavaDoc filename, String JavaDoc value) {
262         File JavaDoc sfile = new File JavaDoc(filename);
263         try {
264             DataOutputStream JavaDoc scan = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(sfile));
265             scan.writeBytes(value);
266             scan.flush();
267             scan.close();
268         } catch (Exception JavaDoc e) {
269             log.error(Logging.stackTrace(e));
270         }
271         return true;
272     }
273 }
274
275
Popular Tags