KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > deployer > AntDeployBuilder


1 package org.enhydra.kelp.ant.deployer;
2 //Kelp
3
import org.enhydra.kelp.common.*;
4 import org.enhydra.kelp.common.event.WriteListener;
5 import org.enhydra.kelp.ant.AntFacade;
6
7 //Toolbox
8
import org.enhydra.tool.common.ToolException;
9
10 //java
11
import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.BufferedReader JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import org.enhydra.kelp.ant.node.AntProject;
18 import java.io.FileWriter JavaDoc;
19 import java.io.BufferedWriter JavaDoc;
20 import java.util.Date JavaDoc;
21 import org.enhydra.kelp.ant.ErrorReader;
22
23
24 /**
25  * <p>Title: </p>
26  * <p>Description: </p>
27  * <p>Copyright: Copyright (c) 2003</p>
28  * <p>Company: </p>
29  * @author Damir Milovic
30  * @version 1.0
31  */

32
33 public class AntDeployBuilder extends AbstractEchoBuilder {
34
35   public AntDeployBuilder() {
36       super(null);
37   }
38
39   public AntDeployBuilder(WriteListener listener) {
40       super(listener);
41   }
42
43   public AntDeployBuilder(WriteListener listener, boolean buffer) {
44       super(listener, buffer);
45   }
46
47   public AntDeployBuilder(WriteListener[] listeners) {
48     super();
49     for (int i = 0; i < listeners.length; i++) {
50       addWriteListener(listeners[i]);
51     }
52   }
53
54   //Implementation
55
protected void buildImpl() throws org.enhydra.tool.common.ToolException {
56     File JavaDoc root = new File JavaDoc(getProject().getWorkingPath());
57     try{
58       String JavaDoc enhydraDir = getProject().getProperty(AntProject.ENHYDRA_DIR);
59       String JavaDoc execLine = enhydraDir + "/bin/ant";
60       if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
61           execLine += ".bat";
62       }
63       Process JavaDoc process = Runtime.getRuntime().exec(execLine + " deploy", null, root);
64
65           InputStream JavaDoc inputStream = process.getInputStream();
66           InputStream JavaDoc errorStream = process.getErrorStream();
67           BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
68           BufferedReader JavaDoc errorBufferedReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(errorStream));
69           // init log (ouput) file:
70
File JavaDoc logFile = null;
71           FileWriter JavaDoc logFileWriter = null;
72           BufferedWriter JavaDoc logBuffer = null;
73           PrintWriter JavaDoc logPrint = null;
74           boolean logFileOK = false;
75           String JavaDoc logFileName = ((AntProject)getProject()).getOutputFilename();
76           if (logFileName != null) {
77             logFile = new File JavaDoc(logFileName);
78           }
79           if ((logFile != null) && (!logFile.isDirectory())
80                 && ((AntProject)getProject()).isOutputFileEnabled()) {
81               if (logFile.length() > 1e6) {
82                   logFileWriter = new FileWriter JavaDoc(logFile, false /* overwrite */ );
83               } else {
84                   logFileWriter = new FileWriter JavaDoc(logFile, true /* append */ );
85               }
86               logBuffer = new BufferedWriter JavaDoc(logFileWriter, 4096);
87               logPrint = new PrintWriter JavaDoc(logBuffer, false /* auto flush */);
88               logPrint.println("------------------------------ "+(new Date JavaDoc()).toString()+" ----------------------");
89               logFileOK = true;
90           }
91
92           String JavaDoc s = null;
93
94           (new ErrorReader(errorBufferedReader,this,logPrint)).start();
95
96
97         while((s = bufferedReader.readLine()) != null){
98             if(s != null){
99               getEchoWriter().println(s);
100               if (logFileOK) {
101                   logPrint.println(s);
102               }
103               s = null;
104             }
105         }
106
107           process.waitFor();
108           int k = process.exitValue();
109           if (logFileOK) {
110               logPrint.flush();
111               logPrint.close();
112           }
113
114     }catch(Exception JavaDoc e){
115       throw new ToolException(e,"Deployer error: "+e.toString());
116     }
117   }
118
119 }
Popular Tags