Merge commit '9e0e173de813c3dd825becb9c3f67739f25e936b' into jetty-8

This commit is contained in:
Thomas Becker 2013-09-05 17:34:39 +02:00
commit 28f3c1ac01
6 changed files with 153 additions and 34 deletions

View File

@ -27,6 +27,7 @@ import org.eclipse.jetty.deploy.ConfigurationManager;
import org.eclipse.jetty.deploy.util.FileID; import org.eclipse.jetty.deploy.util.FileID;
import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration; import org.eclipse.jetty.xml.XmlConfiguration;
/** Context directory App Provider. /** Context directory App Provider.
@ -37,6 +38,8 @@ import org.eclipse.jetty.xml.XmlConfiguration;
public class ContextProvider extends ScanningAppProvider public class ContextProvider extends ScanningAppProvider
{ {
private ConfigurationManager _configurationManager; private ConfigurationManager _configurationManager;
private boolean _parentLoaderPriority = false;
private String _defaultsDescriptor;
public ContextProvider() public ContextProvider()
{ {
@ -79,7 +82,22 @@ public class ContextProvider extends ScanningAppProvider
if (resource.exists() && FileID.isXmlFile(file)) 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()); xmlc.getIdMap().put("Server",getDeploymentManager().getServer());
if (getConfigurationManager() != null) if (getConfigurationManager() != null)
@ -89,5 +107,44 @@ public class ContextProvider extends ScanningAppProvider
throw new IllegalStateException("App resouce does not exist "+resource); 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;
}
} }

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.xml; package org.eclipse.jetty.xml;
import java.net.URL; import java.net.URL;
import java.util.Map;
/** /**
* A ConfigurationProcessor for non XmlConfiguration format files. * A ConfigurationProcessor for non XmlConfiguration format files.
@ -32,7 +31,7 @@ import java.util.Map;
*/ */
public interface ConfigurationProcessor 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( Object obj) throws Exception;
public Object configure() throws Exception; public Object configure() throws Exception;

View File

@ -227,7 +227,7 @@ public class XmlConfiguration
{ {
throw new IllegalArgumentException("Unknown XML tag:"+config.getTag()); 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(); 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 private static class JettyXmlConfiguration implements ConfigurationProcessor
{ {
XmlParser.Node _config; XmlParser.Node _root;
Map<String, Object> _idMap; XmlConfiguration _configuration;
Map<String, String> _propertyMap;
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; _root=root;
_idMap=idMap; _configuration=configuration;
_propertyMap=properties;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public Object configure(Object obj) throws Exception public Object configure(Object obj) throws Exception
{ {
// Check the class of the object // Check the class of the object
Class<?> oClass = nodeClass(_config); Class<?> oClass = nodeClass(_root);
if (oClass != null && !oClass.isInstance(obj)) if (oClass != null && !oClass.isInstance(obj))
{ {
String loaders = (oClass.getClassLoader()==obj.getClass().getClassLoader())?"":"Object Class and type Class are from different loaders."; 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); 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; return obj;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public Object configure() throws Exception public Object configure() throws Exception
{ {
Class<?> oClass = nodeClass(_config); Class<?> oClass = nodeClass(_root);
String id = _config.getAttribute("id"); String id = _root.getAttribute("id");
Object obj = id == null?null:_idMap.get(id); Object obj = id == null?null:_configuration.getIdMap().get(id);
if (obj == null && oClass != null) if (obj == null && oClass != null)
{
obj = oClass.newInstance(); obj = oClass.newInstance();
_configuration.initializeDefaults(obj);
}
if (oClass != null && !oClass.isInstance(obj)) if (oClass != null && !oClass.isInstance(obj))
throw new ClassCastException(oClass.toString()); throw new ClassCastException(oClass.toString());
configure(obj,_config,0); configure(obj,_root,0);
return obj; return obj;
} }
@ -368,7 +380,7 @@ public class XmlConfiguration
{ {
String id = cfg.getAttribute("id"); String id = cfg.getAttribute("id");
if (id != null) if (id != null)
_idMap.put(id,obj); _configuration.getIdMap().put(id,obj);
for (; i < cfg.size(); i++) for (; i < cfg.size(); i++)
{ {
@ -558,6 +570,7 @@ public class XmlConfiguration
} }
Constructor<?> cons = sClass.getConstructor(vClass); Constructor<?> cons = sClass.getConstructor(vClass);
arg[0] = cons.newInstance(arg); arg[0] = cons.newInstance(arg);
_configuration.initializeDefaults(arg[0]);
set.invoke(obj,arg); set.invoke(obj,arg);
return; return;
} }
@ -675,7 +688,7 @@ public class XmlConfiguration
} }
} }
if (id != null) if (id != null)
_idMap.put(id,obj); _configuration.getIdMap().put(id,obj);
return obj; return obj;
} }
@ -730,7 +743,7 @@ public class XmlConfiguration
{ {
Object n= TypeUtil.call(oClass,method,obj,arg); Object n= TypeUtil.call(oClass,method,obj,arg);
if (id != null) if (id != null)
_idMap.put(id,n); _configuration.getIdMap().put(id,n);
configure(n,node,argi); configure(n,node,argi);
return n; return n;
} }
@ -792,6 +805,7 @@ public class XmlConfiguration
try try
{ {
n = constructors[c].newInstance(arg); n = constructors[c].newInstance(arg);
_configuration.initializeDefaults(n);
called = true; called = true;
} }
catch (IllegalAccessException e) catch (IllegalAccessException e)
@ -809,7 +823,7 @@ public class XmlConfiguration
if (called) if (called)
{ {
if (id != null) if (id != null)
_idMap.put(id,n); _configuration.getIdMap().put(id,n);
configure(n,node,argi); configure(n,node,argi);
return n; return n;
} }
@ -827,7 +841,7 @@ public class XmlConfiguration
private Object refObj(Object obj, XmlParser.Node node) throws Exception private Object refObj(Object obj, XmlParser.Node node) throws Exception
{ {
String id = node.getAttribute("id"); String id = node.getAttribute("id");
obj = _idMap.get(id); obj = _configuration.getIdMap().get(id);
if (obj == null) if (obj == null)
throw new IllegalStateException("No object for id=" + id); throw new IllegalStateException("No object for id=" + id);
configure(obj,node,0); configure(obj,node,0);
@ -870,12 +884,12 @@ public class XmlConfiguration
Object v = value(obj,item); Object v = value(obj,item);
al = LazyList.add(al,(v == null && aClass.isPrimitive())?0:v); al = LazyList.add(al,(v == null && aClass.isPrimitive())?0:v);
if (nid != null) if (nid != null)
_idMap.put(nid,v); _configuration.getIdMap().put(nid,v);
} }
Object array = LazyList.toArray(al,aClass); Object array = LazyList.toArray(al,aClass);
if (id != null) if (id != null)
_idMap.put(id,array); _configuration.getIdMap().put(id,array);
return array; return array;
} }
@ -889,7 +903,7 @@ public class XmlConfiguration
Map<Object, Object> map = new HashMap<Object, Object>(); Map<Object, Object> map = new HashMap<Object, Object>();
if (id != null) if (id != null)
_idMap.put(id,map); _configuration.getIdMap().put(id,map);
for (Object o : node) for (Object o : node)
{ {
@ -925,9 +939,9 @@ public class XmlConfiguration
map.put(k,v); map.put(k,v);
if (kid != null) if (kid != null)
_idMap.put(kid,k); _configuration.getIdMap().put(kid,k);
if (vid != null) if (vid != null)
_idMap.put(vid,v); _configuration.getIdMap().put(vid,v);
} }
return map; return map;
@ -947,12 +961,13 @@ public class XmlConfiguration
String name = node.getAttribute("name"); String name = node.getAttribute("name");
String defaultValue = node.getAttribute("default"); String defaultValue = node.getAttribute("default");
Object prop; Object prop;
if (_propertyMap != null && _propertyMap.containsKey(name)) Map<String,String> property_map=_configuration.getProperties();
prop = _propertyMap.get(name); if (property_map != null && property_map.containsKey(name))
prop = property_map.get(name);
else else
prop = defaultValue; prop = defaultValue;
if (id != null) if (id != null)
_idMap.put(id,prop); _configuration.getIdMap().put(id,prop);
if (prop != null) if (prop != null)
configure(prop,node,0); configure(prop,node,0);
return prop; return prop;
@ -975,7 +990,7 @@ public class XmlConfiguration
String ref = node.getAttribute("ref"); String ref = node.getAttribute("ref");
if (ref != null) if (ref != null)
{ {
value = _idMap.get(ref); value = _configuration.getIdMap().get(ref);
} }
else else
{ {

View File

@ -34,6 +34,7 @@ public class TestConfiguration extends HashMap<String,Object>
public static int VALUE=77; public static int VALUE=77;
public TestConfiguration nested; public TestConfiguration nested;
public String testString="default";
public Object testObject; public Object testObject;
public int testInt; public int testInt;
public URL url; public URL url;
@ -65,6 +66,25 @@ public class TestConfiguration extends HashMap<String,Object>
propValue=value; 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() public void call()
{ {
@ -73,7 +93,6 @@ public class TestConfiguration extends HashMap<String,Object>
public TestConfiguration call(Boolean b) public TestConfiguration call(Boolean b)
{ {
nested=new TestConfiguration();
nested.put("Arg",b); nested.put("Arg",b);
return nested; return nested;
} }

View File

@ -21,11 +21,12 @@ package org.eclipse.jetty.xml;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; 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.is;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
@ -127,10 +128,28 @@ public class XmlConfigurationTest
properties.put("whatever", "xxx"); properties.put("whatever", "xxx");
URL url = XmlConfigurationTest.class.getClassLoader().getResource(_configure); 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); configuration.getProperties().putAll(properties);
TestConfiguration tc = (TestConfiguration)configuration.configure(); 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 String","SetValue",tc.testObject);
assertEquals("Set Type",2,tc.testInt); assertEquals("Set Type",2,tc.testInt);

View File

@ -83,6 +83,16 @@
<Put name="Float" type="Float">2.3</Put> <Put name="Float" type="Float">2.3</Put>
<Put name="Env"><Env name="HOME"/></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 name="call">
</Call> </Call>