mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-01 11:29:29 +00:00
464606 - Support property expansion in "default" attribute of Property.
This commit is contained in:
parent
55e74c6867
commit
337e0bd8c9
@ -46,6 +46,8 @@ import java.util.Queue;
|
|||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.ArrayQueue;
|
import org.eclipse.jetty.util.ArrayQueue;
|
||||||
import org.eclipse.jetty.util.LazyList;
|
import org.eclipse.jetty.util.LazyList;
|
||||||
@ -79,16 +81,13 @@ import org.xml.sax.SAXException;
|
|||||||
public class XmlConfiguration
|
public class XmlConfiguration
|
||||||
{
|
{
|
||||||
private static final Logger LOG = Log.getLogger(XmlConfiguration.class);
|
private static final Logger LOG = Log.getLogger(XmlConfiguration.class);
|
||||||
|
|
||||||
private static final Class<?>[] __primitives =
|
private static final Class<?>[] __primitives =
|
||||||
{Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE};
|
{Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE};
|
||||||
|
|
||||||
private static final Class<?>[] __boxedPrimitives =
|
private static final Class<?>[] __boxedPrimitives =
|
||||||
{Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Void.class};
|
{Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Void.class};
|
||||||
|
|
||||||
private static final Class<?>[] __supportedCollections =
|
private static final Class<?>[] __supportedCollections =
|
||||||
{ArrayList.class, ArrayQueue.class, HashSet.class, Queue.class, List.class, Set.class, Collection.class,};
|
{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 Iterable<ConfigurationProcessorFactory> __factoryLoader = ServiceLoader.load(ConfigurationProcessorFactory.class);
|
||||||
private static final XmlParser __parser = initParser();
|
private static final XmlParser __parser = initParser();
|
||||||
private static XmlParser initParser()
|
private static XmlParser initParser()
|
||||||
@ -949,9 +948,23 @@ public class XmlConfiguration
|
|||||||
String defaultValue = node.getAttribute("default");
|
String defaultValue = node.getAttribute("default");
|
||||||
|
|
||||||
Object value = null;
|
Object value = null;
|
||||||
boolean present = false;
|
|
||||||
Map<String,String> properties = _configuration.getProperties();
|
Map<String,String> properties = _configuration.getProperties();
|
||||||
if (properties != null && nameAttr != null)
|
if (properties != null && nameAttr != null)
|
||||||
|
value = resolve(properties, nameAttr);
|
||||||
|
|
||||||
|
if (value == null && defaultValue != null)
|
||||||
|
value = interpolate(properties, defaultValue);
|
||||||
|
|
||||||
|
if (idAttr != null)
|
||||||
|
_configuration.getIdMap().put(idAttr, value);
|
||||||
|
|
||||||
|
if (value != null)
|
||||||
|
configure(value, node, 0);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolve(Map<String, String> properties, String nameAttr)
|
||||||
{
|
{
|
||||||
String preferredName = null;
|
String preferredName = null;
|
||||||
String[] names = nameAttr.split(",");
|
String[] names = nameAttr.split(",");
|
||||||
@ -963,27 +976,44 @@ public class XmlConfiguration
|
|||||||
if (preferredName == null)
|
if (preferredName == null)
|
||||||
preferredName = name;
|
preferredName = name;
|
||||||
|
|
||||||
if (properties.containsKey(name))
|
String value = properties.get(name);
|
||||||
|
if (value != null)
|
||||||
{
|
{
|
||||||
if (!name.equals(preferredName))
|
if (!name.equals(preferredName))
|
||||||
LOG.warn("Property '{}' is deprecated, use '{}' instead", name, preferredName);
|
LOG.warn("Property '{}' is deprecated, use '{}' instead", name, preferredName);
|
||||||
present = true;
|
|
||||||
value = properties.get(name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!present)
|
|
||||||
value = defaultValue;
|
|
||||||
|
|
||||||
if (idAttr != null)
|
|
||||||
_configuration.getIdMap().put(idAttr, value);
|
|
||||||
|
|
||||||
if (value != null)
|
|
||||||
configure(value, node, 0);
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String interpolate(Map<String, String> properties, String text)
|
||||||
|
{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
Matcher matcher = __propertyPattern.matcher(text);
|
||||||
|
int start = 0;
|
||||||
|
while (matcher.find(start))
|
||||||
|
{
|
||||||
|
int match = matcher.start();
|
||||||
|
result.append(text.substring(start, match));
|
||||||
|
String name = matcher.group(1);
|
||||||
|
String dftValue = null;
|
||||||
|
int bar = name.indexOf('|');
|
||||||
|
if (bar > 0)
|
||||||
|
{
|
||||||
|
dftValue = name.substring(bar + 1).trim();
|
||||||
|
name = name.substring(0, bar).trim();
|
||||||
|
}
|
||||||
|
String value = resolve(properties, name);
|
||||||
|
if (value == null)
|
||||||
|
value = dftValue;
|
||||||
|
result.append(value);
|
||||||
|
start = matcher.end();
|
||||||
|
}
|
||||||
|
result.append(text.substring(start, text.length()));
|
||||||
|
String r = result.toString();
|
||||||
|
return r.isEmpty() ? null : r;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the value of an element. If no value type is specified, then white space is trimmed out of the value. If it contains multiple value elements they
|
* Get the value of an element. If no value type is specified, then white space is trimmed out of the value. If it contains multiple value elements they
|
||||||
|
@ -18,13 +18,6 @@
|
|||||||
|
|
||||||
package org.eclipse.jetty.xml;
|
package org.eclipse.jetty.xml;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.hamcrest.CoreMatchers.not;
|
|
||||||
import static org.hamcrest.CoreMatchers.nullValue;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -36,6 +29,13 @@ import org.junit.Assert;
|
|||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.CoreMatchers.not;
|
||||||
|
import static org.hamcrest.CoreMatchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class XmlConfigurationTest
|
public class XmlConfigurationTest
|
||||||
{
|
{
|
||||||
protected String _configure="org/eclipse/jetty/xml/configure.xml";
|
protected String _configure="org/eclipse/jetty/xml/configure.xml";
|
||||||
@ -820,4 +820,32 @@ public class XmlConfigurationTest
|
|||||||
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
|
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
|
||||||
assertEquals(value, config.getFirst());
|
assertEquals(value, config.getFirst());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPropertyNotFoundWithPropertyInDefaultValue() throws Exception
|
||||||
|
{
|
||||||
|
String name = "bar";
|
||||||
|
String value = "bar";
|
||||||
|
String defaultValue = "_${bar}_${bar}_";
|
||||||
|
String expectedValue = "_" + value + "_" + value + "_";
|
||||||
|
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
|
||||||
|
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
|
||||||
|
" <Set name=\"first\"><Property name=\"not_found\" default=\"" + defaultValue + "\"/></Set> " +
|
||||||
|
"</Configure>");
|
||||||
|
xmlConfiguration.getProperties().put(name, value);
|
||||||
|
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
|
||||||
|
assertEquals(expectedValue, config.getFirst());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPropertyNotFoundWithPropertyInDefaultValueNotFoundWithDefault() throws Exception
|
||||||
|
{
|
||||||
|
String value = "bar";
|
||||||
|
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
|
||||||
|
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
|
||||||
|
" <Set name=\"first\"><Property name=\"not_found\" default=\"${not_found|" + value + "}\"/></Set> " +
|
||||||
|
"</Configure>");
|
||||||
|
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
|
||||||
|
assertEquals(value, config.getFirst());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user