StramClientUtils { public static void evalProperties(Properties target, Configuration vars) { ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); Pattern substitutionPattern = Pattern.compile("\\$\\{(.+?)\\}"); Pattern evalPattern = Pattern.compile("\\{% (.+?) %\\}"); try { engine.eval("var _prop = {}"); for (Map.Entry<String, String> entry : vars) { String evalString = String.format("_prop[\"%s\"] = \"%s\"", StringEscapeUtils.escapeJava(entry.getKey()), StringEscapeUtils.escapeJava(entry.getValue())); engine.eval(evalString); } } catch (ScriptException ex) { LOG.warn("Javascript error: {}", ex.getMessage()); } for (Map.Entry<Object, Object> entry : target.entrySet()) { String value = entry.getValue().toString(); Matcher matcher = substitutionPattern.matcher(value); if (matcher.find()) { StringBuilder newValue = new StringBuilder(); int cursor = 0; do { newValue.append(value.substring(cursor, matcher.start())); String subst = vars.get(matcher.group(1)); if (subst != null) { newValue.append(subst); } cursor = matcher.end(); } while (matcher.find()); newValue.append(value.substring(cursor)); target.put(entry.getKey(), newValue.toString()); } matcher = evalPattern.matcher(value); if (matcher.find()) { StringBuilder newValue = new StringBuilder(); int cursor = 0; do { newValue.append(value.substring(cursor, matcher.start())); try { Object result = engine.eval(matcher.group(1)); String eval = result.toString(); if (eval != null) { newValue.append(eval); } } catch (ScriptException ex) { LOG.warn("JavaScript exception {}", ex.getMessage()); } cursor = matcher.end(); } while (matcher.find()); newValue.append(value.substring(cursor)); target.put(entry.getKey(), newValue.toString()); } } }  static YarnClient createYarnClient(Configuration conf); static String getHostName(); static File getUserDTDirectory(); static File getConfigDir(); static File getInstallationDir(); static boolean isDevelopmentMode(); static File getBackupsDirectory(); static Configuration addDTDefaultResources(Configuration conf); static Configuration addDTSiteResources(Configuration conf); static void addDTLocalResources(Configuration conf); static URL getDTSiteXmlFile(); static FileSystem newFileSystemInstance(Configuration conf); static Path getApexDFSRootDir(FileSystem fs, Configuration conf); static Path getDTDFSRootDir(FileSystem fs, Configuration conf); static Path getDTDFSConfigDir(FileSystem fs, Configuration conf); static Path getDTDFSProfilesDir(FileSystem fs, Configuration conf); static void changeDTEnvironment(String key, String value); static void copyFromLocalFileNoChecksum(FileSystem fs, File fromLocal, Path toDFS); static boolean configComplete(Configuration conf); static void evalProperties(Properties target, Configuration vars); static void evalConfiguration(Configuration conf); static T doAs(String userName, PrivilegedExceptionAction<T> action); static ApplicationReport getStartedAppInstanceByName(YarnClient clientRMService, String appName, String user, String excludeAppId); static InetSocketAddress getRMWebAddress(Configuration conf, String rmId); static InetSocketAddress getRMWebAddress(Configuration conf, boolean sslEnabled, String rmId); static String getSocketConnectString(InetSocketAddress socketAddress); static List<InetSocketAddress> getRMAddresses(Configuration conf); static List<ApplicationReport> cleanAppDirectories(YarnClient clientRMService, Configuration conf, FileSystem fs, long finishedBefore); static AppPackage.AppInfo jsonFileToAppInfo(File file, Configuration config); static void addAttributeToArgs(Attribute<String> attribute, Context context, List<CharSequence> vargs); static final String DT_VERSION; static final String DT_DFS_ROOT_DIR; static final String APEX_APP_DFS_ROOT_DIR; static final String DT_DFS_USER_NAME; static final String DT_CONFIG_STATUS; static final String SUBDIR_APPS; static final String SUBDIR_PROFILES; static final String SUBDIR_CONF; static final long RESOURCEMANAGER_CONNECT_MAX_WAIT_MS_OVERRIDE; static final String DT_HDFS_TOKEN_MAX_LIFE_TIME; static final String DT_HDFS_TOKEN_RENEW_INTERVAL; static final String HDFS_TOKEN_MAX_LIFE_TIME; static final String HDFS_TOKEN_RENEW_INTERVAL; static final String DT_RM_TOKEN_MAX_LIFE_TIME; static final String DT_RM_TOKEN_RENEW_INTERVAL; @Deprecated
  static final String KEY_TAB_FILE; static final String TOKEN_ANTICIPATORY_REFRESH_FACTOR; static final long DELEGATION_TOKEN_MAX_LIFETIME_DEFAULT; static final long DELEGATION_TOKEN_RENEW_INTERVAL_DEFAULT; static final String TOKEN_REFRESH_PRINCIPAL; static final String TOKEN_REFRESH_KEYTAB; static final String DT_DEFAULT_XML_FILE; static final String DT_SITE_XML_FILE; static final String DT_SITE_GLOBAL_XML_FILE; static final String DT_ENV_SH_FILE; static final String CUSTOM_ENV_SH_FILE; static final String BACKUPS_DIRECTORY; }
@Test public void testEvalExpression() throws Exception { Configuration conf = new Configuration(); conf.set("a.b.c", "123"); conf.set("d.e.f", "456"); conf.set("x.y.z", "foobar"); Properties prop = new Properties(); prop.put("product.result", "Product result is {% (_prop[\"a.b.c\"] * _prop[\"d.e.f\"]).toFixed(0) %}..."); prop.put("concat.result", "Concat result is {% _prop[\"x.y.z\"] %} ... {% _prop[\"a.b.c\"] %} blah"); StramClientUtils.evalProperties(prop, conf); Assert.assertEquals("Product result is " + (123 * 456) + "...", prop.get("product.result")); Assert.assertEquals("Concat result is foobar ... 123 blah", prop.get("concat.result")); Assert.assertEquals("123", conf.get("a.b.c")); Assert.assertEquals("456", conf.get("d.e.f")); Assert.assertEquals("foobar", conf.get("x.y.z")); }
@Test public void testVariableSubstitution() throws Exception { Configuration conf = new Configuration(); conf.set("a.b.c", "123"); conf.set("x.y.z", "foobar"); Properties prop = new Properties(); prop.put("var.result", "1111 ${a.b.c} xxx ${x.y.z} yyy"); StramClientUtils.evalProperties(prop, conf); Assert.assertEquals("1111 123 xxx foobar yyy", prop.get("var.result")); }