ConfigurationOverrides { private void updateConfig(ResourceLoader loader, String overridesResource, String configFilePath, Element configRoot) throws JDOMException, IOException { Element overrides = loader.loadXmlResource(overridesResource); if (overrides == null) { return; } Properties properties = loadProperties(overrides); @SuppressWarnings("unchecked") List<Element> files = overrides.getChildren(FILE_NODE_NAME); for (Element file : files) { String expectedfileName = file.getAttributeValue(FILE_NAME_ATT_NAME); if (Pattern.matches(expectedfileName, configFilePath.replace(File.separator, "/"))) { Log.info(Log.JEEVES, "Overrides being applied to configuration file: " + expectedfileName); @SuppressWarnings("unchecked") List<Element> elements = file.getChildren(); for (Element element : elements) { switch (Updates.valueOf(element.getName().toUpperCase())) { case ADDXML: addXml(properties, element, configRoot); break; case REMOVEXML: removeXml(properties, element, configRoot); break; case REPLACEXML: replaceXml(properties, element, configRoot); break; case REPLACEATT: replaceAtts(properties, element, configRoot); break; case REPLACETEXT: replaceText(properties, element, configRoot); break; default: throw new IllegalArgumentException(element.getName() + " is not a recognized update tag"); } } } } } ConfigurationOverrides(final String overrides); void updateLoggingAsAccordingToOverrides(ServletContext context, Path appPath); void updateWithOverrides(String configFile, ServletContext context, Path appPath, Element configElement); Element loadXmlFileAndUpdate(String servletRelativePath, ServletContext context); List<String> loadTextFileAndUpdate(String configFilePath, ServletContext context, Path appPath, BufferedReader reader); @SuppressWarnings("unchecked") void importSpringConfigurations(XmlBeanDefinitionReader reader, ConfigurableBeanFactory beanFactory, ServletContext servletContext, Path appPath); void postProcessSpringBeanFactory(ConfigurableListableBeanFactory beanFactory, ServletContext servletContext,
                                             Path appPath); void onSpringApplicationContextFinishedRefresh(ConfigurableListableBeanFactory beanFactory, ServletContext servletContext,
                                                          Path appPath); static final String OVERRIDES_KEY; static final ConfigurationOverrides DEFAULT; }
@Test public void updateConfig() throws JDOMException, IOException { Element config = Xml.loadFile(classLoader.getResource("test-config.xml")); Element config2 = (Element) Xml.loadFile(classLoader.getResource("test-config.xml")).clone(); ConfigurationOverrides.DEFAULT.updateWithOverrides("config.xml", null, appPath, config); ConfigurationOverrides.DEFAULT.updateWithOverrides("config2.xml", null, appPath, config2); assertLang("fre", config); assertLang("ger", config2); assertEquals("xml/europeanCountries.xml", Xml.selectString(config, "default/gui/xml[@name = 'countries']/@file")); assertEquals("xml/other.xml", Xml.selectString(config2, "default/gui/xml[@name = 'countries']/@file")); assertTrue(Xml.selectNodes(config, "default/gui/@removeAtt").isEmpty()); assertEquals(1, Xml.selectNodes(config, "default/gui/@newAtt").size()); assertEquals("newValue", Xml.selectString(config, "default/gui/@newAtt")); assertEquals(1, Xml.selectElement(config, "resources").getChildren().size()); assertEquals(1, Xml.selectNodes(config, "resources/resource/config/url").size()); assertEquals("jdbc:oracle:thin:@localhost:1521:fs", Xml.selectElement(config, "resources/resource/config/url").getTextTrim()); assertTrue(Xml.selectNodes(config, "* assertTrue(Xml.selectNodes(config, "* assertEquals(1, Xml.selectNodes(config, "newNode").size()); assertEquals(1, Xml.selectNodes(config, "default/gui").size()); assertEquals(1, Xml.selectNodes(config, "default/gui/text()").size()); assertEquals("ExtraText", Xml.selectString(config, "default/gui/text()")); }