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