aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Becker2013-09-05 11:30:59 (EDT)
committerThomas Becker2013-09-05 11:30:59 (EDT)
commit9e0e173de813c3dd825becb9c3f67739f25e936b (patch)
treeaec44d8a09fba91ff9eb391f69d4286e37f627ef
parente764a0f2e7e7d320a7b3dbd76e750a9353d2416a (diff)
downloadorg.eclipse.jetty.project-9e0e173de813c3dd825becb9c3f67739f25e936b.zip
org.eclipse.jetty.project-9e0e173de813c3dd825becb9c3f67739f25e936b.tar.gz
org.eclipse.jetty.project-9e0e173de813c3dd825becb9c3f67739f25e936b.tar.bz2
415401 Add XmlConfiguration.initializeDefaults that allows to set default values for any XmlConfiguration that may be overridden in the config file
-rw-r--r--jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java59
-rw-r--r--jetty-xml/src/main/java/org/eclipse/jetty/xml/ConfigurationProcessor.java3
-rw-r--r--jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java71
-rw-r--r--jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java21
-rw-r--r--jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java23
-rw-r--r--jetty-xml/src/test/resources/org/eclipse/jetty/xml/configure.xml10
6 files changed, 153 insertions, 34 deletions
diff --git a/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java b/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java
index fa07f7a..1e33328 100644
--- a/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java
+++ b/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java
@@ -27,6 +27,7 @@ import org.eclipse.jetty.deploy.ConfigurationManager;
import org.eclipse.jetty.deploy.util.FileID;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.resource.Resource;
+import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
/** Context directory App Provider.
@@ -37,6 +38,8 @@ import org.eclipse.jetty.xml.XmlConfiguration;
public class ContextProvider extends ScanningAppProvider
{
private ConfigurationManager _configurationManager;
+ private boolean _parentLoaderPriority = false;
+ private String _defaultsDescriptor;
public ContextProvider()
{
@@ -79,7 +82,22 @@ public class ContextProvider extends ScanningAppProvider
if (resource.exists() && FileID.isXmlFile(file))
{
- XmlConfiguration xmlc = new XmlConfiguration(resource.getURL());
+ XmlConfiguration xmlc = new XmlConfiguration(resource.getURL())
+ {
+ @Override
+ public void initializeDefaults(Object context)
+ {
+ super.initializeDefaults(context);
+
+ if (context instanceof WebAppContext)
+ {
+ WebAppContext webapp = (WebAppContext)context;
+ webapp.setParentLoaderPriority(_parentLoaderPriority);
+ if (_defaultsDescriptor!=null)
+ webapp.setDefaultsDescriptor(_defaultsDescriptor);
+ }
+ }
+ };
xmlc.getIdMap().put("Server",getDeploymentManager().getServer());
if (getConfigurationManager() != null)
@@ -89,5 +107,44 @@ public class ContextProvider extends ScanningAppProvider
throw new IllegalStateException("App resouce does not exist "+resource);
}
+
+ /* ------------------------------------------------------------ */
+ /** Get the parentLoaderPriority.
+ * @return the parentLoaderPriority
+ */
+ public boolean isParentLoaderPriority()
+ {
+ return _parentLoaderPriority;
+ }
+
+ /* ------------------------------------------------------------ */
+ /** Set the parentLoaderPriority.
+ * <p>If the context created is a WebAppContext, then set the
+ * default value for {@link WebAppContext#setParentLoaderPriority(boolean)}.
+ * @param parentLoaderPriority the parentLoaderPriority to set
+ */
+ public void setParentLoaderPriority(boolean parentLoaderPriority)
+ {
+ _parentLoaderPriority = parentLoaderPriority;
+ }
+ /* ------------------------------------------------------------ */
+ /** Get the defaultsDescriptor.
+ * @return the defaultsDescriptor
+ */
+ public String getDefaultsDescriptor()
+ {
+ return _defaultsDescriptor;
+ }
+
+ /* ------------------------------------------------------------ */
+ /** Set the defaultsDescriptor.
+ * <p>If the context created is a WebAppContext, then set the
+ * default value for {@link WebAppContext#setDefaultsDescriptor(String)}
+ * @param defaultsDescriptor the defaultsDescriptor to set
+ */
+ public void setDefaultsDescriptor(String defaultsDescriptor)
+ {
+ _defaultsDescriptor = defaultsDescriptor;
+ }
}
diff --git a/jetty-xml/src/main/java/org/eclipse/jetty/xml/ConfigurationProcessor.java b/jetty-xml/src/main/java/org/eclipse/jetty/xml/ConfigurationProcessor.java
index b179da4..c6f9151 100644
--- a/jetty-xml/src/main/java/org/eclipse/jetty/xml/ConfigurationProcessor.java
+++ b/jetty-xml/src/main/java/org/eclipse/jetty/xml/ConfigurationProcessor.java
@@ -19,7 +19,6 @@
package org.eclipse.jetty.xml;
import java.net.URL;
-import java.util.Map;
/**
* A ConfigurationProcessor for non XmlConfiguration format files.
@@ -32,7 +31,7 @@ import java.util.Map;
*/
public interface ConfigurationProcessor
{
- public void init(URL url, XmlParser.Node config, Map<String, Object> idMap, Map<String, String> properties);
+ public void init(URL url, XmlParser.Node root, XmlConfiguration configuration);
public Object configure( Object obj) throws Exception;
public Object configure() throws Exception;
diff --git a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
index 42c234c..a415f8c 100644
--- a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
+++ b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
@@ -227,7 +227,7 @@ public class XmlConfiguration
{
throw new IllegalArgumentException("Unknown XML tag:"+config.getTag());
}
- _processor.init(_url,config,_idMap, _propertyMap);
+ _processor.init(_url,config,this);
}
@@ -295,52 +295,64 @@ public class XmlConfiguration
{
return _processor.configure();
}
+
+ /* ------------------------------------------------------------ */
+ /** Initialize a new Object defaults.
+ * <p>This method must be called by any {@link ConfigurationProcessor} when it
+ * creates a new instance of an object before configuring it, so that a derived
+ * XmlConfiguration class may inject default values.
+ * @param object
+ */
+ public void initializeDefaults(Object object)
+ {
+ }
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
private static class JettyXmlConfiguration implements ConfigurationProcessor
{
- XmlParser.Node _config;
- Map<String, Object> _idMap;
- Map<String, String> _propertyMap;
+ XmlParser.Node _root;
+ XmlConfiguration _configuration;
- public void init(URL url, XmlParser.Node config, Map<String, Object> idMap, Map<String, String> properties)
+ public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
{
- _config=config;
- _idMap=idMap;
- _propertyMap=properties;
+ _root=root;
+ _configuration=configuration;
}
/* ------------------------------------------------------------ */
public Object configure(Object obj) throws Exception
{
// Check the class of the object
- Class<?> oClass = nodeClass(_config);
+ Class<?> oClass = nodeClass(_root);
if (oClass != null && !oClass.isInstance(obj))
{
String loaders = (oClass.getClassLoader()==obj.getClass().getClassLoader())?"":"Object Class and type Class are from different loaders.";
throw new IllegalArgumentException("Object of class '"+obj.getClass().getCanonicalName()+"' is not of type '" + oClass.getCanonicalName()+"'. "+loaders);
}
- configure(obj,_config,0);
+ configure(obj,_root,0);
return obj;
}
/* ------------------------------------------------------------ */
public Object configure() throws Exception
{
- Class<?> oClass = nodeClass(_config);
+ Class<?> oClass = nodeClass(_root);
- String id = _config.getAttribute("id");
- Object obj = id == null?null:_idMap.get(id);
+ String id = _root.getAttribute("id");
+ Object obj = id == null?null:_configuration.getIdMap().get(id);
if (obj == null && oClass != null)
+ {
obj = oClass.newInstance();
+ _configuration.initializeDefaults(obj);
+ }
if (oClass != null && !oClass.isInstance(obj))
throw new ClassCastException(oClass.toString());
- configure(obj,_config,0);
+ configure(obj,_root,0);
return obj;
}
@@ -368,7 +380,7 @@ public class XmlConfiguration
{
String id = cfg.getAttribute("id");
if (id != null)
- _idMap.put(id,obj);
+ _configuration.getIdMap().put(id,obj);
for (; i < cfg.size(); i++)
{
@@ -558,6 +570,7 @@ public class XmlConfiguration
}
Constructor<?> cons = sClass.getConstructor(vClass);
arg[0] = cons.newInstance(arg);
+ _configuration.initializeDefaults(arg[0]);
set.invoke(obj,arg);
return;
}
@@ -675,7 +688,7 @@ public class XmlConfiguration
}
}
if (id != null)
- _idMap.put(id,obj);
+ _configuration.getIdMap().put(id,obj);
return obj;
}
@@ -730,7 +743,7 @@ public class XmlConfiguration
{
Object n= TypeUtil.call(oClass,method,obj,arg);
if (id != null)
- _idMap.put(id,n);
+ _configuration.getIdMap().put(id,n);
configure(n,node,argi);
return n;
}
@@ -792,6 +805,7 @@ public class XmlConfiguration
try
{
n = constructors[c].newInstance(arg);
+ _configuration.initializeDefaults(n);
called = true;
}
catch (IllegalAccessException e)
@@ -809,7 +823,7 @@ public class XmlConfiguration
if (called)
{
if (id != null)
- _idMap.put(id,n);
+ _configuration.getIdMap().put(id,n);
configure(n,node,argi);
return n;
}
@@ -827,7 +841,7 @@ public class XmlConfiguration
private Object refObj(Object obj, XmlParser.Node node) throws Exception
{
String id = node.getAttribute("id");
- obj = _idMap.get(id);
+ obj = _configuration.getIdMap().get(id);
if (obj == null)
throw new IllegalStateException("No object for id=" + id);
configure(obj,node,0);
@@ -870,12 +884,12 @@ public class XmlConfiguration
Object v = value(obj,item);
al = LazyList.add(al,(v == null && aClass.isPrimitive())?0:v);
if (nid != null)
- _idMap.put(nid,v);
+ _configuration.getIdMap().put(nid,v);
}
Object array = LazyList.toArray(al,aClass);
if (id != null)
- _idMap.put(id,array);
+ _configuration.getIdMap().put(id,array);
return array;
}
@@ -889,7 +903,7 @@ public class XmlConfiguration
Map<Object, Object> map = new HashMap<Object, Object>();
if (id != null)
- _idMap.put(id,map);
+ _configuration.getIdMap().put(id,map);
for (Object o : node)
{
@@ -925,9 +939,9 @@ public class XmlConfiguration
map.put(k,v);
if (kid != null)
- _idMap.put(kid,k);
+ _configuration.getIdMap().put(kid,k);
if (vid != null)
- _idMap.put(vid,v);
+ _configuration.getIdMap().put(vid,v);
}
return map;
@@ -947,12 +961,13 @@ public class XmlConfiguration
String name = node.getAttribute("name");
String defaultValue = node.getAttribute("default");
Object prop;
- if (_propertyMap != null && _propertyMap.containsKey(name))
- prop = _propertyMap.get(name);
+ Map<String,String> property_map=_configuration.getProperties();
+ if (property_map != null && property_map.containsKey(name))
+ prop = property_map.get(name);
else
prop = defaultValue;
if (id != null)
- _idMap.put(id,prop);
+ _configuration.getIdMap().put(id,prop);
if (prop != null)
configure(prop,node,0);
return prop;
@@ -975,7 +990,7 @@ public class XmlConfiguration
String ref = node.getAttribute("ref");
if (ref != null)
{
- value = _idMap.get(ref);
+ value = _configuration.getIdMap().get(ref);
}
else
{
diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java
index 6b33d7d..cfa96b1 100644
--- a/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java
+++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java
@@ -34,6 +34,7 @@ public class TestConfiguration extends HashMap<String,Object>
public static int VALUE=77;
public TestConfiguration nested;
+ public String testString="default";
public Object testObject;
public int testInt;
public URL url;
@@ -65,6 +66,25 @@ public class TestConfiguration extends HashMap<String,Object>
propValue=value;
}
+ public TestConfiguration getNested()
+ {
+ return nested;
+ }
+
+ public void setNested(TestConfiguration nested)
+ {
+ this.nested = nested;
+ }
+
+ public String getTestString()
+ {
+ return testString;
+ }
+
+ public void setTestString(String testString)
+ {
+ this.testString = testString;
+ }
public void call()
{
@@ -73,7 +93,6 @@ public class TestConfiguration extends HashMap<String,Object>
public TestConfiguration call(Boolean b)
{
- nested=new TestConfiguration();
nested.put("Arg",b);
return nested;
}
diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java
index 819be68..e01b3f5 100644
--- a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java
+++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java
@@ -21,11 +21,12 @@ package org.eclipse.jetty.xml;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Assert;
import org.junit.Test;
-import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
@@ -127,10 +128,28 @@ public class XmlConfigurationTest
properties.put("whatever", "xxx");
URL url = XmlConfigurationTest.class.getClassLoader().getResource(_configure);
- XmlConfiguration configuration = new XmlConfiguration(url);
+ final AtomicInteger count = new AtomicInteger(0);
+ XmlConfiguration configuration = new XmlConfiguration(url)
+ {
+ @Override
+ public void initializeDefaults(Object object)
+ {
+ if (object instanceof TestConfiguration)
+ {
+ count.incrementAndGet();
+ ((TestConfiguration)object).setNested(null);
+ ((TestConfiguration)object).setTestString("NEW DEFAULT");
+ }
+ }
+ };
configuration.getProperties().putAll(properties);
TestConfiguration tc = (TestConfiguration)configuration.configure();
+ assertEquals(3,count.get());
+ assertEquals("NEW DEFAULT",tc.getTestString());
+ assertEquals("nested",tc.getNested().getTestString());
+ assertEquals("NEW DEFAULT",tc.getNested().getNested().getTestString());
+
assertEquals("Set String","SetValue",tc.testObject);
assertEquals("Set Type",2,tc.testInt);
diff --git a/jetty-xml/src/test/resources/org/eclipse/jetty/xml/configure.xml b/jetty-xml/src/test/resources/org/eclipse/jetty/xml/configure.xml
index 32cda2b..c1bb61e 100644
--- a/jetty-xml/src/test/resources/org/eclipse/jetty/xml/configure.xml
+++ b/jetty-xml/src/test/resources/org/eclipse/jetty/xml/configure.xml
@@ -83,6 +83,16 @@
<Put name="Float" type="Float">2.3</Put>
<Put name="Env"><Env name="HOME"/></Put>
+ <Set name="nested">
+ <New class="org.eclipse.jetty.xml.TestConfiguration">
+ <Set name="testString">nested</Set>
+ <Set name="nested">
+ <New class="org.eclipse.jetty.xml.TestConfiguration">
+ </New>
+ </Set>
+ </New>
+ </Set>
+
<Call name="call">
</Call>