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