415401 Add initalizeDefaults call to SpringConfigurationProcessor
This commit is contained in:
parent
9e90e1267f
commit
77889089a9
|
@ -30,6 +30,9 @@ import org.eclipse.jetty.xml.ConfigurationProcessor;
|
|||
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;
|
||||
|
@ -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)
|
||||
|
@ -78,7 +79,14 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor
|
|||
"<!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)
|
||||
{
|
||||
|
@ -105,7 +113,7 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor
|
|||
|
||||
private void doConfigure()
|
||||
{
|
||||
_beanFactory.registerSingleton("properties", _propertyMap);
|
||||
_beanFactory.registerSingleton("properties", _configuration.getProperties());
|
||||
|
||||
// Look for the main bean;
|
||||
for (String bean : _beanFactory.getBeanDefinitionNames())
|
||||
|
@ -122,25 +130,26 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor
|
|||
_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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,16 +21,18 @@ package org.eclipse.jetty.spring;
|
|||
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";
|
||||
|
@ -48,7 +50,7 @@ public class SpringXmlConfigurationTest
|
|||
if (matcher.matches())
|
||||
{
|
||||
String minor = matcher.group(1);
|
||||
Assume.assumeTrue(Integer.parseInt(minor) > 5);
|
||||
assumeTrue(Integer.parseInt(minor) > 5);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,54 +76,71 @@ public class SpringXmlConfigurationTest
|
|||
|
||||
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);
|
||||
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<>();
|
||||
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());
|
||||
|
||||
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.getTestString0());
|
||||
assertEquals(-1, tc.getTestInt0());
|
||||
assertEquals("SetValue", tc.getTestString1());
|
||||
assertEquals(1, tc.getTestInt1());
|
||||
|
||||
Assert.assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
|
||||
Assert.assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());
|
||||
assertEquals(newDefaultValue, tc.getNested().getTestString0());
|
||||
assertEquals("nested", tc.getNested().getTestString1());
|
||||
assertEquals(newDefaultValue, tc.getNested().getNested().getTestString0());
|
||||
assertEquals("deep", tc.getNested().getNested().getTestString1());
|
||||
|
||||
Assert.assertEquals("xxx", tc.getTestString2());
|
||||
assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
|
||||
assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());
|
||||
|
||||
assertEquals("xxx", tc.getTestString2());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue