Default values for properties in ini files

While this feature is not strictly needed, the patch contains some good code cleanups.  So it will be applied and then the default feature
removed in a subsequent commit.
This commit is contained in:
Greg Wilkins 2015-06-19 15:27:47 +10:00
parent b0a3c7c5ea
commit 13b63c194b
3 changed files with 25 additions and 15 deletions

View File

@ -42,6 +42,9 @@ import org.eclipse.jetty.start.Props.Prop;
*/
public final class Props implements Iterable<Prop>
{
private static final Pattern __propertyPattern = Pattern.compile("(?<=[^$]|^)\\$\\{([^:}]*)(:=([^}]*))?\\}");
public static class Prop
{
public String key;
@ -200,8 +203,7 @@ public final class Props implements Iterable<Prop>
return str;
}
Pattern pat = Pattern.compile("(?<=[^$]|^)(\\$\\{[^}]*\\})");
Matcher mat = pat.matcher(str);
Matcher mat = __propertyPattern.matcher(str);
StringBuilder expanded = new StringBuilder();
int offset = 0;
String property;
@ -209,7 +211,8 @@ public final class Props implements Iterable<Prop>
while (mat.find(offset))
{
property = cleanReference(mat.group(1));
property = mat.group(1);
String dftValue = mat.groupCount()>2?mat.group(3):null;
// Loop detection
if (seenStack.contains(property))
@ -229,13 +232,15 @@ public final class Props implements Iterable<Prop>
seenStack.push(property);
// find property name
expanded.append(str.subSequence(offset,mat.start(1)));
expanded.append(str.subSequence(offset,mat.start()));
// get property value
value = getString(property);
if (value==null)
value=dftValue;
if (value == null)
{
StartLog.trace("Unable to expand: %s",property);
expanded.append(mat.group(1));
expanded.append(mat.group());
}
else
{
@ -244,7 +249,7 @@ public final class Props implements Iterable<Prop>
expanded.append(value);
}
// update offset
offset = mat.end(1);
offset = mat.end();
}
// leftover

View File

@ -97,6 +97,19 @@ public class PropsTest
assertThat(props.expand("id=${unknown}-${wibble}"),is("id=${unknown}-${wibble}"));
}
@Test
public void testSimpleExpandWithDefaults()
{
Props props = new Props();
props.setProperty("name","jetty",FROM_TEST);
props.setProperty("version","9.1",FROM_TEST);
assertThat(props.expand("port=8080"),is("port=8080"));
assertThat(props.expand("jdk=${java.version:=WRONG}"),is("jdk=" + System.getProperty("java.version")));
assertThat(props.expand("id=${name:=WRONG}-${version:=WRONG}"),is("id=jetty-9.1"));
assertThat(props.expand("id=${unknown:=UNKNOWN}-${wibble}"),is("id=UNKNOWN-${wibble}"));
}
@Test
public void testNoExpandDoubleDollar()
{

View File

@ -46,7 +46,6 @@ import java.util.Queue;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import org.eclipse.jetty.util.ArrayQueue;
import org.eclipse.jetty.util.LazyList;
@ -86,7 +85,6 @@ public class XmlConfiguration
{Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Void.class};
private static final Class<?>[] __supportedCollections =
{ArrayList.class, ArrayQueue.class, HashSet.class, Queue.class, List.class, Set.class, Collection.class};
private static final Pattern __propertyPattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
private static final Iterable<ConfigurationProcessorFactory> __factoryLoader = ServiceLoader.load(ConfigurationProcessorFactory.class);
private static final XmlParser __parser = initParser();
private static XmlParser initParser()
@ -1305,17 +1303,11 @@ public class XmlConfiguration
return StringUtil.valueOf(get(elementName,false));
}
public Object get(String elementName) throws Exception
{
return get(elementName,false);
}
public String getString(String elementName, boolean manditory) throws Exception
{
return StringUtil.valueOf(get(elementName,manditory));
}
public Object get(String elementName, boolean manditory) throws Exception
{
String attrName=StringUtil.asciiToLowerCase(elementName);
@ -1493,7 +1485,7 @@ public class XmlConfiguration
{
if (!args[i].toLowerCase(Locale.ENGLISH).endsWith(".properties") && (args[i].indexOf('=')<0))
{
XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURL());
XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURI().toURL());
if (last != null)
configuration.getIdMap().putAll(last.getIdMap());
if (properties.size() > 0)