001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.marshall.hocon; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021 022import java.util.*; 023import java.util.function.*; 024import java.util.stream.*; 025 026/** 027 * Resolves ${var} and ${?var} substitutions in the parsed HOCON tree. 028 * 029 * <p> 030 * Resolution algorithm: single-pass depth-first over the tree. Substitutions reference paths in the 031 * root object (e.g., ${database.host}). Optional substitutions (${?var}) are silently removed if 032 * the path does not exist. Required substitutions (${var}) throw if the path is missing. 033 * Circular references are detected and cause an error. 034 * 035 * <h2 class='section'>See Also:</h2><ul> 036 * <li class='link'><a class="doclink" href="https://github.com/lightbend/config/blob/main/HOCON.md">HOCON Specification</a> 037 * </ul> 038 */ 039public class HoconResolver { 040 041 private final HoconValue.HoconObject root; 042 private final Set<String> inProgress = new HashSet<>(); 043 044 /** Maximum total number of characters produced by all concatenations during one resolution. */ 045 private static final long MAX_CONCAT_OUTPUT = 10_000_000L; 046 047 private long concatOutput; 048 049 /** 050 * Constructor. 051 * 052 * @param root The root HOCON object (after parsing, before resolution). 053 * <br>Must not be <jk>null</jk> ({@link #resolve()} dereferences it). 054 */ 055 public HoconResolver(HoconValue.HoconObject root) { 056 this.root = root; 057 } 058 059 /** 060 * Resolves all substitutions in-place in the tree. 061 * 062 * @throws RuntimeException If a required substitution path is missing or circular. 063 */ 064 public void resolve() { 065 resolveObject(root); 066 } 067 068 private void resolveObject(HoconValue.HoconObject obj) { 069 var it = obj.getMembers().entrySet().iterator(); 070 while (it.hasNext()) { 071 var e = it.next(); 072 var resolved = resolveValue(e.getValue()); 073 if (resolved == null) 074 it.remove(); 075 else 076 e.setValue(resolved); 077 } 078 } 079 080 private HoconValue resolveValue(HoconValue val) { 081 if (val == null) 082 return null; 083 return switch (val.getType()) { 084 case STRING, NUMBER, BOOLEAN, NULL -> val; 085 case OBJECT -> { 086 resolveObject((HoconValue.HoconObject) val); 087 yield val; 088 } 089 case ARRAY -> { 090 var arr = (HoconValue.HoconArray) val; 091 var resolved = arr.getElements().stream().map(this::resolveValue).filter(Objects::nonNull).collect(Collectors.toCollection(ArrayList::new)); 092 arr.setElements(resolved); 093 yield val; 094 } 095 case SUBSTITUTION -> resolveSubstitution((HoconValue.HoconSubstitution) val, false); 096 case OPT_SUBSTITUTION -> resolveSubstitution((HoconValue.HoconSubstitution) val, true); 097 case CONCAT -> resolveConcat((HoconValue.HoconConcat) val); 098 default -> val; 099 }; 100 } 101 102 private HoconValue resolveSubstitution(HoconValue.HoconSubstitution sub, boolean optional) { 103 var path = sub.getPath(); 104 if (inProgress.contains(path)) 105 throw rex("Circular substitution: %s", path); 106 inProgress.add(path); 107 try { 108 var found = lookup(path); 109 if (found == null) { 110 if (optional) 111 return null; 112 throw rex("Substitution '$%s' not found", path); 113 } 114 var resolved = resolveValue(found); 115 if (resolved != null) 116 return resolved; 117 return optional ? null : found; 118 } finally { 119 inProgress.remove(path); 120 } 121 } 122 123 private HoconValue resolveConcat(HoconValue.HoconConcat concat) { 124 var sb = new StringBuilder(); 125 for (var p : concat.getParts()) { 126 var resolved = resolveValue(p); 127 if (resolved != null) 128 sb.append(resolveConcatPart(resolved)); 129 } 130 return concatResult(sb); 131 } 132 133 private HoconValue concatResult(StringBuilder sb) { 134 concatOutput += sb.length(); 135 if (concatOutput > MAX_CONCAT_OUTPUT) 136 throw rex("Resolved output is too large."); 137 return new HoconValue.HoconString(sb.toString()); 138 } 139 140 private static String resolveConcatPart(HoconValue val) { 141 if (val == null) 142 return ""; 143 return switch (val.getType()) { 144 case STRING -> ((HoconValue.HoconString) val).getValue(); 145 case NUMBER -> ((HoconValue.HoconNumber) val).getValue().toString(); 146 case BOOLEAN -> String.valueOf(((HoconValue.HoconBoolean) val).getValue()); 147 case NULL -> "null"; 148 default -> val.toString(); 149 }; 150 } 151 152 /** 153 * Looks up a dotted path in the root object. 154 * 155 * @param path The path (e.g., "database.host"). 156 * <br>Can be <jk>null</jk> (returns <jk>null</jk>). 157 * @return The value at that path, or <jk>null</jk> if the path is <jk>null</jk>, blank, or does not resolve to an 158 * existing value. 159 */ 160 public HoconValue lookup(String path) { 161 if (isEmpty(path)) 162 return null; 163 var parts = path.split("\\.", -1); 164 return root.getPath(parts); 165 } 166 167 /** 168 * Resolves a concatenation value with a custom lookup (for self-referential substitution). 169 * When the concat contains ${path}, the lookup is used instead of the root object. 170 * This allows <code>path = ${path}"/bin"</code> to append to the previous value. 171 * 172 * @param concat The concatenation to resolve. 173 * <br>Must not be <jk>null</jk>. 174 * @param lookup Custom lookup; for path X returns the value to use (e.g. previous value for self-ref). 175 * <br>Must not be <jk>null</jk>. 176 * @return The resolved string value. 177 */ 178 public HoconValue resolveConcatWithLookup(HoconValue.HoconConcat concat, Function<String,HoconValue> lookup) { 179 var sb = new StringBuilder(); 180 for (var p : concat.getParts()) { 181 var resolved = resolveValueWithLookup(p, lookup); 182 if (resolved != null) 183 sb.append(resolveConcatPart(resolved)); 184 } 185 return concatResult(sb); 186 } 187 188 private HoconValue resolveValueWithLookup(HoconValue val, Function<String,HoconValue> lookup) { 189 if (val == null) 190 return null; 191 return switch (val.getType()) { 192 case STRING, NUMBER, BOOLEAN, NULL -> val; 193 case OBJECT -> { 194 resolveObject((HoconValue.HoconObject) val); 195 yield val; 196 } 197 case ARRAY -> { 198 var arr = (HoconValue.HoconArray) val; 199 var resolved = arr.getElements().stream().map(el -> resolveValueWithLookup(el, lookup)).collect(Collectors.toCollection(ArrayList::new)); 200 arr.setElements(resolved); 201 yield val; 202 } 203 case SUBSTITUTION -> resolveSubstitutionWithLookup((HoconValue.HoconSubstitution) val, false, lookup); 204 case OPT_SUBSTITUTION -> resolveSubstitutionWithLookup((HoconValue.HoconSubstitution) val, true, lookup); 205 case CONCAT -> resolveConcatWithLookup((HoconValue.HoconConcat) val, lookup); 206 default -> val; 207 }; 208 } 209 210 private HoconValue resolveSubstitutionWithLookup(HoconValue.HoconSubstitution sub, boolean optional, 211 Function<String,HoconValue> lookup) { 212 var path = sub.getPath(); 213 if (inProgress.contains(path)) 214 throw rex("Circular substitution: %s", path); 215 inProgress.add(path); 216 try { 217 var found = lookup.apply(path); 218 if (found == null) { 219 if (optional) 220 return null; 221 throw rex("Substitution '$%s' not found", path); 222 } 223 var resolved = resolveValueWithLookup(found, lookup); 224 if (resolved != null) 225 return resolved; 226 return optional ? null : found; 227 } finally { 228 inProgress.remove(path); 229 } 230 } 231 232}