summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
blob: ea320366b1c7d256432f829a92c84257b7ac0fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/** @file
This file is to define nested element which is meant for specifying a tool

Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/
package org.tianocore.framework.tasks;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.tools.ant.BuildException;
import org.tianocore.common.logger.EdkLog;

/**
 Class Tool is to define an external tool to be used for genffsfile
 **/
public class Tool implements EfiDefine, Section {

    private int alignment = 0;
    private String toolName     = "";
    private ToolArg toolArgList = new ToolArg();
    private Input inputFiles = new Input();
    private Input tempInputFile = new Input();
    private String outputPath;
    private String outputFileName ;
    private static Random ran = new Random(9999); 
    private List<Section>  gensectList = new ArrayList<Section>();
    /**
     Call extern tool

     @param     buffer  The buffer to put the result with alignment
     **/
    public void toBuffer (DataOutputStream buffer){
        ///
        /// call extern tool
        ///
        try {
            executeTool ();
        } catch (Exception e) {
            throw new BuildException("Call to executeTool failed!\n" + e.getMessage());
        }

        ///
        /// check if file exist
        ///
        File outputFile = new File (this.outputFileName);
        if (!outputFile.exists()) {
            throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");
        }

        ///
        /// Read output file and write it's cotains to buffer
        ///
        FileInputStream fs = null;
        DataInputStream in = null;
        try {
            fs  = new FileInputStream (outputFile);
            in  = new DataInputStream (fs);


            int fileLen = (int)outputFile.length();
            byte[] data  = new byte[fileLen];
            in.read(data);
            buffer.write(data, 0, fileLen);

            ///
            /// 4 byte alignment
            ///
            while ((fileLen & 0x03) != 0) {
                fileLen++;
                buffer.writeByte(0);
            }
        } catch (Exception e) {
            EdkLog.log(e.getMessage());
            throw new BuildException("Tool call, toBuffer failed!\n");
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (fs != null) {
                    fs.close();
                }
                outputFile.delete(); 
            } catch (Exception e) {
                EdkLog.log("WARNING: Cannot close " + outputFile.getPath());
            }
        }
    }

    ///
    /// execute external tool for genffsfile
    ///
    private void executeTool () {
        String command   = "";
        String argument  = "";
        command          = toolName;
        
        //
        //  Get each section which under the compress {};
        //  And add it is contains to File;
        //
        Section sect;
        try{
            Iterator SectionIter = this.gensectList.iterator();
            while (SectionIter.hasNext()){
                sect = (Section)SectionIter.next();
                //
                // Parse <genSection> element
                //
                File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));
                FileOutputStream bo = new FileOutputStream(outputFile);
                DataOutputStream Do = new DataOutputStream (bo);
                //
                //  Call each section class's toBuffer function.
                //
                try {
                    sect.toBuffer(Do);
                }
                catch (BuildException e) {
                    EdkLog.log(e.getMessage());
                    throw new BuildException ("GenSection failed at Tool!");
                } finally {
                    if (Do != null){
                        Do.close();    
                    }
                    
                } 
                this.tempInputFile.insFile(outputFile.getPath());
            }        
        } catch (IOException e){
            throw new BuildException ("Gensection failed at tool!");
        } 

        try {
            this.outputFileName = "Temp" + getRand();
            argument   = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ") 
                         + tempInputFile.toString(" ")+ " -o " + outputFileName;
            EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);
            ///
            /// execute command line
            ///
            Process process = Runtime.getRuntime().exec(command + " " + argument);
            process.waitFor();
            Iterator tempFile = tempInputFile.getNameList().iterator();
            while (tempFile.hasNext()){
                File file = new File((String)tempFile.next());
                if (file.exists()) {
                    file.delete();
                }
            }
        } catch (Exception e) {
            EdkLog.log(e.getMessage());
            throw new BuildException("Execution of externalTool task failed!\n");
        }
    }

    /**
     Add method of ANT task/datatype for nested ToolArg type of element

     @param     toolArg     The ToolArg object containing arguments for the tool
     **/
    public void addConfiguredToolArg (ToolArg toolArg) {
        toolArgList.insert(toolArg);
    }

    /**
     Get method of ANT task/datatype for attribute "OutputPath"

     @returns   The name of output path
     **/
    public String getOutputPath() {
        return outputPath;
    }

    /**
     Set method of ANT task/datatype for attribute "OutputPath"

     @param     outputPath  The name of output path
     **/
    public void setOutputPath(String outPutPath) {
        this.outputPath = outPutPath;
    }

    /**
     Get method of ANT task/datatype for attribute "ToolName"

     @returns   The name of the tool.
     **/
    public String getToolName() {
        return toolName;
    }

    /**
     Set method of ANT task/datatype for attribute "ToolName"

     @param     toolName    The name of the tool
     **/
    public void setToolName(String toolName) {
        this.toolName = toolName;
    }

    /**
     Add method of ANT task/datatype for nested Input type of element

     @param     file    The Input objec which represents a file
     **/
    public void addConfiguredInput(Input file) {
        inputFiles.insert(file);
    }
    
//    /**
//      addTool
//      
//      This function is to add instance of Tool to list.
//      
//      @param tool             instance of Tool.
//    **/
//    public void addTool(Tool tool){
//        this.toolList.add(tool);
//    }
    
    public void addGenSection(GenSectionTask genSect){
        this.gensectList.add(genSect);
    }

    /**
     Get random number.

     @returns   The random integer.
     **/
    public synchronized int getRand() {
        return ran.nextInt();
    }

    public int getAlignment() {
        return alignment;
    }

    public void setAlignment(int alignment) {
        if (alignment > 7) {
            this.alignment = 7;
        } else {
            this.alignment = alignment;
        }
    }

}