This repository has been archived by the owner on Apr 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 941
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
environment-specific beans and greenhouse namespace
- Loading branch information
Keith Donald
committed
Aug 26, 2010
1 parent
78db3dd
commit de9bc13
Showing
10 changed files
with
205 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/springsource/greenhouse/config/EnvironmentBeanDefinitionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.springsource.greenhouse.config; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.BeanDefinitionHolder; | ||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; | ||
import org.springframework.beans.factory.xml.BeanDefinitionParser; | ||
import org.springframework.beans.factory.xml.ParserContext; | ||
import org.springframework.util.xml.DomUtils; | ||
import org.w3c.dom.Element; | ||
|
||
class EnvironmentBeanDefinitionParser implements BeanDefinitionParser { | ||
|
||
public BeanDefinition parse(Element element, ParserContext parserContext) { | ||
String environment = getEnvironment(parserContext); | ||
String beanName = element.getAttribute("id"); | ||
List<Element> whenElements = DomUtils.getChildElementsByTagName(element, "when"); | ||
for (Element whenElement : whenElements) { | ||
String value = whenElement.getAttribute("environment"); | ||
if (value.equals(environment)) { | ||
parseAndRegisterBeanDefinition(beanName, whenElement, parserContext); | ||
return null; | ||
} | ||
} | ||
Element otherwiseElement = DomUtils.getChildElementByTagName(element, "otherwise"); | ||
if (otherwiseElement == null) { | ||
Object source = parserContext.extractSource(element); | ||
parserContext.getReaderContext().error("No <bean/> matched in the current environment and no <otherwise/> <bean/> is defined", source); | ||
} | ||
parseAndRegisterBeanDefinition(beanName, otherwiseElement, parserContext); | ||
return null; | ||
} | ||
|
||
private String getEnvironment(ParserContext context) { | ||
return System.getProperty("environment"); | ||
} | ||
|
||
private void parseAndRegisterBeanDefinition(String beanName, Element parentElement, ParserContext parserContext) { | ||
Element beanElement = DomUtils.getChildElementByTagName(parentElement, "bean"); | ||
BeanDefinition beanDef = parserContext.getDelegate().parseBeanDefinitionElement(beanElement, beanName, null); | ||
BeanDefinitionHolder beanDefHolder = new BeanDefinitionHolder(beanDef, beanName); | ||
parserContext.getDelegate().decorateBeanDefinitionIfRequired(beanElement, beanDefHolder); | ||
BeanDefinitionReaderUtils.registerBeanDefinition(beanDefHolder, parserContext.getRegistry()); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/springsource/greenhouse/config/GreenhouseNamespaceHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.springsource.greenhouse.config; | ||
|
||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport; | ||
|
||
public class GreenhouseNamespaceHandler extends NamespaceHandlerSupport { | ||
|
||
public void init() { | ||
registerBeanDefinitionParser("environment-bean", new EnvironmentBeanDefinitionParser()); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http\://www.springsource.com/schema/greenhouse=com.springsource.greenhouse.config.GreenhouseNamespaceHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http\://www.springsource.com/schema/greenhouse/greenhouse-1.0.xsd=com/springsource/greenhouse/config/greenhouse-1.0.xsd |
70 changes: 70 additions & 0 deletions
70
src/main/resources/com/springsource/greenhouse/config/greenhouse-1.0.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xsd:schema xmlns="http://www.springsource.com/schema/greenhouse" | ||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" | ||
xmlns:tool="http://www.springframework.org/schema/tool" | ||
targetNamespace="http://www.springsource.com/schema/greenhouse" | ||
elementFormDefault="qualified" attributeFormDefault="unqualified"> | ||
|
||
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" /> | ||
<xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.0.xsd" /> | ||
|
||
<xsd:element name="environment-bean"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
<xsd:complexType> | ||
<xsd:complexContent> | ||
<xsd:extension base="beans:identifiedType"> | ||
<xsd:sequence> | ||
<xsd:element name="when"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
<xsd:complexType> | ||
<xsd:choice> | ||
<xsd:element ref="beans:bean"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
</xsd:element> | ||
</xsd:choice> | ||
<xsd:attribute name="environment" type="xsd:string" use="required"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
</xsd:attribute> | ||
</xsd:complexType> | ||
</xsd:element> | ||
<xsd:element name="otherwise"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
<xsd:complexType> | ||
<xsd:choice> | ||
<xsd:element ref="beans:bean"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
</xsd:element> | ||
</xsd:choice> | ||
</xsd:complexType> | ||
</xsd:element> | ||
</xsd:sequence> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
</xsd:element> | ||
|
||
</xsd:schema> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/java/com/springsource/greenhouse/config/EnvironmentBeanTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.springsource.greenhouse.config; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
import com.springsource.greenhouse.account.PasswordEncoder; | ||
import com.springsource.greenhouse.account.StandardPasswordEncoder; | ||
|
||
public class EnvironmentBeanTests { | ||
|
||
private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/springsource/greenhouse/config/context.xml"); | ||
|
||
@Test | ||
public void testOk() { | ||
PasswordEncoder encoder = context.getBean("passwordEncoder", PasswordEncoder.class); | ||
assertTrue(encoder instanceof StandardPasswordEncoder); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/springsource/greenhouse/config/context.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:greenhouse="http://www.springsource.com/schema/greenhouse" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | ||
http://www.springsource.com/schema/greenhouse http://www.springsource.com/schema/greenhouse/greenhouse-1.0.xsd"> | ||
|
||
<greenhouse:environment-bean id="passwordEncoder"> | ||
<greenhouse:when environment="dev"> | ||
<bean class="com.springsource.greenhouse.account.NoOpPasswordEncoder" /> | ||
</greenhouse:when> | ||
<greenhouse:otherwise> | ||
<bean class="com.springsource.greenhouse.account.StandardPasswordEncoder"> | ||
<constructor-arg value="MD5" /> | ||
<constructor-arg value="test" /> | ||
</bean> | ||
</greenhouse:otherwise> | ||
</greenhouse:environment-bean> | ||
|
||
</beans> |