1 23 24 29 30 package com.sun.enterprise.tools.upgrade.miscconfig; 31 32 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.parsers.FactoryConfigurationError ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXParseException ; 43 44 import java.io.File ; 45 import java.io.IOException ; 46 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.DOMException ; 49 import org.w3c.dom.Element ; 50 import org.w3c.dom.Node ; 51 import org.w3c.dom.NodeList ; 52 53 import javax.xml.transform.Transformer ; 55 import javax.xml.transform.TransformerFactory ; 56 import javax.xml.transform.TransformerException ; 57 import javax.xml.transform.TransformerConfigurationException ; 58 59 import javax.xml.transform.dom.DOMSource ; 60 import javax.xml.transform.stream.StreamResult ; 61 62 import java.io.*; 63 import java.util.logging.*; 64 import com.sun.enterprise.util.i18n.StringManager; 65 import com.sun.enterprise.tools.upgrade.logging.*; 66 67 public class DefaultWebXMLTransfer { 68 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.miscconfig"); 69 private Logger logger = com.sun.enterprise.tools.upgrade.common.CommonInfoModel.getDefaultLogger(); 70 71 public DefaultWebXMLTransfer() { 72 } 73 public void transform(String sourceFileName, String targetFileName){ 74 logger.log(Level.INFO, stringManager.getString("upgrade.configTransfers.defaultWebXML.startMessage")); 75 File sourceFile = new File (sourceFileName); 76 File targetFile = new File (targetFileName); 77 boolean renamed = targetFile.renameTo(new File (targetFileName+".bak")); 78 if(!renamed){ 79 renamed = targetFile.delete(); 81 } 82 if(renamed){ 83 targetFile = new File (targetFileName); 84 try{ 85 targetFile.createNewFile(); 86 this.transferFileContents(sourceFile, targetFile); 87 this.modifyContent(targetFile); 88 }catch(Exception ex){ 89 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.defaultWebXML.startFailureMessage",ex)); 91 } 92 }else{ 93 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.defaultWebXML.renameFailureMessage")); 95 } 96 97 } 98 private void transferFileContents(File source, File target) throws Exception { 99 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(source))); 100 PrintWriter writer = new PrintWriter(new FileOutputStream(target)); 101 String readLine = null; 102 while((readLine = reader.readLine()) != null){ 103 writer.println(readLine); 104 } 105 writer.flush(); 106 writer.close(); 107 reader.close(); 108 } 109 private void modifyContent(File xmlFile){ 110 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 111 factory.setNamespaceAware(true); 113 try { 114 DocumentBuilder builder = factory.newDocumentBuilder(); 115 builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 116 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 117 Document document = builder.parse( xmlFile ); 118 Element docEle = document.getDocumentElement(); 119 NodeList servlets = docEle.getElementsByTagName("servlet"); 121 for(int lh =0; lh < servlets.getLength(); lh++){ 122 Element servlet = ((Element )servlets.item(lh)); 123 NodeList ssNames = servlet.getElementsByTagName("servlet-name"); 124 if(this.getTextNodeData((Element )ssNames.item(0)).equals("jsp")){ 126 NodeList ssClNames = servlet.getElementsByTagName("servlet-class"); 127 this.setTextNodeData((Element )ssClNames.item(0), "org.apache.jasper.servlet.JspServlet"); 128 break; 129 } 130 } 131 this.addServletMapping(docEle); 132 TransformerFactory tFactory = TransformerFactory.newInstance(); 133 Transformer transformer = tFactory.newTransformer(); 134 DOMSource source = new DOMSource (document); 135 StreamResult result = new StreamResult (new FileOutputStream(xmlFile)); 136 transformer.transform(source, result); 137 138 }catch (Exception ex){ 139 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.defaultWebXML.startFailureMessage",ex.getMessage()), ex); 141 } 142 } 143 private String getTextNodeData(Element element){ 144 NodeList children = element.getChildNodes(); 145 for(int index=0; index < children.getLength(); index++){ 146 if(children.item(index).getNodeType() == Node.TEXT_NODE){ 147 return children.item(index).getNodeValue(); 148 } 149 } 150 return ""; 151 } 152 private void setTextNodeData(Element element, String text){ 153 NodeList children = element.getChildNodes(); 154 for(int index=0; index < children.getLength(); index++){ 155 if(children.item(index).getNodeType() == Node.TEXT_NODE){ 156 children.item(index).setNodeValue(text); 157 } 158 } 159 } 160 private void addServletMapping(Element documentElement){ 161 Element sMap = documentElement.getOwnerDocument().createElement("servlet-mapping"); 162 documentElement.appendChild(sMap); 163 Element sName = documentElement.getOwnerDocument().createElement("servlet-name"); 164 Node sNameTextNode = documentElement.getOwnerDocument().createTextNode("jsp"); 165 sName.appendChild(sNameTextNode); 166 sMap.appendChild(sName); 167 168 Element urlPat = documentElement.getOwnerDocument().createElement("url-pattern"); 169 Node urlTextNode = documentElement.getOwnerDocument().createTextNode("*.jspx"); 170 urlPat.appendChild(urlTextNode); 171 sMap.appendChild(urlPat); 172 } 173 } 174 | Popular Tags |