001: package com.jat.core.installer.step;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import javax.xml.parsers.ParserConfigurationException;
006:
007: import org.w3c.dom.Document;
008: import org.w3c.dom.Node;
009: import org.w3c.dom.NodeList;
010: import org.xml.sax.SAXException;
011: import com.jat.core.installer.util.CommandExec;
012: import com.jat.core.log.LogManager;
013: import com.jat.util.file.FileUtil;
014: import com.jat.util.xml.XMLDocument;
015: import com.jat.util.xml.XMLUtil;
016:
017: /**
018: * <p>Title: JAT</p>
019: * <p>Description:</p>
020: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
021: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
022: * @author stf
023: * @version 1.1
024: * @since 1.2
025: */
026:
027: public class WebApplication {
028:
029: public static WebApplication getWebApplication() {
030: String osName = System.getProperty("os.name");
031: if (osName != null
032: && osName.toLowerCase().indexOf("microsoft") == -1
033: && osName.toLowerCase().indexOf("window") == -1
034: && osName.toLowerCase().indexOf("ms") == -1)
035: return new UnixWebApplication();
036: return new WebApplication();
037: }
038:
039: public void unwar(String warFile, String tempDir) throws Exception {
040: createPath(tempDir);
041: CommandExec commandEx = new CommandExec();
042: String[] cmd = new String[4];
043: cmd[0] = JatProject.getFilename(new File("bin")
044: .getAbsolutePath(), "unwar.bat");
045: cmd[1] = new File(tempDir).getAbsolutePath();
046: cmd[2] = "-xf";
047: cmd[3] = new File(warFile).getAbsolutePath();
048: commandEx.execute(cmd);
049: }
050:
051: public void modifyWebXml(String tempDir, String projectPath,
052: String configFileName) throws Exception {
053: File webXml = new File(JatProject.getFilename(JatProject
054: .getFilename(tempDir, "WEB-INF"), "web.xml"));
055: String dtd = "<!DOCTYPE web-app PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\" \"http://java.sun.com/dtd/web-app_2_3.dtd\">";
056: try {
057: String content = com.jat.util.StringUtil.replace(FileUtil
058: .readFile(webXml), dtd, "");
059: XMLDocument doc = XMLUtil.getXMLDocument(content, false);
060: Document w3cDoc = doc.getDocument();
061: Node sub = getNode4Change(w3cDoc);
062: sub.setNodeValue(JatProject.getFilename(projectPath,
063: configFileName));
064: doc.saveToFile(JatProject.getFilename(JatProject
065: .getFilename(tempDir, "WEB-INF"), "web.xml"));
066: } catch (SAXException ex) {
067: LogManager.sendError("Installer error: " + ex.getMessage());
068: throw new Exception(ex);
069: } catch (ParserConfigurationException ex) {
070: LogManager.sendError("Installer error: " + ex.getMessage());
071: throw new Exception(ex);
072: } catch (IOException ex) {
073: LogManager.sendError("Installer error: " + ex.getMessage());
074: throw new Exception(ex);
075: }
076: }
077:
078: public String war(String tempDir, String projectName)
079: throws Exception {
080: CommandExec commandEx = new CommandExec();
081: String[] cmd = new String[5];
082: cmd[0] = JatProject.getFilename(new File("bin")
083: .getAbsolutePath(), "war.bat");
084: cmd[1] = new File(tempDir).getAbsolutePath();
085: cmd[2] = "-cf0";
086: cmd[3] = projectName + ".war";
087: cmd[4] = "*";
088: commandEx.execute(cmd);
089: return projectName + ".war";
090: }
091:
092: public void moveWar(String tempDir, String fileName, String appDir)
093: throws Exception {
094: // check se appDir esiste
095: File dir = new File(appDir);
096: if (!dir.exists()) {
097: LogManager.sendError("Installer error: appDir '" + appDir
098: + "' doesn't exist");
099: throw new Exception("appDir '" + appDir + "' doesn't exist");
100: }
101: File fileFromFile = new File(JatProject.getFilename(tempDir,
102: fileName));
103:
104: File fileToFile = new File(JatProject.getFilename(appDir,
105: fileName));
106: if (fileToFile.exists()) {
107: fileToFile.delete();
108: }
109: boolean moveFile = fileFromFile.renameTo(new File(JatProject
110: .getFilename(appDir, fileName)));
111: if (!moveFile) {
112: LogManager.sendError("Installer error: cannot war file '"
113: + fileName + "'");
114: throw new Exception("cannot war file '" + fileName + "'");
115: }
116:
117: }
118:
119: public void deleteTemp(String tempDir) throws Exception {
120: File tempToDelete = new File(tempDir);
121: deleteEntireDir(tempToDelete.getAbsoluteFile());
122: }
123:
124: protected WebApplication() {
125: }
126:
127: private Node getNode4Change(Document w3cDoc) throws Exception {
128: Node substition = null;
129: NodeList list = w3cDoc.getElementsByTagName("servlet");
130: Node change = null;
131: for (int i = 0; i < list.getLength(); i++) {
132: if (change != null) {
133: break;
134: }
135: Node aNode = (Node) list.item(i);
136: NodeList aSubList = aNode.getChildNodes();
137: for (int j = 0; j < aSubList.getLength(); j++) {
138: Node aSubNode = (Node) aSubList.item(j);
139:
140: if (aSubNode.getNodeName().equals("servlet-name")) {
141: NodeList a = aSubNode.getChildNodes();
142: for (int k = 0; k < a.getLength(); k++) {
143: Node b = a.item(k);
144: if (b.getNodeValue().equals("projectservlet")) {
145: change = aSubNode.getParentNode();
146: break;
147: }
148: }
149: }
150: }
151: }
152: if (change == null)
153: throw new Exception("projectservlet not found");
154: NodeList aList = change.getChildNodes();
155: for (int i = 0; i < aList.getLength(); i++) {
156: Node aNode = aList.item(i);
157: if (aNode.getNodeName().equals("init-param")) {
158: NodeList aSubList = aNode.getChildNodes();
159: for (int j = 0; j < aSubList.getLength(); j++) {
160: Node aSubNode = aSubList.item(j);
161: if (aSubNode.getNodeName().equals("param-value")) {
162: substition = aSubNode.getFirstChild();
163: }
164: }
165: }
166: }
167: return substition;
168: }
169:
170: protected void createPath(String path) throws Exception {
171: File dir = new File(path);
172: if (dir.exists()) {
173: deleteEntireDir(dir);
174: }
175: dir.mkdirs();
176: }
177:
178: private void deleteEntireDir(File dir) throws IOException {
179: FileUtil.deleteDirectory(dir);
180: }
181: }
|