]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /** @file\r |
2 | This file is to define nested element which is meant for specifying a tool\r | |
3 | \r | |
4 | Copyright (c) 2006, Intel Corporation\r | |
5 | All rights reserved. This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | package org.tianocore.framework.tasks;\r | |
15 | \r | |
16 | import java.io.DataInputStream;\r | |
17 | import java.io.DataOutputStream;\r | |
18 | import java.io.File;\r | |
19 | import java.io.FileInputStream;\r | |
a1ffb10f | 20 | import java.io.FileOutputStream;\r |
21 | import java.io.IOException;\r | |
878ddf1f | 22 | import java.util.ArrayList;\r |
23 | import java.util.Iterator;\r | |
24 | import java.util.List;\r | |
c493be6c | 25 | import java.util.Random;\r |
878ddf1f | 26 | \r |
27 | import org.apache.tools.ant.BuildException;\r | |
a1ffb10f | 28 | import org.tianocore.common.logger.EdkLog;\r |
878ddf1f | 29 | \r |
30 | /**\r | |
31 | Class Tool is to define an external tool to be used for genffsfile\r | |
32 | **/\r | |
33 | public class Tool implements EfiDefine, Section {\r | |
34 | \r | |
e3cc4061 | 35 | private int alignment = 0;\r |
93f5dd0a | 36 | private String toolName = "";\r |
37 | private ToolArg toolArgList = new ToolArg();\r | |
38 | private Input inputFiles = new Input();\r | |
34a0c844 | 39 | private Input tempInputFile = new Input();\r |
93f5dd0a | 40 | private String outputPath;\r |
c493be6c | 41 | private String outputFileName ;\r |
4a557ace | 42 | private static Random ran = new Random(9999); \r |
93f5dd0a | 43 | private List<Section> gensectList = new ArrayList<Section>();\r |
878ddf1f | 44 | /**\r |
45 | Call extern tool\r | |
46 | \r | |
47 | @param buffer The buffer to put the result with alignment\r | |
48 | **/\r | |
87379bbe | 49 | public void toBuffer (DataOutputStream buffer){\r |
878ddf1f | 50 | ///\r |
51 | /// call extern tool\r | |
52 | ///\r | |
53 | try {\r | |
54 | executeTool ();\r | |
55 | } catch (Exception e) {\r | |
0fdb42ac | 56 | throw new BuildException("Call to executeTool failed!\n" + e.getMessage());\r |
878ddf1f | 57 | }\r |
58 | \r | |
59 | ///\r | |
60 | /// check if file exist\r | |
61 | ///\r | |
c493be6c | 62 | File outputFile = new File (this.outputFileName);\r |
82810f3b | 63 | if (!outputFile.exists()) {\r |
64 | throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");\r | |
878ddf1f | 65 | }\r |
66 | \r | |
67 | ///\r | |
68 | /// Read output file and write it's cotains to buffer\r | |
69 | ///\r | |
82810f3b | 70 | FileInputStream fs = null;\r |
71 | DataInputStream in = null;\r | |
878ddf1f | 72 | try {\r |
82810f3b | 73 | fs = new FileInputStream (outputFile);\r |
74 | in = new DataInputStream (fs);\r | |
75 | \r | |
76 | \r | |
77 | int fileLen = (int)outputFile.length();\r | |
78 | byte[] data = new byte[fileLen];\r | |
79 | in.read(data);\r | |
80 | buffer.write(data, 0, fileLen);\r | |
878ddf1f | 81 | \r |
82 | ///\r | |
83 | /// 4 byte alignment\r | |
84 | ///\r | |
85 | while ((fileLen & 0x03) != 0) {\r | |
86 | fileLen++;\r | |
87 | buffer.writeByte(0);\r | |
88 | }\r | |
878ddf1f | 89 | } catch (Exception e) {\r |
0fdb42ac | 90 | EdkLog.log(e.getMessage());\r |
3f7b510e | 91 | throw new BuildException("Tool call, toBuffer failed!\n");\r |
82810f3b | 92 | } finally {\r |
93 | try {\r | |
94 | if (in != null) {\r | |
95 | in.close();\r | |
96 | }\r | |
97 | if (fs != null) {\r | |
98 | fs.close();\r | |
99 | }\r | |
34a0c844 | 100 | outputFile.delete(); \r |
82810f3b | 101 | } catch (Exception e) {\r |
0fdb42ac | 102 | EdkLog.log("WARNING: Cannot close " + outputFile.getPath());\r |
82810f3b | 103 | }\r |
878ddf1f | 104 | }\r |
105 | }\r | |
106 | \r | |
107 | ///\r | |
108 | /// execute external tool for genffsfile\r | |
109 | ///\r | |
110 | private void executeTool () {\r | |
111 | String command = "";\r | |
112 | String argument = "";\r | |
113 | command = toolName;\r | |
a1ffb10f | 114 | \r |
115 | //\r | |
116 | // Get each section which under the compress {};\r | |
117 | // And add it is contains to File;\r | |
118 | //\r | |
119 | Section sect;\r | |
120 | try{\r | |
121 | Iterator SectionIter = this.gensectList.iterator();\r | |
122 | while (SectionIter.hasNext()){\r | |
123 | sect = (Section)SectionIter.next();\r | |
124 | //\r | |
125 | // Parse <genSection> element\r | |
126 | //\r | |
93f5dd0a | 127 | File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));\r |
a1ffb10f | 128 | FileOutputStream bo = new FileOutputStream(outputFile);\r |
129 | DataOutputStream Do = new DataOutputStream (bo);\r | |
130 | //\r | |
131 | // Call each section class's toBuffer function.\r | |
132 | //\r | |
133 | try {\r | |
134 | sect.toBuffer(Do);\r | |
135 | }\r | |
136 | catch (BuildException e) {\r | |
0fdb42ac | 137 | EdkLog.log(e.getMessage());\r |
a1ffb10f | 138 | throw new BuildException ("GenSection failed at Tool!");\r |
c493be6c | 139 | } finally {\r |
140 | if (Do != null){\r | |
141 | Do.close(); \r | |
142 | }\r | |
143 | \r | |
144 | } \r | |
34a0c844 | 145 | this.tempInputFile.insFile(outputFile.getPath());\r |
a1ffb10f | 146 | } \r |
147 | } catch (IOException e){\r | |
148 | throw new BuildException ("Gensection failed at tool!");\r | |
c493be6c | 149 | } \r |
878ddf1f | 150 | \r |
878ddf1f | 151 | try {\r |
2320bb14 | 152 | this.outputFileName = "Temp" + getRand();\r |
93f5dd0a | 153 | argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ") \r |
c493be6c | 154 | + tempInputFile.toString(" ")+ " -o " + outputFileName;\r |
93f5dd0a | 155 | EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);\r |
878ddf1f | 156 | ///\r |
157 | /// execute command line\r | |
158 | ///\r | |
a1ffb10f | 159 | Process process = Runtime.getRuntime().exec(command + " " + argument);\r |
160 | process.waitFor();\r | |
34a0c844 | 161 | Iterator tempFile = tempInputFile.getNameList().iterator();\r |
162 | while (tempFile.hasNext()){\r | |
163 | File file = new File((String)tempFile.next());\r | |
164 | if (file.exists()) {\r | |
165 | file.delete();\r | |
166 | }\r | |
167 | }\r | |
2320bb14 | 168 | } catch (Exception e) {\r |
169 | EdkLog.log(e.getMessage());\r | |
3f7b510e | 170 | throw new BuildException("Execution of externalTool task failed!\n");\r |
878ddf1f | 171 | }\r |
172 | }\r | |
173 | \r | |
174 | /**\r | |
175 | Add method of ANT task/datatype for nested ToolArg type of element\r | |
176 | \r | |
177 | @param toolArg The ToolArg object containing arguments for the tool\r | |
178 | **/\r | |
93f5dd0a | 179 | public void addConfiguredToolArg (ToolArg toolArg) {\r |
180 | toolArgList.insert(toolArg);\r | |
878ddf1f | 181 | }\r |
182 | \r | |
183 | /**\r | |
184 | Get method of ANT task/datatype for attribute "OutputPath"\r | |
185 | \r | |
186 | @returns The name of output path\r | |
187 | **/\r | |
188 | public String getOutputPath() {\r | |
189 | return outputPath;\r | |
190 | }\r | |
191 | \r | |
192 | /**\r | |
193 | Set method of ANT task/datatype for attribute "OutputPath"\r | |
194 | \r | |
195 | @param outputPath The name of output path\r | |
196 | **/\r | |
197 | public void setOutputPath(String outPutPath) {\r | |
198 | this.outputPath = outPutPath;\r | |
199 | }\r | |
200 | \r | |
201 | /**\r | |
202 | Get method of ANT task/datatype for attribute "ToolName"\r | |
203 | \r | |
204 | @returns The name of the tool.\r | |
205 | **/\r | |
206 | public String getToolName() {\r | |
207 | return toolName;\r | |
208 | }\r | |
209 | \r | |
210 | /**\r | |
211 | Set method of ANT task/datatype for attribute "ToolName"\r | |
212 | \r | |
213 | @param toolName The name of the tool\r | |
214 | **/\r | |
215 | public void setToolName(String toolName) {\r | |
216 | this.toolName = toolName;\r | |
217 | }\r | |
218 | \r | |
219 | /**\r | |
220 | Add method of ANT task/datatype for nested Input type of element\r | |
221 | \r | |
222 | @param file The Input objec which represents a file\r | |
223 | **/\r | |
93f5dd0a | 224 | public void addConfiguredInput(Input file) {\r |
225 | inputFiles.insert(file);\r | |
878ddf1f | 226 | }\r |
a1ffb10f | 227 | \r |
228 | // /**\r | |
229 | // addTool\r | |
230 | // \r | |
231 | // This function is to add instance of Tool to list.\r | |
232 | // \r | |
233 | // @param tool instance of Tool.\r | |
234 | // **/\r | |
235 | // public void addTool(Tool tool){\r | |
236 | // this.toolList.add(tool);\r | |
237 | // }\r | |
238 | \r | |
239 | public void addGenSection(GenSectionTask genSect){\r | |
240 | this.gensectList.add(genSect);\r | |
2320bb14 | 241 | }\r |
242 | \r | |
243 | /**\r | |
244 | Get random number.\r | |
245 | \r | |
246 | @returns The random integer.\r | |
247 | **/\r | |
248 | public synchronized int getRand() {\r | |
249 | return ran.nextInt();\r | |
250 | }\r | |
e3cc4061 | 251 | \r |
252 | public int getAlignment() {\r | |
253 | return alignment;\r | |
254 | }\r | |
255 | \r | |
256 | public void setAlignment(int alignment) {\r | |
257 | if (alignment > 7) {\r | |
258 | this.alignment = 7;\r | |
259 | } else {\r | |
260 | this.alignment = alignment;\r | |
261 | }\r | |
262 | }\r | |
263 | \r | |
878ddf1f | 264 | }\r |
265 | \r | |
266 | \r |