PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ant/src/org/apache/hadoop/hive/ant/GetVersionPref.java

#
Java | 94 lines | 41 code | 14 blank | 39 comment | 4 complexity | 9867398c0bd8921213e37848b25a0563 MD5 | raw file
 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 */
18
19package org.apache.hadoop.hive.ant;
20
21import org.apache.tools.ant.AntClassLoader;
22import org.apache.tools.ant.BuildException;
23import org.apache.tools.ant.Task;
24import org.apache.tools.ant.Project;
25
26import java.util.regex.Pattern;
27import java.util.regex.Matcher;
28import java.io.*;
29
30/**
31 * Implementation of the ant task <getversionpref property="nameoftheproperty" input="versionstring"/>.
32 *
33 * This ant task takes an input version string (e.g. 0.17.2) and set an ant property (whose name
34 * is specified in the property attribute) with the version prefix. For 0.17.2, the version prefix
35 * is 0.17. Similarly, for 0.18.0, the version prefix is 0.18. The version prefix is the first two
36 * components of the version string.
37 */
38public class GetVersionPref extends Task {
39
40  /**
41   * The name of the property that gets the version prefix.
42   */
43  protected String property;
44
45  /**
46   * The input string that contains the version string.
47   */
48  protected String input;
49 
50  public void setProperty(String property) {
51    this.property = property;
52  }
53
54  public String getProperty() {
55    return property;
56  }
57
58  public void setInput(String input) {
59    this.input = input;
60  }
61
62  public String getInput() {
63    return input;
64  }
65
66  /**
67   * Executes the ant task <getversionperf>.
68   *
69   * It extracts the version prefix using regular expressions on the version string. It then sets
70   * the property in the project with the extracted prefix. The property is set to an empty string
71   * in case no match is found for the prefix regular expression (which will happen in case the
72   * version string does not conform to the version format).
73   */
74  @Override
75  public void execute() throws BuildException {
76
77    if (property == null) {
78      throw new BuildException("No property specified");
79    }
80
81    if (input == null) {
82      throw new BuildException("No input stringspecified");
83    }
84
85    try {
86      Pattern p = Pattern.compile("^(\\d+\\.\\d+).*");
87      Matcher m = p.matcher(input);
88      getProject().setProperty(property, m.matches() ? m.group(1) : "");
89    }
90    catch (Exception e) {
91      throw new BuildException("Failed with: " + e.getMessage());
92    }
93  }
94}