source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/record/compiler/ant/RccTask.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.record.compiler.ant;
19
20import java.io.File;
21import java.util.ArrayList;
22import org.apache.hadoop.record.compiler.generated.Rcc;
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.DirectoryScanner;
25import org.apache.tools.ant.Project;
26import org.apache.tools.ant.Task;
27import org.apache.tools.ant.types.FileSet;
28
29/**
30 * Hadoop record compiler ant Task
31 *<p> This task takes the given record definition files and compiles them into
32 * java or c++
33 * files. It is then up to the user to compile the generated files.
34 *
35 * <p> The task requires the <code>file</code> or the nested fileset element to be
36 * specified. Optional attributes are <code>language</code> (set the output
37 * language, default is "java"),
38 * <code>destdir</code> (name of the destination directory for generated java/c++
39 * code, default is ".") and <code>failonerror</code> (specifies error handling
40 * behavior. default is true).
41 * <p><h4>Usage</h4>
42 * <pre>
43 * &lt;recordcc
44 *       destdir="${basedir}/gensrc"
45 *       language="java"&gt;
46 *   &lt;fileset include="**\/*.jr" /&gt;
47 * &lt;/recordcc&gt;
48 * </pre>
49 */
50public class RccTask extends Task {
51 
52  private String language = "java";
53  private File src;
54  private File dest = new File(".");
55  private final ArrayList<FileSet> filesets = new ArrayList<FileSet>();
56  private boolean failOnError = true;
57 
58  /** Creates a new instance of RccTask */
59  public RccTask() {
60  }
61 
62  /**
63   * Sets the output language option
64   * @param language "java"/"c++"
65   */
66  public void setLanguage(String language) {
67    this.language = language;
68  }
69 
70  /**
71   * Sets the record definition file attribute
72   * @param file record definition file
73   */
74  public void setFile(File file) {
75    this.src = file;
76  }
77 
78  /**
79   * Given multiple files (via fileset), set the error handling behavior
80   * @param flag true will throw build exception in case of failure (default)
81   */
82  public void setFailonerror(boolean flag) {
83    this.failOnError = flag;
84  }
85 
86  /**
87   * Sets directory where output files will be generated
88   * @param dir output directory
89   */
90  public void setDestdir(File dir) {
91    this.dest = dir;
92  }
93 
94  /**
95   * Adds a fileset that can consist of one or more files
96   * @param set Set of record definition files
97   */
98  public void addFileset(FileSet set) {
99    filesets.add(set);
100  }
101 
102  /**
103   * Invoke the Hadoop record compiler on each record definition file
104   */
105  public void execute() throws BuildException {
106    if (src == null && filesets.size()==0) {
107      throw new BuildException("There must be a file attribute or a fileset child element");
108    }
109    if (src != null) {
110      doCompile(src);
111    }
112    Project myProject = getProject();
113    for (int i = 0; i < filesets.size(); i++) {
114      FileSet fs = filesets.get(i);
115      DirectoryScanner ds = fs.getDirectoryScanner(myProject);
116      File dir = fs.getDir(myProject);
117      String[] srcs = ds.getIncludedFiles();
118      for (int j = 0; j < srcs.length; j++) {
119        doCompile(new File(dir, srcs[j]));
120      }
121    }
122  }
123 
124  private void doCompile(File file) throws BuildException {
125    String[] args = new String[5];
126    args[0] = "--language";
127    args[1] = this.language;
128    args[2] = "--destdir";
129    args[3] = this.dest.getPath();
130    args[4] = file.getPath();
131    int retVal = Rcc.driver(args);
132    if (retVal != 0 && failOnError) {
133      throw new BuildException("Hadoop record compiler returned error code "+retVal);
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.