11 changed files with 207 additions and 11 deletions
@ -0,0 +1,64 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010 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.document.mongodb.config; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.BeanDefinitionStoreException; |
||||||
|
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.document.mongodb.MongoFactoryBean; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
import org.w3c.dom.Element; |
||||||
|
|
||||||
|
/** |
||||||
|
* Parser for <mongo;gt; definitions. |
||||||
|
* |
||||||
|
* @author Mark Pollack |
||||||
|
*/ |
||||||
|
public class MongoParser extends AbstractSingleBeanDefinitionParser { |
||||||
|
|
||||||
|
protected Class<?> getBeanClass(Element element) { |
||||||
|
return MongoFactoryBean.class; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doParse(Element element, BeanDefinitionBuilder builder) { |
||||||
|
super.doParse(element, builder); |
||||||
|
|
||||||
|
setPropertyValue(element, builder, "port", "port"); |
||||||
|
setPropertyValue(element, builder, "host", "host"); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) |
||||||
|
throws BeanDefinitionStoreException { |
||||||
|
String name = super.resolveId(element, definition, parserContext); |
||||||
|
if (!StringUtils.hasText(name)) { |
||||||
|
name = "mongo"; |
||||||
|
} |
||||||
|
return name; |
||||||
|
} |
||||||
|
|
||||||
|
private void setPropertyValue(Element element, BeanDefinitionBuilder builder, String attrName, String propertyName) { |
||||||
|
String attr = element.getAttribute(attrName); |
||||||
|
if (StringUtils.hasText(attr)) { |
||||||
|
builder.addPropertyValue(propertyName, attr); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -1,7 +1,7 @@ |
|||||||
package org.springframework.data.document.mongodb.repository.config; |
package org.springframework.data.document.mongodb.config; |
||||||
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
||||||
import org.springframework.data.document.mongodb.repository.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration; |
import org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration; |
||||||
import org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser; |
import org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser; |
||||||
import org.w3c.dom.Element; |
import org.w3c.dom.Element; |
||||||
|
|
||||||
@ -1,4 +1,4 @@ |
|||||||
package org.springframework.data.document.mongodb.repository.config; |
package org.springframework.data.document.mongodb.config; |
||||||
|
|
||||||
import org.springframework.data.document.mongodb.repository.MongoRepository; |
import org.springframework.data.document.mongodb.repository.MongoRepository; |
||||||
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean; |
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean; |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
/** |
||||||
|
* Spring XML namespace configuration for Mongo DB specific repositories. |
||||||
|
*/ |
||||||
|
package org.springframework.data.document.mongodb.config; |
||||||
@ -1,4 +0,0 @@ |
|||||||
/** |
|
||||||
* Namespace configuration for Mongo DB specific repositories. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.repository.config; |
|
||||||
@ -1 +1 @@ |
|||||||
http\://www.springframework.org/schema/data/mongo=org.springframework.data.document.mongodb.repository.config.MongoRepositoryNamespaceHandler |
http\://www.springframework.org/schema/data/mongo=org.springframework.data.document.mongodb.config.MongoRepositoryNamespaceHandler |
||||||
|
|||||||
@ -1,2 +1,2 @@ |
|||||||
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd |
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/springframework/data/document/mongodb/config/spring-mongo-1.0.xsd |
||||||
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd |
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/document/mongodb/config/spring-mongo-1.0.xsd |
||||||
|
|||||||
@ -0,0 +1,79 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010 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.document.mongodb.config; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertNull; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
import java.lang.reflect.Field; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.context.ApplicationContext; |
||||||
|
import org.springframework.data.document.mongodb.MongoFactoryBean; |
||||||
|
import org.springframework.test.context.ContextConfiguration; |
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@ContextConfiguration |
||||||
|
public class MongoNamespaceTests { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private ApplicationContext ctx; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void testMongoSingleton() throws Exception { |
||||||
|
assertTrue(ctx.containsBean("mongo")); |
||||||
|
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&mongo"); |
||||||
|
assertNull(readField("host", mfb)); |
||||||
|
assertNull(readField("port", mfb)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testMongoSingletonWithAttributes() throws Exception { |
||||||
|
assertTrue(ctx.containsBean("defaultMongo")); |
||||||
|
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&defaultMongo"); |
||||||
|
String host = readField("host", mfb); |
||||||
|
Integer port = readField("port", mfb); |
||||||
|
assertEquals("localhost", host); |
||||||
|
assertEquals(new Integer(27017), port); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static <T> T readField(String name, Object target) throws Exception { |
||||||
|
Field field = null; |
||||||
|
Class<?> clazz = target.getClass(); |
||||||
|
do { |
||||||
|
try { |
||||||
|
field = clazz.getDeclaredField(name); |
||||||
|
} catch (Exception ex) { |
||||||
|
} |
||||||
|
|
||||||
|
clazz = clazz.getSuperclass(); |
||||||
|
} while (field == null && !clazz.equals(Object.class)); |
||||||
|
|
||||||
|
if (field == null) |
||||||
|
throw new IllegalArgumentException("Cannot find field '" + name + "' in the class hierarchy of " |
||||||
|
+ target.getClass()); |
||||||
|
field.setAccessible(true); |
||||||
|
return (T) field.get(target); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,16 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:mongo="http://www.springframework.org/schema/data/mongo" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd |
||||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<mongo:mongo/> |
||||||
|
|
||||||
|
<mongo:mongo id="defaultMongo" host="localhost" port="27017"/> |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans> |
||||||
Loading…
Reference in new issue