Skip to content

Commit

Permalink
Merge commit '1f765bf9ae124eb10562b9de85d3dc5dfdc916f1' into jetty-9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Becker committed Sep 6, 2013
2 parents 3d7c132 + 1f765bf commit 3048763
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.eclipse.jetty.xml.ConfigurationProcessorFactory;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.eclipse.jetty.xml.XmlParser;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -59,17 +62,15 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor
{
private static final Logger LOG = Log.getLogger(SpringConfigurationProcessor.class);

private Map<String, Object> _idMap;
private Map<String, String> _propertyMap;
private XmlConfiguration _configuration;
private XmlBeanFactory _beanFactory;
private String _main;

public void init(URL url, XmlParser.Node config, Map<String, Object> idMap, Map<String, String> properties)
public void init(URL url, XmlParser.Node config, XmlConfiguration configuration)
{
try
{
_idMap = idMap;
_propertyMap = properties;
_configuration = configuration;

Resource resource = url != null
? new UrlResource(url)
Expand All @@ -78,7 +79,14 @@ public void init(URL url, XmlParser.Node config, Map<String, Object> idMap, Map<
"<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">" +
config).getBytes("UTF-8"));

_beanFactory = new XmlBeanFactory(resource);
_beanFactory = new XmlBeanFactory(resource){
@Override
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs)
{
_configuration.initializeDefaults(bw.getWrappedInstance());
super.applyPropertyValues(beanName, mbd, bw, pvs);
}
};
}
catch (Exception e)
{
Expand All @@ -105,7 +113,7 @@ public Object configure() throws Exception

private void doConfigure()
{
_beanFactory.registerSingleton("properties", _propertyMap);
_beanFactory.registerSingleton("properties", _configuration.getProperties());

// Look for the main bean;
for (String bean : _beanFactory.getBeanDefinitionNames())
Expand All @@ -122,25 +130,26 @@ private void doConfigure()
_main = _beanFactory.getBeanDefinitionNames()[0];

// Register id beans as singletons
LOG.debug("idMap {}", _idMap);
for (String id : _idMap.keySet())
Map<String, Object> idMap = _configuration.getIdMap();
LOG.debug("idMap {}", idMap);
for (String id : idMap.keySet())
{
LOG.debug("register {}", id);
_beanFactory.registerSingleton(id, _idMap.get(id));
_beanFactory.registerSingleton(id, idMap.get(id));
}

// Apply configuration to existing singletons
for (String id : _idMap.keySet())
for (String id : idMap.keySet())
{
if (_beanFactory.containsBeanDefinition(id))
{
LOG.debug("reconfigure {}", id);
_beanFactory.configureBean(_idMap.get(id), id);
_beanFactory.configureBean(idMap.get(id), id);
}
}

// Extract id's for next time.
for (String id : _beanFactory.getSingletonNames())
_idMap.put(id, _beanFactory.getBean(id));
idMap.put(id, _beanFactory.getBean(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

public class SpringXmlConfigurationTest
{
protected String _configure="org/eclipse/jetty/spring/configure.xml";
Expand All @@ -48,7 +50,7 @@ public void init() throws Exception
if (matcher.matches())
{
String minor = matcher.group(1);
Assume.assumeTrue(Integer.parseInt(minor) > 5);
assumeTrue(Integer.parseInt(minor) > 5);
}
}

Expand All @@ -74,54 +76,71 @@ public void testPassedObject() throws Exception

tc=(TestConfiguration)configuration.configure(tc);

Assert.assertEquals("preconfig", tc.getTestString0());
Assert.assertEquals(42, tc.getTestInt0());
Assert.assertEquals("SetValue", tc.getTestString1());
Assert.assertEquals(1, tc.getTestInt1());
assertEquals("preconfig", tc.getTestString0());
assertEquals(42, tc.getTestInt0());
assertEquals("SetValue", tc.getTestString1());
assertEquals(1, tc.getTestInt1());

Assert.assertEquals("nested", tc.getNested().getTestString0());
Assert.assertEquals("nested", tc.getNested().getTestString1());
Assert.assertEquals("default", tc.getNested().getNested().getTestString0());
Assert.assertEquals("deep", tc.getNested().getNested().getTestString1());
assertEquals("nested", tc.getNested().getTestString0());
assertEquals("nested", tc.getNested().getTestString1());
assertEquals("default", tc.getNested().getNested().getTestString0());
assertEquals("deep", tc.getNested().getNested().getTestString1());

Assert.assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
Assert.assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());
assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());

Assert.assertEquals("xxx", tc.getTestString2());
assertEquals("xxx", tc.getTestString2());
}

@Test
public void testNewObject() throws Exception
{
final String newDefaultValue = "NEW DEFAULT";
TestConfiguration.VALUE=71;

URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource(_configure);
XmlConfiguration configuration = new XmlConfiguration(url);

Map<String,String> properties = new HashMap<>();
final AtomicInteger count = new AtomicInteger(0);
XmlConfiguration configuration = new XmlConfiguration(url)
{
@Override
public void initializeDefaults(Object object)
{
super.initializeDefaults(object);
if (object instanceof TestConfiguration)
{
count.incrementAndGet();
((TestConfiguration)object).setTestString0(newDefaultValue);
((TestConfiguration)object).setTestString1("WILL BE OVERRIDDEN");
}
}
};

Map<String,String> properties = new HashMap<String,String>();
properties.put("test", "xxx");

TestConfiguration nested = new TestConfiguration();
nested.setTestString0("nested");
configuration.getIdMap().put("nested",nested);
configuration.getIdMap().put("nested", nested);

configuration.getProperties().putAll(properties);
TestConfiguration tc = (TestConfiguration)configuration.configure();

Assert.assertEquals("default", tc.getTestString0());
Assert.assertEquals(-1, tc.getTestInt0());
Assert.assertEquals("SetValue", tc.getTestString1());
Assert.assertEquals(1, tc.getTestInt1());
assertEquals(3,count.get());

assertEquals(newDefaultValue, tc.getTestString0());
assertEquals(-1, tc.getTestInt0());
assertEquals("SetValue", tc.getTestString1());
assertEquals(1, tc.getTestInt1());

Assert.assertEquals("nested", tc.getNested().getTestString0());
Assert.assertEquals("nested", tc.getNested().getTestString1());
Assert.assertEquals("default", tc.getNested().getNested().getTestString0());
Assert.assertEquals("deep", tc.getNested().getNested().getTestString1());
assertEquals(newDefaultValue, tc.getNested().getTestString0());
assertEquals("nested", tc.getNested().getTestString1());
assertEquals(newDefaultValue, tc.getNested().getNested().getTestString0());
assertEquals("deep", tc.getNested().getNested().getTestString1());

Assert.assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
Assert.assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());
assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());

Assert.assertEquals("xxx", tc.getTestString2());
assertEquals("xxx", tc.getTestString2());
}

@Test
Expand Down
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
Loading

0 comments on commit 3048763

Please sign in to comment.