-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATACOUCH-6 - Add simple support for XML config
- Loading branch information
Showing
10 changed files
with
274 additions
and
23 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/org/springframework/data/couchbase/config/BeanNames.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,28 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.couchbase.config; | ||
|
||
/** | ||
* @author Michael Nitschinger | ||
*/ | ||
public class BeanNames { | ||
static final String MAPPING_CONTEXT = "mappingContext"; | ||
static final String COUCHBASE = "couchbase"; | ||
static final String DB_FACTORY = "couchbaseDbFactory"; | ||
static final String DEFAULT_CONVERTER_BEAN_NAME = "mappingConverter"; | ||
static final String COUCHBASE_TEMPLATE = "couchbaseTemplate"; | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/springframework/data/couchbase/config/CouchbaseJmxParser.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,66 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.couchbase.config; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.parsing.BeanComponentDefinition; | ||
import org.springframework.beans.factory.parsing.CompositeComponentDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.xml.BeanDefinitionParser; | ||
import org.springframework.beans.factory.xml.ParserContext; | ||
import org.springframework.data.couchbase.monitor.ClientInfo; | ||
import org.springframework.data.couchbase.monitor.ClusterInfo; | ||
import org.springframework.util.StringUtils; | ||
import org.w3c.dom.Element; | ||
|
||
/** | ||
* Enables Parsing of "<couchbase:jmx />" configurations. | ||
* | ||
* @author Michael Nitschinger | ||
*/ | ||
public class CouchbaseJmxParser implements BeanDefinitionParser { | ||
|
||
public BeanDefinition parse(final Element element, final ParserContext parserContext) { | ||
String name = element.getAttribute("couchbase-ref"); | ||
if (!StringUtils.hasText(name)) { | ||
name = BeanNames.COUCHBASE; | ||
} | ||
registerJmxComponents(name, element, parserContext); | ||
return null; | ||
} | ||
|
||
protected void registerJmxComponents(String refName, Element element, ParserContext parserContext) { | ||
Object eleSource = parserContext.extractSource(element); | ||
CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource); | ||
|
||
createBeanDefEntry(ClientInfo.class, compositeDef, refName, eleSource, parserContext); | ||
createBeanDefEntry(ClusterInfo.class, compositeDef, refName, eleSource, parserContext); | ||
|
||
parserContext.registerComponent(compositeDef); | ||
} | ||
|
||
protected void createBeanDefEntry(Class<?> clazz, CompositeComponentDefinition compositeDef, | ||
String refName, Object eleSource, ParserContext parserContext) { | ||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz); | ||
builder.getRawBeanDefinition().setSource(eleSource); | ||
builder.addConstructorArgReference(refName); | ||
BeanDefinition assertDef = builder.getBeanDefinition(); | ||
String assertName = parserContext.getReaderContext().registerWithGeneratedName(assertDef); | ||
compositeDef.addNestedComponent(new BeanComponentDefinition(assertDef, assertName)); | ||
} | ||
|
||
} |
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
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
55 changes: 55 additions & 0 deletions
55
src/main/java/org/springframework/data/couchbase/config/CouchbaseTemplateParser.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,55 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.couchbase.config; | ||
|
||
import org.springframework.beans.factory.support.AbstractBeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; | ||
import org.springframework.beans.factory.xml.ParserContext; | ||
import org.springframework.data.couchbase.core.CouchbaseTemplate; | ||
import org.springframework.util.StringUtils; | ||
import org.w3c.dom.Element; | ||
|
||
/** | ||
* @author Michael Nitschinger | ||
*/ | ||
public class CouchbaseTemplateParser extends AbstractSingleBeanDefinitionParser { | ||
|
||
protected String resolveId(final Element element, final AbstractBeanDefinition definition, | ||
final ParserContext parserContext) { | ||
String id = super.resolveId(element, definition, parserContext); | ||
return StringUtils.hasText(id) ? id : BeanNames.COUCHBASE_TEMPLATE; | ||
} | ||
|
||
@Override | ||
protected Class getBeanClass(final Element element) { | ||
return CouchbaseTemplate.class; | ||
} | ||
|
||
@Override | ||
protected void doParse(final Element element, final BeanDefinitionBuilder bean) { | ||
String converterRef = element.getAttribute("converter-ref"); | ||
String dbRef = element.getAttribute("db-ref"); | ||
|
||
bean.addConstructorArgReference(StringUtils.hasText(dbRef) ? dbRef : BeanNames.COUCHBASE); | ||
|
||
if (StringUtils.hasText(converterRef)) { | ||
bean.addConstructorArgReference(converterRef); | ||
} | ||
} | ||
|
||
} |
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
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
53 changes: 53 additions & 0 deletions
53
...ava/org/springframework/data/couchbase/config/CouchbaseTemplateParserIntegrationTest.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,53 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.couchbase.config; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionReader; | ||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Michael Nitschinger | ||
*/ | ||
public class CouchbaseTemplateParserIntegrationTest { | ||
|
||
DefaultListableBeanFactory factory; | ||
BeanDefinitionReader reader; | ||
|
||
@Before | ||
public void setUp() { | ||
factory = new DefaultListableBeanFactory(); | ||
reader = new XmlBeanDefinitionReader(factory); | ||
} | ||
|
||
@Test | ||
public void readsCouchbaseTemplateAttributesCorrectly() { | ||
reader.loadBeanDefinitions(new ClassPathResource("namespace/couchbase-template-bean.xml")); | ||
|
||
BeanDefinition definition = factory.getBeanDefinition("couchbaseTemplate"); | ||
assertEquals(1, definition.getConstructorArgumentValues().getArgumentCount()); | ||
|
||
factory.getBean("couchbaseTemplate"); | ||
} | ||
|
||
} |
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
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 @@ | ||
<?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:couchbase="http://www.springframework.org/schema/data/couchbase" | ||
xsi:schemaLocation="http://www.springframework.org/schema/data/couchbase http://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | ||
|
||
<couchbase:couchbase/> | ||
|
||
<couchbase:template/> | ||
|
||
</beans> |