Skip to content

Commit c5a99b5

Browse files
baumgartnerodrotbohm
authored andcommitted
DATAMONGO-140, DATAMONGO-628 - Namespace elements for MongoTemplate and GridFsTemplate.
1 parent 9564bcb commit c5a99b5

File tree

7 files changed

+351
-16
lines changed

7 files changed

+351
-16
lines changed
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
/*
2-
* Copyright (c) 2011 by the original author(s).
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mongodb.config;
1817

1918
/**
20-
* @author Jon Brisbin <jbrisbin@vmware.com>
19+
* Constants to declare bean names used by the namespace configuration.
20+
*
21+
* @author Jon Brisbin
22+
* @author Oliver Gierke
23+
* @author Martin Baumgartner
2124
*/
2225
public abstract class BeanNames {
2326

@@ -28,4 +31,6 @@ public abstract class BeanNames {
2831
static final String VALIDATING_EVENT_LISTENER = "validatingMongoEventListener";
2932
static final String IS_NEW_STRATEGY_FACTORY = "isNewStrategyFactory";
3033
static final String DEFAULT_CONVERTER_BEAN_NAME = "mappingConverter";
34+
static final String MONGO_TEMPLATE = "mongoTemplate";
35+
static final String GRID_FS_TEMPLATE = "gridFsTemplate";
3136
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.config;
17+
18+
import org.springframework.beans.factory.BeanDefinitionStoreException;
19+
import org.springframework.beans.factory.config.BeanDefinition;
20+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
21+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22+
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
23+
import org.springframework.beans.factory.xml.BeanDefinitionParser;
24+
import org.springframework.beans.factory.xml.ParserContext;
25+
import org.springframework.data.config.BeanComponentDefinitionBuilder;
26+
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
27+
import org.springframework.util.StringUtils;
28+
import org.w3c.dom.Element;
29+
30+
/**
31+
* {@link BeanDefinitionParser} to parse {@code gridFsTemplate} elements into {@link BeanDefinition}s.
32+
*
33+
* @author Martin Baumgartner
34+
*/
35+
class GridFsTemplateParser extends AbstractBeanDefinitionParser {
36+
37+
/*
38+
* (non-Javadoc)
39+
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext)
40+
*/
41+
@Override
42+
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
43+
throws BeanDefinitionStoreException {
44+
45+
String id = super.resolveId(element, definition, parserContext);
46+
return StringUtils.hasText(id) ? id : BeanNames.GRID_FS_TEMPLATE;
47+
}
48+
49+
/*
50+
* (non-Javadoc)
51+
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#parseInternal(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext)
52+
*/
53+
@Override
54+
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
55+
56+
BeanComponentDefinitionBuilder helper = new BeanComponentDefinitionBuilder(element, parserContext);
57+
58+
String converterRef = element.getAttribute("converter-ref");
59+
String dbFactoryRef = element.getAttribute("db-factory-ref");
60+
61+
BeanDefinitionBuilder gridFsTemplateBuilder = BeanDefinitionBuilder.genericBeanDefinition(GridFsTemplate.class);
62+
63+
if (StringUtils.hasText(dbFactoryRef)) {
64+
gridFsTemplateBuilder.addConstructorArgReference(dbFactoryRef);
65+
} else {
66+
gridFsTemplateBuilder.addConstructorArgReference(BeanNames.DB_FACTORY);
67+
}
68+
69+
if (StringUtils.hasText(converterRef)) {
70+
gridFsTemplateBuilder.addConstructorArgReference(converterRef);
71+
} else {
72+
gridFsTemplateBuilder.addConstructorArgReference(BeanNames.DEFAULT_CONVERTER_BEAN_NAME);
73+
}
74+
75+
return (AbstractBeanDefinition) helper.getComponentIdButFallback(gridFsTemplateBuilder, BeanNames.GRID_FS_TEMPLATE)
76+
.getBeanDefinition();
77+
}
78+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoNamespaceHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2012 the original author or authors.
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
* {@link org.springframework.beans.factory.xml.NamespaceHandler} for Mongo DB configuration.
2525
*
2626
* @author Oliver Gierke
27+
* @author Martin Baumgartner
2728
*/
2829
public class MongoNamespaceHandler extends NamespaceHandlerSupport {
2930

@@ -42,5 +43,7 @@ public void init() {
4243
registerBeanDefinitionParser("db-factory", new MongoDbFactoryParser());
4344
registerBeanDefinitionParser("jmx", new MongoJmxParser());
4445
registerBeanDefinitionParser("auditing", new MongoAuditingBeanDefinitionParser());
46+
registerBeanDefinitionParser("template", new MongoTemplateParser());
47+
registerBeanDefinitionParser("gridFsTemplate", new GridFsTemplateParser());
4548
}
4649
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2011-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.config;
17+
18+
import static org.springframework.data.config.ParsingUtils.*;
19+
import static org.springframework.data.mongodb.config.MongoParsingUtils.*;
20+
21+
import org.springframework.beans.factory.BeanDefinitionStoreException;
22+
import org.springframework.beans.factory.config.BeanDefinition;
23+
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
24+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
25+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26+
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
27+
import org.springframework.beans.factory.xml.BeanDefinitionParser;
28+
import org.springframework.beans.factory.xml.ParserContext;
29+
import org.springframework.data.config.BeanComponentDefinitionBuilder;
30+
import org.springframework.data.mongodb.core.MongoTemplate;
31+
import org.springframework.util.StringUtils;
32+
import org.w3c.dom.Element;
33+
34+
/**
35+
* {@link BeanDefinitionParser} to parse {@code template} elements into {@link BeanDefinition}s.
36+
*
37+
* @author Martin Baumgartner
38+
*/
39+
class MongoTemplateParser extends AbstractBeanDefinitionParser {
40+
41+
/*
42+
* (non-Javadoc)
43+
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext)
44+
*/
45+
@Override
46+
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
47+
throws BeanDefinitionStoreException {
48+
49+
String id = super.resolveId(element, definition, parserContext);
50+
return StringUtils.hasText(id) ? id : BeanNames.MONGO_TEMPLATE;
51+
}
52+
53+
/*
54+
* (non-Javadoc)
55+
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#parseInternal(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext)
56+
*/
57+
@Override
58+
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
59+
60+
BeanComponentDefinitionBuilder helper = new BeanComponentDefinitionBuilder(element, parserContext);
61+
62+
String converterRef = element.getAttribute("converter-ref");
63+
String dbFactoryRef = element.getAttribute("db-factory-ref");
64+
65+
BeanDefinitionBuilder mongoTemplateBuilder = BeanDefinitionBuilder.genericBeanDefinition(MongoTemplate.class);
66+
setPropertyValue(mongoTemplateBuilder, element, "write-concern", "writeConcern");
67+
68+
if (StringUtils.hasText(dbFactoryRef)) {
69+
mongoTemplateBuilder.addConstructorArgReference(dbFactoryRef);
70+
} else {
71+
mongoTemplateBuilder.addConstructorArgReference(BeanNames.DB_FACTORY);
72+
}
73+
74+
if (StringUtils.hasText(converterRef)) {
75+
mongoTemplateBuilder.addConstructorArgReference(converterRef);
76+
}
77+
78+
BeanDefinitionBuilder writeConcernPropertyEditorBuilder = getWriteConcernPropertyEditorBuilder();
79+
80+
BeanComponentDefinition component = helper.getComponent(writeConcernPropertyEditorBuilder);
81+
parserContext.registerBeanComponent(component);
82+
83+
return (AbstractBeanDefinition) helper.getComponentIdButFallback(mongoTemplateBuilder, BeanNames.MONGO_TEMPLATE)
84+
.getBeanDefinition();
85+
}
86+
}

spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.3.xsd

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,108 @@ This controls if the driver is allowed to read from secondaries or slaves. Defa
490490
</xsd:attribute>
491491
</xsd:complexType>
492492

493+
<xsd:simpleType name="converterRef">
494+
<xsd:annotation>
495+
<xsd:appinfo>
496+
<tool:annotation kind="ref">
497+
<tool:assignable-to type="org.springframework.data.mongodb.core.convert.MongoConverter"/>
498+
</tool:annotation>
499+
</xsd:appinfo>
500+
</xsd:annotation>
501+
<xsd:union memberTypes="xsd:string"/>
502+
</xsd:simpleType>
503+
504+
<xsd:element name="template">
505+
<xsd:annotation>
506+
<xsd:documentation><![CDATA[
507+
Defines a MongoDbFactory for connecting to a specific database
508+
]]></xsd:documentation>
509+
</xsd:annotation>
510+
<xsd:complexType>
511+
<xsd:attribute name="id" type="xsd:ID" use="optional">
512+
<xsd:annotation>
513+
<xsd:documentation><![CDATA[
514+
The name of the mongo definition (by default "mongoDbFactory").]]></xsd:documentation>
515+
</xsd:annotation>
516+
</xsd:attribute>
517+
<xsd:attribute name="converter-ref" type="converterRef" use="optional">
518+
<xsd:annotation>
519+
<xsd:documentation><![CDATA[
520+
The reference to a Mongoconverter instance.
521+
]]>
522+
</xsd:documentation>
523+
<xsd:appinfo>
524+
<tool:annotation kind="ref">
525+
<tool:assignable-to type="org.springframework.data.mongodb.core.convert.MongoConverter"/>
526+
</tool:annotation>
527+
</xsd:appinfo>
528+
</xsd:annotation>
529+
</xsd:attribute>
530+
<xsd:attribute name="db-factory-ref" type="xsd:string"
531+
use="optional">
532+
<xsd:annotation>
533+
<xsd:documentation>
534+
The reference to a DbFactory.
535+
</xsd:documentation>
536+
<xsd:appinfo>
537+
<tool:annotation kind="ref">
538+
<tool:assignable-to
539+
type="org.springframework.data.mongodb.MongoDbFactory" />
540+
</tool:annotation>
541+
</xsd:appinfo>
542+
</xsd:annotation>
543+
</xsd:attribute>
544+
<xsd:attribute name="write-concern">
545+
<xsd:annotation>
546+
<xsd:documentation>
547+
The WriteConcern that will be the default value used when asking the MongoDbFactory for a DB object
548+
</xsd:documentation>
549+
</xsd:annotation>
550+
<xsd:simpleType>
551+
<xsd:union memberTypes="writeConcernEnumeration xsd:string"/>
552+
</xsd:simpleType>
553+
</xsd:attribute>
554+
</xsd:complexType>
555+
</xsd:element>
556+
557+
<xsd:element name="gridFsTemplate">
558+
<xsd:annotation>
559+
<xsd:documentation><![CDATA[
560+
Defines a MongoDbFactory for connecting to a specific database
561+
]]></xsd:documentation>
562+
</xsd:annotation>
563+
<xsd:complexType>
564+
<xsd:attribute name="id" type="xsd:ID" use="optional">
565+
<xsd:annotation>
566+
<xsd:documentation><![CDATA[
567+
The name of the mongo definition (by default "mongoDbFactory").]]></xsd:documentation>
568+
</xsd:annotation>
569+
</xsd:attribute>
570+
<xsd:attribute name="converter-ref" type="converterRef" use="optional">
571+
<xsd:annotation>
572+
<xsd:documentation><![CDATA[
573+
The reference to a Mongoconverter instance.
574+
]]>
575+
</xsd:documentation>
576+
<xsd:appinfo>
577+
<tool:annotation kind="ref">
578+
<tool:assignable-to type="org.springframework.data.mongodb.core.convert.MongoConverter"/>
579+
</tool:annotation>
580+
</xsd:appinfo>
581+
</xsd:annotation>
582+
</xsd:attribute>
583+
<xsd:attribute name="db-factory-ref" type="xsd:string" use="optional">
584+
<xsd:annotation>
585+
<xsd:documentation>
586+
The reference to a DbFactory.
587+
</xsd:documentation>
588+
<xsd:appinfo>
589+
<tool:annotation kind="ref">
590+
<tool:assignable-to type="org.springframework.data.mongodb.MongoDbFactory" />
591+
</tool:annotation>
592+
</xsd:appinfo>
593+
</xsd:annotation>
594+
</xsd:attribute>
595+
</xsd:complexType>
596+
</xsd:element>
493597
</xsd:schema>

0 commit comments

Comments
 (0)