1 7 package com.bull.eclipse.jonas.utils.xml; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileNotFoundException ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 15 import javax.xml.parsers.ParserConfigurationException ; 16 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.NamedNodeMap ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 import org.xml.sax.SAXException ; 23 24 import com.bull.eclipse.jonas.JonasLauncherPlugin; 25 import com.bull.eclipse.jonas.JonasProject; 26 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 27 28 34 public class UpdateXmlFile { 35 36 public static void updateWebXmlFileWithAxisServlet(File webXmlFile) { 37 File xmlFileTemp = new File (TemplateDirUtil.getTemplateDir() + File.separator + "ws" + File.separator + "axis.web.xml"); 38 try { 39 FileInputStream is = new FileInputStream (webXmlFile); 40 Document docXmlWeb = XMLUtils.newDocument(is,true); 41 42 Document docXmlTemp = XMLUtils.newDocument(new FileInputStream (xmlFileTemp),false); 43 44 NodeList nlSC = docXmlWeb.getElementsByTagName("servlet-class"); 46 for(int i = 0;i < nlSC.getLength(); i++) { 47 if (nlSC.item(i).getFirstChild().getNodeValue().equals("org.apache.axis.transport.http.AxisServlet") || 48 nlSC.item(i).getFirstChild().getNodeValue().equals("org.apache.axis.transport.http.AdminServlet") || 49 nlSC.item(i).getFirstChild().getNodeValue().equals("org.apache.axis.monitor.SOAPMonitorService")) 50 return; 51 52 } 53 54 NodeList nl1 = docXmlTemp.getElementsByTagName("servlet"); 57 58 Node servletAxisNode = null; 59 60 Node webNode = getNodeInWebFile(docXmlWeb); 63 64 65 for(int i = 0;i < nl1.getLength(); i++) { 66 servletAxisNode = docXmlWeb.importNode(nl1.item(i),true); 67 if (webNode.getNodeName().equals("web-app")) 68 docXmlWeb.getDocumentElement().appendChild(servletAxisNode); 69 else 70 docXmlWeb.getDocumentElement().insertBefore(servletAxisNode,webNode); 71 } 72 73 NodeList nl2 = docXmlTemp.getElementsByTagName("servlet-mapping"); 74 for(int i = 0;i < nl2.getLength(); i++) { 75 servletAxisNode = docXmlWeb.importNode(nl2.item(i),true); 76 if (webNode.getNodeName().equals("web-app")) 77 docXmlWeb.getDocumentElement().appendChild(servletAxisNode); 78 else 79 docXmlWeb.getDocumentElement().insertBefore(servletAxisNode,webNode); 80 } 81 82 XMLSerializer xManu = XMLSerializer.getInstance(webNode.getOwnerDocument()); 83 FileOutputStream fo = new FileOutputStream (webXmlFile); 84 xManu.serialize(fo); 85 } catch (FileNotFoundException e) { 86 e.printStackTrace(); 88 } catch (ParserConfigurationException e) { 89 e.printStackTrace(); 91 } catch (SAXException e) { 92 e.printStackTrace(); 94 } catch (IOException e) { 95 e.printStackTrace(); 97 } 98 99 100 } 101 102 public static void updateServerConfigXmlFile(String packName, JonasProject jp, String jarName) { 103 File serverConfigFile = new File (jp.getWebInfFolder().getLocation().append("server-config.wsdd").toOSString()); 104 File deployFile = new File (jp.getProject().getLocation().append(jp.getSrcEJBDir() + File.separator + packName + File.separator + "deploy.wsdd").toOSString()); 105 try { 107 Document docXmlSC = XMLUtils.newDocument(new FileInputStream (serverConfigFile),false); 108 Document docXmlDeploy = XMLUtils.newDocument(new FileInputStream (deployFile),false); 109 110 NodeList nlSC = docXmlSC.getElementsByTagName("service"); 111 JonasLauncherPlugin.log("service node = " + nlSC.item(0)); 112 Node nl1 = docXmlDeploy.getElementsByTagName("service").item(0); 114 Node scUpdate = null; 115 scUpdate = docXmlSC.importNode(nl1,true); 116 JonasLauncherPlugin.log("service node = " + nl1); 117 boolean serviceExistant = false; 118 XMLSerializer xManu = null; 119 for(int i = 0;i < nlSC.getLength(); i++) { 120 JonasLauncherPlugin.log("Node = " + nlSC.item(i)); 121 NamedNodeMap nnm = nlSC.item(i).getAttributes(); 122 String value = nnm.item(0).getNodeValue(); 123 if (nnm.item(0).getNodeValue().equals(jarName + "EndPoint")) { 124 JonasLauncherPlugin.log("On passe dans service deja existant"); 126 Node replaceNode = nlSC.item(i); 127 docXmlSC.getDocumentElement().replaceChild(scUpdate,replaceNode); 128 xManu = XMLSerializer.getInstance(replaceNode.getOwnerDocument()); 129 serviceExistant = true; 130 } 131 } 132 133 if (!serviceExistant) { 134 JonasLauncherPlugin.log("Service non existant"); 135 Node transportNode = docXmlSC.getElementsByTagName("transport").item(0); 136 docXmlSC.getDocumentElement().insertBefore(scUpdate,transportNode); 137 xManu = XMLSerializer.getInstance(transportNode.getOwnerDocument()); 138 } 139 140 FileOutputStream fo = new FileOutputStream (serverConfigFile); 141 xManu.serialize(fo); 142 } catch (FileNotFoundException e) { 143 e.printStackTrace(); 145 } catch (ParserConfigurationException e) { 146 e.printStackTrace(); 148 } catch (SAXException e) { 149 e.printStackTrace(); 151 } catch (IOException e) { 152 e.printStackTrace(); 154 } catch (Exception e) { 155 e.printStackTrace(); 156 } 157 158 159 } 160 161 162 public static void updateEjbStandardXmlFile(File ejbStanXmlFile, File xmlFile) { 163 try { 164 FileInputStream is = new FileInputStream (ejbStanXmlFile); 165 Document docXmlEjb = XMLUtils.newDocument(is,true); 166 Document docXmlTemp = XMLUtils.newDocument(new FileInputStream (xmlFile),false); 167 168 169 170 171 NodeList nlSC = docXmlEjb.getElementsByTagName("ejb-name"); 174 String ejbSessionName = docXmlTemp.getElementsByTagName("ejb-name").item(0).getFirstChild().getNodeValue(); 175 for(int i = 0;i < nlSC.getLength(); i++) { 176 String test = nlSC.item(i).getFirstChild().getNodeValue(); 177 if (nlSC.item(i).getFirstChild().getNodeValue().equals(ejbSessionName)) 178 return; 179 180 } 181 182 Node session = docXmlTemp.getElementsByTagName("session").item(0); 184 Node ejbSessionNode = docXmlEjb.getElementsByTagName("entity").item(0); 185 Node sessionNode = docXmlEjb.importNode(session,true); 186 docXmlEjb.getDocumentElement().getElementsByTagName("enterprise-beans").item(0).insertBefore(sessionNode, ejbSessionNode); 187 188 Node transaction = docXmlTemp.getElementsByTagName("container-transaction").item(0); 190 Node ejbTransactionNode = docXmlEjb.getElementsByTagName("container-transaction").item(0); 191 Node transactionNode = docXmlEjb.importNode(transaction,true); 192 docXmlEjb.getDocumentElement().getElementsByTagName("assembly-descriptor").item(0).insertBefore(transactionNode, ejbTransactionNode); 193 194 XMLSerializer xManu = XMLSerializer.getInstance(ejbSessionNode.getOwnerDocument()); 195 FileOutputStream fo = new FileOutputStream (ejbStanXmlFile); 196 xManu.serialize(fo); 197 } catch (FileNotFoundException e) { 198 e.printStackTrace(); 200 } catch (ParserConfigurationException e) { 201 e.printStackTrace(); 203 } catch (SAXException e) { 204 e.printStackTrace(); 206 } catch (IOException e) { 207 e.printStackTrace(); 209 } 210 211 212 } 213 214 215 public static void updateEjbJOnASXmlFile(File ejbJOnASXmlFile, File xmlFile) { 216 try { 217 FileInputStream is = new FileInputStream (ejbJOnASXmlFile); 218 Document docXmlEjb = XMLUtils.newDocument(is,true); 219 Document docXmlTemp = XMLUtils.newDocument(new FileInputStream (xmlFile),false); 220 221 222 223 224 NodeList nlSC = docXmlEjb.getElementsByTagName("ejb-name"); 227 String ejbSessionName = docXmlTemp.getElementsByTagName("ejb-name").item(0).getFirstChild().getNodeValue(); 228 for(int i = 0;i < nlSC.getLength(); i++) { 229 String test = nlSC.item(i).getFirstChild().getNodeValue(); 230 if (nlSC.item(i).getFirstChild().getNodeValue().equals(ejbSessionName)) 231 return; 232 233 } 234 235 Node session = docXmlTemp.getElementsByTagName("jonas-session").item(0); 237 Node ejbSessionNode = docXmlEjb.getElementsByTagName("jonas-entity").item(0); 238 Node sessionNode = docXmlEjb.importNode(session,true); 239 docXmlEjb.getDocumentElement().insertBefore(sessionNode, ejbSessionNode); 240 241 XMLSerializer xManu = XMLSerializer.getInstance(ejbSessionNode.getOwnerDocument()); 242 FileOutputStream fo = new FileOutputStream (ejbJOnASXmlFile); 243 xManu.serialize(fo); 244 } catch (FileNotFoundException e) { 245 e.printStackTrace(); 247 } catch (ParserConfigurationException e) { 248 e.printStackTrace(); 250 } catch (SAXException e) { 251 e.printStackTrace(); 253 } catch (IOException e) { 254 e.printStackTrace(); 256 } 257 258 259 } 260 261 private static Node getNodeInWebFile(Document webXmlFile) { 262 Node webNode = webXmlFile.getElementsByTagName("servlet-mapping").item(0); 265 if (webNode == null) 266 webNode = webXmlFile.getElementsByTagName("session-config").item(0); 267 if (webNode == null) 268 webNode = webXmlFile.getElementsByTagName("mime-mapping").item(0); 269 if (webNode == null) 270 webNode = webXmlFile.getElementsByTagName("welcome-file-list").item(0); 271 if (webNode == null) 272 webNode = webXmlFile.getElementsByTagName("error-page").item(0); 273 if (webNode == null) 274 webNode = webXmlFile.getElementsByTagName("taglib").item(0); 275 if (webNode == null) 276 webNode = webXmlFile.getElementsByTagName("resource-env-ref").item(0); 277 if (webNode == null) 278 webNode = webXmlFile.getElementsByTagName("resource-ref").item(0); 279 if (webNode == null) 280 webNode = webXmlFile.getElementsByTagName("security-constraint").item(0); 281 if (webNode == null) 282 webNode = webXmlFile.getElementsByTagName("login-config").item(0); 283 if (webNode == null) 284 webNode = webXmlFile.getElementsByTagName("security-role").item(0); 285 if (webNode == null) 286 webNode = webXmlFile.getElementsByTagName("env-entry").item(0); 287 if (webNode == null) 288 webNode = webXmlFile.getElementsByTagName("ejb-ref").item(0); 289 if (webNode == null) 290 webNode = webXmlFile.getElementsByTagName("ejb-local-ref").item(0); 291 if (webNode == null) 292 webNode = webXmlFile.getElementsByTagName("web-app").item(0); 293 294 return webNode; 295 } 296 297 298 public static void updateHbmXmlFile(File hbmXmlFile, File xmlNode) { 299 try { 300 FileInputStream is = new FileInputStream (hbmXmlFile); 301 Document docXmlHbm = XMLUtils.newDocument(is,true); 302 Document docXmlTemp = XMLUtils.newDocument(new FileInputStream (xmlNode),false); 303 304 Node compositeTemp = docXmlTemp.getElementsByTagName("composite-id").item(0); 305 Node hbmComposite = docXmlHbm.getElementsByTagName("composite-id").item(0); 306 Node compositeInHBM = docXmlHbm.importNode(compositeTemp,true); 307 docXmlHbm.getDocumentElement().getElementsByTagName("class").item(0).replaceChild(compositeInHBM, hbmComposite); 308 309 XMLSerializer xManu = XMLSerializer.getInstance(hbmComposite.getOwnerDocument()); 310 FileOutputStream fo = new FileOutputStream (hbmXmlFile); 311 xManu.serialize(fo); 312 } catch (FileNotFoundException e) { 313 e.printStackTrace(); 315 } catch (ParserConfigurationException e) { 316 e.printStackTrace(); 318 } catch (SAXException e) { 319 e.printStackTrace(); 321 } catch (IOException e) { 322 e.printStackTrace(); 324 } 325 326 327 } 328 329 } 330 | Popular Tags |