1 7 package org.mmbase.applications.packaging.packagehandlers; 8 9 import java.io.BufferedOutputStream ; 10 import java.io.DataOutputStream ; 11 import java.io.File ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.Iterator ; 16 import java.util.jar.JarEntry ; 17 import java.util.jar.JarFile ; 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 ; 26 import org.xml.sax.InputSource ; 27 28 33 public class FunctionSetPackage extends BasicPackage implements PackageInterface { 34 35 private static Logger log = Logging.getLoggerInstance(DisplayHtmlPackage.class); 36 37 40 public final static String DTD_FUNCTIONSETS_1_0 = "functionsets_1_0.dtd"; 41 42 45 public final static String PUBLIC_ID_FUNCTIONSETS_1_0 = "-//MMBase//DTD functionsets config 1.0//EN"; 46 47 48 52 public static void registerPublicIDs() { 53 XMLEntityResolver.registerPublicID(PUBLIC_ID_FUNCTIONSETS_1_0, DTD_FUNCTIONSETS_1_0, FunctionSetPackage.class); 54 } 55 56 57 60 public FunctionSetPackage() { } 61 62 63 68 public boolean install() { 69 boolean result = true; 70 try { 72 73 installStep step = getNextInstallStep(); 75 step.setUserFeedBack("function/set installer started"); 76 77 step = getNextInstallStep(); 79 step.setUserFeedBack("receiving package .."); 80 JarFile jf = getJarFile(); 81 if (jf != null) { 82 step.setUserFeedBack("receiving package ... done (" + jf + ")"); 83 84 step = getNextInstallStep(); 86 step.setUserFeedBack("checking dependencies .."); 87 if (dependsInstalled(jf, step)) { 88 89 step.setUserFeedBack("checking dependencies ... done"); 90 91 step = getNextInstallStep(); 93 step.setUserFeedBack("installing functionsets .."); 94 installFunctionSets(jf, step); 95 step.setUserFeedBack("installing functionsets ... done"); 96 97 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 ee) {} 113 } 114 115 step = getNextInstallStep(); 117 step.setUserFeedBack("function/set installer ended"); 118 119 } catch (Exception e) { 120 log.error("install crash on : " + this); 121 result = false; 122 } 123 return result; 124 } 125 126 127 132 public boolean uninstall() { 133 try { 134 135 installStep step = getNextInstallStep(); 137 step.setUserFeedBack("function/set uninstaller started"); 138 139 step = getNextInstallStep(); 141 step.setUserFeedBack("updating mmbase registry .."); 142 updateRegistryUninstalled(); 143 step.setUserFeedBack("updating mmbase registry ... done"); 144 145 step = getNextInstallStep(); 147 step.setUserFeedBack("function/set installer ended"); 148 149 } catch (Exception e) { 150 log.error("install crash on : " + this); 151 } 152 return true; 153 } 154 155 156 163 private boolean installFunctionSets(JarFile jf, installStep step) { 164 JarEntry je = jf.getJarEntry("functionsets.xml"); 165 if (je == null) { 166 je = jf.getJarEntry("functionsetfile.xml"); 168 } 169 if (je != null) { 170 try { 171 InputStream input = jf.getInputStream(je); 172 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource (input), FunctionSetPackage.class); 173 for (Iterator ns = reader.getChildElements("functionsets", "functionset"); ns.hasNext(); ) { 174 Element n = (Element ) ns.next(); 175 String name = n.getAttribute("name"); 176 String file = n.getAttribute("file"); 177 if (file != null) { 178 if (!installFunctionSet(jf, step, name, file)) { 179 return false; 180 } 181 } 182 } 183 } catch (Exception 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 203 private boolean installFunctionSet(JarFile jf, installStep step, String name, String file) { 204 String functiondir = PackageManager.getConfigPath() + File.separator + "functions" + File.separator; 205 installStep substep = step.getNextInstallStep(); 206 207 JarEntry je = jf.getJarEntry(file); 208 if (je != null) { 209 substep.setUserFeedBack("creating fuction file : " + functiondir + file + ".. "); 210 try { 211 InputStream in = jf.getInputStream(je); 212 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (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 f) { 220 substep.setUserFeedBack("creating file : " + functiondir + file + ".. failed"); 221 substep.setType(installStep.TYPE_ERROR); 222 f.printStackTrace(); 223 return false; 224 } 225 } 226 ExtendedDocumentReader reader = new ExtendedDocumentReader(functiondir + "functionsets.xml", FunctionSetPackage.class); 230 String 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 ns = reader.getChildElements("functionsets", "functionset"); ns.hasNext(); ) { 235 Element n = (Element ) ns.next(); 236 String oldname = n.getAttribute("name"); 237 String 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 261 static boolean saveFile(String filename, String value) { 262 File sfile = new File (filename); 263 try { 264 DataOutputStream scan = new DataOutputStream (new FileOutputStream (sfile)); 265 scan.writeBytes(value); 266 scan.flush(); 267 scan.close(); 268 } catch (Exception e) { 269 log.error(Logging.stackTrace(e)); 270 } 271 return true; 272 } 273 } 274 275 | Popular Tags |