KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > CompilePanelAutomationHelper


1 /*
2  /*
3  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
4  *
5  * http://www.izforge.com/izpack/
6  * http://developer.berlios.de/projects/izpack/
7  *
8  * Copyright 2003 Jonathan Halliday
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.panels;
24
25 import java.io.IOException JavaDoc;
26
27 import net.n3.nanoxml.XMLElement;
28
29 import com.izforge.izpack.installer.AutomatedInstallData;
30 import com.izforge.izpack.installer.CompileHandler;
31 import com.izforge.izpack.installer.CompileResult;
32 import com.izforge.izpack.installer.CompileWorker;
33 import com.izforge.izpack.installer.PanelAutomation;
34 import com.izforge.izpack.installer.PanelAutomationHelper;
35
36 /**
37  * Functions to support automated usage of the CompilePanel
38  *
39  * @author Jonathan Halliday
40  * @author Tino Schwarze
41  */

42 public class CompilePanelAutomationHelper extends PanelAutomationHelper implements PanelAutomation,
43         CompileHandler
44 {
45
46     private CompileWorker worker = null;
47
48     private int job_max = 0;
49
50     private String JavaDoc job_name = null;
51
52     private int last_line_len = 0;
53
54     /**
55      * Save data for running automated.
56      *
57      * @param installData installation parameters
58      * @param panelRoot unused.
59      */

60     public void makeXMLData(AutomatedInstallData installData, XMLElement panelRoot)
61     {
62         // not used here - during automatic installation, no automatic
63
// installation information is generated
64
}
65
66     /**
67      * Perform the installation actions.
68      *
69      * @param panelRoot The panel XML tree root.
70      */

71     public boolean runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
72     {
73         XMLElement compiler_xml = panelRoot.getFirstChildNamed("compiler");
74
75         String JavaDoc compiler = null;
76
77         if (compiler_xml != null) compiler = compiler_xml.getContent();
78
79         if (compiler == null)
80         {
81             System.out.println("invalid automation data: could not find compiler");
82             return false;
83         }
84
85         XMLElement args_xml = panelRoot.getFirstChildNamed("arguments");
86
87         String JavaDoc args = null;
88
89         if (args_xml != null) args = args_xml.getContent();
90
91         if (args_xml == null)
92         {
93             System.out.println("invalid automation data: could not find compiler arguments");
94             return false;
95         }
96
97         try
98         {
99             this.worker = new CompileWorker(idata, this);
100             this.worker.setCompiler(compiler);
101             this.worker.setCompilerArguments(args);
102
103             this.worker.run();
104             
105             return this.worker.getResult().isSuccess();
106         }
107         catch (IOException JavaDoc e)
108         {
109             e.printStackTrace();
110             return false;
111         }
112     }
113
114     /**
115      * Reports progress on System.out
116      *
117      * @see com.izforge.izpack.util.AbstractUIProgressHandler#startAction(String, int)
118      */

119     public void startAction(String JavaDoc name, int noOfJobs)
120     {
121         System.out.println("[ Starting compilation ]");
122         this.job_name = "";
123     }
124
125     /**
126      * Reports the error to System.err
127      *
128      * @param error the error
129      * @see CompileHandler#handleCompileError(CompileResult)
130      */

131     public void handleCompileError(CompileResult error)
132     {
133         System.out.println();
134         System.out.println("[ Compilation failed ]");
135         System.err.println("Command line: " + error.getCmdline());
136         System.err.println();
137         System.err.println("stdout of compiler:");
138         System.err.println(error.getStdout());
139         System.err.println("stderr of compiler:");
140         System.err.println(error.getStderr());
141         // abort instantly and make installation fail
142
error.setAction(CompileResult.ACTION_ABORT);
143     }
144
145     /**
146      * Sets state variable for thread sync.
147      *
148      * @see com.izforge.izpack.util.AbstractUIProgressHandler#stopAction()
149      */

150     public void stopAction()
151     {
152         if ((this.job_name != null) && (this.last_line_len > 0))
153         {
154             String JavaDoc line = this.job_name + ": done.";
155             System.out.print("\r" + line);
156             for (int i = line.length(); i < this.last_line_len; i++)
157                 System.out.print(' ');
158             System.out.println();
159         }
160
161         if (this.worker.getResult().isSuccess()) System.out.println("[ Compilation successful ]");
162     }
163
164     /**
165      * Tell about progress.
166      *
167      * @param val
168      * @param msg
169      * @see com.izforge.izpack.util.AbstractUIProgressHandler#progress(int, String)
170      */

171     public void progress(int val, String JavaDoc msg)
172     {
173         double percentage = ((double) val) * 100.0d / (double) this.job_max;
174
175         String JavaDoc percent = (new Integer JavaDoc((int) percentage)).toString() + '%';
176         String JavaDoc line = this.job_name + ": " + percent;
177
178         int line_len = line.length();
179
180         System.out.print("\r" + line);
181         for (int i = line_len; i < this.last_line_len; i++)
182             System.out.print(' ');
183
184         this.last_line_len = line_len;
185     }
186
187     /**
188      * Reports progress to System.out
189      *
190      * @param jobName The next job's name.
191      * @param max unused
192      * @param jobNo The next job's number.
193      * @see com.izforge.izpack.util.AbstractUIProgressHandler#nextStep(String, int, int)
194      */

195     public void nextStep(String JavaDoc jobName, int max, int jobNo)
196     {
197         if ((this.job_name != null) && (this.last_line_len > 0))
198         {
199             String JavaDoc line = this.job_name + ": done.";
200             System.out.print("\r" + line);
201             for (int i = line.length(); i < this.last_line_len; i++)
202                 System.out.print(' ');
203             System.out.println();
204         }
205
206         this.job_max = max;
207         this.job_name = jobName;
208         this.last_line_len = 0;
209     }
210 }
211
Popular Tags