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