001: package org.enhydra.kelp.ant.deployer;
002:
003: //Kelp
004: import org.enhydra.kelp.common.*;
005: import org.enhydra.kelp.common.event.WriteListener;
006:
007: //Toolbox
008: import org.enhydra.tool.common.ToolException;
009:
010: //java
011: import java.io.File;
012: import java.io.PrintWriter;
013: import java.io.InputStream;
014: import java.io.BufferedReader;
015: import java.io.InputStreamReader;
016: import org.enhydra.kelp.ant.node.AntProject;
017: import java.io.FileWriter;
018: import java.io.BufferedWriter;
019: import java.util.Date;
020: import org.enhydra.kelp.ant.ErrorReader;
021:
022: /**
023: * <p>Title: </p>
024: * <p>Description: </p>
025: * <p>Copyright: Copyright (c) 2003</p>
026: * <p>Company: </p>
027: * @author Damir Milovic
028: * @version 1.0
029: */
030:
031: public class AntDeployBuilder extends AbstractEchoBuilder {
032:
033: public AntDeployBuilder() {
034: super (null);
035: }
036:
037: public AntDeployBuilder(WriteListener listener) {
038: super (listener);
039: }
040:
041: public AntDeployBuilder(WriteListener listener, boolean buffer) {
042: super (listener, buffer);
043: }
044:
045: public AntDeployBuilder(WriteListener[] listeners) {
046: super ();
047: for (int i = 0; i < listeners.length; i++) {
048: addWriteListener(listeners[i]);
049: }
050: }
051:
052: //Implementation
053: protected void buildImpl()
054: throws org.enhydra.tool.common.ToolException {
055: File root = new File(getProject().getWorkingPath());
056: try {
057: String enhydraDir = getProject().getProperty(
058: AntProject.ENHYDRA_DIR);
059: String execLine = enhydraDir + "/bin/enhydra-ant";
060: if (System.getProperty("os.name").toLowerCase().startsWith(
061: "windows")) {
062: execLine += ".bat";
063: }
064: Process process = Runtime.getRuntime().exec(
065: execLine + " deploy", null, root);
066:
067: InputStream inputStream = process.getInputStream();
068: InputStream errorStream = process.getErrorStream();
069: BufferedReader bufferedReader = new BufferedReader(
070: new InputStreamReader(inputStream));
071: BufferedReader errorBufferedReader = new BufferedReader(
072: new InputStreamReader(errorStream));
073: // init log (ouput) file:
074: File logFile = null;
075: FileWriter logFileWriter = null;
076: BufferedWriter logBuffer = null;
077: PrintWriter logPrint = null;
078: boolean logFileOK = false;
079: String logFileName = ((AntProject) getProject())
080: .getOutputFilename();
081: if (logFileName != null) {
082: logFile = new File(logFileName);
083: }
084: if ((logFile != null)
085: && (!logFile.isDirectory())
086: && ((AntProject) getProject())
087: .isOutputFileEnabled()) {
088: if (logFile.length() > 1e6) {
089: logFileWriter = new FileWriter(logFile, false /* overwrite */);
090: } else {
091: logFileWriter = new FileWriter(logFile, true /* append */);
092: }
093: logBuffer = new BufferedWriter(logFileWriter, 4096);
094: logPrint = new PrintWriter(logBuffer, false /* auto flush */);
095: logPrint.println("------------------------------ "
096: + (new Date()).toString()
097: + " ----------------------");
098: logFileOK = true;
099: }
100:
101: String s = null;
102:
103: (new ErrorReader(errorBufferedReader, this , logPrint))
104: .start();
105:
106: while ((s = bufferedReader.readLine()) != null) {
107: if (s != null) {
108: getEchoWriter().println(s);
109: if (logFileOK) {
110: logPrint.println(s);
111: }
112: s = null;
113: }
114: }
115:
116: process.waitFor();
117: if (logFileOK) {
118: logPrint.flush();
119: logPrint.close();
120: }
121:
122: } catch (Exception e) {
123: throw new ToolException(e, "Deployer error: "
124: + e.toString());
125: }
126: }
127:
128: }
|