You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by is...@apache.org on 2020/12/05 04:37:34 UTC

[lucene-solr] 07/18: substitute properties real time

This is an automated email from the ASF dual-hosted git repository.

ishan pushed a commit to branch jira/solr14827
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit cecf1f9aa32652c986dad2ce4893d47a6e2e2054
Author: noblepaul <no...@gmail.com>
AuthorDate: Wed Sep 9 18:20:37 2020 +1000

    substitute properties real time
---
 .../java/org/apache/solr/schema/IndexSchema.java   |  5 +--
 .../java/org/apache/solr/util/DataConfigNode.java  | 36 +++++++++++++++++-----
 .../apache/solr/common/util/PropertiesUtil.java    | 13 +++++---
 3 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index dfb82fd..772e049 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -483,12 +483,13 @@ public class IndexSchema {
     try {
       // pass the config resource loader to avoid building an empty one for no reason:
       // in the current case though, the stream is valid so we wont load the resource by name
-      XmlConfigFile schemaConf = new XmlConfigFile(loader, SCHEMA, is, SLASH+SCHEMA+SLASH, substitutableProperties);
+      XmlConfigFile schemaConf = new XmlConfigFile(loader, SCHEMA, is, SLASH+SCHEMA+SLASH, null);
 //      Document document = schemaConf.getDocument();
 //      final XPath xpath = schemaConf.getXPath();
 //      String expression = stepsToPath(SCHEMA, AT + NAME);
 //      Node nd = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
-      ConfigNode rootNode = new DataConfigNode(new DOMConfigNode(schemaConf.getDocument().getDocumentElement()) , Collections.singleton("similarity")) ;
+      ConfigNode rootNode = new DataConfigNode(new DOMConfigNode(schemaConf.getDocument().getDocumentElement()),
+          substitutableProperties::getProperty) ;
       name = rootNode.attributes().get("name");
       StringBuilder sb = new StringBuilder();
       // Another case where the initialization from the test harness is different than the "real world"
diff --git a/solr/core/src/java/org/apache/solr/util/DataConfigNode.java b/solr/core/src/java/org/apache/solr/util/DataConfigNode.java
index d7bc679..3867af7 100644
--- a/solr/core/src/java/org/apache/solr/util/DataConfigNode.java
+++ b/solr/core/src/java/org/apache/solr/util/DataConfigNode.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.BiConsumer;
 import java.util.function.Function;
 import java.util.function.Predicate;
 
@@ -37,25 +38,44 @@ public class DataConfigNode implements ConfigNode {
   final SimpleMap<String> attributes;
   private final Map<String, List<ConfigNode>> kids = new HashMap<>();
   private String textData;
+  private final Function<String, String> propertySubstitution;
 
-  public DataConfigNode(ConfigNode root, Set<String> ignore) {
+  public DataConfigNode(ConfigNode root, Function<String, String> propertySubstitution) {
+    this.propertySubstitution = propertySubstitution;
     name = root.name();
-    attributes = root.attributes();
+    attributes = wrap(root.attributes());
     textData = root.textValue();
-    if (textData != null) textData = textData.trim();
+    if (textData != null) textData = PropertiesUtil.substitute(textData.trim(), propertySubstitution);
     root.forEachChild(it -> {
       List<ConfigNode> nodes = kids.computeIfAbsent(it.name(),
           k -> new ArrayList<>());
-      if (ignore != null && ignore.contains(it.name())) {
-        nodes.add(it);
-      } else {
-        nodes.add(new DataConfigNode(it, ignore));
-      }
+
+     nodes.add(new DataConfigNode(it,  propertySubstitution));
       return Boolean.TRUE;
     });
 
   }
 
+  private SimpleMap<String> wrap(SimpleMap<String> delegate) {
+    return propertySubstitution == null ? delegate :
+        new SimpleMap<>() {
+          @Override
+          public String get(String key) {
+            return PropertiesUtil.substitute(delegate.get(key), propertySubstitution);
+          }
+
+          @Override
+          public void forEachEntry(BiConsumer<String, ? super String> fun) {
+            delegate.forEachEntry((k, v) -> fun.accept(k, PropertiesUtil.substitute(v, propertySubstitution)));
+          }
+
+          @Override
+          public int size() {
+            return delegate.size();
+          }
+        };
+  }
+
   @Override
   public String name() {
     return name;
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/PropertiesUtil.java b/solr/solrj/src/java/org/apache/solr/common/util/PropertiesUtil.java
index 7650a5c..57861c1 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/PropertiesUtil.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/PropertiesUtil.java
@@ -16,23 +16,28 @@
  */
 package org.apache.solr.common.util;
 
-import org.apache.solr.common.SolrException;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import java.util.function.Function;
+
+import org.apache.solr.common.SolrException;
 
 /**
  * Breaking out some utility methods into a separate class as part of SOLR-4196. These utils have nothing to do with
  * the DOM (they came from DomUtils) and it's really confusing to see them in something labeled DOM
  */
 public class PropertiesUtil {
+  public static String substituteProperty(String value, Properties coreProperties) {
+    if(coreProperties == null) return substitute(value, null);
+    return substitute(value, coreProperties::getProperty);
+  }
   /*
   * This method borrowed from Ant's PropertyHelper.replaceProperties:
   *   http://svn.apache.org/repos/asf/ant/core/trunk/src/main/org/apache/tools/ant/PropertyHelper.java
   */
-  public static String substituteProperty(String value, Properties coreProperties) {
+  public static String substitute(String value, Function<String,String> coreProperties) {
     if (value == null || value.indexOf('$') == -1) {
       return value;
     }
@@ -56,7 +61,7 @@ public class PropertiesUtil {
           propertyName = propertyName.substring(0, colon_index);
         }
         if (coreProperties != null) {
-          fragment = coreProperties.getProperty(propertyName);
+          fragment = coreProperties.apply(propertyName);
         }
         if (fragment == null) {
           fragment = System.getProperty(propertyName, defaultValue);