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