PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/parse/VariableSubstitution.java

#
Java | 82 lines | 58 code | 6 blank | 18 comment | 14 complexity | 66dad19e34d00757b7b63429e59a57b0 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 */
18package org.apache.hadoop.hive.ql.parse;
19
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
22
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.hadoop.hive.conf.HiveConf;
26import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
27import org.apache.hadoop.hive.ql.processors.SetProcessor;
28
29public class VariableSubstitution {
30
31  private static final Log l4j = LogFactory.getLog(VariableSubstitution.class);
32  protected static Pattern varPat = Pattern.compile("\\$\\{[^\\}\\$\u0020]+\\}");
33  protected static int MAX_SUBST = 40;
34
35  public String substitute (HiveConf conf, String expr) {
36
37    if (conf.getBoolVar(ConfVars.HIVEVARIABLESUBSTITUTE)){
38      l4j.debug("Substitution is on: "+expr);
39    } else {
40      return expr;
41    }
42    if (expr == null) {
43      return null;
44    }
45    Matcher match = varPat.matcher("");
46    String eval = expr;
47    for(int s=0; s<MAX_SUBST; s++) {
48      match.reset(eval);
49      if (!match.find()) {
50        return eval;
51      }
52      String var = match.group();
53      var = var.substring(2, var.length()-1); // remove ${ .. }
54      String val = null;
55      try {
56        if (var.startsWith(SetProcessor.SYSTEM_PREFIX)) {
57          val = System.getProperty(var.substring(SetProcessor.SYSTEM_PREFIX.length()));
58        }
59      } catch(SecurityException se) {
60        l4j.warn("Unexpected SecurityException in Configuration", se);
61      }
62      if (val ==null){
63        if (var.startsWith(SetProcessor.ENV_PREFIX)){
64          val = System.getenv(var.substring(SetProcessor.ENV_PREFIX.length()));
65        }
66      }
67      if (val == null) {
68        if (var.startsWith(SetProcessor.HIVECONF_PREFIX)){
69          val = conf.get(var.substring(SetProcessor.HIVECONF_PREFIX.length()));
70        }
71      }
72      if (val == null) {
73        l4j.debug("Interpolation result: "+eval);
74        return eval; // return literal ${var}: var is unbound
75      }
76      // substitute
77      eval = eval.substring(0, match.start())+val+eval.substring(match.end());
78    }
79    throw new IllegalStateException("Variable substitution depth too large: "
80                                    + MAX_SUBST + " " + expr);
81  }
82}