Skip to content

Commit

Permalink
415401 Add XmlConfiguration.initializeDefaults that allows to set def…
Browse files Browse the repository at this point in the history
…ault values for any XmlConfiguration that may be overridden in the config file
  • Loading branch information
Thomas Becker committed Sep 5, 2013
1 parent e764a0f commit 9e0e173
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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.
Expand All @@ -37,6 +38,8 @@
public class ContextProvider extends ScanningAppProvider
{
private ConfigurationManager _configurationManager;
private boolean _parentLoaderPriority = false;
private String _defaultsDescriptor;

public ContextProvider()
{
Expand Down Expand Up @@ -79,7 +82,22 @@ public ContextHandler createContextHandler(App app) throws Exception

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)
Expand All @@ -89,5 +107,44 @@ public ContextHandler createContextHandler(App app) throws Exception

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.eclipse.jetty.xml;

import java.net.URL;
import java.util.Map;

/**
* A ConfigurationProcessor for non XmlConfiguration format files.
Expand All @@ -32,7 +31,7 @@
*/
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;
Expand Down
71 changes: 43 additions & 28 deletions jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ else if (__factoryLoader!=null)
{
throw new IllegalArgumentException("Unknown XML tag:"+config.getTag());
}
_processor.init(_url,config,_idMap, _propertyMap);
_processor.init(_url,config,this);
}


Expand Down Expand Up @@ -295,52 +295,64 @@ public Object configure() throws Exception
{
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;
}

Expand Down Expand Up @@ -368,7 +380,7 @@ public void configure(Object obj, XmlParser.Node cfg, int i) throws Exception
{
String id = cfg.getAttribute("id");
if (id != null)
_idMap.put(id,obj);
_configuration.getIdMap().put(id,obj);

for (; i < cfg.size(); i++)
{
Expand Down Expand Up @@ -558,6 +570,7 @@ private void set(Object obj, XmlParser.Node node) throws Exception
}
Constructor<?> cons = sClass.getConstructor(vClass);
arg[0] = cons.newInstance(arg);
_configuration.initializeDefaults(arg[0]);
set.invoke(obj,arg);
return;
}
Expand Down Expand Up @@ -675,7 +688,7 @@ private Object get(Object obj, XmlParser.Node node) throws Exception
}
}
if (id != null)
_idMap.put(id,obj);
_configuration.getIdMap().put(id,obj);
return obj;
}

Expand Down Expand Up @@ -730,7 +743,7 @@ else if (obj != null)
{
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;
}
Expand Down Expand Up @@ -792,6 +805,7 @@ private Object newObj(Object obj, XmlParser.Node node) throws Exception
try
{
n = constructors[c].newInstance(arg);
_configuration.initializeDefaults(n);
called = true;
}
catch (IllegalAccessException e)
Expand All @@ -809,7 +823,7 @@ private Object newObj(Object obj, XmlParser.Node node) throws Exception
if (called)
{
if (id != null)
_idMap.put(id,n);
_configuration.getIdMap().put(id,n);
configure(n,node,argi);
return n;
}
Expand All @@ -827,7 +841,7 @@ private Object newObj(Object obj, XmlParser.Node node) throws Exception
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);
Expand Down Expand Up @@ -870,12 +884,12 @@ else if ("InetAddress".equals(type))
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;
}

Expand All @@ -889,7 +903,7 @@ private Object newMap(Object obj, XmlParser.Node node) throws Exception

Map<Object, Object> map = new HashMap<Object, Object>();
if (id != null)
_idMap.put(id,map);
_configuration.getIdMap().put(id,map);

for (Object o : node)
{
Expand Down Expand Up @@ -925,9 +939,9 @@ private Object newMap(Object obj, XmlParser.Node node) throws Exception
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;
Expand All @@ -947,12 +961,13 @@ private Object propertyObj(XmlParser.Node node) throws Exception
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;
Expand All @@ -975,7 +990,7 @@ private Object value(Object obj, XmlParser.Node node) throws Exception
String ref = node.getAttribute("ref");
if (ref != null)
{
value = _idMap.get(ref);
value = _configuration.getIdMap().get(ref);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +66,25 @@ public void setPropertyTest(int 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()
{
Expand All @@ -73,7 +93,6 @@ public void call()

public TestConfiguration call(Boolean b)
{
nested=new TestConfiguration();
nested.put("Arg",b);
return nested;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
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;
Expand Down Expand Up @@ -127,10 +128,28 @@ public void testNewObject() throws Exception
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);

Expand Down
Loading

0 comments on commit 9e0e173

Please sign in to comment.