KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > miscconfig > DefaultWebXMLTransfer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * DefaultWebXMLTransfer.java
26  *
27  * Created on September 8, 2003, 10:56 AM
28  */

29
30 package com.sun.enterprise.tools.upgrade.miscconfig;
31
32 /**
33  *
34  * @author prakash
35  */

36 import javax.xml.parsers.DocumentBuilder JavaDoc;
37 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
38 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
39 import javax.xml.parsers.ParserConfigurationException JavaDoc;
40
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.SAXParseException JavaDoc;
43
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 import org.w3c.dom.Document JavaDoc;
48 import org.w3c.dom.DOMException JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.Node JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52
53 // For transformation. Not really needed to retain.
54
import javax.xml.transform.Transformer JavaDoc;
55 import javax.xml.transform.TransformerFactory JavaDoc;
56 import javax.xml.transform.TransformerException JavaDoc;
57 import javax.xml.transform.TransformerConfigurationException JavaDoc;
58
59 import javax.xml.transform.dom.DOMSource JavaDoc;
60 import javax.xml.transform.stream.StreamResult JavaDoc;
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     /** Creates a new instance of DefaultWebXMLTransfer */
71     public DefaultWebXMLTransfer() {
72     }
73     public void transform(String JavaDoc sourceFileName, String JavaDoc targetFileName){
74         logger.log(Level.INFO, stringManager.getString("upgrade.configTransfers.defaultWebXML.startMessage"));
75         File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
76         File JavaDoc targetFile = new File JavaDoc(targetFileName);
77         boolean renamed = targetFile.renameTo(new File JavaDoc(targetFileName+".bak"));
78         if(!renamed){
79             // This is possible if user is running the upgrade again and .bak is already created.
80
renamed = targetFile.delete();
81         }
82         if(renamed){
83             targetFile = new File JavaDoc(targetFileName);
84             try{
85                 targetFile.createNewFile();
86                 this.transferFileContents(sourceFile, targetFile);
87                 this.modifyContent(targetFile);
88             }catch(Exception JavaDoc ex){
89                 // Lot Error
90
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.defaultWebXML.startFailureMessage",ex));
91             }
92         }else{
93             // Log error message
94
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.defaultWebXML.renameFailureMessage"));
95         }
96         
97     }
98     private void transferFileContents(File JavaDoc source, File JavaDoc target) throws Exception JavaDoc{
99         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
100         PrintWriter writer = new PrintWriter(new FileOutputStream(target));
101         String JavaDoc 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 JavaDoc xmlFile){
110         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
111         //factory.setValidating(true);
112
factory.setNamespaceAware(true);
113         try {
114             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
115             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
116                 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
117             Document JavaDoc document = builder.parse( xmlFile );
118             Element JavaDoc docEle = document.getDocumentElement();
119             // Need to obtain the servlet element named jsp
120
NodeList JavaDoc servlets = docEle.getElementsByTagName("servlet");
121             for(int lh =0; lh < servlets.getLength(); lh++){
122                 Element JavaDoc servlet = ((Element JavaDoc)servlets.item(lh));
123                 NodeList JavaDoc ssNames = servlet.getElementsByTagName("servlet-name");
124                 // There should be only one servlet-name
125
if(this.getTextNodeData((Element JavaDoc)ssNames.item(0)).equals("jsp")){
126                     NodeList JavaDoc ssClNames = servlet.getElementsByTagName("servlet-class");
127                     this.setTextNodeData((Element JavaDoc)ssClNames.item(0), "org.apache.jasper.servlet.JspServlet");
128                     break;
129                 }
130             }
131             this.addServletMapping(docEle);
132             TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
133             Transformer JavaDoc transformer = tFactory.newTransformer();
134             DOMSource JavaDoc source = new DOMSource JavaDoc(document);
135             StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(xmlFile));
136             transformer.transform(source, result);
137             
138         }catch (Exception JavaDoc ex){
139             // Log error
140
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.defaultWebXML.startFailureMessage",ex.getMessage()), ex);
141         }
142     }
143     private String JavaDoc getTextNodeData(Element JavaDoc element){
144         NodeList JavaDoc 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 JavaDoc element, String JavaDoc text){
153         NodeList JavaDoc 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 JavaDoc documentElement){
161         Element JavaDoc sMap = documentElement.getOwnerDocument().createElement("servlet-mapping");
162         documentElement.appendChild(sMap);
163         Element JavaDoc sName = documentElement.getOwnerDocument().createElement("servlet-name");
164         Node JavaDoc sNameTextNode = documentElement.getOwnerDocument().createTextNode("jsp");
165         sName.appendChild(sNameTextNode);
166         sMap.appendChild(sName);
167         
168         Element JavaDoc urlPat = documentElement.getOwnerDocument().createElement("url-pattern");
169         Node JavaDoc urlTextNode = documentElement.getOwnerDocument().createTextNode("*.jspx");
170         urlPat.appendChild(urlTextNode);
171         sMap.appendChild(urlPat);
172     }
173 }
174
Popular Tags