FileDocCategorySizeDatePackage
DefaultWebXMLTransfer.javaAPI DocGlassfish v2 API7648Tue Jun 05 03:13:58 BST 2007com.sun.enterprise.tools.upgrade.miscconfig

DefaultWebXMLTransfer

public class DefaultWebXMLTransfer extends Object
author
prakash

Fields Summary
private com.sun.enterprise.util.i18n.StringManager
stringManager
private Logger
logger
private CommonInfoModel
commonInfo
Constructors Summary
public DefaultWebXMLTransfer(CommonInfoModel commonInfo)
Creates a new instance of DefaultWebXMLTransfer

    
                 
       
        this.commonInfo = commonInfo;
    
Methods Summary
private voidaddServletMapping(org.w3c.dom.Element documentElement)

        Element sMap = documentElement.getOwnerDocument().createElement("servlet-mapping");
        documentElement.appendChild(sMap);
        Element sName = documentElement.getOwnerDocument().createElement("servlet-name");
        Node sNameTextNode = documentElement.getOwnerDocument().createTextNode("jsp");
        sName.appendChild(sNameTextNode);
        sMap.appendChild(sName);
        
        Element urlPat = documentElement.getOwnerDocument().createElement("url-pattern");
        Node urlTextNode = documentElement.getOwnerDocument().createTextNode("*.jspx");
        urlPat.appendChild(urlTextNode);
        sMap.appendChild(urlPat);   
    
private java.lang.StringgetTextNodeData(org.w3c.dom.Element element)

        NodeList children = element.getChildNodes();
        for(int index=0; index < children.getLength(); index++){
            if(children.item(index).getNodeType() == Node.TEXT_NODE){
                return children.item(index).getNodeValue();
            }
        }
        return "";
    
private voidmodifyContent(java.io.File xmlFile)

        UpgradeUtils upgrUtils = UpgradeUtils.getUpgradeUtils(commonInfo);
        Document document = upgrUtils.getDomainDocumentElement(xmlFile.toString());
        try {
            Element docEle = document.getDocumentElement();
            // Need to obtain the servlet element named jsp
            NodeList servlets = docEle.getElementsByTagName("servlet");
            for(int lh =0; lh < servlets.getLength(); lh++){
                Element servlet = ((Element)servlets.item(lh));
                NodeList ssNames = servlet.getElementsByTagName("servlet-name");
                // There should be only one servlet-name
                if(this.getTextNodeData((Element)ssNames.item(0)).equals("jsp")){
                    NodeList ssClNames = servlet.getElementsByTagName("servlet-class");
                    this.setTextNodeData((Element)ssClNames.item(0), 
                            "org.apache.jasper.servlet.JspServlet");
                    break;
                }
            }
            this.addServletMapping(docEle);
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new FileOutputStream(xmlFile));
            transformer.transform(source, result);
            
        }catch (Exception ex){
            // Log error
            logger.log(Level.SEVERE, stringManager.getString(
                    "upgrade.configTransfers.defaultWebXML.startFailureMessage",
                    ex.getMessage()), ex);
        }
    
private voidsetTextNodeData(org.w3c.dom.Element element, java.lang.String text)

        NodeList children = element.getChildNodes();
        for(int index=0; index < children.getLength(); index++){
            if(children.item(index).getNodeType() == Node.TEXT_NODE){
                children.item(index).setNodeValue(text);
            }
        }        
    
private voidtransferFileContents(java.io.File source, java.io.File target)
Method to transfer file contents from source to target

        BufferedReader reader = new BufferedReader(new InputStreamReader
                (new FileInputStream(source)));
        PrintWriter writer = new PrintWriter(new FileOutputStream(target));
        String readLine = null;
        while((readLine = reader.readLine()) != null){
            writer.println(readLine);
        }
        writer.flush();
        writer.close();
        reader.close();
    
public voidtransform(java.lang.String targetFileName)
Method to transform default-web.xml file from source to target

        logger.log(Level.INFO, stringManager.getString(
                "upgrade.configTransfers.defaultWebXML.startMessage"));
        File targetFile = new File(targetFileName);
        this.modifyContent(targetFile);