10 changed files with 296 additions and 66 deletions
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException; |
||||
|
||||
public class InvalidMongoDbApiUsageException extends InvalidDataAccessApiUsageException { |
||||
|
||||
public InvalidMongoDbApiUsageException(String msg) { |
||||
super(msg); |
||||
} |
||||
|
||||
public InvalidMongoDbApiUsageException(String msg, Throwable cause) { |
||||
super(msg, cause); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.dao.UncategorizedDataAccessException; |
||||
|
||||
public class UncategorizedMongoDbException extends UncategorizedDataAccessException { |
||||
|
||||
public UncategorizedMongoDbException(String msg, Throwable cause) { |
||||
super(msg, cause); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,97 @@
@@ -0,0 +1,97 @@
|
||||
/* |
||||
* Copyright (c) 2011 by the original author(s). |
||||
* |
||||
* 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.support.BeanDefinitionBuilder; |
||||
import org.springframework.beans.factory.support.ManagedList; |
||||
import org.springframework.beans.factory.xml.ParserContext; |
||||
import org.springframework.data.document.mongodb.MongoOptionsFactoryBean; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.util.xml.DomUtils; |
||||
import org.w3c.dom.Element; |
||||
|
||||
import com.mongodb.ServerAddress; |
||||
|
||||
abstract class ParsingUtils { |
||||
|
||||
/** |
||||
* Parses the mongo replica-set element. |
||||
* @param parserContext the parser context |
||||
* @param element the mongo element |
||||
* @param mongoBuilder the bean definition builder to populate |
||||
* @return true if parsing actually occured, false otherwise |
||||
*/ |
||||
static boolean parseReplicaSet(ParserContext parserContext, Element element, BeanDefinitionBuilder mongoBuilder) { |
||||
|
||||
|
||||
String replicaSetString = element.getAttribute("replica-set"); |
||||
if (StringUtils.hasText(replicaSetString)) { |
||||
ManagedList<Object> serverAddresses = new ManagedList<Object>(); |
||||
String[] replicaSetStringArray = StringUtils.commaDelimitedListToStringArray(replicaSetString); |
||||
for (int i = 0; i < replicaSetStringArray.length; i++) { |
||||
String[] hostAndPort = StringUtils.delimitedListToStringArray(replicaSetStringArray[i], ":"); |
||||
BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(ServerAddress.class); |
||||
defBuilder.addConstructorArgValue(hostAndPort[0]); |
||||
defBuilder.addConstructorArgValue(hostAndPort[1]); |
||||
serverAddresses.add(defBuilder.getBeanDefinition()); |
||||
} |
||||
if (!serverAddresses.isEmpty()) { |
||||
mongoBuilder.addPropertyValue("replicaSetSeeds", serverAddresses); |
||||
} |
||||
} |
||||
return true; |
||||
|
||||
} |
||||
/** |
||||
* Parses the mongo:options sub-element. Populates the given attribute factory with the proper attributes. |
||||
* |
||||
* @return true if parsing actually occured, false otherwise |
||||
*/ |
||||
static boolean parseMongoOptions(ParserContext parserContext, Element element, BeanDefinitionBuilder mongoBuilder) { |
||||
Element optionsElement = DomUtils.getChildElementByTagName(element, "options"); |
||||
if (optionsElement == null) |
||||
return false; |
||||
|
||||
BeanDefinitionBuilder optionsDefBuilder = BeanDefinitionBuilder |
||||
.genericBeanDefinition(MongoOptionsFactoryBean.class); |
||||
|
||||
setPropertyValue(optionsElement, optionsDefBuilder, "connections-per-host", "connectionsPerHost"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "threads-allowed-to-block-for-connection-multiplier", |
||||
"threadsAllowedToBlockForConnectionMultiplier"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "max-wait-time", "maxWaitTime"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "connect-timeout", "connectTimeout"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "socket-timeout", "socketTimeout"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "socket-keep-alive", "socketKeepAlive"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "auto-connect-retry", "autoConnectRetry"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "write-number", "writeNumber"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "write-timeout", "writeTimeout"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "write-fsync", "writeFsync"); |
||||
setPropertyValue(optionsElement, optionsDefBuilder, "slave-ok", "slaveOk"); |
||||
|
||||
|
||||
|
||||
mongoBuilder.addPropertyValue("mongoOptions", optionsDefBuilder.getBeanDefinition()); |
||||
return true; |
||||
} |
||||
|
||||
static void setPropertyValue(Element element, BeanDefinitionBuilder builder, String attrName, String propertyName) { |
||||
String attr = element.getAttribute(attrName); |
||||
if (StringUtils.hasText(attr)) { |
||||
builder.addPropertyValue(propertyName, attr); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* 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.*; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.List; |
||||
|
||||
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; |
||||
|
||||
import com.mongodb.Mongo; |
||||
import com.mongodb.MongoOptions; |
||||
import com.mongodb.ServerAddress; |
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@ContextConfiguration |
||||
public class MongoNamespaceReplicaSetTests extends NamespaceTestSupport { |
||||
|
||||
@Autowired |
||||
private ApplicationContext ctx; |
||||
|
||||
|
||||
@Test |
||||
public void testMongoWithReplicaSets() throws Exception { |
||||
assertTrue(ctx.containsBean("replicaSetMongo")); |
||||
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&replicaSetMongo"); |
||||
String host = readField("host", mfb); |
||||
Integer port = readField("port", mfb); |
||||
List<ServerAddress> replicaSetSeeds = readField("replicaSetSeeds", mfb); |
||||
assertNotNull(replicaSetSeeds); |
||||
|
||||
assertEquals("127.0.0.1", replicaSetSeeds.get(0).getHost()); |
||||
assertEquals(27017, replicaSetSeeds.get(0).getPort()); |
||||
|
||||
assertEquals("localhost", replicaSetSeeds.get(1).getHost()); |
||||
assertEquals(27018, replicaSetSeeds.get(1).getPort()); |
||||
|
||||
Mongo mongo = mfb.getObject(); |
||||
|
||||
//TODO test infrastructure to have replica sets
|
||||
//assertEquals(2, mongo.getAllAddress().size());
|
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
/* |
||||
* Copyright 2011 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 java.lang.reflect.Field; |
||||
|
||||
public class NamespaceTestSupport { |
||||
|
||||
|
||||
@SuppressWarnings({ "unchecked" }) |
||||
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,17 @@
@@ -0,0 +1,17 @@
|
||||
<?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:mongo="http://www.springframework.org/schema/data/mongo" |
||||
xmlns:context="http://www.springframework.org/schema/context" |
||||
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 |
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
||||
|
||||
<mongo:mongo id="replicaSetMongo" replica-set="127.0.0.1:27017,localhost:27018"/> |
||||
|
||||
<!-- |
||||
<mongo:db-factory dbname="database"> |
||||
<mongo:mongo replica-set="127.0.0.1:27017,localhost:27018"/> |
||||
</mongo:db-factory> |
||||
--> |
||||
</beans> |
||||
Loading…
Reference in new issue