001: /*
002: /*
003: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
004: *
005: * http://izpack.org/
006: * http://izpack.codehaus.org/
007: *
008: * Copyright 2003 Jonathan Halliday
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: */
022:
023: package com.izforge.izpack.panels;
024:
025: import java.io.IOException;
026: import java.io.PrintStream;
027:
028: import net.n3.nanoxml.XMLElement;
029:
030: import com.izforge.izpack.installer.AutomatedInstallData;
031: import com.izforge.izpack.installer.CompileHandler;
032: import com.izforge.izpack.installer.CompileResult;
033: import com.izforge.izpack.installer.CompileWorker;
034: import com.izforge.izpack.installer.PanelAutomation;
035: import com.izforge.izpack.installer.PanelAutomationHelper;
036:
037: /**
038: * Functions to support automated usage of the CompilePanel
039: *
040: * @author Jonathan Halliday
041: * @author Tino Schwarze
042: */
043: public class CompilePanelAutomationHelper extends PanelAutomationHelper
044: implements PanelAutomation, CompileHandler {
045:
046: private CompileWorker worker = null;
047:
048: private int job_max = 0;
049:
050: private String job_name = null;
051:
052: private int last_line_len = 0;
053:
054: // when using the eclipse compiler, we're capturing System.out and System.err...
055: private PrintStream stdout;
056: private PrintStream stderr;
057:
058: /**
059: * Save data for running automated.
060: *
061: * @param installData installation parameters
062: * @param panelRoot unused.
063: */
064: public void makeXMLData(AutomatedInstallData installData,
065: XMLElement panelRoot) {
066: // not used here - during automatic installation, no automatic
067: // installation information is generated
068: }
069:
070: /**
071: * Perform the installation actions.
072: *
073: * @param panelRoot The panel XML tree root.
074: */
075: public boolean runAutomated(AutomatedInstallData idata,
076: XMLElement panelRoot) {
077: XMLElement compiler_xml = panelRoot
078: .getFirstChildNamed("compiler");
079:
080: String compiler = null;
081:
082: if (compiler_xml != null)
083: compiler = compiler_xml.getContent();
084:
085: if (compiler == null) {
086: System.out
087: .println("invalid automation data: could not find compiler");
088: return false;
089: }
090:
091: XMLElement args_xml = panelRoot.getFirstChildNamed("arguments");
092:
093: String args = null;
094:
095: if (args_xml != null)
096: args = args_xml.getContent();
097:
098: if (args_xml == null) {
099: System.out
100: .println("invalid automation data: could not find compiler arguments");
101: return false;
102: }
103:
104: try {
105: this .worker = new CompileWorker(idata, this );
106: this .worker.setCompiler(compiler);
107: this .worker.setCompilerArguments(args);
108:
109: this .stdout = System.out;
110: this .stderr = System.err;
111:
112: this .worker.run();
113:
114: return this .worker.getResult().isSuccess();
115: } catch (IOException e) {
116: e.printStackTrace();
117: return false;
118: }
119: }
120:
121: /**
122: * Reports progress on System.out
123: *
124: * @see com.izforge.izpack.util.AbstractUIProgressHandler#startAction(String, int)
125: */
126: public void startAction(String name, int noOfJobs) {
127: this .stdout.println("[ Starting compilation ]");
128: this .job_name = "";
129: }
130:
131: /**
132: * Reports the error to System.err
133: *
134: * @param error the error
135: * @see CompileHandler#handleCompileError(CompileResult)
136: */
137: public void handleCompileError(CompileResult error) {
138: this .stdout.println();
139: this .stdout.println("[ Compilation failed ]");
140: this .stderr.println("Command line: " + error.getCmdline());
141: this .stderr.println();
142: this .stderr.println("stdout of compiler:");
143: this .stderr.println(error.getStdout());
144: this .stderr.println("stderr of compiler:");
145: this .stderr.println(error.getStderr());
146: // abort instantly and make installation fail
147: error.setAction(CompileResult.ACTION_ABORT);
148: }
149:
150: /**
151: * Sets state variable for thread sync.
152: *
153: * @see com.izforge.izpack.util.AbstractUIProgressHandler#stopAction()
154: */
155: public void stopAction() {
156: if ((this .job_name != null) && (this .last_line_len > 0)) {
157: String line = this .job_name + ": done.";
158: this .stdout.print("\r" + line);
159: for (int i = line.length(); i < this .last_line_len; i++)
160: this .stdout.print(' ');
161: this .stdout.println();
162: }
163:
164: if (this .worker.getResult().isSuccess())
165: this .stdout.println("[ Compilation successful ]");
166: }
167:
168: /**
169: * Tell about progress.
170: *
171: * @param val
172: * @param msg
173: * @see com.izforge.izpack.util.AbstractUIProgressHandler#progress(int, String)
174: */
175: public void progress(int val, String msg) {
176: double percentage = ((double) val) * 100.0d
177: / (double) this .job_max;
178:
179: String percent = (new Integer((int) percentage)).toString() + '%';
180: String line = this .job_name + ": " + percent;
181:
182: int line_len = line.length();
183:
184: this .stdout.print("\r" + line);
185: for (int i = line_len; i < this .last_line_len; i++)
186: this .stdout.print(' ');
187:
188: this .last_line_len = line_len;
189: }
190:
191: /**
192: * Reports progress to System.out
193: *
194: * @param jobName The next job's name.
195: * @param max unused
196: * @param jobNo The next job's number.
197: * @see com.izforge.izpack.util.AbstractUIProgressHandler#nextStep(String, int, int)
198: */
199: public void nextStep(String jobName, int max, int jobNo) {
200: if ((this .job_name != null) && (this .last_line_len > 0)) {
201: String line = this .job_name + ": done.";
202: this .stdout.print("\r" + line);
203: for (int i = line.length(); i < this .last_line_len; i++)
204: this .stdout.print(' ');
205: this .stdout.println();
206: }
207:
208: this .job_max = max;
209: this .job_name = jobName;
210: this .last_line_len = 0;
211: }
212:
213: /**
214: * {@inheritDoc}
215: */
216: public void setSubStepNo(int no_of_substeps) {
217: this.job_max = no_of_substeps;
218: }
219: }
|