Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
environment-specific beans and greenhouse namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Donald committed Aug 26, 2010
1 parent 78db3dd commit de9bc13
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 39 deletions.
63 changes: 28 additions & 35 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -408,20 +407,43 @@
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.0-beta-1</version>
</plugin>
</plugins>

</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>


<pluginRepositories>
<!-- For testing against latest Spring snapshots -->
<pluginRepository>
<id>org.springframework.maven.snapshot</id>
<name>Spring Maven Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<!-- For Spring releases -->
<pluginRepository>
<id>org.springframework.maven.release</id>
<name>Spring Maven Release Repository</name>
<url>http://maven.springframework.org/release</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
<!-- For developing against latest Spring milestones -->
<pluginRepository>
<id>org.springframework.maven.milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>

<profiles>
<profile>
<id>ghtest</id>

<build>
<plugins>
<plugin>
Expand All @@ -436,11 +458,7 @@
</goals>
<configuration>
<tasks>
<scp file="${project.build.directory}/${project.build.finalName}.${project.packaging}"
todir="${scp.to.dir}"
keyfile="${scp.key.location}"
passphrase=""
trust="true" />
<scp file="${project.build.directory}/${project.build.finalName}.${project.packaging}" todir="${scp.to.dir}" keyfile="${scp.key.location}" passphrase="" trust="true" />
</tasks>
</configuration>
</execution>
Expand All @@ -458,29 +476,4 @@
</profile>
</profiles>


<pluginRepositories>
<!-- For testing against latest Spring snapshots -->
<pluginRepository>
<id>org.springframework.maven.snapshot</id>
<name>Spring Maven Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<!-- For Spring releases -->
<pluginRepository>
<id>org.springframework.maven.release</id>
<name>Spring Maven Release Repository</name>
<url>http://maven.springframework.org/release</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
<!-- For developing against latest Spring milestones -->
<pluginRepository>
<id>org.springframework.maven.milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</project>
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());
}

}
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());
}

}

1 change: 1 addition & 0 deletions src/main/resources/META-INF/spring.handlers
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http\://www.springsource.com/schema/greenhouse=com.springsource.greenhouse.config.GreenhouseNamespaceHandler
1 change: 1 addition & 0 deletions src/main/resources/META-INF/spring.schemas
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
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.
9 changes: 5 additions & 4 deletions src/main/webapp/WEB-INF/spring/root-context.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Root Context: defines shared resources accessible to all other web components -->

Expand Down
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 src/test/java/com/springsource/greenhouse/config/context.xml
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>

0 comments on commit de9bc13

Please sign in to comment.