Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/UpdateWebXmlTask.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/Attic/UpdateWebXmlTask.java,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/UpdateWebXmlTask.java 12 Apr 2006 11:05:02 -0000 1.1 @@ -0,0 +1,326 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $$Id$$ */ +package org.lamsfoundation.lams.tool.deploy; + + +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.apache.commons.lang.StringUtils; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.xml.sax.SAXException; + +/** + * Base class of ant tasks that change a webxml + * @author Fiona Malikoff + */ +public class UpdateWebXmlTask implements Task +{ + + /** + * The lams.ear path file. + */ + protected String lamsEarPath; + + /** + * The value of the tool application context file, including the path + * e.g. /org/lamsfoundation/lams/tool/vote/voteApplicationContext.xml + * It should not include the "classpath:" prefix as this will be added + * automatically. + */ + protected String applicationContextPath; + + private String centralWebXmlPath; + private String learningWebXmlPath; + private String monitoringWebXmlPath; + + /** Creates a new instance of UpdateApplicationXmlTask */ + public UpdateWebXmlTask(final String applicationContextURI, final String lamsEarPath, final String centralWebXmlPath, + final String learningWebXmlPath, final String monitoringWebXmlPath) + { + this.applicationContextPath = applicationContextURI; + this.lamsEarPath = lamsEarPath; + this.centralWebXmlPath = centralWebXmlPath; + this.learningWebXmlPath = learningWebXmlPath; + this.monitoringWebXmlPath = monitoringWebXmlPath; + } + + + /** + * Sets the location of the application xml file to be modified. + * @param appxml New value of property appxml. + */ + public void setLamsEarPath(final String lamsEarPath) + { + this.lamsEarPath = lamsEarPath; + } + + /** + * Execute the task + * @throws org.apache.tools.ant.DeployException In case of any problems + */ + public void execute() throws DeployException + { + if ( applicationContextPath == null ) { + throw new DeployException("UpdateWebXmTask: Unable to update web.xml as the application content path is missing (applicationContextPath)."); + } + + Document doc = null; + + if ( centralWebXmlPath!=null && ! StringUtils.isEmpty(centralWebXmlPath) ) { + doc = parseWebXml(centralWebXmlPath); + updateWebXml(doc, centralWebXmlPath); + writeWebXml(doc, centralWebXmlPath); + } + + if ( learningWebXmlPath!=null && ! StringUtils.isEmpty(learningWebXmlPath) ) { + doc = parseWebXml(learningWebXmlPath); + updateWebXml(doc, learningWebXmlPath); + writeWebXml(doc, learningWebXmlPath); + } + + if ( monitoringWebXmlPath!=null && ! StringUtils.isEmpty(monitoringWebXmlPath) ) { + doc = parseWebXml(monitoringWebXmlPath); + updateWebXml(doc, monitoringWebXmlPath); + writeWebXml(doc, monitoringWebXmlPath); + } + } + + /** + * Add the web uri and context root elements ot the Application xml + */ + protected void updateWebXml(Document doc, String documentPath) throws DeployException + { + + //find & remove web uri element + NodeList contextParamNodeList = doc.getElementsByTagName("context-param"); + Element matchingContextParamElement = findContextParamWithMatchingParamName("contextConfigLocation", contextParamNodeList); + if ( matchingContextParamElement == null ) { + throw new DeployException("No contextConfigLocation can be found in the file "+documentPath); + } + + NodeList contextParamElements = matchingContextParamElement.getChildNodes(); + for ( int c=0; c + * contextConfigLocation + * + * classpath:/org/lamsfoundation/lams/applicationContext.xml + * + * + */ + for (int i = 0, length = nodeList.getLength(); i < length; i++) + { + Node node = nodeList.item(i); + if (node instanceof Element) + { + + NodeList children = node.getChildNodes(); + String paramName = null; + for ( int c=0; c 0) && nameChildren.item(0) instanceof Text) { + paramName = nameChildren.item(0).getNodeValue(); + if ( paramName!=null && paramName.equals(searchParamName) ) + return (Element) node; + } + } + } + } + + } + + } + + return null; + } + + /** + * The value of the tool application context file, including the path + * e.g. /org/lamsfoundation/lams/tool/vote/voteApplicationContext.xml + * It should not include the "classpath:" prefix as this will be added + * automatically. + */ + public String getApplicationContextPath() { + return applicationContextPath; + } + + /** + * The value of the tool application context file, including the path + * e.g. /org/lamsfoundation/lams/tool/vote/voteApplicationContext.xml + * It should not include the "classpath:" prefix as this will be added + * automatically. + */ + public void setApplicationContextPath(String applicationContextPath) { + this.applicationContextPath = applicationContextPath; + } + + + public String getCentralWebXmlPath() { + return centralWebXmlPath; + } + + + public void setCentralWebXmlPath(String centralWebXmlPath) { + this.centralWebXmlPath = centralWebXmlPath; + } + + + public String getLearningWebXmlPath() { + return learningWebXmlPath; + } + + + public void setLearningWebXmlPath(String learningWebXmlPath) { + this.learningWebXmlPath = learningWebXmlPath; + } + + + public String getMonitoringWebXmlPath() { + return monitoringWebXmlPath; + } + + + public void setMonitoringWebXmlPath(String monitoringWebXmlPath) { + this.monitoringWebXmlPath = monitoringWebXmlPath; + } + + +} Index: lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/TestUpdateWebXmlTask.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/Attic/TestUpdateWebXmlTask.java,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/TestUpdateWebXmlTask.java 12 Apr 2006 11:05:02 -0000 1.1 @@ -0,0 +1,51 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ +/* $Id$ */ + +package org.lamsfoundation.lams.tool.deploy; + + +public class TestUpdateWebXmlTask extends ToolDBTest { + + public TestUpdateWebXmlTask(String testName) + { + super(testName); + } + + protected void setUp() throws java.lang.Exception + { + super.setUp(); + } + + protected void tearDown() throws java.lang.Exception + { + super.tearDown(); + } + + public void testExecute() throws Exception + { + UpdateWebXmlTask task = new UpdateWebXmlTask("/org/lamsfoundation/lams/tool/noticeboard/applicationContext.xml",null,"test/file/web.xml",null,null); + task.execute(); + } + +}