]>
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 | |
93f5dd0a | 35 | private String toolName = "";\r |
36 | private ToolArg toolArgList = new ToolArg();\r | |
37 | private Input inputFiles = new Input();\r | |
34a0c844 | 38 | private Input tempInputFile = new Input();\r |
93f5dd0a | 39 | private String outputPath;\r |
c493be6c | 40 | private String outputFileName ;\r |
93f5dd0a | 41 | private List<Section> gensectList = new ArrayList<Section>();\r |
878ddf1f | 42 | /**\r |
43 | Call extern tool\r | |
44 | \r | |
45 | @param buffer The buffer to put the result with alignment\r | |
46 | **/\r | |
87379bbe | 47 | public void toBuffer (DataOutputStream buffer){\r |
878ddf1f | 48 | ///\r |
49 | /// call extern tool\r | |
50 | ///\r | |
51 | try {\r | |
52 | executeTool ();\r | |
53 | } catch (Exception e) {\r | |
0fdb42ac | 54 | throw new BuildException("Call to executeTool failed!\n" + e.getMessage());\r |
878ddf1f | 55 | }\r |
56 | \r | |
57 | ///\r | |
58 | /// check if file exist\r | |
59 | ///\r | |
c493be6c | 60 | File outputFile = new File (this.outputFileName);\r |
82810f3b | 61 | if (!outputFile.exists()) {\r |
62 | throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");\r | |
878ddf1f | 63 | }\r |
64 | \r | |
65 | ///\r | |
66 | /// Read output file and write it's cotains to buffer\r | |
67 | ///\r | |
82810f3b | 68 | FileInputStream fs = null;\r |
69 | DataInputStream in = null;\r | |
878ddf1f | 70 | try {\r |
82810f3b | 71 | fs = new FileInputStream (outputFile);\r |
72 | in = new DataInputStream (fs);\r | |
73 | \r | |
74 | \r | |
75 | int fileLen = (int)outputFile.length();\r | |
76 | byte[] data = new byte[fileLen];\r | |
77 | in.read(data);\r | |
78 | buffer.write(data, 0, fileLen);\r | |
878ddf1f | 79 | \r |
80 | ///\r | |
81 | /// 4 byte alignment\r | |
82 | ///\r | |
83 | while ((fileLen & 0x03) != 0) {\r | |
84 | fileLen++;\r | |
85 | buffer.writeByte(0);\r | |
86 | }\r | |
878ddf1f | 87 | } catch (Exception e) {\r |
0fdb42ac | 88 | EdkLog.log(e.getMessage());\r |
3f7b510e | 89 | throw new BuildException("Tool call, toBuffer failed!\n");\r |
82810f3b | 90 | } finally {\r |
91 | try {\r | |
92 | if (in != null) {\r | |
93 | in.close();\r | |
94 | }\r | |
95 | if (fs != null) {\r | |
96 | fs.close();\r | |
97 | }\r | |
34a0c844 | 98 | outputFile.delete(); \r |
82810f3b | 99 | } catch (Exception e) {\r |
0fdb42ac | 100 | EdkLog.log("WARNING: Cannot close " + outputFile.getPath());\r |
82810f3b | 101 | }\r |
878ddf1f | 102 | }\r |
103 | }\r | |
104 | \r | |
105 | ///\r | |
106 | /// execute external tool for genffsfile\r | |
107 | ///\r | |
108 | private void executeTool () {\r | |
109 | String command = "";\r | |
110 | String argument = "";\r | |
111 | command = toolName;\r | |
a1ffb10f | 112 | \r |
113 | //\r | |
114 | // Get each section which under the compress {};\r | |
115 | // And add it is contains to File;\r | |
116 | //\r | |
117 | Section sect;\r | |
118 | try{\r | |
119 | Iterator SectionIter = this.gensectList.iterator();\r | |
120 | while (SectionIter.hasNext()){\r | |
121 | sect = (Section)SectionIter.next();\r | |
122 | //\r | |
123 | // Parse <genSection> element\r | |
124 | //\r | |
93f5dd0a | 125 | File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));\r |
a1ffb10f | 126 | FileOutputStream bo = new FileOutputStream(outputFile);\r |
127 | DataOutputStream Do = new DataOutputStream (bo);\r | |
128 | //\r | |
129 | // Call each section class's toBuffer function.\r | |
130 | //\r | |
131 | try {\r | |
132 | sect.toBuffer(Do);\r | |
133 | }\r | |
134 | catch (BuildException e) {\r | |
0fdb42ac | 135 | EdkLog.log(e.getMessage());\r |
a1ffb10f | 136 | throw new BuildException ("GenSection failed at Tool!");\r |
c493be6c | 137 | } finally {\r |
138 | if (Do != null){\r | |
139 | Do.close(); \r | |
140 | }\r | |
141 | \r | |
142 | } \r | |
34a0c844 | 143 | this.tempInputFile.insFile(outputFile.getPath());\r |
a1ffb10f | 144 | } \r |
145 | } catch (IOException e){\r | |
146 | throw new BuildException ("Gensection failed at tool!");\r | |
c493be6c | 147 | } \r |
878ddf1f | 148 | \r |
878ddf1f | 149 | try {\r |
c493be6c | 150 | Random ran = new Random(9999); \r |
151 | this.outputFileName = "Temp" + ran.nextInt();\r | |
93f5dd0a | 152 | argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ") \r |
c493be6c | 153 | + tempInputFile.toString(" ")+ " -o " + outputFileName;\r |
93f5dd0a | 154 | EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);\r |
c493be6c | 155 | EdkLog.log(this, EdkLog.EDK_INFO, this.outputFileName);\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 | |
c493be6c | 168 | } catch (Exception e) {\r EdkLog.log(e.getMessage());\r |
3f7b510e | 169 | throw new BuildException("Execution of externalTool task failed!\n");\r |
878ddf1f | 170 | }\r |
171 | }\r | |
172 | \r | |
173 | /**\r | |
174 | Add method of ANT task/datatype for nested ToolArg type of element\r | |
175 | \r | |
176 | @param toolArg The ToolArg object containing arguments for the tool\r | |
177 | **/\r | |
93f5dd0a | 178 | public void addConfiguredToolArg (ToolArg toolArg) {\r |
179 | toolArgList.insert(toolArg);\r | |
878ddf1f | 180 | }\r |
181 | \r | |
182 | /**\r | |
183 | Get method of ANT task/datatype for attribute "OutputPath"\r | |
184 | \r | |
185 | @returns The name of output path\r | |
186 | **/\r | |
187 | public String getOutputPath() {\r | |
188 | return outputPath;\r | |
189 | }\r | |
190 | \r | |
191 | /**\r | |
192 | Set method of ANT task/datatype for attribute "OutputPath"\r | |
193 | \r | |
194 | @param outputPath The name of output path\r | |
195 | **/\r | |
196 | public void setOutputPath(String outPutPath) {\r | |
197 | this.outputPath = outPutPath;\r | |
198 | }\r | |
199 | \r | |
200 | /**\r | |
201 | Get method of ANT task/datatype for attribute "ToolName"\r | |
202 | \r | |
203 | @returns The name of the tool.\r | |
204 | **/\r | |
205 | public String getToolName() {\r | |
206 | return toolName;\r | |
207 | }\r | |
208 | \r | |
209 | /**\r | |
210 | Set method of ANT task/datatype for attribute "ToolName"\r | |
211 | \r | |
212 | @param toolName The name of the tool\r | |
213 | **/\r | |
214 | public void setToolName(String toolName) {\r | |
215 | this.toolName = toolName;\r | |
216 | }\r | |
217 | \r | |
218 | /**\r | |
219 | Add method of ANT task/datatype for nested Input type of element\r | |
220 | \r | |
221 | @param file The Input objec which represents a file\r | |
222 | **/\r | |
93f5dd0a | 223 | public void addConfiguredInput(Input file) {\r |
224 | inputFiles.insert(file);\r | |
878ddf1f | 225 | }\r |
a1ffb10f | 226 | \r |
227 | // /**\r | |
228 | // addTool\r | |
229 | // \r | |
230 | // This function is to add instance of Tool to list.\r | |
231 | // \r | |
232 | // @param tool instance of Tool.\r | |
233 | // **/\r | |
234 | // public void addTool(Tool tool){\r | |
235 | // this.toolList.add(tool);\r | |
236 | // }\r | |
237 | \r | |
238 | public void addGenSection(GenSectionTask genSect){\r | |
239 | this.gensectList.add(genSect);\r | |
93f5dd0a | 240 | } \r |
878ddf1f | 241 | }\r |
242 | \r | |
243 | \r |