Compare commits

...

59 Commits

Author SHA1 Message Date
Spring Buildmaster
4be229312c DATAMONGO-396 - Release 1.1.0.M1. 2012-05-07 06:11:25 -07:00
Oliver Gierke
54ec7af462 DATAMONGO-396 - Updated changelog before 1.1.0.M1 release.
Fixed dependency to Spring Data Commons to 1.3.0.RC2.
2012-05-07 14:54:20 +02:00
Oliver Gierke
7162677992 DATAMONGO-443 - Upgraded to Querydsl 2.5.0.
Updated repository implementation and support classes to use Google Guava API instead of Commons Collections introduced in Querydsl 2.5.0.
2012-05-07 14:28:46 +02:00
Oliver Gierke
cf131aaa7e DATAMONGO-441 - Improved MongoDBUtils API.
Use UserCredentials value object instead of low level String and char[] for username and password. Adapted clients accordingly.
2012-05-07 14:28:45 +02:00
Thomas Risberg
6cd3b7a3aa DATAMONGO-299 - Adding tests ensuring exception alert for multiple conditions for same key. 2012-05-07 14:28:22 +02:00
Oliver Gierke
71e639402c DATAMONGO-439 - Created performance tests.
Added a performance tests that tests two things. Write performance in a variety of WriteConcern configurations as well as write-read performance against WriteConcern.SAFE. The tests can be executed by running the Maven build with the performance-test profile. Fixed some performance hotspots discovered in the course of profiling the test case.
2012-05-03 17:57:26 +02:00
Oliver Gierke
fed31c74e1 DATACMNS-166 - Adapted changes of Spring Data Commons. 2012-05-02 19:54:31 +02:00
Maciej Walkowiak
5b13307e39 DATAMONGO-36 - Added support for JSR-303 validation
Added event listener that uses a java.validation.Validator to trigger validation on an entity before persisting it. This validation is enabled by default if the javx.validation API is present on the classpath. Added namespace attribute 'disable-validation' to allow disabling that auto enabling behavior.
2012-04-27 15:36:29 +02:00
Oliver Gierke
d317bcfa71 DATAMONGO-396 - Upgrade to current Spring Data Commons snapshot version. 2012-04-26 19:23:34 +02:00
Oliver Gierke
883bfbe79e Added Eclipse formatter settings.
Formatted cross-store module with the Spring Data formatter.
2012-04-16 15:29:48 +02:00
Oliver Gierke
7502ac137c DATAMONGO-432 - Upgraded to Spring Data Commons 1.3.0.RC1. 2012-04-16 15:19:59 +02:00
Oliver Gierke
3dfc59bfb8 DATAMONGO-429 - Fixed handling of nested arrays in QueryMapper.
QueryMapper now correctly transforms arrays not concreting them into BasicDBObjects anymore.
2012-04-16 15:19:59 +02:00
Oliver Gierke
9e50621c1b DATAMONGO-431 - Adapted changes in CrudRepository.save(…). 2012-04-16 15:19:59 +02:00
Oliver Gierke
472beaf6d2 DATAMONGO-427 - Added support for Before and After keywords for query creation.
The query creator now supports methods as follows: List<User> findByCreatedAtBefore(Date date);
2012-04-16 15:19:58 +02:00
Oliver Gierke
d6d8a13b0c DATAMONGO-423 - Fixed handling of negated regular expressions.
When using the not() method combined with the regex(…) methods on Criteria we created an invalid query so far. Fixed the regex(…) method to always transform the regex expressions and options into a Pattern instance and render that according to the $not state.
2012-04-16 15:19:58 +02:00
Oliver Gierke
3dcf93744b DATAMONGO-425 - Fixed parameter binding for Dates and manually defined queries.
Replaced manual JSON serialization for special parameters inside StringBasedMongoQuery by calling JSON.serialize(…).
2012-04-16 15:19:58 +02:00
Oliver Gierke
6373a70ca4 DATAMONGO-181 - Improved resource handling for Mongo instance.
SimpleMongoDbFactory now only closes the Mongo instance if it created it itself. Removed public getter for WriteConcern and hold a UserCredentials instead of its parts. Uses improved UserCredentials API introduced in DATACMNS-142.
2012-04-16 15:19:44 +02:00
Oliver Gierke
858afa80f1 DATAMONGO-422 - Fixed invalid UUID conversion.
Removed UUIDToBinaryConverter and BinaryToUUIDConverter as the MongoDB Java driver can handle it itself. Added UUID as Mongo-simple type. Added integration test for reading and writing a UUID property.
2012-04-02 13:32:54 +02:00
Oliver Gierke
97f57e3541 DATAMONGO-418 - Added support for StartingWith, EndingWith and Containing keywords. 2012-03-21 20:44:48 +01:00
Oliver Gierke
05c2045e45 DATAMONGO-347 - Derived repository queries now correctly resolve referenced DBRef properties.
Added toDBRef(…) method to MongoWriter to be able to create a DBRef representation of an object. Polished DBRef creation implementation inside MappingMongoConverter. Removed collection and id attributes from @DBRef annotation as they have neither been documented nor used so far. Refactored MongoQueryCreator to be aware of the property and convert the given value into a DBRef if necessary.
2012-03-12 16:21:56 +01:00
Oliver Gierke
d3598198a6 DATAMONGO-360 - Beautify IndexInfo API.
Added a method to IndexInfo to allow asking the object whether it is an index with a given collection of fields. Refactored RepositoryIndexCreationIntegrationTest to use that API to be more safe against potential duplicate index definitions.
2012-03-12 16:20:15 +01:00
Oliver Gierke
2a8fe5bac7 DATAMONGO-413 - Fixed bug in MongoQueryCreator.
MongoQueryCreator used the outdated OrQuery class to concatenate parts with OR. Refactored the class to use the Criteria.orOperator(…) method. Removed OrQuery class as it is deprecated in the 1.0.x branch.
2012-03-12 11:53:50 +01:00
Oliver Gierke
cdc8f52bad DATAMONGO-356 - CDI integration for Mongo repositories. 2012-03-09 17:52:33 +01:00
Oliver Gierke
39492daa50 DATAMONGO-366 - Polished reference documentation.
Fixed link to bug tracker. Polished docbook files a bit.
2012-03-09 16:03:28 +01:00
Oliver Gierke
16904ba1a7 DATAMONGO-402 - Adapt new entity instantiation API.
Adapt to the entity instantiation API refactored to be able to cope with non-static member classes.
2012-03-09 15:22:24 +01:00
Oliver Gierke
51a469b46f DATAMONGO-412 - General overhaul of the JavaConfig base class.
Refactored JavaConfig base class to reflect the XML namespace a bit more closely (esp. regarding configuration of custom converters and thus registering "simple" types). Prevent duplicate invocation of getUserCredentials(). Added JavaDoc to explain which configuration methods use which other ones to ease detailed configuration.
2012-03-08 13:10:50 +01:00
Oliver Gierke
9f2b1f4ed0 DATAMONGO-273, DATAMONGO-294 - Re-enabled accidentally disabled test case. 2012-03-08 12:16:59 +01:00
Oliver Gierke
e0ea8a6088 Fixed some architecture inconsistencies.
Updated Sonargraph architecture description file to include GridFS subsystem. Moved QueryMapper into convert subsystem. Polished MongoRepository interface. Let SimpleMongoRepository implement MongoRepository.
2012-03-08 12:16:42 +01:00
Oliver Gierke
8861934336 DATAMONGO-360 - Fixed index information creation for geo indexes.
Fixed a ClassCastException that occurred because we didn't consider index information of geo indexes (they return "2d" as direction). Introduced new IndexField abstraction that supersedes the fieldSpec Map in IndexInfo.
2012-03-08 12:11:25 +01:00
Oliver Gierke
93c9713adf DATAMONGO-382 - Fixed potential ClassCastException in MappingMongoConverter.
MappingMongoConverter's convertToMongoType(…) now deals with Sets (and more generally all Collections) correctly.
2012-03-06 21:26:10 +01:00
Oliver Gierke
68a0c1ee2c DATAMONGO-394 - Upgrade to Querydsl 2.3.2. 2012-03-06 19:24:45 +01:00
Oliver Gierke
c3d533d59d DATAMONGO-411 - Double check type of PersistentEntity for index creation.
The Spring container does not check nested generic types of the type parameter of  ApplicationEvent<T>. As T is parameterized in our case as well (PersistentEntity<…, …>) we can code an event listener against that fully parameterized type but might run into ClassCastExceptions as we might get other implementations handed into the method at runtime. We now do an instanceof check to safely invoke checkForIndexes(…) only in case we get the correct event type.
2012-03-02 09:08:24 +01:00
Oliver Gierke
e4b9be6b38 DATAMONGO-408 - Added StringToWriteConverter for XML setup convenience.
When using a PropertyPlaceHolderConfigurer to set WriteConcerns on a MongoFactoryBean just like this:

<bean class="….mongodb.core.MongoFactoryBean">
  <property name="writeConcern" value="${mongodb.writeConcern}"/>
</bean>

we might create invalid WriteConcerns as the BeanFactory will use the WriteConcern's constructor taking a String to create the instance by default. To make Spring use the valueOf(…) method in advance one needs to register either our already existing WriteConcernPropertyEditor or the newly introduced StringToWriteConcernConverter in Springs ConversionService.
2012-03-02 09:07:59 +01:00
Oliver Gierke
09a8b9c9c4 DATAMONGO-401 - Fixed NullPointerException in StringBasedMongoQuery. 2012-02-11 14:52:21 +01:00
Oliver Gierke
e531508bdc DATAMONGO-380 - Improved map handling for keys containing dots.
MappingMongoConverter now rejects objects that would result in field keys containing a dot as we cannot reliably escape and unescape them without potentially wrecking correct keys on reading. However I added a property mapKeyReplacement that can be set to e.g. ~ to have all dots in map keys replaced with ~. This will of course cause ~ to be transformed into dots when reading. If further customization is necessary override potentiallyEscapeMapKey(…) and potentiallyUnescapeMapKey(…).
2012-02-10 18:19:23 +01:00
Oliver Gierke
289c4b95ae DATAMONGO-397 - Replaced references to MongoTemplate with MongoOperations in repository package.
The reference in MongoRepositoryFactoryBean to MongoTemplate was unnecessary on the one hand and could cause problems in case the MongoTemplate is proxied as it can't be wired into the factory anymore then.
2012-02-10 17:16:15 +01:00
Oliver Gierke
25e6d6e025 DATAMONGO-396 - Update Spring Data Commons dependency to 1.3.0.M1. 2012-02-08 21:15:03 +01:00
Oliver Gierke
83d18b5ce6 Readded Spring Test dependency to cross-store module. 2012-02-08 20:39:08 +01:00
Oliver Gierke
8f09748d78 DATAMONGO-394 - Upgrade to Querydsl 2.3.1. 2012-02-08 10:57:29 +01:00
Oliver Gierke
4429971548 DATACMNS-390 - Added UUIDToBinaryConverter to be able to handle UUIDs by default. 2012-02-06 15:08:21 +01:00
Oliver Gierke
065a50bc8c DATAMONGO-391 - Move to SLF4J for logging.
Polished pom.xml files. Upgraded to Log4J 1.2.16 as it excludes unnecessary dependencies. Consolidated test dependencies into parent pom. Remove dependency-management sections. Exclude commons-logging in favor of SLF4J.
2012-02-06 15:00:34 +01:00
Oliver Gierke
f8daa69e53 DATACMNS-129 - Implement CrudRepository.findAll(Iterable<ID> ids). 2012-02-02 16:55:09 +01:00
Oliver Gierke
8a18cd4aef DATACMNS-128 - Adapted to API changes of ParametersParameterAccessor. 2012-02-02 16:54:28 +01:00
Oliver Gierke
3320208a1d DATAMONGO-358 - Fixed collection reading when property type is no a collection.
If you have a property of type object and it contains a collection we didn't property read it back in as creating the collection instance failed due to an invalid call to CollectionFactory. We now default the parameter handed to that call to List in case the property type is not a Collection at all.
2012-02-01 16:29:57 +01:00
Oliver Gierke
4ebc6e96d3 DATAMONGO-385 - Added test case to show repositories working with Long id. 2012-02-01 15:30:20 +01:00
Oliver Gierke
91fe5480a8 DATAMONGO-387 - Repository query execution for GeoPage results are now working correctly.
Added special handling of GeoPage return types for repository query methods.
2012-02-01 14:23:29 +01:00
Oliver Gierke
b6322d7bbc DATAMONGO-374 - Added test case showing custom converters being used for updates. 2012-02-01 11:47:01 +01:00
Oliver Gierke
572c4bb2cd DATAMONGO-6 - Initial support for GridFS.
We now provide a GridFsTemplate for storing, retrieving and deleting documents from GridFS. Introduced GridFsResource implementation to integrate with Spring's Resource abstraction. Beyond that GridFsOperations extends ResourcePatternResolver and thus allows to retrieve GridFsResources via Ant-style filename patterns.
2012-02-01 10:28:28 +01:00
Oliver Gierke
9caa8b15cd DATAMONGO-375 - Polished XSD by removing unnecessary version numbers. 2012-02-01 09:59:43 +01:00
Oliver Gierke
275f4fe6b8 Consolidate Maven repository usage to use repo.springsource.org/libs-snapshot. 2012-01-31 18:46:36 +01:00
Oliver Gierke
4c40ef7e3d DATAMONGO-383 - Adapt new EntityInstantiator API of Spring Data Commons. 2012-01-24 10:49:53 +01:00
Oliver Gierke
609097c0dc DATAMONGO-379 - Improved entity instantiation.
Huge refactoring of the way MappingMongoConverter instantiates entities. The constructor arguments now have to mirror a property exactly in terms of name. Thus we can pick up mapping information from the property to lookup the correct value from the source document. The @Value annotation can be used to either inject completely arbitrary values into the instance (e.g. by referring to a Spring bean) or simply define an expression against DBObject's fields:

class Sample {
  String foo;
  String bar;
  
  Sample(String foo, @Value("#root._bar") String bar) {
    this.foo = foo;
    this.bar = bar;
  }
}

trying to create an instance of this class from

{ "foo" : "FOO" } -> new Sample("FOO", null)
{ "_bar" : "BAR" } -> new Sample(null, "BAR").
2012-01-16 18:19:15 +00:00
Oliver Gierke
307d86498e DATAMONGO-368 - MappingMongoConverter does not remove null values from collections anymore. 2012-01-12 15:51:38 +01:00
Oliver Gierke
19879b8dab DATAMONGO-376 - Fixed potential NPE in SpringDataMongodbSerializer. 2012-01-12 12:23:51 +01:00
Oliver Gierke
b190f22551 Polished formatting and Javadoc. 2012-01-12 12:23:18 +01:00
Oliver Gierke
0e9c6a5de8 DATAMONGO-369 - Fixed query mapping when a DBObject is included in the query object.
Replaced premature return with continue to break the for loop appropriately.
2012-01-12 10:52:27 +01:00
Oliver Gierke
5ec0cc7327 DATAMONGO-373 - Fixed potential ClassCastException in QueryMapper.
QueryMapper assumed finding a BasicBSONList for $(n)or operators. This is generally true if the DBObject was created through our Query abstraction. If you use the MongoDB driver QueryBuilder this will fail. We're now only insisting on an Iterable which fixes the issue.
2012-01-11 20:25:10 +01:00
Oliver Gierke
817e167056 Fixed failing test case.
Include dependency to Person repository to make sure instance creation is triggered.
2012-01-11 20:03:34 +01:00
Oliver Gierke
4a6fa5cade DATAMONGO-357 - Prepare 1.1.0 development branch. 2011-12-22 21:16:53 +01:00
199 changed files with 6775 additions and 2511 deletions

291
etc/formatting.xml Normal file
View File

@@ -0,0 +1,291 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="12">
<profile kind="CodeFormatterProfile" name="Spring Data" version="12">
<setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.disabling_tag" value="@formatter:off"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_annotation" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_annotation_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_field" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.use_on_off_tags" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_annotation_type_member_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_ellipsis" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_multiple_fields" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_conditional_expression" value="80"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_binary_operator" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_array_initializer" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constant" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_after_package" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.continuation_indentation" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_binary_operator" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_package" value="0"/>
<setting id="org.eclipse.jdt.core.compiler.source" value="1.7"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_constructor_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_line_comments" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_block" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_member_type" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.align_type_members_on_columns" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_enum_constant" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_unary_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_case" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.indent_parameter_description" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.lineSplit" value="120"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_explicitconstructorcall_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indentation.size" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.enabling_tag" value="@formatter:on"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_enum_constant" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_assignment" value="0"/>
<setting id="org.eclipse.jdt.core.compiler.problem.assertIdentifier" value="error"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_parameters" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_statements_compare_to_body" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_method" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_for" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_cast" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_method_declaration" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_allocation_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_constant" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_annotation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_throws" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_switch" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_return" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_allocation_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_throw" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.compiler.problem.enumIdentifier" value="error"/>
<setting id="org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_ellipsis" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_inits" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_method_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.compact_else_if" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_array_initializer" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_increments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_constant" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.comment.indent_root_tags" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_declarations" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_type_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_constructor_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_throws" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_empty_lines" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_parameters" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_block_in_case" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_catch" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_constructor_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter" value="insert"/>
<setting id="org.eclipse.jdt.core.compiler.compliance" value="1.7"/>
<setting id="org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_unary_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_anonymous_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_for" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_parameterized_type_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_binary_expression" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while" value="do not insert"/>
<setting id="org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode" value="enabled"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_label" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_javadoc_comments" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="120"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_between_import_groups" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_constant_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_semicolon" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation_type_member_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.wrap_before_binary_operator" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_statements_compare_to_block" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.join_lines_in_comments" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_compact_if" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_inits" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_array_initializer" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_default" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_constructor_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_imports" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_assert" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_html" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_allocation_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_conditional" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_for" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_source_code" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_allocation_expression" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_throws" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_brace_in_array_initializer" value="insert"/>
<setting id="org.eclipse.jdt.core.compiler.codegen.targetPlatform" value="1.7"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_resources_in_try" value="80"/>
<setting id="org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_header" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_enum_constant" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_block" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_catch" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_increments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_assert" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_type_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_catch" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_field_declarations" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_annotation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_parameterized_type_reference" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_invocation_arguments" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.new_lines_at_javadoc_boundaries" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_after_imports" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line" value="false"/>
</profile>
</profiles>

35
pom.xml
View File

@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-dist</artifactId> <artifactId>spring-data-mongodb-dist</artifactId>
<name>Spring Data MongoDB Distribution</name> <name>Spring Data MongoDB Distribution</name>
<version>1.0.0.RELEASE</version> <version>1.1.0.M1</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>spring-data-mongodb</module> <module>spring-data-mongodb</module>
@@ -145,12 +144,12 @@
<htmlCustomization>${project.basedir}/src/docbkx/resources/xsl/html.xsl</htmlCustomization> <htmlCustomization>${project.basedir}/src/docbkx/resources/xsl/html.xsl</htmlCustomization>
<useExtensions>1</useExtensions> <useExtensions>1</useExtensions>
<highlightSource>1</highlightSource> <highlightSource>1</highlightSource>
<highlightDefaultLanguage></highlightDefaultLanguage> <highlightDefaultLanguage />
<!-- callouts --> <!-- callouts -->
<entities> <entities>
<entity> <entity>
<name>version</name> <name>version</name>
<value>${pom.version}</value> <value>${project.version}</value>
</entity> </entity>
</entities> </entities>
<postProcess> <postProcess>
@@ -173,9 +172,7 @@
<include name="*.png" /> <include name="*.png" />
</fileset> </fileset>
</copy> </copy>
<move file="${project.basedir}/target/site/reference/pdf/index.pdf" <move file="${project.basedir}/target/site/reference/pdf/index.pdf" tofile="${project.basedir}/target/site/reference/pdf/spring-data-mongo-reference.pdf" failonerror="false" />
tofile="${project.basedir}/target/site/reference/pdf/spring-data-mongo-reference.pdf"
failonerror="false"/>
</postProcess> </postProcess>
</configuration> </configuration>
</plugin> </plugin>
@@ -184,7 +181,7 @@
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>2.5</version> <version>2.5</version>
<configuration> <configuration>
<javadoc:aggregate>true</javadoc:aggregate> <aggregate>true</aggregate>
<breakiterator>true</breakiterator> <breakiterator>true</breakiterator>
<header>Spring Data Document</header> <header>Spring Data Document</header>
<source>1.5</source> <source>1.5</source>
@@ -263,16 +260,10 @@
</build> </build>
<pluginRepositories> <pluginRepositories>
<!-- Necessary for the build extension -->
<pluginRepository> <pluginRepository>
<!-- necessary for bundlor and utils --> <id>spring-plugins-release</id>
<id>repository.plugin.springsource.release</id> <url>http://repo.springsource.org/plugins-release</url>
<name>SpringSource Maven Repository</name>
<url>http://repository.springsource.com/maven/bundles/release</url>
</pluginRepository>
<pluginRepository>
<id>repository.springframework.maven.release</id>
<name>Spring Framework Maven Release Repository</name>
<url>http://repo.springsource.org/release</url>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
@@ -282,13 +273,13 @@
<site> <site>
<id>static.springframework.org</id> <id>static.springframework.org</id>
<url> <url>
scp://static.springframework.org/var/www/domains/springframework.org/static/htdocs/spring-data/data-mongodb/docs/${project.version} scp://static.springframework.org/var/www/domains/springframework.org/static/htdocs/spring-data/data-mongodb/snapshot-site
</url> </url>
</site> </site>
<repository> <repository>
<id>spring-release</id> <id>spring-milestone</id>
<name>Spring Release Repository</name> <name>Spring Milestone Repository</name>
<url>s3://maven.springframework.org/release</url> <url>s3://maven.springframework.org/milestone</url>
</repository> </repository>
<snapshotRepository> <snapshotRepository>
<id>spring-snapshot</id> <id>spring-snapshot</id>

View File

@@ -1,10 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId> <artifactId>spring-data-mongodb-parent</artifactId>
<version>1.0.0.RELEASE</version> <version>1.1.0.M1</version>
<relativePath>../spring-data-mongodb-parent/pom.xml</relativePath> <relativePath>../spring-data-mongodb-parent/pom.xml</relativePath>
</parent> </parent>
<artifactId>spring-data-mongodb-cross-store</artifactId> <artifactId>spring-data-mongodb-cross-store</artifactId>
@@ -15,10 +15,12 @@
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId> <artifactId>spring-beans</artifactId>
<version>${org.springframework.version.range}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId> <artifactId>spring-tx</artifactId>
<version>${org.springframework.version.range}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
@@ -28,82 +30,19 @@
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId> <artifactId>spring-orm</artifactId>
</dependency> <version>${org.springframework.version.range}</version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<!-- Spring Data --> <!-- Spring Data -->
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId> <artifactId>spring-data-commons-core</artifactId>
<version>${data.commons.version}</version>
</dependency> </dependency>
<!-- <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-aspects</artifactId>
</dependency> -->
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId> <artifactId>spring-data-mongodb</artifactId>
</dependency> <version>1.1.0.M1</version>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> </dependency>
<dependency> <dependency>
@@ -151,25 +90,7 @@
</dependency> </dependency>
</dependencies> </dependencies>
<repositories>
<repository>
<id>jboss-repository</id>
<name>JBoss Public Repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public-jboss</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-maven-milestones</id>
<name>Springframework Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-maven-release</id>
<name>Springframework Maven Release Repository</name>
<url>http://maven.springframework.org/release</url>
</pluginRepository>
</pluginRepositories>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@@ -58,9 +58,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
this.entityManagerFactory = entityManagerFactory; this.entityManagerFactory = entityManagerFactory;
} }
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet)
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass,
Object id, final ChangeSet changeSet)
throws DataAccessException, NotFoundException { throws DataAccessException, NotFoundException {
if (id == null) { if (id == null) {
@@ -77,8 +75,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
log.debug("Loading MongoDB data for " + dbk); log.debug("Loading MongoDB data for " + dbk);
} }
mongoTemplate.execute(collName, new CollectionCallback<Object>() { mongoTemplate.execute(collName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
throws MongoException, DataAccessException {
for (DBObject dbo : collection.find(dbk)) { for (DBObject dbo : collection.find(dbk)) {
String key = (String) dbo.get(ENTITY_FIELD_NAME); String key = (String) dbo.get(ENTITY_FIELD_NAME);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@@ -87,9 +84,8 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
if (!changeSet.getValues().containsKey(key)) { if (!changeSet.getValues().containsKey(key)) {
String className = (String) dbo.get(ENTITY_FIELD_CLASS); String className = (String) dbo.get(ENTITY_FIELD_CLASS);
if (className == null) { if (className == null) {
throw new DataIntegrityViolationException( throw new DataIntegrityViolationException("Unble to convert property " + key + ": Invalid metadata, "
"Unble to convert property " + key + ENTITY_FIELD_CLASS + " not available");
+ ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
} }
Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
Object value = mongoTemplate.getConverter().read(clazz, dbo); Object value = mongoTemplate.getConverter().read(clazz, dbo);
@@ -135,10 +131,8 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
dbQuery.put(ENTITY_ID, getPersistentId(entity, cs)); dbQuery.put(ENTITY_ID, getPersistentId(entity, cs));
dbQuery.put(ENTITY_CLASS, entity.getClass().getName()); dbQuery.put(ENTITY_CLASS, entity.getClass().getName());
dbQuery.put(ENTITY_FIELD_NAME, key); dbQuery.put(ENTITY_FIELD_NAME, key);
DBObject dbId = mongoTemplate.execute(collName, DBObject dbId = mongoTemplate.execute(collName, new CollectionCallback<DBObject>() {
new CollectionCallback<DBObject>() { public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
public DBObject doInCollection(DBCollection collection)
throws MongoException, DataAccessException {
return collection.findOne(dbQuery); return collection.findOne(dbQuery);
} }
}); });
@@ -147,14 +141,12 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
log.debug("Flush: removing: " + dbQuery); log.debug("Flush: removing: " + dbQuery);
} }
mongoTemplate.execute(collName, new CollectionCallback<Object>() { mongoTemplate.execute(collName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
throws MongoException, DataAccessException {
collection.remove(dbQuery); collection.remove(dbQuery);
return null; return null;
} }
}); });
} } else {
else {
final DBObject dbDoc = new BasicDBObject(); final DBObject dbDoc = new BasicDBObject();
dbDoc.putAll(dbQuery); dbDoc.putAll(dbQuery);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@@ -166,8 +158,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
dbDoc.put("_id", dbId.get("_id")); dbDoc.put("_id", dbId.get("_id"));
} }
mongoTemplate.execute(collName, new CollectionCallback<Object>() { mongoTemplate.execute(collName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
throws MongoException, DataAccessException {
collection.save(dbDoc); collection.save(dbDoc);
return null; return null;
} }

View File

@@ -38,21 +38,18 @@ import org.springframework.data.crossstore.HashMapChangeSet;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
/** /**
* Aspect to turn an object annotated with @Document into a persistent document * Aspect to turn an object annotated with @Document into a persistent document using Mongo.
* using Mongo.
* *
* @author Thomas Risberg * @author Thomas Risberg
*/ */
public aspect MongoDocumentBacking { public aspect MongoDocumentBacking {
private static final Log LOGGER = LogFactory private static final Log LOGGER = LogFactory.getLog(MongoDocumentBacking.class);
.getLog(MongoDocumentBacking.class);
// Aspect shared config // Aspect shared config
private ChangeSetPersister<Object> changeSetPersister; private ChangeSetPersister<Object> changeSetPersister;
public void setChangeSetPersister( public void setChangeSetPersister(ChangeSetPersister<Object> changeSetPersister) {
ChangeSetPersister<Object> changeSetPersister) {
this.changeSetPersister = changeSetPersister; this.changeSetPersister = changeSetPersister;
} }
@@ -71,8 +68,7 @@ public aspect MongoDocumentBacking {
!execution((DocumentBacked+).new(ChangeSet)) && !execution((DocumentBacked+).new(ChangeSet)) &&
this(entity); this(entity);
pointcut finderConstructorOfChangeSetBackedObject(DocumentBacked entity, pointcut finderConstructorOfChangeSetBackedObject(DocumentBacked entity, ChangeSet cs) :
ChangeSet cs) :
execution((DocumentBacked+).new(ChangeSet)) && execution((DocumentBacked+).new(ChangeSet)) &&
this(entity) && this(entity) &&
args(cs); args(cs);
@@ -124,13 +120,11 @@ public aspect MongoDocumentBacking {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ChangeSetPersister<Object> changeSetPersister = (ChangeSetPersister<Object>) documentEntity.itdChangeSetPersister; ChangeSetPersister<Object> changeSetPersister = (ChangeSetPersister<Object>) documentEntity.itdChangeSetPersister;
try { try {
changeSetPersister.getPersistentState( changeSetPersister.getPersistentState(documentEntity.getClass(), documentEntity.get_persistent_id(),
documentEntity.getClass(),
documentEntity.get_persistent_id(),
documentEntity.getChangeSet()); documentEntity.getChangeSet());
} catch (DataAccessException e) {
} catch (NotFoundException e) {
} }
catch (DataAccessException e) {}
catch (NotFoundException e) {}
for (String key : entity.getChangeSet().getValues().keySet()) { for (String key : entity.getChangeSet().getValues().keySet()) {
nulledCs.set(key, null); nulledCs.set(key, null);
} }
@@ -138,14 +132,11 @@ public aspect MongoDocumentBacking {
} }
before(DocumentBacked entity) : arbitraryUserConstructorOfChangeSetBackedObject(entity) { before(DocumentBacked entity) : arbitraryUserConstructorOfChangeSetBackedObject(entity) {
LOGGER LOGGER.debug("User-defined constructor called on DocumentBacked object of class " + entity.getClass());
.debug("User-defined constructor called on DocumentBacked object of class "
+ entity.getClass());
// Populate all ITD fields // Populate all ITD fields
entity.setChangeSet(new HashMapChangeSet()); entity.setChangeSet(new HashMapChangeSet());
entity.itdChangeSetPersister = changeSetPersister; entity.itdChangeSetPersister = changeSetPersister;
entity.itdTransactionSynchronization = entity.itdTransactionSynchronization = new ChangeSetBackedTransactionSynchronization(changeSetPersister, entity);
new ChangeSetBackedTransactionSynchronization(changeSetPersister, entity);
// registerTransactionSynchronization(entity); // registerTransactionSynchronization(entity);
} }
@@ -156,14 +147,12 @@ public aspect MongoDocumentBacking {
LOGGER.debug("Adding transaction synchronization for " + entity); LOGGER.debug("Adding transaction synchronization for " + entity);
} }
TransactionSynchronizationManager.registerSynchronization(entity.itdTransactionSynchronization); TransactionSynchronizationManager.registerSynchronization(entity.itdTransactionSynchronization);
} } else {
else {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Transaction synchronization already active for " + entity); LOGGER.debug("Transaction synchronization already active for " + entity);
} }
} }
} } else {
else {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Transaction synchronization is not active for " + entity); LOGGER.debug("Transaction synchronization is not active for " + entity);
} }
@@ -174,11 +163,14 @@ public aspect MongoDocumentBacking {
// ChangeSet-related mixins // ChangeSet-related mixins
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Introduced field // Introduced field
@Transient private ChangeSet DocumentBacked.changeSet; @Transient
private ChangeSet DocumentBacked.changeSet;
@Transient private ChangeSetPersister<?> DocumentBacked.itdChangeSetPersister; @Transient
private ChangeSetPersister<?> DocumentBacked.itdChangeSetPersister;
@Transient private ChangeSetBackedTransactionSynchronization DocumentBacked.itdTransactionSynchronization; @Transient
private ChangeSetBackedTransactionSynchronization DocumentBacked.itdTransactionSynchronization;
public void DocumentBacked.setChangeSet(ChangeSet cs) { public void DocumentBacked.setChangeSet(ChangeSet cs) {
this.changeSet = cs; this.changeSet = cs;
@@ -199,32 +191,41 @@ public aspect MongoDocumentBacking {
} }
// lifecycle methods // lifecycle methods
@javax.persistence.PostPersist public void DocumentBacked.itdPostPersist() { @javax.persistence.PostPersist
public void DocumentBacked.itdPostPersist() {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("JPA lifecycle event PrePersist: " + this.getClass().getName()); LOGGER.debug("JPA lifecycle event PrePersist: " + this.getClass().getName());
} }
registerTransactionSynchronization(this); registerTransactionSynchronization(this);
} }
@javax.persistence.PreUpdate public void DocumentBacked.itdPreUpdate() {
@javax.persistence.PreUpdate
public void DocumentBacked.itdPreUpdate() {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("JPA lifecycle event PreUpdate: " + this.getClass().getName() + " :: " + this); LOGGER.debug("JPA lifecycle event PreUpdate: " + this.getClass().getName() + " :: " + this);
} }
registerTransactionSynchronization(this); registerTransactionSynchronization(this);
} }
@javax.persistence.PostUpdate public void DocumentBacked.itdPostUpdate() {
@javax.persistence.PostUpdate
public void DocumentBacked.itdPostUpdate() {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("JPA lifecycle event PostUpdate: " + this.getClass().getName() + " :: " + this); LOGGER.debug("JPA lifecycle event PostUpdate: " + this.getClass().getName() + " :: " + this);
} }
registerTransactionSynchronization(this); registerTransactionSynchronization(this);
} }
@javax.persistence.PostRemove public void DocumentBacked.itdPostRemove() {
@javax.persistence.PostRemove
public void DocumentBacked.itdPostRemove() {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("JPA lifecycle event PostRemove: " + this.getClass().getName() + " :: " + this); LOGGER.debug("JPA lifecycle event PostRemove: " + this.getClass().getName() + " :: " + this);
} }
registerTransactionSynchronization(this); registerTransactionSynchronization(this);
removeChangeSetValues(this); removeChangeSetValues(this);
} }
@javax.persistence.PostLoad public void DocumentBacked.itdPostLoad() {
@javax.persistence.PostLoad
public void DocumentBacked.itdPostLoad() {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("JPA lifecycle event PostLoad: " + this.getClass().getName() + " :: " + this); LOGGER.debug("JPA lifecycle event PostLoad: " + this.getClass().getName() + " :: " + this);
} }
@@ -237,12 +238,11 @@ public aspect MongoDocumentBacking {
Object around(DocumentBacked entity): entityFieldGet(entity) { Object around(DocumentBacked entity): entityFieldGet(entity) {
Field f = field(thisJoinPoint); Field f = field(thisJoinPoint);
String propName = f.getName(); String propName = f.getName();
LOGGER.trace("GET " + f + " -> ChangeSet value property [" + propName LOGGER.trace("GET " + f + " -> ChangeSet value property [" + propName + "] using: " + entity.getChangeSet());
+ "] using: " + entity.getChangeSet());
if (entity.getChangeSet().getValues().get(propName) == null) { if (entity.getChangeSet().getValues().get(propName) == null) {
try { try {
this.changeSetPersister.getPersistentState(entity.getClass(), this.changeSetPersister
entity.get_persistent_id(), entity.getChangeSet()); .getPersistentState(entity.getClass(), entity.get_persistent_id(), entity.getChangeSet());
} catch (NotFoundException e) { } catch (NotFoundException e) {
} }
} }
@@ -259,8 +259,7 @@ public aspect MongoDocumentBacking {
Object around(DocumentBacked entity, Object newVal) : entityFieldSet(entity, newVal) { Object around(DocumentBacked entity, Object newVal) : entityFieldSet(entity, newVal) {
Field f = field(thisJoinPoint); Field f = field(thisJoinPoint);
String propName = f.getName(); String propName = f.getName();
LOGGER.trace("SET " + f + " -> ChangeSet number value property [" + propName LOGGER.trace("SET " + f + " -> ChangeSet number value property [" + propName + "] with value=[" + newVal + "]");
+ "] with value=[" + newVal + "]");
entity.getChangeSet().set(propName, newVal); entity.getChangeSet().set(propName, newVal);
return proceed(entity, newVal); return proceed(entity, newVal);
} }

View File

@@ -24,8 +24,6 @@ import java.lang.annotation.Target;
* @author Thomas Risberg * @author Thomas Risberg
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ @Target({ ElementType.FIELD })
ElementType.FIELD
})
public @interface RelatedDocument { public @interface RelatedDocument {
} }

View File

@@ -90,8 +90,7 @@ public class CrossStoreMongoTests {
Assert.assertNotNull(found); Assert.assertNotNull(found);
Assert.assertEquals(Long.valueOf(1), found.getId()); Assert.assertEquals(Long.valueOf(1), found.getId());
Assert.assertNotNull(found.getResume()); Assert.assertNotNull(found.getResume());
Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " + "VMware, Developer, 2007-", found.getResume().getJobs());
+ "VMware, Developer, 2007-", found.getResume().getJobs());
found.getResume().addJob("SpringDeveloper.com, Consultant, 2005-2006"); found.getResume().addJob("SpringDeveloper.com, Consultant, 2005-2006");
found.setAge(44); found.setAge(44);
} }
@@ -106,8 +105,7 @@ public class CrossStoreMongoTests {
Assert.assertNotNull(found); Assert.assertNotNull(found);
Assert.assertEquals(Long.valueOf(1), found.getId()); Assert.assertEquals(Long.valueOf(1), found.getId());
Assert.assertNotNull(found.getResume()); Assert.assertNotNull(found.getResume());
Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " + "VMware, Developer, 2007-" + "; "
+ "VMware, Developer, 2007-" + "; "
+ "SpringDeveloper.com, Consultant, 2005-2006", found.getResume().getJobs()); + "SpringDeveloper.com, Consultant, 2005-2006", found.getResume().getJobs());
} }

View File

@@ -23,8 +23,7 @@ public class Address {
private String state; private String state;
private String zip; private String zip;
public Address(Integer streetNumber, String streetName, String city, public Address(Integer streetNumber, String streetName, String city, String state, String zip) {
String state, String zip) {
super(); super();
this.streetNumber = streetNumber; this.streetNumber = streetNumber;
this.streetName = streetName; this.streetName = streetName;
@@ -36,34 +35,41 @@ public class Address {
public Integer getStreetNumber() { public Integer getStreetNumber() {
return streetNumber; return streetNumber;
} }
public void setStreetNumber(Integer streetNumber) { public void setStreetNumber(Integer streetNumber) {
this.streetNumber = streetNumber; this.streetNumber = streetNumber;
} }
public String getStreetName() { public String getStreetName() {
return streetName; return streetName;
} }
public void setStreetName(String streetName) { public void setStreetName(String streetName) {
this.streetName = streetName; this.streetName = streetName;
} }
public String getCity() { public String getCity() {
return city; return city;
} }
public void setCity(String city) { public void setCity(String city) {
this.city = city; this.city = city;
} }
public String getState() { public String getState() {
return state; return state;
} }
public void setState(String state) { public void setState(String state) {
this.state = state; this.state = state;
} }
public String getZip() { public String getZip() {
return zip; return zip;
} }
public void setZip(String zip) { public void setZip(String zip) {
this.zip = zip; this.zip = zip;
} }
} }

View File

@@ -1,11 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId> <artifactId>spring-data-mongodb-parent</artifactId>
<version>1.0.0.RELEASE</version> <version>1.1.0.M1</version>
<relativePath>../spring-data-mongodb-parent/pom.xml</relativePath> <relativePath>../spring-data-mongodb-parent/pom.xml</relativePath>
</parent> </parent>
<artifactId>spring-data-mongodb-log4j</artifactId> <artifactId>spring-data-mongodb-log4j</artifactId>
@@ -28,45 +27,7 @@
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<exclusions> <version>${log4j.version}</version>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -1,63 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId> <artifactId>spring-data-mongodb-parent</artifactId>
<name>Spring Data MongoDB Parent</name> <name>Spring Data MongoDB Parent</name>
<url>http://www.springsource.org/spring-data/mongodb</url> <url>http://www.springsource.org/spring-data/mongodb</url>
<version>1.0.0.RELEASE</version> <version>1.1.0.M1</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- versions for commonly-used dependencies --> <!-- versions for commonly-used dependencies -->
<junit.version>4.8.1</junit.version> <junit.version>4.8.1</junit.version>
<log4j.version>1.2.15</log4j.version> <log4j.version>1.2.16</log4j.version>
<org.mockito.version>1.8.4</org.mockito.version> <org.mockito.version>1.8.4</org.mockito.version>
<org.slf4j.version>1.5.10</org.slf4j.version> <org.slf4j.version>1.6.1</org.slf4j.version>
<org.codehaus.jackson.version>1.6.1</org.codehaus.jackson.version> <org.codehaus.jackson.version>1.6.1</org.codehaus.jackson.version>
<org.springframework.version.30>3.0.7.RELEASE</org.springframework.version.30> <org.springframework.version.30>3.0.7.RELEASE</org.springframework.version.30>
<org.springframework.version.40>4.0.0.RELEASE</org.springframework.version.40> <org.springframework.version.40>4.0.0.RELEASE</org.springframework.version.40>
<org.springframework.version.range>[${org.springframework.version.30}, ${org.springframework.version.40})</org.springframework.version.range> <org.springframework.version.range>[${org.springframework.version.30}, ${org.springframework.version.40})</org.springframework.version.range>
<data.commons.version>1.2.0.RELEASE</data.commons.version> <data.commons.version>1.3.0.RC2</data.commons.version>
<aspectj.version>1.6.11.RELEASE</aspectj.version> <aspectj.version>1.6.11.RELEASE</aspectj.version>
</properties> </properties>
<profiles>
<profile>
<id>strict</id>
<properties>
<maven.test.failure.ignore>false</maven.test.failure.ignore>
</properties>
</profile>
<profile>
<id>fast</id>
<properties>
<maven.test.skip>true</maven.test.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>
</profile>
<profile>
<id>staging</id>
<distributionManagement>
<site>
<id>spring-site-staging</id>
<url>file:///${java.io.tmpdir}/spring-data/mongodb/docs</url>
</site>
<repository>
<id>spring-milestone-staging</id>
<url>file:///${java.io.tmpdir}/spring-data/mongodb/milestone</url>
</repository>
<snapshotRepository>
<id>spring-snapshot-staging</id>
<url>file:///${java.io.tmpdir}/spring-data/mongodb/snapshot</url>
</snapshotRepository>
</distributionManagement>
</profile>
<profile>
<id>bootstrap</id>
<!-- TODO: move the repositories in here before release -->
</profile>
</profiles>
<distributionManagement> <distributionManagement>
<!-- see 'staging' profile for dry-run deployment settings --> <!-- see 'staging' profile for dry-run deployment settings -->
<downloadUrl>http://www.springsource.com/download/community <downloadUrl>http://www.springsource.com/download/community
@@ -65,13 +29,13 @@
<site> <site>
<id>static.springframework.org</id> <id>static.springframework.org</id>
<url> <url>
scp://static.springframework.org/var/www/domains/springframework.org/static/htdocs/spring-data/data-mongodb/docs/${project.version} scp://static.springframework.org/var/www/domains/springframework.org/static/htdocs/spring-data/data-mongodb/snapshot-site
</url> </url>
</site> </site>
<repository> <repository>
<id>spring-release</id> <id>spring-milestone</id>
<name>Spring Release Repository</name> <name>Spring Milestone Repository</name>
<url>s3://maven.springframework.org/release</url> <url>s3://maven.springframework.org/milestone</url>
</repository> </repository>
<snapshotRepository> <snapshotRepository>
<id>spring-snapshot</id> <id>spring-snapshot</id>
@@ -79,133 +43,10 @@
<url>s3://maven.springframework.org/snapshot</url> <url>s3://maven.springframework.org/snapshot</url>
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
<dependencyManagement>
<!--
inheritable <dependency> declarations for child poms. children still
must explicitly declare the groupId/artifactId of these dependencies
in order for them to show up on the classpath, but metadata like
<version> and <scope> are inherited, which cuts down on verbosity.
see
http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-dep-manage.html
-->
<dependencies> <dependencies>
<!-- Spring --> <!-- Test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version.range}</version>
<scope>test</scope>
</dependency>
<!-- Spring Data -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>${data.commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-aspects</artifactId>
<version>${data.commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchdb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId> <artifactId>mockito-all</artifactId>
@@ -213,6 +54,13 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@@ -220,22 +68,43 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> <dependency>
</dependencyManagement> <groupId>joda-time</groupId>
<dependencies> <artifactId>joda-time</artifactId>
<!-- <version>1.6</version>
dependency definitions to be inherited by child poms. any <scope>test</scope>
<dependency> declarations here will automatically show up on child </dependency>
project classpaths. only items that are truly common across all
projects (modules and samples) should go here. otherwise, consider <dependency>
<dependencyManagement> above <groupId>org.slf4j</groupId>
--> <artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>${log4j.version}</version> <version>${log4j.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version.range}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -317,6 +186,9 @@
<includes> <includes>
<include>**/*Tests.java</include> <include>**/*Tests.java</include>
</includes> </includes>
<excludes>
<exclude>**/PerformanceTests.java</exclude>
</excludes>
<junitArtifactName>junit:junit</junitArtifactName> <junitArtifactName>junit:junit</junitArtifactName>
</configuration> </configuration>
</plugin> </plugin>
@@ -369,22 +241,14 @@
</build> </build>
<pluginRepositories> <pluginRepositories>
<pluginRepository> <pluginRepository>
<!-- necessary for bundlor and utils --> <id>spring-plugins-release</id>
<id>repository.plugin.springsource.release</id> <url>http://repo.springsource.org/plugins-release</url>
<name>SpringSource Maven Repository</name>
<url>http://repository.springsource.com/maven/bundles/release</url>
</pluginRepository>
<pluginRepository>
<id>repository.springframework.maven.release</id>
<name>Spring Framework Maven Release Repository</name>
<url>http://repo.springsource.org/release</url>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
<repositories> <repositories>
<repository> <repository>
<id>repository.springframework.maven.release</id> <id>spring-libs-snapshot</id>
<name>Spring Framework Maven Release Repository</name> <url>http://repo.springsource.org/libs-snapshot</url>
<url>http://repo.springsource.org/release</url>
</repository> </repository>
</repositories> </repositories>
<reporting> <reporting>

View File

@@ -49,6 +49,12 @@
</element> </element>
<dependency type="AllowedDependency" toName="Project|spring-data-mongodb::Layer|Core"/> <dependency type="AllowedDependency" toName="Project|spring-data-mongodb::Layer|Core"/>
</element> </element>
<element name="GridFS" type="Layer">
<element name="Assignment" type="TypeFilter">
<element name="**.gridfs.**" type="IncludeTypePattern"/>
</element>
<dependency type="AllowedDependency" toName="Project|spring-data-mongodb::Layer|Core"/>
</element>
<element name="Core" type="Layer"> <element name="Core" type="Layer">
<element name="Assignment" type="TypeFilter"> <element name="Assignment" type="TypeFilter">
<element name="**.core.**" type="IncludeTypePattern"/> <element name="**.core.**" type="IncludeTypePattern"/>

View File

@@ -1,11 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId> <artifactId>spring-data-mongodb-parent</artifactId>
<version>1.0.0.RELEASE</version> <version>1.1.0.M1</version>
<relativePath>../spring-data-mongodb-parent/pom.xml</relativePath> <relativePath>../spring-data-mongodb-parent/pom.xml</relativePath>
</parent> </parent>
<artifactId>spring-data-mongodb</artifactId> <artifactId>spring-data-mongodb</artifactId>
@@ -13,24 +12,45 @@
<properties> <properties>
<mongo.version>2.7.1</mongo.version> <mongo.version>2.7.1</mongo.version>
<querydsl.version>2.3.0</querydsl.version> <querydsl.version>2.5.0</querydsl.version>
<cdi.version>1.0</cdi.version>
<validation.version>1.0.0.GA</validation.version>
<webbeans.version>1.1.3</webbeans.version>
</properties> </properties>
<dependencies> <dependencies>
<!-- Spring --> <!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId> <artifactId>spring-beans</artifactId>
<version>${org.springframework.version.range}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version.range}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId> <artifactId>spring-expression</artifactId>
</dependency> <version>${org.springframework.version.range}</version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<!-- Spring Data --> <!-- Spring Data -->
@@ -76,58 +96,80 @@
<dependency> <dependency>
<groupId>javax.annotation</groupId> <groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId> <artifactId>jsr250-api</artifactId>
<version>1.0</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- Test dependencies --> <!-- CDI -->
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>javax.enterprise</groupId>
<artifactId>mockito-all</artifactId> <artifactId>cdi-api</artifactId>
<version>${cdi.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>${cdi.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.apache.openwebbeans.test</groupId>
<artifactId>hamcrest-all</artifactId> <artifactId>cditest-owb</artifactId>
<version>1.1</version> <version>${webbeans.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>javax.servlet</groupId>
<artifactId>junit</artifactId> <artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- JSR 303 Validation -->
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>javax.validation</groupId>
<artifactId>joda-time</artifactId> <artifactId>validation-api</artifactId>
<version>1.6</version> <version>${validation.version}</version>
<scope>test</scope> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.hibernate</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>hibernate-validator</artifactId>
<scope>test</scope> <version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<id>performance-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<includes>
<include>**/PerformanceTests.java</include>
</includes>
<excludes>
<exclude>none</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@@ -155,14 +197,4 @@
</plugins> </plugins>
</build> </build>
<repositories>
<repository>
<id>querydsl</id>
<name>Mysema QueryDsl</name>
<url>http://source.mysema.com/maven2/releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project> </project>

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,42 +16,51 @@
package org.springframework.data.mongodb; package org.springframework.data.mongodb;
import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.data.authentication.UserCredentials;
/**
* Exception being thrown in case we cannot connect to a MongoDB instance.
*
* @author Oliver Gierke
*/
public class CannotGetMongoDbConnectionException extends DataAccessResourceFailureException { public class CannotGetMongoDbConnectionException extends DataAccessResourceFailureException {
private String username; private final UserCredentials credentials;
private final String database;
private char[] password;
private String database;
private static final long serialVersionUID = 1172099106475265589L; private static final long serialVersionUID = 1172099106475265589L;
public CannotGetMongoDbConnectionException(String msg, Throwable cause) { public CannotGetMongoDbConnectionException(String msg, Throwable cause) {
super(msg, cause); super(msg, cause);
this.database = null;
this.credentials = UserCredentials.NO_CREDENTIALS;
} }
public CannotGetMongoDbConnectionException(String msg) { public CannotGetMongoDbConnectionException(String msg) {
super(msg); this(msg, null, UserCredentials.NO_CREDENTIALS);
} }
public CannotGetMongoDbConnectionException(String msg, String database, String username, char[] password) { public CannotGetMongoDbConnectionException(String msg, String database, UserCredentials credentials) {
super(msg); super(msg);
this.username = username;
this.password = password == null ? null : password.clone();
this.database = database; this.database = database;
this.credentials = credentials;
} }
public String getUsername() { /**
return username; * Returns the {@link UserCredentials} that were used when trying to connect to the MongoDB instance.
} *
* @return
public char[] getPassword() { */
return password; public UserCredentials getCredentials() {
return this.credentials;
} }
/**
* Returns the name of the database trying to be accessed.
*
* @return
*/
public String getDatabase() { public String getDatabase() {
return database; return database;
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,88 +15,176 @@
*/ */
package org.springframework.data.mongodb.config; package org.springframework.data.mongodb.config;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import com.mongodb.Mongo;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent; import org.springframework.data.annotation.Persistent;
import org.springframework.data.authentication.UserCredentials; import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.mongodb.Mongo;
/**
* Base class for Spring Data Mongo configuration using JavaConfig.
*
* @author Mark Pollack
* @author Oliver Gierke
*/
@Configuration @Configuration
public abstract class AbstractMongoConfiguration { public abstract class AbstractMongoConfiguration {
public abstract String getDatabaseName(); /**
* Return the name of the database to connect to.
*
* @return must not be {@literal null}.
*/
protected abstract String getDatabaseName();
/**
* Return the {@link Mongo} instance to connect to.
*
* @return
* @throws Exception
*/
@Bean @Bean
public abstract Mongo mongo() throws Exception; public abstract Mongo mongo() throws Exception;
/**
* Creates a {@link MongoTemplate}.
*
* @return
* @throws Exception
*/
@Bean @Bean
public MongoTemplate mongoTemplate() throws Exception { public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory(), mappingMongoConverter()); return new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
} }
/**
* Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link Mongo} instance
* configured in {@link #mongo()}.
*
* @see #mongo()
* @see #mongoTemplate()
* @return
* @throws Exception
*/
@Bean @Bean
public MongoDbFactory mongoDbFactory() throws Exception { public SimpleMongoDbFactory mongoDbFactory() throws Exception {
if (getUserCredentials() == null) {
UserCredentials creadentials = getUserCredentials();
if (creadentials == null) {
return new SimpleMongoDbFactory(mongo(), getDatabaseName()); return new SimpleMongoDbFactory(mongo(), getDatabaseName());
} else { } else {
return new SimpleMongoDbFactory(mongo(), getDatabaseName(), getUserCredentials()); return new SimpleMongoDbFactory(mongo(), getDatabaseName(), creadentials);
} }
} }
public String getMappingBasePackage() { /**
return ""; * Return the base package to scan for mapped {@link Document}s.
} *
* @return
public UserCredentials getUserCredentials() { */
protected String getMappingBasePackage() {
return null; return null;
} }
/**
* Return {@link UserCredentials} to be used when connecting to the MongoDB instance or {@literal null} if none shall
* be used.
*
* @return
*/
protected UserCredentials getUserCredentials() {
return null;
}
/**
* Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package.
*
* @see #getMappingBasePackage()
* @return
* @throws ClassNotFoundException
*/
@Bean @Bean
public MongoMappingContext mongoMappingContext() throws ClassNotFoundException, LinkageError { public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
MongoMappingContext mappingContext = new MongoMappingContext(); MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setInitialEntitySet(getInitialEntitySet());
mappingContext.setSimpleTypeHolder(customConversions().getSimpleTypeHolder());
mappingContext.afterPropertiesSet();
return mappingContext;
}
/**
* Register custom {@link Converter}s in a {@link CustomConversions} object if required. These
* {@link CustomConversions} will be registered with the {@link #mappingMongoConverter()} and
* {@link #mongoMappingContext()}. Returns an empty {@link CustomConversions} instance by default.
*
* @return must not be {@literal null}.
*/
@Bean
public CustomConversions customConversions() {
return new CustomConversions(Collections.emptyList());
}
/**
* Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and
* {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied.
*
* @see #customConversions()
* @see #mongoMappingContext()
* @see #mongoDbFactory()
* @return
* @throws Exception
*/
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), mongoMappingContext());
converter.setCustomConversions(customConversions());
return converter;
}
/**
* Scans the mapping base package for classes annotated with {@link Document}.
*
* @see #getMappingBasePackage()
* @return
* @throws ClassNotFoundException
*/
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
String basePackage = getMappingBasePackage(); String basePackage = getMappingBasePackage();
Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
if (StringUtils.hasText(basePackage)) { if (StringUtils.hasText(basePackage)) {
ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider( ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(
false); false);
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class)); componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class)); componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), mappingContext.getClass() initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
.getClassLoader())); AbstractMongoConfiguration.class.getClassLoader()));
} }
mappingContext.setInitialEntitySet(initialEntitySet);
}
return mappingContext;
} }
@Bean return initialEntitySet;
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), mongoMappingContext());
afterMappingMongoConverterCreation(converter);
return converter;
}
/**
* Hook that allows post-processing after the MappingMongoConverter has been successfully created.
*
* @param converter
*/
protected void afterMappingMongoConverterCreation(MappingMongoConverter converter) {
} }
} }

View File

@@ -25,4 +25,5 @@ public abstract class BeanNames {
static final String INDEX_HELPER = "indexCreationHelper"; static final String INDEX_HELPER = "indexCreationHelper";
static final String MONGO = "mongo"; static final String MONGO = "mongo";
static final String DB_FACTORY = "mongoDbFactory"; static final String DB_FACTORY = "mongoDbFactory";
static final String VALIDATING_EVENT_LISTENER = "validatingMongoEventListener";
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 by the original author(s). * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.config; package org.springframework.data.mongodb.config;
import static org.springframework.data.mongodb.config.BeanNames.*; import static org.springframework.data.mongodb.config.BeanNames.*;
@@ -30,12 +29,14 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.ManagedList; import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedSet; import org.springframework.beans.factory.support.ManagedSet;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
@@ -52,18 +53,25 @@ import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element; import org.w3c.dom.Element;
/** /**
* @author Jon Brisbin <jbrisbin@vmware.com> * Bean definition parser for the {@code mapping-converter} element.
*
* @author Jon Brisbin
* @author Oliver Gierke * @author Oliver Gierke
* @author Maciej Walkowiak
*/ */
public class MappingMongoConverterParser extends AbstractBeanDefinitionParser { public class MappingMongoConverterParser extends AbstractBeanDefinitionParser {
private static final String BASE_PACKAGE = "base-package"; private static final String BASE_PACKAGE = "base-package";
private static final boolean jsr303Present = ClassUtils.isPresent("javax.validation.Validator",
MappingMongoConverterParser.class.getClassLoader());
@Override @Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
@@ -107,9 +115,53 @@ public class MappingMongoConverterParser extends AbstractBeanDefinitionParser {
registry.registerBeanDefinition(INDEX_HELPER, indexHelperBuilder.getBeanDefinition()); registry.registerBeanDefinition(INDEX_HELPER, indexHelperBuilder.getBeanDefinition());
} }
BeanDefinition validatingMongoEventListener = potentiallyCreateValidatingMongoEventListener(element, parserContext);
if (validatingMongoEventListener != null) {
registry.registerBeanDefinition(VALIDATING_EVENT_LISTENER, validatingMongoEventListener);
}
return converterBuilder.getBeanDefinition(); return converterBuilder.getBeanDefinition();
} }
private BeanDefinition potentiallyCreateValidatingMongoEventListener(Element element, ParserContext parserContext) {
String disableValidation = element.getAttribute("disable-validation");
boolean validationDisabled = StringUtils.hasText(disableValidation) && Boolean.valueOf(disableValidation);
if (!validationDisabled) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
RuntimeBeanReference validator = getValidator(builder, parserContext);
if (validator != null) {
builder.getRawBeanDefinition().setBeanClass(ValidatingMongoEventListener.class);
builder.addConstructorArgValue(validator);
return builder.getBeanDefinition();
}
}
return null;
}
private RuntimeBeanReference getValidator(Object source, ParserContext parserContext) {
if (!jsr303Present) {
return null;
}
RootBeanDefinition validatorDef = new RootBeanDefinition(
"org.springframework.validation.beanvalidation.LocalValidatorFactoryBean");
validatorDef.setSource(source);
validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
String validatorName = parserContext.getReaderContext().registerWithGeneratedName(validatorDef);
parserContext.registerComponent(new BeanComponentDefinition(validatorDef, validatorName));
return new RuntimeBeanReference(validatorName);
}
private String potentiallyCreateMappingContext(Element element, ParserContext parserContext, private String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
BeanDefinition conversionsDefinition) { BeanDefinition conversionsDefinition) {
@@ -158,8 +210,8 @@ public class MappingMongoConverterParser extends AbstractBeanDefinitionParser {
String packageToScan = customerConvertersElement.getAttribute(BASE_PACKAGE); String packageToScan = customerConvertersElement.getAttribute(BASE_PACKAGE);
if (StringUtils.hasText(packageToScan)) { if (StringUtils.hasText(packageToScan)) {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.addExcludeFilter(new NegatingFilter(new AssignableTypeFilter(Converter.class), new AssignableTypeFilter( provider.addExcludeFilter(new NegatingFilter(new AssignableTypeFilter(Converter.class),
GenericConverter.class))); new AssignableTypeFilter(GenericConverter.class)));
for (BeanDefinition candidate : provider.findCandidateComponents(packageToScan)) { for (BeanDefinition candidate : provider.findCandidateComponents(packageToScan)) {
converterBeans.add(candidate); converterBeans.add(candidate);

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012 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.mongodb.config;
import org.springframework.core.convert.converter.Converter;
import com.mongodb.WriteConcern;
/**
* Converter to create {@link WriteConcern} instances from String representations.
*
* @author Oliver Gierke
*/
public class StringToWriteConcernConverter implements Converter<String, WriteConcern> {
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
public WriteConcern convert(String source) {
WriteConcern writeConcern = WriteConcern.valueOf(source);
return writeConcern != null ? writeConcern : new WriteConcern(source);
}
}

View File

@@ -16,14 +16,11 @@
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexDefinition;
import org.springframework.data.mongodb.core.index.IndexField;
import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.data.mongodb.core.query.Order; import org.springframework.data.mongodb.core.query.Order;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -123,25 +120,27 @@ public class DefaultIndexOperations implements IndexOperations {
return getIndexData(dbObjectList); return getIndexData(dbObjectList);
} }
@SuppressWarnings("unchecked")
private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) { private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {
List<IndexInfo> indexInfoList = new ArrayList<IndexInfo>(); List<IndexInfo> indexInfoList = new ArrayList<IndexInfo>();
for (DBObject ix : dbObjectList) { for (DBObject ix : dbObjectList) {
Map<String, Order> keyOrderMap = new LinkedHashMap<String, Order>();
DBObject keyDbObject = (DBObject) ix.get("key"); DBObject keyDbObject = (DBObject) ix.get("key");
Iterator<?> entries = keyDbObject.toMap().entrySet().iterator(); int numberOfElements = keyDbObject.keySet().size();
while (entries.hasNext()) { List<IndexField> indexFields = new ArrayList<IndexField>(numberOfElements);
Entry<Object, Integer> thisEntry = (Entry<Object, Integer>) entries.next();
String key = thisEntry.getKey().toString(); for (String key : keyDbObject.keySet()) {
int value = thisEntry.getValue();
if (value == 1) { Object value = keyDbObject.get(key);
keyOrderMap.put(key, Order.ASCENDING);
} else { if (Integer.valueOf(1).equals(value)) {
keyOrderMap.put(key, Order.DESCENDING); indexFields.add(IndexField.create(key, Order.ASCENDING));
} else if (Integer.valueOf(-1).equals(value)) {
indexFields.add(IndexField.create(key, Order.DESCENDING));
} else if ("2d".equals(value)) {
indexFields.add(IndexField.geo(key));
} }
} }
@@ -151,12 +150,11 @@ public class DefaultIndexOperations implements IndexOperations {
boolean dropDuplicates = ix.containsField("dropDups") ? (Boolean) ix.get("dropDups") : false; boolean dropDuplicates = ix.containsField("dropDups") ? (Boolean) ix.get("dropDups") : false;
boolean sparse = ix.containsField("sparse") ? (Boolean) ix.get("sparse") : false; boolean sparse = ix.containsField("sparse") ? (Boolean) ix.get("sparse") : false;
indexInfoList.add(new IndexInfo(keyOrderMap, name, unique, dropDuplicates, sparse)); indexInfoList.add(new IndexInfo(indexFields, name, unique, dropDuplicates, sparse));
} }
return indexInfoList; return indexInfoList;
} }
}); });
} }
} }

View File

@@ -21,12 +21,13 @@ import com.mongodb.DBObject;
import com.mongodb.MongoException; import com.mongodb.MongoException;
/** /**
* An interface used by {@link MongoTemplate} for processing documents returned from a MongoDB query on a per-document basis. * An interface used by {@link MongoTemplate} for processing documents returned from a MongoDB query on a per-document
* Implementations of this interface perform the actual work of prcoessing each document but don't need to worry about * basis. Implementations of this interface perform the actual work of prcoessing each document but don't need to worry
* exception handling. {@MongoException}s will be caught and translated by the calling MongoTemplate * about exception handling. {@MongoException}s will be caught and translated by the calling
* MongoTemplate
* *
* An DocumentCallbackHandler is typically stateful: It keeps the result state within the object, to be available later for later * An DocumentCallbackHandler is typically stateful: It keeps the result state within the object, to be available later
* inspection. * for later inspection.
* *
* @author Mark Pollack * @author Mark Pollack
* *

View File

@@ -59,6 +59,4 @@ public class FindAndModifyOptions {
return remove; return remove;
} }
} }

View File

@@ -20,6 +20,7 @@ import java.util.List;
import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexDefinition;
import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.data.mongodb.core.index.IndexInfo;
/** /**
* Index operations on a collection. * Index operations on a collection.
* *

View File

@@ -23,12 +23,14 @@ import com.mongodb.WriteConcern;
* Represents an action taken against the collection. Used by {@link WriteConcernResolver} to determine a custom * Represents an action taken against the collection. Used by {@link WriteConcernResolver} to determine a custom
* WriteConcern based on this information. * WriteConcern based on this information.
* *
* Properties that will always be not-null are collectionName and defaultWriteConcern. * Properties that will always be not-null are collectionName and defaultWriteConcern. The EntityClass is null only for
* The EntityClass is null only for the MongoActionOperaton.INSERT_LIST. * the MongoActionOperaton.INSERT_LIST.
* *
* INSERT, SAVE have null query, * <ul>
* REMOVE has null document * <li>INSERT, SAVE have null query</li>
* INSERT_LIST has null entityClass, document, and query. * <li>REMOVE has null document</li>
* <li>INSERT_LIST has null entityClass, document, and query</li>
* </ul>
* *
* @author Mark Pollack * @author Mark Pollack
* *
@@ -49,6 +51,7 @@ public class MongoAction {
/** /**
* Create an instance of a MongoAction * Create an instance of a MongoAction
*
* @param defaultWriteConcern the default write concern * @param defaultWriteConcern the default write concern
* @param mongoActionOperation action being taken against the collection * @param mongoActionOperation action being taken against the collection
* @param collectionName the collection name * @param collectionName the collection name
@@ -91,6 +94,4 @@ public class MongoAction {
return document; return document;
} }
} }

View File

@@ -16,8 +16,8 @@
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
/** /**
* Enumeration for operations on a collection. Used with {@link MongoAction} to help determine the * Enumeration for operations on a collection. Used with {@link MongoAction} to help determine the WriteConcern to use
* WriteConcern to use for a given mutating operation * for a given mutating operation
* *
* @author Mark Pollack * @author Mark Pollack
* @see MongoAction * @see MongoAction
@@ -25,9 +25,5 @@ package org.springframework.data.mongodb.core;
*/ */
public enum MongoActionOperation { public enum MongoActionOperation {
REMOVE, REMOVE, UPDATE, INSERT, INSERT_LIST, SAVE
UPDATE,
INSERT,
INSERT_LIST,
SAVE
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,32 +15,28 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import com.mongodb.DB; import org.springframework.data.authentication.UserCredentials;
import com.mongodb.Mongo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.Assert;
import com.mongodb.DB;
import com.mongodb.Mongo;
/** /**
* Mongo server administration exposed via JMX annotations * Mongo server administration exposed via JMX annotations
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Mongo Admin Operations") @ManagedResource(description = "Mongo Admin Operations")
public class MongoAdmin implements MongoAdminOperations { public class MongoAdmin implements MongoAdminOperations {
/** private final Mongo mongo;
* Logger available to subclasses
*/
protected final Log logger = LogFactory.getLog(getClass());
private Mongo mongo;
private String username; private String username;
private String password; private String password;
public MongoAdmin(Mongo mongo) { public MongoAdmin(Mongo mongo) {
Assert.notNull(mongo);
this.mongo = mongo; this.mongo = mongo;
} }
@@ -83,11 +79,10 @@ public class MongoAdmin implements MongoAdminOperations {
* @param password The password to use * @param password The password to use
*/ */
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
DB getDB(String databaseName) { DB getDB(String databaseName) {
return MongoDbUtils.getDB(mongo, databaseName, username, password == null ? null : password.toCharArray()); return MongoDbUtils.getDB(mongo, databaseName, new UserCredentials(username, password));
} }
} }

View File

@@ -15,14 +15,16 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import com.mongodb.DB; import org.slf4j.Logger;
import com.mongodb.Mongo; import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log; import org.springframework.data.authentication.UserCredentials;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.CannotGetMongoDbConnectionException; import org.springframework.data.mongodb.CannotGetMongoDbConnectionException;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.DB;
import com.mongodb.Mongo;
/** /**
* Helper class featuring helper methods for internal MongoDb classes. * Helper class featuring helper methods for internal MongoDb classes.
* <p/> * <p/>
@@ -36,7 +38,7 @@ import org.springframework.util.Assert;
*/ */
public abstract class MongoDbUtils { public abstract class MongoDbUtils {
private static final Log LOGGER = LogFactory.getLog(MongoDbUtils.class); private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbUtils.class);
/** /**
* Private constructor to prevent instantiation. * Private constructor to prevent instantiation.
@@ -48,29 +50,32 @@ public abstract class MongoDbUtils {
/** /**
* Obtains a {@link DB} connection for the given {@link Mongo} instance and database name * Obtains a {@link DB} connection for the given {@link Mongo} instance and database name
* *
* @param mongo The {@link Mongo} instance * @param mongo the {@link Mongo} instance, must not be {@literal null}.
* @param databaseName The database name * @param databaseName the database name, must not be {@literal null} or empty.
* @return The {@link DB} connection * @return the {@link DB} connection
*/ */
public static DB getDB(Mongo mongo, String databaseName) { public static DB getDB(Mongo mongo, String databaseName) {
return doGetDB(mongo, databaseName, null, null, true); return doGetDB(mongo, databaseName, UserCredentials.NO_CREDENTIALS, true);
} }
/** /**
* Obtains a {@link DB} connection for the given {@link Mongo} instance and database name * Obtains a {@link DB} connection for the given {@link Mongo} instance and database name
* *
* @param mongo The {@link Mongo} instance * @param mongo the {@link Mongo} instance, must not be {@literal null}.
* @param databaseName The database name * @param databaseName the database name, must not be {@literal null} or empty.
* @param username The username to authenticate with * @param credentials the credentials to use, must not be {@literal null}.
* @param password The password to authenticate with * @return the {@link DB} connection
* @return The {@link DB} connection
*/ */
public static DB getDB(Mongo mongo, String databaseName, String username, char[] password) { public static DB getDB(Mongo mongo, String databaseName, UserCredentials credentials) {
return doGetDB(mongo, databaseName, username, password, true);
Assert.notNull(mongo, "No Mongo instance specified!");
Assert.hasText(databaseName, "Database name must be given!");
Assert.notNull(credentials, "Credentials must not be null, use UserCredentials.NO_CREDENTIALS!");
return doGetDB(mongo, databaseName, credentials, true);
} }
public static DB doGetDB(Mongo mongo, String databaseName, String username, char[] password, boolean allowCreate) { private static DB doGetDB(Mongo mongo, String databaseName, UserCredentials credentials, boolean allowCreate) {
Assert.notNull(mongo, "No Mongo instance specified");
DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo); DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo);
if (dbHolder != null && !dbHolder.isEmpty()) { if (dbHolder != null && !dbHolder.isEmpty()) {
@@ -93,13 +98,15 @@ public abstract class MongoDbUtils {
LOGGER.trace("Getting Mongo Database name=[" + databaseName + "]"); LOGGER.trace("Getting Mongo Database name=[" + databaseName + "]");
DB db = mongo.getDB(databaseName); DB db = mongo.getDB(databaseName);
boolean credentialsGiven = username != null && password != null; boolean credentialsGiven = credentials.hasUsername() && credentials.hasPassword();
if (credentialsGiven && !db.isAuthenticated()) { if (credentialsGiven && !db.isAuthenticated()) {
// Note, can only authenticate once against the same com.mongodb.DB object. // Note, can only authenticate once against the same com.mongodb.DB object.
if (!db.authenticate(username, password)) { String username = credentials.getUsername();
String password = credentials.hasPassword() ? credentials.getPassword() : null;
if (!db.authenticate(username, password == null ? null : password.toCharArray())) {
throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName
+ "], username = [" + username + "], password = [" + new String(password) + "]", databaseName, username, + "], username = [" + username + "], password = [" + password + "]", databaseName, credentials);
password);
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log; import org.springframework.beans.factory.DisposableBean;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.CannotGetMongoDbConnectionException; import org.springframework.data.mongodb.CannotGetMongoDbConnectionException;
@@ -39,12 +38,10 @@ import com.mongodb.WriteConcern;
* @author Oliver Gierke * @author Oliver Gierke
* @since 1.0 * @since 1.0
*/ */
public class MongoFactoryBean implements FactoryBean<Mongo>, PersistenceExceptionTranslator { public class MongoFactoryBean implements FactoryBean<Mongo>, InitializingBean, DisposableBean,
PersistenceExceptionTranslator {
/** private Mongo mongo;
* Logger, available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
private MongoOptions mongoOptions; private MongoOptions mongoOptions;
private String host; private String host;
@@ -89,34 +86,7 @@ public class MongoFactoryBean implements FactoryBean<Mongo>, PersistenceExceptio
} }
public Mongo getObject() throws Exception { public Mongo getObject() throws Exception {
Mongo mongo;
ServerAddress defaultOptions = new ServerAddress();
if (mongoOptions == null) {
mongoOptions = new MongoOptions();
}
if (replicaPair != null) {
if (replicaPair.size() < 2) {
throw new CannotGetMongoDbConnectionException("A replica pair must have two server entries");
}
mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions);
} else if (replicaSetSeeds != null) {
mongo = new Mongo(replicaSetSeeds, mongoOptions);
} else {
String mongoHost = host != null ? host : defaultOptions.getHost();
mongo = port != null ? new Mongo(new ServerAddress(mongoHost, port), mongoOptions) : new Mongo(mongoHost,
mongoOptions);
}
if (writeConcern != null) {
mongo.setWriteConcern(writeConcern);
}
return mongo; return mongo;
} }
/* /*
@@ -142,4 +112,45 @@ public class MongoFactoryBean implements FactoryBean<Mongo>, PersistenceExceptio
public DataAccessException translateExceptionIfPossible(RuntimeException ex) { public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
return exceptionTranslator.translateExceptionIfPossible(ex); return exceptionTranslator.translateExceptionIfPossible(ex);
} }
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Mongo mongo;
ServerAddress defaultOptions = new ServerAddress();
if (mongoOptions == null) {
mongoOptions = new MongoOptions();
}
if (replicaPair != null) {
if (replicaPair.size() < 2) {
throw new CannotGetMongoDbConnectionException("A replica pair must have two server entries");
}
mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions);
} else if (replicaSetSeeds != null) {
mongo = new Mongo(replicaSetSeeds, mongoOptions);
} else {
String mongoHost = host != null ? host : defaultOptions.getHost();
mongo = port != null ? new Mongo(new ServerAddress(mongoHost, port), mongoOptions) : new Mongo(mongoHost,
mongoOptions);
}
if (writeConcern != null) {
mongo.setWriteConcern(writeConcern);
}
this.mongo = mongo;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.DisposableBean#destroy()
*/
public void destroy() throws Exception {
this.mongo.close();
}
} }

View File

@@ -1,11 +1,7 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2012 the original author or authors.
* *
* Licensed under t * Licensed under the Apache License, Version 2.0 (the "License");
import org.springframework.util.ResourceUtils;
import org.springframework.data.convert.EntityReader;
he Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
@@ -33,9 +29,9 @@ import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@@ -57,6 +53,7 @@ import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoWriter; import org.springframework.data.mongodb.core.convert.MongoWriter;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.geo.Distance; import org.springframework.data.mongodb.core.geo.Distance;
import org.springframework.data.mongodb.core.geo.GeoResult; import org.springframework.data.mongodb.core.geo.GeoResult;
import org.springframework.data.mongodb.core.geo.GeoResults; import org.springframework.data.mongodb.core.geo.GeoResults;
@@ -111,7 +108,7 @@ import com.mongodb.util.JSON;
*/ */
public class MongoTemplate implements MongoOperations, ApplicationContextAware { public class MongoTemplate implements MongoOperations, ApplicationContextAware {
private static final Log LOGGER = LogFactory.getLog(MongoTemplate.class); private static final Logger LOGGER = LoggerFactory.getLogger(MongoTemplate.class);
private static final String ID = "_id"; private static final String ID = "_id";
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE; private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@@ -830,11 +827,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject queryObj = query == null ? new BasicDBObject() DBObject queryObj = query == null ? new BasicDBObject()
: mapper.getMappedObject(query.getQueryObject(), entity); : mapper.getMappedObject(query.getQueryObject(), entity);
DBObject updateObj = update.getUpdateObject(); DBObject updateObj = update == null ? new BasicDBObject() : mapper.getMappedObject(update.getUpdateObject(),
entity);
for (String key : updateObj.keySet()) {
updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key)));
}
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj + " in collection: " LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj + " in collection: "

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@ package org.springframework.data.mongodb.core;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.data.authentication.UserCredentials; import org.springframework.data.authentication.UserCredentials;
@@ -39,12 +37,10 @@ import com.mongodb.WriteConcern;
*/ */
public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory { public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
protected final Log logger = LogFactory.getLog(getClass());
private final Mongo mongo; private final Mongo mongo;
private final String databaseName; private final String databaseName;
private String username; private final boolean mongoInstanceCreated;
private String password; private final UserCredentials credentials;
private WriteConcern writeConcern; private WriteConcern writeConcern;
/** /**
@@ -54,11 +50,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
* @param databaseName database name, not be {@literal null}. * @param databaseName database name, not be {@literal null}.
*/ */
public SimpleMongoDbFactory(Mongo mongo, String databaseName) { public SimpleMongoDbFactory(Mongo mongo, String databaseName) {
Assert.notNull(mongo, "Mongo must not be null"); this(mongo, databaseName, UserCredentials.NO_CREDENTIALS, false);
Assert.hasText(databaseName, "Database name must not be empty");
Assert.isTrue(databaseName.matches("[\\w-]+"), "Database name must only contain letters, numbers, underscores and dashes!");
this.mongo = mongo;
this.databaseName = databaseName;
} }
/** /**
@@ -66,12 +58,10 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
* *
* @param mongo Mongo instance, must not be {@literal null}. * @param mongo Mongo instance, must not be {@literal null}.
* @param databaseName Database name, must not be {@literal null}. * @param databaseName Database name, must not be {@literal null}.
* @param userCredentials username and password must not be {@literal null}. * @param credentials username and password.
*/ */
public SimpleMongoDbFactory(Mongo mongo, String databaseName, UserCredentials userCredentials) { public SimpleMongoDbFactory(Mongo mongo, String databaseName, UserCredentials credentials) {
this(mongo, databaseName); this(mongo, databaseName, credentials, false);
this.username = userCredentials.getUsername();
this.password = userCredentials.getPassword();
} }
/** /**
@@ -83,7 +73,21 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
* @see MongoURI * @see MongoURI
*/ */
public SimpleMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException { public SimpleMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException {
this(new Mongo(uri), uri.getDatabase(), new UserCredentials(uri.getUsername(), parseChars(uri.getPassword()))); this(new Mongo(uri), uri.getDatabase(), new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())), true);
}
private SimpleMongoDbFactory(Mongo mongo, String databaseName, UserCredentials credentials,
boolean mongoInstanceCreated) {
Assert.notNull(mongo, "Mongo must not be null");
Assert.hasText(databaseName, "Database name must not be empty");
Assert.isTrue(databaseName.matches("[\\w-]+"),
"Database name must only contain letters, numbers, underscores and dashes!");
this.mongo = mongo;
this.databaseName = databaseName;
this.mongoInstanceCreated = mongoInstanceCreated;
this.credentials = credentials == null ? UserCredentials.NO_CREDENTIALS : credentials;
} }
/** /**
@@ -95,10 +99,6 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
this.writeConcern = writeConcern; this.writeConcern = writeConcern;
} }
public WriteConcern getWriteConcern() {
return writeConcern;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.MongoDbFactory#getDb() * @see org.springframework.data.mongodb.MongoDbFactory#getDb()
@@ -115,7 +115,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
Assert.hasText(dbName, "Database name must not be empty."); Assert.hasText(dbName, "Database name must not be empty.");
DB db = MongoDbUtils.getDB(mongo, dbName, username, password == null ? null : password.toCharArray()); DB db = MongoDbUtils.getDB(mongo, dbName, credentials);
if (writeConcern != null) { if (writeConcern != null) {
db.setWriteConcern(writeConcern); db.setWriteConcern(writeConcern);
@@ -125,17 +125,17 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
} }
/** /**
* Clean up the Mongo instance. * Clean up the Mongo instance if it was created by the factory itself.
*
* @see DisposableBean#destroy()
*/ */
public void destroy() throws Exception { public void destroy() throws Exception {
if (mongoInstanceCreated) {
mongo.close(); mongo.close();
} }
}
public static String parseChars(char[] chars) { private static String parseChars(char[] chars) {
if (chars == null) { return chars == null ? null : String.valueOf(chars);
return null;
} else {
return String.valueOf(chars);
}
} }
} }

View File

@@ -30,7 +30,9 @@ public interface WriteConcernResolver {
/** /**
* Resolve the WriteConcern given the MongoAction * Resolve the WriteConcern given the MongoAction
* @param action describes the context of the Mongo action. Contains a default WriteConcern to use if one should not be resolved. *
* @param action describes the context of the Mongo action. Contains a default WriteConcern to use if one should not
* be resolved.
* @return a WriteConcern based on the passed in MongoAction value, maybe null * @return a WriteConcern based on the passed in MongoAction value, maybe null
*/ */
WriteConcern resolve(MongoAction action); WriteConcern resolve(MongoAction action);

View File

@@ -23,6 +23,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.core.convert.support.GenericConversionService; import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToObjectIdConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToObjectIdConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToBigIntegerConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToBigIntegerConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToStringConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToStringConverter;
@@ -39,6 +40,7 @@ public abstract class AbstractMongoConverter implements MongoConverter, Initiali
protected final GenericConversionService conversionService; protected final GenericConversionService conversionService;
protected CustomConversions conversions = new CustomConversions(); protected CustomConversions conversions = new CustomConversions();
protected EntityInstantiators instantiators = new EntityInstantiators();
/** /**
* Creates a new {@link AbstractMongoConverter} using the given {@link GenericConversionService}. * Creates a new {@link AbstractMongoConverter} using the given {@link GenericConversionService}.
@@ -60,6 +62,15 @@ public abstract class AbstractMongoConverter implements MongoConverter, Initiali
this.conversions = conversions; this.conversions = conversions;
} }
/**
* Registers {@link EntityInstantiators} to customize entity instantiation.
*
* @param instantiators
*/
public void setInstantiators(EntityInstantiators instantiators) {
this.instantiators = instantiators == null ? new EntityInstantiators() : instantiators;
}
/** /**
* Registers additional converters that will be available when using the {@link ConversionService} directly (e.g. for * Registers additional converters that will be available when using the {@link ConversionService} directly (e.g. for
* id conversion). These converters are not custom conversions as they'd introduce unwanted conversions (e.g. * id conversion). These converters are not custom conversions as they'd introduce unwanted conversions (e.g.

View File

@@ -22,8 +22,8 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log; import org.slf4j.Logger;
import org.apache.commons.logging.LogFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.GenericTypeResolver; import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
@@ -52,7 +52,7 @@ import org.springframework.util.Assert;
*/ */
public class CustomConversions { public class CustomConversions {
private static final Log LOG = LogFactory.getLog(CustomConversions.class); private static final Logger LOG = LoggerFactory.getLogger(CustomConversions.class);
private static final String READ_CONVERTER_NOT_SIMPLE = "Registering converter from %s to %s as reading converter although it doesn't convert from a Mongo supported type! You might wanna check you annotation setup at the converter implementation."; private static final String READ_CONVERTER_NOT_SIMPLE = "Registering converter from %s to %s as reading converter although it doesn't convert from a Mongo supported type! You might wanna check you annotation setup at the converter implementation.";
private static final String WRITE_CONVERTER_NOT_SIMPLE = "Registering converter from %s to %s as writing converter although it doesn't convert to a Mongo supported type! You might wanna check you annotation setup at the converter implementation."; private static final String WRITE_CONVERTER_NOT_SIMPLE = "Registering converter from %s to %s as writing converter although it doesn't convert to a Mongo supported type! You might wanna check you annotation setup at the converter implementation.";

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2012 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.mongodb.core.convert;
import java.util.Map;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import com.mongodb.DBObject;
/**
* {@link PropertyAccessor} to allow entity based field access to {@link DBObject}s.
*
* @author Oliver Gierke
*/
class DBObjectPropertyAccessor extends MapAccessor {
static MapAccessor INSTANCE = new DBObjectPropertyAccessor();
@Override
public Class<?>[] getSpecificTargetClasses() {
return new Class[] { DBObject.class };
}
@Override
public boolean canRead(EvaluationContext context, Object target, String name) {
return true;
}
@Override
@SuppressWarnings("unchecked")
public TypedValue read(EvaluationContext context, Object target, String name) {
Map<String, Object> source = (Map<String, Object>) target;
Object value = source.get(name);
return value == null ? TypedValue.NULL : new TypedValue(value);
}
}

View File

@@ -66,7 +66,6 @@ public class DefaultMongoTypeMapper extends DefaultTypeMapper<DBObject> implemen
this.typeKey = typeKey; this.typeKey = typeKey;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.MongoTypeMapper#isTypeKey(java.lang.String) * @see org.springframework.data.mongodb.core.convert.MongoTypeMapper#isTypeKey(java.lang.String)
@@ -75,7 +74,6 @@ public class DefaultMongoTypeMapper extends DefaultTypeMapper<DBObject> implemen
return typeKey == null ? false : typeKey.equals(key); return typeKey == null ? false : typeKey.equals(key);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.springframework.data.convert.DefaultTypeMapper#getFallbackTypeFor(java.lang.Object) * @see org.springframework.data.convert.DefaultTypeMapper#getFallbackTypeFor(java.lang.Object)
*/ */

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2011 by the original author(s). * Copyright 2011-2012 by the original author(s).
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/ */
package org.springframework.data.mongodb.core.convert; package org.springframework.data.mongodb.core.convert;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -25,34 +24,34 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.logging.Log; import org.slf4j.Logger;
import org.apache.commons.logging.LogFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.CollectionFactory; import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.convert.TypeMapper; import org.springframework.data.convert.TypeMapper;
import org.springframework.data.mapping.Association; import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.AssociationHandler; import org.springframework.data.mapping.AssociationHandler;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.BeanWrapper; import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.ParameterValueProvider; import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.SpELAwareParameterValueProvider; import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
import org.springframework.data.mapping.model.PropertyValueProvider;
import org.springframework.data.mapping.model.SpELContext;
import org.springframework.data.mapping.model.SpELExpressionEvaluator;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.QueryMapper;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@@ -72,7 +71,7 @@ import com.mongodb.DBRef;
*/ */
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware { public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
protected static final Log log = LogFactory.getLog(MappingMongoConverter.class); protected static final Logger log = LoggerFactory.getLogger(MappingMongoConverter.class);
protected final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext; protected final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
protected final SpelExpressionParser spelExpressionParser = new SpelExpressionParser(); protected final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
@@ -81,6 +80,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
protected ApplicationContext applicationContext; protected ApplicationContext applicationContext;
protected boolean useFieldAccessOnly = true; protected boolean useFieldAccessOnly = true;
protected MongoTypeMapper typeMapper; protected MongoTypeMapper typeMapper;
protected String mapKeyDotReplacement = null;
private SpELContext spELContext;
/** /**
* Creates a new {@link MappingMongoConverter} given the new {@link MongoDbFactory} and {@link MappingContext}. * Creates a new {@link MappingMongoConverter} given the new {@link MongoDbFactory} and {@link MappingContext}.
@@ -101,6 +103,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
this.mappingContext = mappingContext; this.mappingContext = mappingContext;
this.typeMapper = new DefaultMongoTypeMapper(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, mappingContext); this.typeMapper = new DefaultMongoTypeMapper(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, mappingContext);
this.idMapper = new QueryMapper(this); this.idMapper = new QueryMapper(this);
this.spELContext = new SpELContext(DBObjectPropertyAccessor.INSTANCE);
} }
/** /**
@@ -116,6 +120,18 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
mappingContext) : typeMapper; mappingContext) : typeMapper;
} }
/**
* Configure the characters dots potentially contained in a {@link Map} shall be replaced with. By default we don't do
* any translation but rather reject a {@link Map} with keys containing dots causing the conversion for the entire
* object to fail. If further customization of the translation is needed, have a look at
* {@link #potentiallyEscapeMapKey(String)} as well as {@link #potentiallyUnescapeMapKey(String)}.
*
* @param mapKeyDotReplacement the mapKeyDotReplacement to set
*/
public void setMapKeyDotReplacement(String mapKeyDotReplacement) {
this.mapKeyDotReplacement = mapKeyDotReplacement;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.convert.EntityConverter#getMappingContext() * @see org.springframework.data.convert.EntityConverter#getMappingContext()
@@ -140,7 +156,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/ */
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.spELContext = new SpELContext(this.spELContext, applicationContext);
} }
/* /*
@@ -151,8 +169,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return read(ClassTypeInformation.from(clazz), dbo); return read(ClassTypeInformation.from(clazz), dbo);
} }
@SuppressWarnings("unchecked")
protected <S extends Object> S read(TypeInformation<S> type, DBObject dbo) { protected <S extends Object> S read(TypeInformation<S> type, DBObject dbo) {
return read(type, dbo, null);
}
@SuppressWarnings("unchecked")
protected <S extends Object> S read(TypeInformation<S> type, DBObject dbo, Object parent) {
if (null == dbo) { if (null == dbo) {
return null; return null;
@@ -170,11 +192,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
} }
if (typeToUse.isCollectionLike() && dbo instanceof BasicDBList) { if (typeToUse.isCollectionLike() && dbo instanceof BasicDBList) {
return (S) readCollectionOrArray(typeToUse, (BasicDBList) dbo); return (S) readCollectionOrArray(typeToUse, (BasicDBList) dbo, parent);
} }
if (typeToUse.isMap()) { if (typeToUse.isMap()) {
return (S) readMap(typeToUse, dbo); return (S) readMap(typeToUse, dbo, parent);
} }
// Retrieve persistent entity info // Retrieve persistent entity info
@@ -184,72 +206,43 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
throw new MappingException("No mapping metadata found for " + rawType.getName()); throw new MappingException("No mapping metadata found for " + rawType.getName());
} }
return read(persistentEntity, dbo); return read(persistentEntity, dbo, parent);
} }
private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo) { private ParameterValueProvider<MongoPersistentProperty> getParameterProvider(MongoPersistentEntity<?> entity,
DBObject source, DefaultSpELExpressionEvaluator evaluator, Object parent) {
final StandardEvaluationContext spelCtx = new StandardEvaluationContext(); MongoDbPropertyValueProvider provider = new MongoDbPropertyValueProvider(source, evaluator, parent);
if (null != applicationContext) { PersistentEntityParameterValueProvider<MongoPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<MongoPersistentProperty>(
spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext)); entity, provider, parent);
} parameterProvider.setSpELEvaluator(evaluator);
if (!(dbo instanceof BasicDBList)) {
String[] keySet = dbo.keySet().toArray(new String[dbo.keySet().size()]); return parameterProvider;
for (String key : keySet) {
spelCtx.setVariable(key, dbo.get(key));
}
} }
final List<String> ctorParamNames = new ArrayList<String>(); private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo, Object parent) {
final MongoPersistentProperty idProperty = entity.getIdProperty();
ParameterValueProvider provider = new SpELAwareParameterValueProvider(spelExpressionParser, spelCtx) { final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(dbo, spELContext);
@Override
@SuppressWarnings("unchecked")
public <T> T getParameterValue(PreferredConstructor.Parameter<T> parameter) {
if (parameter.getKey() != null) { ParameterValueProvider<MongoPersistentProperty> provider = getParameterProvider(entity, dbo, evaluator, parent);
return super.getParameterValue(parameter); EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
} S instance = instantiator.createInstance(entity, provider);
String name = parameter.getName(); final BeanWrapper<MongoPersistentEntity<S>, S> wrapper = BeanWrapper.create(instance, conversionService);
TypeInformation<T> type = parameter.getType(); final S result = wrapper.getBean();
Class<T> rawType = parameter.getRawType();
String key = idProperty == null ? name : idProperty.getName().equals(name) ? idProperty.getFieldName() : name;
Object obj = dbo.get(key);
ctorParamNames.add(name);
if (obj instanceof DBRef) {
return read(type, ((DBRef) obj).fetch());
} else if (obj instanceof BasicDBList) {
BasicDBList objAsDbList = (BasicDBList) obj;
return conversionService.convert(readCollectionOrArray(type, objAsDbList), rawType);
} else if (obj instanceof DBObject) {
return read(type, ((DBObject) obj));
} else if (null != obj && obj.getClass().isAssignableFrom(rawType)) {
return (T) obj;
} else if (null != obj) {
return conversionService.convert(obj, rawType);
}
return null;
}
};
final BeanWrapper<MongoPersistentEntity<S>, S> wrapper = BeanWrapper.create(entity, provider, conversionService);
// Set properties not already set in the constructor // Set properties not already set in the constructor
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() { entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
public void doWithPersistentProperty(MongoPersistentProperty prop) { public void doWithPersistentProperty(MongoPersistentProperty prop) {
boolean isConstructorProperty = ctorParamNames.contains(prop.getName()); boolean isConstructorProperty = entity.isConstructorArgument(prop);
boolean hasValueForProperty = dbo.containsField(prop.getFieldName()); boolean hasValueForProperty = dbo.containsField(prop.getFieldName());
if (!hasValueForProperty || isConstructorProperty) { if (!hasValueForProperty || isConstructorProperty) {
return; return;
} }
Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression()); Object obj = getValueInternal(prop, dbo, evaluator, result);
wrapper.setProperty(prop, obj, useFieldAccessOnly); wrapper.setProperty(prop, obj, useFieldAccessOnly);
} }
}); });
@@ -258,7 +251,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() { entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
public void doWithAssociation(Association<MongoPersistentProperty> association) { public void doWithAssociation(Association<MongoPersistentProperty> association) {
MongoPersistentProperty inverseProp = association.getInverse(); MongoPersistentProperty inverseProp = association.getInverse();
Object obj = getValueInternal(inverseProp, dbo, spelCtx, inverseProp.getSpelExpression()); Object obj = getValueInternal(inverseProp, dbo, evaluator, result);
try { try {
wrapper.setProperty(inverseProp, obj); wrapper.setProperty(inverseProp, obj);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
@@ -269,7 +262,23 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
} }
}); });
return wrapper.getBean(); return result;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.MongoWriter#toDBRef(java.lang.Object, org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)
*/
public DBRef toDBRef(Object object, MongoPersistentProperty referingProperty) {
org.springframework.data.mongodb.core.mapping.DBRef annotation = null;
if (referingProperty != null) {
annotation = referingProperty.getDBRef();
Assert.isTrue(annotation != null, "The referenced property has to be mapped with @DBRef!");
}
return createDBRef(object, annotation);
} }
/** /**
@@ -499,13 +508,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
for (Object element : source) { for (Object element : source) {
if (element == null) { Class<?> elementType = element == null ? null : element.getClass();
continue;
}
Class<?> elementType = element.getClass(); if (elementType == null || conversions.isSimpleType(elementType)) {
if (conversions.isSimpleType(elementType)) {
sink.add(getPotentiallyConvertedSimpleWrite(element)); sink.add(getPotentiallyConvertedSimpleWrite(element));
} else if (element instanceof Collection || elementType.isArray()) { } else if (element instanceof Collection || elementType.isArray()) {
sink.add(writeCollectionInternal(asCollection(element), componentType, new BasicDBList())); sink.add(writeCollectionInternal(asCollection(element), componentType, new BasicDBList()));
@@ -535,7 +540,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
if (conversions.isSimpleType(key.getClass())) { if (conversions.isSimpleType(key.getClass())) {
// Don't use conversion service here as removal of ObjectToString converter results in some primitive types not // Don't use conversion service here as removal of ObjectToString converter results in some primitive types not
// being convertable // being convertable
String simpleKey = key.toString(); String simpleKey = potentiallyEscapeMapKey(key.toString());
if (val == null || conversions.isSimpleType(val.getClass())) { if (val == null || conversions.isSimpleType(val.getClass())) {
writeSimpleInternal(val, dbo, simpleKey); writeSimpleInternal(val, dbo, simpleKey);
} else if (val instanceof Collection || val.getClass().isArray()) { } else if (val instanceof Collection || val.getClass().isArray()) {
@@ -543,7 +548,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
writeCollectionInternal(asCollection(val), propertyType.getMapValueType(), new BasicDBList())); writeCollectionInternal(asCollection(val), propertyType.getMapValueType(), new BasicDBList()));
} else { } else {
DBObject newDbo = new BasicDBObject(); DBObject newDbo = new BasicDBObject();
TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType() : ClassTypeInformation.OBJECT; TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType()
: ClassTypeInformation.OBJECT;
writeInternal(val, newDbo, valueTypeInfo); writeInternal(val, newDbo, valueTypeInfo);
dbo.put(simpleKey, newDbo); dbo.put(simpleKey, newDbo);
} }
@@ -555,6 +561,39 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return dbo; return dbo;
} }
/**
* Potentially replaces dots in the given map key with the configured map key replacement if configured or aborts
* conversion if none is configured.
*
* @see #setMapKeyDotReplacement(String)
* @param source
* @return
*/
protected String potentiallyEscapeMapKey(String source) {
if (!source.contains(".")) {
return source;
}
if (mapKeyDotReplacement == null) {
throw new MappingException(String.format("Map key %s contains dots but no replacement was configured! Make "
+ "sure map keys don't contain dots in the first place or configure an appropriate replacement!", source));
}
return source.replaceAll("\\.", mapKeyDotReplacement);
}
/**
* Translates the map key replacements in the given key just read with a dot in case a map key replacement has been
* configured.
*
* @param source
* @return
*/
protected String potentiallyUnescapeMapKey(String source) {
return mapKeyDotReplacement == null ? source : source.replaceAll(mapKeyDotReplacement, "\\.");
}
/** /**
* Adds custom type information to the given {@link DBObject} if necessary. That is if the value is not the same as * Adds custom type information to the given {@link DBObject} if necessary. That is if the value is not the same as
* the one given. This is usually the case if you store a subtype of the actual declared type of the property. * the one given. This is usually the case if you store a subtype of the actual declared type of the property.
@@ -635,12 +674,20 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
protected DBRef createDBRef(Object target, org.springframework.data.mongodb.core.mapping.DBRef dbref) { protected DBRef createDBRef(Object target, org.springframework.data.mongodb.core.mapping.DBRef dbref) {
Assert.notNull(target);
MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
if (null == targetEntity || null == targetEntity.getIdProperty()) {
return null; if (null == targetEntity) {
throw new MappingException("No mapping metadata found for " + target.getClass());
} }
MongoPersistentProperty idProperty = targetEntity.getIdProperty(); MongoPersistentProperty idProperty = targetEntity.getIdProperty();
if (idProperty == null) {
throw new MappingException("No id property found on class " + targetEntity.getType());
}
BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(target, conversionService); BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(target, conversionService);
Object id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly); Object id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly);
@@ -648,69 +695,17 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
throw new MappingException("Cannot create a reference to an object with a NULL id."); throw new MappingException("Cannot create a reference to an object with a NULL id.");
} }
String collection = dbref.collection(); DB db = mongoDbFactory.getDb();
if ("".equals(collection)) { db = dbref != null && StringUtils.hasText(dbref.db()) ? mongoDbFactory.getDb(dbref.db()) : db;
collection = targetEntity.getCollection();
return new DBRef(db, targetEntity.getCollection(), idMapper.convertId(id));
} }
String dbname = dbref.db(); protected Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, SpELExpressionEvaluator eval,
DB db = StringUtils.hasText(dbname) ? mongoDbFactory.getDb(dbname) : mongoDbFactory.getDb(); Object parent) {
return new DBRef(db, collection, idMapper.convertId(id)); MongoDbPropertyValueProvider provider = new MongoDbPropertyValueProvider(dbo, spELContext, parent);
} return provider.getPropertyValue(prop);
@SuppressWarnings("unchecked")
protected Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx,
String spelExpr) {
Object o;
if (null != spelExpr) {
Expression x = spelExpressionParser.parseExpression(spelExpr);
o = x.getValue(ctx);
} else {
Object sourceValue = dbo.get(prop.getFieldName());
if (sourceValue == null) {
return null;
}
Class<?> propertyType = prop.getType();
if (conversions.hasCustomReadTarget(sourceValue.getClass(), propertyType)) {
return conversionService.convert(sourceValue, propertyType);
}
if (sourceValue instanceof DBRef) {
sourceValue = ((DBRef) sourceValue).fetch();
}
if (sourceValue instanceof DBObject) {
if (prop.isMap()) {
return readMap(prop.getTypeInformation(), (DBObject) sourceValue);
} else if (prop.isArray() && sourceValue instanceof BasicDBObject
&& ((DBObject) sourceValue).keySet().size() == 0) {
// It's empty
return Array.newInstance(prop.getComponentType(), 0);
} else if (prop.isCollectionLike() && sourceValue instanceof BasicDBList) {
return readCollectionOrArray((TypeInformation<? extends Collection<?>>) prop.getTypeInformation(),
(BasicDBList) sourceValue);
}
TypeInformation<?> toType = typeMapper.readType((DBObject) sourceValue);
// It's a complex object, have to read it in
if (toType != null) {
// TODO: why do we remove the type?
// dbo.removeField(CUSTOM_TYPE_KEY);
o = read(toType, (DBObject) sourceValue);
} else {
o = read(mappingContext.getPersistentEntity(prop.getTypeInformation()), (DBObject) sourceValue);
}
} else {
o = sourceValue;
}
}
return o;
} }
/** /**
@@ -721,21 +716,29 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
* @return the converted {@link Collections}, will never be {@literal null}. * @return the converted {@link Collections}, will never be {@literal null}.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Collection<?> readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue) { private Collection<?> readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue, Object parent) {
Assert.notNull(targetType); Assert.notNull(targetType);
if (sourceValue.isEmpty()) {
return Collections.emptySet();
}
Class<?> collectionType = targetType.getType();
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>() : CollectionFactory Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>() : CollectionFactory
.createCollection(targetType.getType(), sourceValue.size()); .createCollection(collectionType, sourceValue.size());
TypeInformation<?> componentType = targetType.getComponentType();
for (int i = 0; i < sourceValue.size(); i++) { for (int i = 0; i < sourceValue.size(); i++) {
Object dbObjItem = sourceValue.get(i); Object dbObjItem = sourceValue.get(i);
if (dbObjItem instanceof DBRef) { if (dbObjItem instanceof DBRef) {
items.add(read(targetType.getComponentType(), ((DBRef) dbObjItem).fetch())); items.add(read(componentType, ((DBRef) dbObjItem).fetch(), parent));
} else if (dbObjItem instanceof DBObject) { } else if (dbObjItem instanceof DBObject) {
items.add(read(targetType.getComponentType(), (DBObject) dbObjItem)); items.add(read(componentType, (DBObject) dbObjItem, parent));
} else { } else {
items.add(getPotentiallyConvertedSimpleRead(dbObjItem, targetType.getComponentType().getType())); items.add(getPotentiallyConvertedSimpleRead(dbObjItem, componentType == null ? null : componentType.getType()));
} }
} }
@@ -750,7 +753,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
* @return * @return
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject) { protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject, Object parent) {
Assert.notNull(dbObject); Assert.notNull(dbObject);
@@ -763,19 +766,19 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
continue; continue;
} }
Object key = entry.getKey(); Object key = potentiallyUnescapeMapKey(entry.getKey());
TypeInformation<?> keyTypeInformation = type.getComponentType(); TypeInformation<?> keyTypeInformation = type.getComponentType();
if (keyTypeInformation != null) { if (keyTypeInformation != null) {
Class<?> keyType = keyTypeInformation.getType(); Class<?> keyType = keyTypeInformation.getType();
key = conversionService.convert(entry.getKey(), keyType); key = conversionService.convert(key, keyType);
} }
Object value = entry.getValue(); Object value = entry.getValue();
TypeInformation<?> valueType = type.getMapValueType(); TypeInformation<?> valueType = type.getMapValueType();
if (value instanceof DBObject) { if (value instanceof DBObject) {
map.put(key, read(valueType, (DBObject) value)); map.put(key, read(valueType, (DBObject) value, parent));
} else { } else {
Class<?> valueClass = valueType == null ? null : valueType.getType(); Class<?> valueClass = valueType == null ? null : valueType.getType();
map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass)); map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
@@ -838,14 +841,14 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return result; return result;
} }
if (obj instanceof List) {
return maybeConvertList((List<?>) obj);
}
if (obj.getClass().isArray()) { if (obj.getClass().isArray()) {
return maybeConvertList(Arrays.asList((Object[]) obj)); return maybeConvertList(Arrays.asList((Object[]) obj));
} }
if (obj instanceof Collection) {
return maybeConvertList((Collection<?>) obj);
}
DBObject newDbo = new BasicDBObject(); DBObject newDbo = new BasicDBObject();
this.write(obj, newDbo); this.write(obj, newDbo);
return removeTypeInfoRecursively(newDbo); return removeTypeInfoRecursively(newDbo);
@@ -895,4 +898,55 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return dbObject; return dbObject;
} }
private class MongoDbPropertyValueProvider implements PropertyValueProvider<MongoPersistentProperty> {
private final DBObject source;
private final SpELExpressionEvaluator evaluator;
private final Object parent;
public MongoDbPropertyValueProvider(DBObject source, SpELContext factory, Object parent) {
this(source, new DefaultSpELExpressionEvaluator(source, factory), parent);
}
public MongoDbPropertyValueProvider(DBObject source, DefaultSpELExpressionEvaluator evaluator, Object parent) {
Assert.notNull(source);
Assert.notNull(evaluator);
this.source = source;
this.evaluator = evaluator;
this.parent = parent;
}
/*
* (non-Javadoc)
* @see org.springframework.data.convert.PropertyValueProvider#getPropertyValue(org.springframework.data.mapping.PersistentProperty)
*/
@SuppressWarnings("unchecked")
public <T> T getPropertyValue(MongoPersistentProperty property) {
String expression = property.getSpelExpression();
Object value = expression != null ? evaluator.evaluate(expression) : source.get(property.getFieldName());
if (value == null) {
return null;
}
TypeInformation<?> type = property.getTypeInformation();
Class<?> rawType = type.getType();
if (conversions.hasCustomReadTarget(value.getClass(), rawType)) {
return (T) conversionService.convert(value, rawType);
} else if (value instanceof DBRef) {
return (T) read(type, ((DBRef) value).fetch(), parent);
} else if (value instanceof BasicDBList) {
return (T) getPotentiallyConvertedSimpleRead(readCollectionOrArray(type, (BasicDBList) value, parent), rawType);
} else if (value instanceof DBObject) {
return (T) read(type, (DBObject) value, parent);
} else {
return (T) getPotentiallyConvertedSimpleRead(value, rawType);
}
}
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,8 +16,10 @@
package org.springframework.data.mongodb.core.convert; package org.springframework.data.mongodb.core.convert;
import org.springframework.data.convert.EntityWriter; import org.springframework.data.convert.EntityWriter;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.DBRef;
/** /**
* A MongoWriter is responsible for converting an object of type T to the native MongoDB representation DBObject. * A MongoWriter is responsible for converting an object of type T to the native MongoDB representation DBObject.
@@ -37,4 +39,14 @@ public interface MongoWriter<T> extends EntityWriter<T, DBObject> {
* @return * @return
*/ */
Object convertToMongoType(Object obj); Object convertToMongoType(Object obj);
/**
* Creates a {@link DBRef} to refer to the given object.
*
* @param object the object to create a {@link DBRef} to link to. The object's type has to carry an id attribute.
* @param referingProperty the client-side property referring to the object which might carry additional metadata for
* the {@link DBRef} object to create. Can be {@literal null}.
* @return will never be {@literal null}.
*/
DBRef toDBRef(Object object, MongoPersistentProperty referingProperty);
} }

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core.convert;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -25,11 +25,12 @@ import org.bson.types.ObjectId;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
@@ -41,6 +42,9 @@ import com.mongodb.DBObject;
*/ */
public class QueryMapper { public class QueryMapper {
private static final List<String> DEFAULT_ID_NAMES = Arrays.asList("id", "_id");
private static final String N_OR_PATTERN = "\\$.*or";
private final ConversionService conversionService; private final ConversionService conversionService;
private final MongoConverter converter; private final MongoConverter converter;
@@ -59,8 +63,8 @@ public class QueryMapper {
* Replaces the property keys used in the given {@link DBObject} with the appropriate keys by using the * Replaces the property keys used in the given {@link DBObject} with the appropriate keys by using the
* {@link PersistentEntity} metadata. * {@link PersistentEntity} metadata.
* *
* @param query * @param query must not be {@literal null}.
* @param entity * @param entity can be {@literal null}.
* @return * @return
*/ */
public DBObject getMappedObject(DBObject query, MongoPersistentEntity<?> entity) { public DBObject getMappedObject(DBObject query, MongoPersistentEntity<?> entity) {
@@ -68,8 +72,11 @@ public class QueryMapper {
DBObject newDbo = new BasicDBObject(); DBObject newDbo = new BasicDBObject();
for (String key : query.keySet()) { for (String key : query.keySet()) {
MongoPersistentEntity<?> nestedEntity = getNestedEntity(entity, key);
String newKey = key; String newKey = key;
Object value = query.get(key); Object value = query.get(key);
if (isIdKey(key, entity)) { if (isIdKey(key, entity)) {
if (value instanceof DBObject) { if (value instanceof DBObject) {
DBObject valueDbo = (DBObject) value; DBObject valueDbo = (DBObject) value;
@@ -81,34 +88,51 @@ public class QueryMapper {
} }
valueDbo.put(inKey, ids.toArray(new Object[ids.size()])); valueDbo.put(inKey, ids.toArray(new Object[ids.size()]));
} else { } else {
value = getMappedObject((DBObject) value, entity); value = getMappedObject((DBObject) value, nestedEntity);
} }
} else { } else {
value = convertId(value); value = convertId(value);
} }
newKey = "_id"; newKey = "_id";
} else if (key.startsWith("$") && key.endsWith("or")) { } else if (key.matches(N_OR_PATTERN)) {
// $or/$nor // $or/$nor
BasicBSONList conditions = (BasicBSONList) value; Iterable<?> conditions = (Iterable<?>) value;
BasicBSONList newConditions = new BasicBSONList(); BasicBSONList newConditions = new BasicBSONList();
Iterator<Object> iter = conditions.iterator(); Iterator<?> iter = conditions.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
newConditions.add(getMappedObject((DBObject) iter.next(), entity)); newConditions.add(getMappedObject((DBObject) iter.next(), nestedEntity));
} }
value = newConditions; value = newConditions;
} else if (key.equals("$ne")) { } else if (key.equals("$ne")) {
value = convertId(value); value = convertId(value);
} else if (value instanceof DBObject) {
newDbo.put(newKey, getMappedObject((DBObject) value, entity));
return newDbo;
} }
newDbo.put(newKey, converter.convertToMongoType(value)); newDbo.put(newKey, convertSimpleOrDBObject(value, nestedEntity));
} }
return newDbo; return newDbo;
} }
/**
* Retriggers mapping if the given source is a {@link DBObject} or simply invokes the
*
* @param source
* @param entity
* @return
*/
private Object convertSimpleOrDBObject(Object source, MongoPersistentEntity<?> entity) {
if (source instanceof BasicDBList) {
return converter.convertToMongoType(source);
}
if (source instanceof DBObject) {
return getMappedObject((DBObject) source, entity);
}
return converter.convertToMongoType(source);
}
/** /**
* Returns whether the given key will be considered an id key. * Returns whether the given key will be considered an id key.
* *
@@ -123,7 +147,19 @@ public class QueryMapper {
return idProperty.getName().equals(key) || idProperty.getFieldName().equals(key); return idProperty.getName().equals(key) || idProperty.getFieldName().equals(key);
} }
return Arrays.asList("id", "_id").contains(key); return DEFAULT_ID_NAMES.contains(key);
}
private MongoPersistentEntity<?> getNestedEntity(MongoPersistentEntity<?> entity, String key) {
MongoPersistentProperty property = entity == null ? null : entity.getPersistentProperty(key);
if (property == null || !property.isEntity()) {
return null;
}
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> context = converter.getMappingContext();
return context.getPersistentEntity(property);
} }
/** /**

View File

@@ -0,0 +1,132 @@
/*
* Copyright 2012 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.mongodb.core.index;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Value object for an index field.
*
* @author Oliver Gierke
*/
public final class IndexField {
private final String key;
private final Order order;
private final boolean isGeo;
private IndexField(String key, Order order, boolean isGeo) {
Assert.hasText(key);
Assert.isTrue(order != null ^ isGeo);
this.key = key;
this.order = order;
this.isGeo = isGeo;
}
/**
* Creates a default {@link IndexField} with the given key and {@link Order}.
*
* @param key must not be {@literal null} or emtpy.
* @param order must not be {@literal null}.
* @return
*/
public static IndexField create(String key, Order order) {
Assert.notNull(order);
return new IndexField(key, order, false);
}
/**
* Creates a geo {@link IndexField} for the given key.
*
* @param key must not be {@literal null} or empty.
* @return
*/
public static IndexField geo(String key) {
return new IndexField(key, null, true);
}
/**
* @return the key
*/
public String getKey() {
return key;
}
/**
* Returns the order of the {@link IndexField} or {@literal null} in case we have a geo index field.
*
* @return the order
*/
public Order getOrder() {
return order;
}
/**
* Returns whether the {@link IndexField} is a geo index field.
*
* @return the isGeo
*/
public boolean isGeo() {
return isGeo;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof IndexField)) {
return false;
}
IndexField that = (IndexField) obj;
return this.key.equals(that.key) && ObjectUtils.nullSafeEquals(this.order, that.order) && this.isGeo == that.isGeo;
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int result = 17;
result += 31 * ObjectUtils.nullSafeHashCode(key);
result += 31 * ObjectUtils.nullSafeHashCode(order);
result += 31 * ObjectUtils.nullSafeHashCode(isGeo);
return result;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("IndexField [ key: %s, order: %s, isGeo: %s]", key, order, isGeo);
}
}

View File

@@ -15,34 +15,57 @@
*/ */
package org.springframework.data.mongodb.core.index; package org.springframework.data.mongodb.core.index;
import java.util.Map; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.data.mongodb.core.query.Order; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
public class IndexInfo { public class IndexInfo {
private final Map<String, Order> fieldSpec; private final List<IndexField> indexFields;
private String name; private final String name;
private final boolean unique;
private final boolean dropDuplicates;
private final boolean sparse;
private boolean unique = false; public IndexInfo(List<IndexField> indexFields, String name, boolean unique, boolean dropDuplicates, boolean sparse) {
private boolean dropDuplicates = false; this.indexFields = Collections.unmodifiableList(indexFields);
private boolean sparse = false;
public IndexInfo(Map<String, Order> fieldSpec, String name, boolean unique, boolean dropDuplicates, boolean sparse) {
super();
this.fieldSpec = fieldSpec;
this.name = name; this.name = name;
this.unique = unique; this.unique = unique;
this.dropDuplicates = dropDuplicates; this.dropDuplicates = dropDuplicates;
this.sparse = sparse; this.sparse = sparse;
} }
public Map<String, Order> getFieldSpec() { /**
return fieldSpec; * Returns the individual index fields of the index.
*
* @return
*/
public List<IndexField> getIndexFields() {
return this.indexFields;
}
/**
* Returns whether the index is covering exactly the fields given independently of the order.
*
* @param keys must not be {@literal null}.
* @return
*/
public boolean isIndexForFields(Collection<String> keys) {
Assert.notNull(keys);
List<String> indexKeys = new ArrayList<String>(indexFields.size());
for (IndexField field : indexFields) {
indexKeys.add(field.getKey());
}
return indexKeys.containsAll(keys);
} }
public String getName() { public String getName() {
@@ -63,7 +86,7 @@ public class IndexInfo {
@Override @Override
public String toString() { public String toString() {
return "IndexInfo [fieldSpec=" + fieldSpec + ", name=" + name + ", unique=" + unique + ", dropDuplicates=" return "IndexInfo [indexFields=" + indexFields + ", name=" + name + ", unique=" + unique + ", dropDuplicates="
+ dropDuplicates + ", sparse=" + sparse + "]"; + dropDuplicates + ", sparse=" + sparse + "]";
} }
@@ -72,7 +95,7 @@ public class IndexInfo {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + (dropDuplicates ? 1231 : 1237); result = prime * result + (dropDuplicates ? 1231 : 1237);
result = prime * result + ((fieldSpec == null) ? 0 : fieldSpec.hashCode()); result = prime * result + ObjectUtils.nullSafeHashCode(indexFields);
result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (sparse ? 1231 : 1237); result = prime * result + (sparse ? 1231 : 1237);
result = prime * result + (unique ? 1231 : 1237); result = prime * result + (unique ? 1231 : 1237);
@@ -81,35 +104,39 @@ public class IndexInfo {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) {
return true; return true;
if (obj == null) }
if (obj == null) {
return false; return false;
if (getClass() != obj.getClass()) }
if (getClass() != obj.getClass()) {
return false; return false;
}
IndexInfo other = (IndexInfo) obj; IndexInfo other = (IndexInfo) obj;
if (dropDuplicates != other.dropDuplicates) if (dropDuplicates != other.dropDuplicates) {
return false; return false;
if (fieldSpec == null) { }
if (other.fieldSpec != null) if (indexFields == null) {
if (other.indexFields != null) {
return false; return false;
} else if (!fieldSpec.equals(other.fieldSpec)) }
} else if (!indexFields.equals(other.indexFields)) {
return false; return false;
}
if (name == null) { if (name == null) {
if (other.name != null) if (other.name != null) {
return false; return false;
} else if (!name.equals(other.name)) }
} else if (!name.equals(other.name)) {
return false; return false;
if (sparse != other.sparse) }
if (sparse != other.sparse) {
return false; return false;
if (unique != other.unique) }
if (unique != other.unique) {
return false; return false;
}
return true; return true;
} }
/**
* [{ "v" : 1 , "key" : { "_id" : 1} , "ns" : "database.person" , "name" : "_id_"},
{ "v" : 1 , "key" : { "age" : -1} , "ns" : "database.person" , "name" : "age_-1" , "unique" : true , "dropDups" : true}]
*/
} }

View File

@@ -23,8 +23,8 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
/** /**
* An implementation of ApplicationEventPublisher that will only fire MappingContextEvents for use by the index creator when * An implementation of ApplicationEventPublisher that will only fire MappingContextEvents for use by the index creator
* MongoTemplate is used 'stand-alone', that is not declared inside a Spring ApplicationContext. * when MongoTemplate is used 'stand-alone', that is not declared inside a Spring ApplicationContext.
* *
* Declare MongoTemplate inside an ApplicationContext to enable the publishing of all persistence events such as * Declare MongoTemplate inside an ApplicationContext to enable the publishing of all persistence events such as
* {@link AfterLoadEvent}, {@link AfterSaveEvent}, etc. * {@link AfterLoadEvent}, {@link AfterSaveEvent}, etc.

View File

@@ -20,9 +20,10 @@ import java.lang.reflect.Field;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log; import org.slf4j.Logger;
import org.apache.commons.logging.LogFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.event.MappingContextEvent; import org.springframework.data.mapping.event.MappingContextEvent;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
@@ -47,7 +48,7 @@ import com.mongodb.util.JSON;
public class MongoPersistentEntityIndexCreator implements public class MongoPersistentEntityIndexCreator implements
ApplicationListener<MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty>> { ApplicationListener<MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty>> {
private static final Log log = LogFactory.getLog(MongoPersistentEntityIndexCreator.class); private static final Logger log = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class);
private final Map<Class<?>, Boolean> classesSeen = new ConcurrentHashMap<Class<?>, Boolean>(); private final Map<Class<?>, Boolean> classesSeen = new ConcurrentHashMap<Class<?>, Boolean>();
private final MongoDbFactory mongoDbFactory; private final MongoDbFactory mongoDbFactory;
@@ -76,8 +77,14 @@ public class MongoPersistentEntityIndexCreator implements
*/ */
public void onApplicationEvent( public void onApplicationEvent(
MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty> event) { MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty> event) {
PersistentEntity<?, ?> entity = event.getPersistentEntity();
// Double check type as Spring infrastructure does not consider nested generics
if (entity instanceof MongoPersistentEntity) {
checkForIndexes(event.getPersistentEntity()); checkForIndexes(event.getPersistentEntity());
} }
}
protected void checkForIndexes(final MongoPersistentEntity<?> entity) { protected void checkForIndexes(final MongoPersistentEntity<?> entity) {
final Class<?> type = entity.getType(); final Class<?> type = entity.getType();

View File

@@ -21,9 +21,9 @@ import java.math.BigInteger;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mapping.Association; import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -39,7 +39,7 @@ import com.mongodb.DBObject;
public class BasicMongoPersistentProperty extends AnnotationBasedPersistentProperty<MongoPersistentProperty> implements public class BasicMongoPersistentProperty extends AnnotationBasedPersistentProperty<MongoPersistentProperty> implements
MongoPersistentProperty { MongoPersistentProperty {
private static final Log LOG = LogFactory.getLog(BasicMongoPersistentProperty.class); private static final Logger LOG = LoggerFactory.getLogger(BasicMongoPersistentProperty.class);
private static final String ID_FIELD_NAME = "_id"; private static final String ID_FIELD_NAME = "_id";
private static final Set<Class<?>> SUPPORTED_ID_TYPES = new HashSet<Class<?>>(); private static final Set<Class<?>> SUPPORTED_ID_TYPES = new HashSet<Class<?>>();

View File

@@ -28,6 +28,7 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
public class CachingMongoPersistentProperty extends BasicMongoPersistentProperty { public class CachingMongoPersistentProperty extends BasicMongoPersistentProperty {
private Boolean isIdProperty; private Boolean isIdProperty;
private Boolean isAssociation;
private String fieldName; private String fieldName;
/** /**
@@ -57,6 +58,18 @@ public class CachingMongoPersistentProperty extends BasicMongoPersistentProperty
return this.isIdProperty; return this.isIdProperty;
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#isAssociation()
*/
@Override
public boolean isAssociation() {
if (this.isAssociation == null) {
this.isAssociation = super.isAssociation();
}
return this.isAssociation;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#getFieldName() * @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#getFieldName()

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 by the original author(s). * Copyright 2011-2012 by the original author(s).
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core.mapping; package org.springframework.data.mongodb.core.mapping;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@@ -24,19 +24,21 @@ import java.lang.annotation.Target;
import org.springframework.data.annotation.Reference; import org.springframework.data.annotation.Reference;
/** /**
* An annotation that indicates the annotated field is to be stored using a com.mongodb.DBRef * An annotation that indicates the annotated field is to be stored using a {@link com.mongodb.DBRef}.
* *
* @author Jon Brisbin <jbrisbin@vmware.com> * @author Jon Brisbin
* @authot Oliver Gierke
*/ */
@Documented
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD }) @Target({ ElementType.FIELD })
@Reference @Reference
public @interface DBRef { public @interface DBRef {
String collection() default ""; /**
* The database the referred entity resides in.
String id() default ""; *
* @return
*/
String db() default ""; String db() default "";
} }

View File

@@ -30,7 +30,8 @@ import org.springframework.data.util.TypeInformation;
* @author Jon Brisbin <jbrisbin@vmware.com> * @author Jon Brisbin <jbrisbin@vmware.com>
* @author Oliver Gierke ogierke@vmware.com * @author Oliver Gierke ogierke@vmware.com
*/ */
public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersistentEntity<?>, MongoPersistentProperty> implements ApplicationContextAware { public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersistentEntity<?>, MongoPersistentProperty>
implements ApplicationContextAware {
private ApplicationContext context; private ApplicationContext context;

View File

@@ -19,8 +19,10 @@ import java.math.BigInteger;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bson.types.Binary;
import org.bson.types.CodeWScope; import org.bson.types.CodeWScope;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -50,6 +52,8 @@ public abstract class MongoSimpleTypes {
simpleTypes.add(CodeWScope.class); simpleTypes.add(CodeWScope.class);
simpleTypes.add(DBObject.class); simpleTypes.add(DBObject.class);
simpleTypes.add(Pattern.class); simpleTypes.add(Pattern.class);
simpleTypes.add(Binary.class);
simpleTypes.add(UUID.class);
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes); MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
} }

View File

@@ -15,12 +15,13 @@
*/ */
package org.springframework.data.mongodb.core.mapping.event; package org.springframework.data.mongodb.core.mapping.event;
import com.mongodb.DBObject; import org.slf4j.Logger;
import org.apache.commons.logging.Log; import org.slf4j.LoggerFactory;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.GenericTypeResolver; import org.springframework.core.GenericTypeResolver;
import com.mongodb.DBObject;
/** /**
* Base class to implement domain class specific {@link ApplicationListener}s. * Base class to implement domain class specific {@link ApplicationListener}s.
* *
@@ -29,7 +30,7 @@ import org.springframework.core.GenericTypeResolver;
*/ */
public abstract class AbstractMongoEventListener<E> implements ApplicationListener<MongoMappingEvent<?>> { public abstract class AbstractMongoEventListener<E> implements ApplicationListener<MongoMappingEvent<?>> {
protected final Log LOG = LogFactory.getLog(getClass()); private static final Logger LOG = LoggerFactory.getLogger(AbstractMongoEventListener.class);
private final Class<?> domainClass; private final Class<?> domainClass;
/** /**

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 by the original author(s). * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,22 +15,31 @@
*/ */
package org.springframework.data.mongodb.core.mapping.event; package org.springframework.data.mongodb.core.mapping.event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** /**
* @author Jon Brisbin <jbrisbin@vmware.com> * @author Jon Brisbin <jbrisbin@vmware.com>
*/ */
public class LoggingEventListener extends AbstractMongoEventListener<Object> { public class LoggingEventListener extends AbstractMongoEventListener<Object> {
private Log log = LogFactory.getLog(getClass()); private static final Logger log = LoggerFactory.getLogger(LoggingEventListener.class);
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onBeforeConvert(java.lang.Object)
*/
@Override @Override
public void onBeforeConvert(Object source) { public void onBeforeConvert(Object source) {
log.info("onBeforeConvert: " + source); log.info("onBeforeConvert: " + source);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onBeforeSave(java.lang.Object, com.mongodb.DBObject)
*/
@Override @Override
public void onBeforeSave(Object source, DBObject dbo) { public void onBeforeSave(Object source, DBObject dbo) {
try { try {
@@ -39,19 +48,30 @@ public class LoggingEventListener extends AbstractMongoEventListener<Object> {
} }
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onAfterSave(java.lang.Object, com.mongodb.DBObject)
*/
@Override @Override
public void onAfterSave(Object source, DBObject dbo) { public void onAfterSave(Object source, DBObject dbo) {
log.info("onAfterSave: " + source + ", " + dbo); log.info("onAfterSave: " + source + ", " + dbo);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onAfterLoad(com.mongodb.DBObject)
*/
@Override @Override
public void onAfterLoad(DBObject dbo) { public void onAfterLoad(DBObject dbo) {
log.info("onAfterLoad: " + dbo); log.info("onAfterLoad: " + dbo);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onAfterConvert(com.mongodb.DBObject, java.lang.Object)
*/
@Override @Override
public void onAfterConvert(DBObject dbo, Object source) { public void onAfterConvert(DBObject dbo, Object source) {
log.info("onAfterConvert: " + dbo + ", " + source); log.info("onAfterConvert: " + dbo + ", " + source);
} }
} }

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2012 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.mongodb.core.mapping.event;
import java.util.Set;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import com.mongodb.DBObject;
/**
* javax.validation dependant entities validator. When it is registered as Spring component its automatically invoked
* before entities are saved in database.
*
* @author Maciej Walkowiak
*/
public class ValidatingMongoEventListener extends AbstractMongoEventListener<Object> {
private static final Logger LOG = LoggerFactory.getLogger(ValidatingMongoEventListener.class);
private final Validator validator;
/**
* Creates a new {@link ValidatingMongoEventListener} using the given {@link Validator}.
*
* @param validator must not be {@literal null}.
*/
public ValidatingMongoEventListener(Validator validator) {
Assert.notNull(validator);
this.validator = validator;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onBeforeSave(java.lang.Object, com.mongodb.DBObject)
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onBeforeSave(Object source, DBObject dbo) {
LOG.debug("Validating object: {}", source);
Set violations = validator.validate(source);
if (!violations.isEmpty()) {
LOG.info("During object: {} validation violations found: {}", source, violations);
throw new ConstraintViolationException(violations);
}
}
}

View File

@@ -19,24 +19,19 @@ import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
/** /**
* Collects the parameters required to perform a group operation on a collection. The query condition and the input collection are specified on the group method as method arguments * Collects the parameters required to perform a group operation on a collection. The query condition and the input
* to be consistent with other operations, e.g. map-reduce. * collection are specified on the group method as method arguments to be consistent with other operations, e.g.
* map-reduce.
* *
* @author Mark Pollack * @author Mark Pollack
*
*/ */
public class GroupBy { public class GroupBy {
private DBObject dboKeys; private DBObject dboKeys;
private String keyFunction; private String keyFunction;
private String initial; private String initial;
private DBObject initialDbObject; private DBObject initialDbObject;
private String reduce; private String reduce;
private String finalize; private String finalize;
public GroupBy(String... keys) { public GroupBy(String... keys) {
@@ -87,8 +82,6 @@ public class GroupBy {
return this; return this;
} }
public DBObject getGroupByObject() { public DBObject getGroupByObject() {
// return new GroupCommand(dbCollection, dboKeys, condition, initial, reduce, finalize); // return new GroupCommand(dbCollection, dboKeys, condition, initial, reduce, finalize);
BasicDBObject dbo = new BasicDBObject(); BasicDBObject dbo = new BasicDBObject();
@@ -111,8 +104,4 @@ public class GroupBy {
return dbo; return dbo;
} }
} }

View File

@@ -26,19 +26,15 @@ import com.mongodb.DBObject;
* Collects the results of executing a group operation. * Collects the results of executing a group operation.
* *
* @author Mark Pollack * @author Mark Pollack
*
* @param <T> The class in which the results are mapped onto, accessible via an interator. * @param <T> The class in which the results are mapped onto, accessible via an interator.
*/ */
public class GroupByResults<T> implements Iterable<T> { public class GroupByResults<T> implements Iterable<T> {
private final List<T> mappedResults; private final List<T> mappedResults;
private final DBObject rawResults;
private DBObject rawResults;
private double count; private double count;
private int keys; private int keys;
private String serverUsed; private String serverUsed;
public GroupByResults(List<T> mappedResults, DBObject rawResults) { public GroupByResults(List<T> mappedResults, DBObject rawResults) {

View File

@@ -15,13 +15,14 @@
*/ */
package org.springframework.data.mongodb.core.mapreduce; package org.springframework.data.mongodb.core.mapreduce;
/**
* @author Mark Pollack
*/
public class MapReduceCounts { public class MapReduceCounts {
private int inputCount; private final int inputCount;
private final int emitCount;
private int emitCount; private final int outputCount;
private int outputCount;
public MapReduceCounts(int inputCount, int emitCount, int outputCount) { public MapReduceCounts(int inputCount, int emitCount, int outputCount) {
super(); super();
@@ -42,12 +43,20 @@ public class MapReduceCounts {
return outputCount; return outputCount;
} }
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override @Override
public String toString() { public String toString() {
return "MapReduceCounts [inputCount=" + inputCount + ", emitCount=" + emitCount + ", outputCount=" + outputCount return "MapReduceCounts [inputCount=" + inputCount + ", emitCount=" + emitCount + ", outputCount=" + outputCount
+ "]"; + "]";
} }
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@@ -58,24 +67,31 @@ public class MapReduceCounts {
return result; return result;
} }
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) {
return true; return true;
if (obj == null) }
if (obj == null) {
return false; return false;
if (getClass() != obj.getClass()) }
if (getClass() != obj.getClass()) {
return false; return false;
}
MapReduceCounts other = (MapReduceCounts) obj; MapReduceCounts other = (MapReduceCounts) obj;
if (emitCount != other.emitCount) if (emitCount != other.emitCount) {
return false; return false;
if (inputCount != other.inputCount) }
if (inputCount != other.inputCount) {
return false; return false;
if (outputCount != other.outputCount) }
if (outputCount != other.outputCount) {
return false; return false;
}
return true; return true;
} }
} }

View File

@@ -42,7 +42,6 @@ public class MapReduceOptions {
private Map<String, Object> extraOptions = new HashMap<String, Object>(); private Map<String, Object> extraOptions = new HashMap<String, Object>();
/** /**
* Static factory method to create a MapReduceOptions instance * Static factory method to create a MapReduceOptions instance
* *
@@ -191,10 +190,10 @@ public class MapReduceOptions {
} }
/** /**
* Add additional extra options that may not have a method on this class. This method will help if you use a * Add additional extra options that may not have a method on this class. This method will help if you use a version
* version of this client library with a server version that has added additional map-reduce options that do not * of this client library with a server version that has added additional map-reduce options that do not yet have an
* yet have an method for use in setting them. * method for use in setting them. options
* options *
* @param key The key option * @param key The key option
* @param value The value of the option * @param value The value of the option
* @return MapReduceOptions so that methods can be chained in a fluent API style * @return MapReduceOptions so that methods can be chained in a fluent API style
@@ -236,7 +235,6 @@ public class MapReduceOptions {
return this.scopeVariables; return this.scopeVariables;
} }
public DBObject getOptionsObject() { public DBObject getOptionsObject() {
BasicDBObject cmd = new BasicDBObject(); BasicDBObject cmd = new BasicDBObject();

View File

@@ -24,6 +24,7 @@ import com.mongodb.DBObject;
/** /**
* Collects the results of performing a MapReduce operations. * Collects the results of performing a MapReduce operations.
*
* @author Mark Pollack * @author Mark Pollack
* *
* @param <T> The class in which the results are mapped onto, accessible via an interator. * @param <T> The class in which the results are mapped onto, accessible via an interator.
@@ -76,8 +77,7 @@ public class MapReduceResults<T> implements Iterable<T> {
DBObject timing = (DBObject) rawResults.get("timing"); DBObject timing = (DBObject) rawResults.get("timing");
if (timing != null) { if (timing != null) {
if (timing.get("mapTime") != null && timing.get("emitLoop") != null && timing.get("total") != null) { if (timing.get("mapTime") != null && timing.get("emitLoop") != null && timing.get("total") != null) {
mapReduceTiming = new MapReduceTiming( (Long)timing.get("mapTime"), mapReduceTiming = new MapReduceTiming((Long) timing.get("mapTime"), (Integer) timing.get("emitLoop"),
(Integer)timing.get("emitLoop"),
(Integer) timing.get("total")); (Integer) timing.get("total"));
} }
} else { } else {
@@ -85,12 +85,12 @@ public class MapReduceResults<T> implements Iterable<T> {
} }
} }
protected void parseCounts(DBObject rawResults) { protected void parseCounts(DBObject rawResults) {
DBObject counts = (DBObject) rawResults.get("counts"); DBObject counts = (DBObject) rawResults.get("counts");
if (counts != null) { if (counts != null) {
if (counts.get("input") != null && counts.get("emit") != null && counts.get("output") != null) { if (counts.get("input") != null && counts.get("emit") != null && counts.get("output") != null) {
mapReduceCounts = new MapReduceCounts( (Integer)counts.get("input"), (Integer)counts.get("emit"), (Integer)counts.get("output")); mapReduceCounts = new MapReduceCounts((Integer) counts.get("input"), (Integer) counts.get("emit"),
(Integer) counts.get("output"));
} }
} else { } else {
mapReduceCounts = new MapReduceCounts(-1, -1, -1); mapReduceCounts = new MapReduceCounts(-1, -1, -1);

View File

@@ -74,7 +74,4 @@ public class MapReduceTiming {
return true; return true;
} }
} }

View File

@@ -20,14 +20,15 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import org.bson.BSON;
import org.bson.types.BasicBSONList; import org.bson.types.BasicBSONList;
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException; import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
import org.springframework.data.mongodb.core.geo.Circle; import org.springframework.data.mongodb.core.geo.Circle;
import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.geo.Point;
import org.springframework.data.mongodb.core.geo.Shape; import org.springframework.data.mongodb.core.geo.Shape;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
@@ -97,13 +98,17 @@ public class Criteria implements CriteriaDefinition {
throw new InvalidMongoDbApiUsageException( throw new InvalidMongoDbApiUsageException(
"Multiple 'is' values declared. You need to use 'and' with multiple criteria"); "Multiple 'is' values declared. You need to use 'and' with multiple criteria");
} }
if (this.criteria.size() > 0 && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1])) { if (lastOperatorWasNot()) {
throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead."); throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead.");
} }
this.isValue = o; this.isValue = o;
return this; return this;
} }
private boolean lastOperatorWasNot() {
return this.criteria.size() > 0 && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1]);
}
/** /**
* Creates a criterion using the $ne operator * Creates a criterion using the $ne operator
* *
@@ -269,7 +274,11 @@ public class Criteria implements CriteriaDefinition {
* @return * @return
*/ */
public Criteria not() { public Criteria not() {
criteria.put("$not", null); return not(null);
}
private Criteria not(Object value) {
criteria.put("$not", value);
return this; return this;
} }
@@ -280,8 +289,7 @@ public class Criteria implements CriteriaDefinition {
* @return * @return
*/ */
public Criteria regex(String re) { public Criteria regex(String re) {
criteria.put("$regex", re); return regex(re, null);
return this;
} }
/** /**
@@ -292,13 +300,32 @@ public class Criteria implements CriteriaDefinition {
* @return * @return
*/ */
public Criteria regex(String re, String options) { public Criteria regex(String re, String options) {
criteria.put("$regex", re); return regex(toPattern(re, options));
if (StringUtils.hasText(options)) {
criteria.put("$options", options);
} }
/**
* Syntactical sugar for {@link #is(Object)} making obvious that we create a regex predicate.
*
* @param pattern
* @return
*/
public Criteria regex(Pattern pattern) {
Assert.notNull(pattern);
if (lastOperatorWasNot()) {
return not(pattern);
}
this.isValue = pattern;
return this; return this;
} }
private Pattern toPattern(String regex, String options) {
Assert.notNull(regex);
return Pattern.compile(regex, options == null ? 0 : BSON.regexFlags(options));
}
/** /**
* Creates a geospatial criterion using a $within $center operation. This is only available for Mongo 1.7 and higher. * Creates a geospatial criterion using a $within $center operation. This is only available for Mongo 1.7 and higher.
* *
@@ -397,7 +424,6 @@ public class Criteria implements CriteriaDefinition {
return this; return this;
} }
public String getKey() { public String getKey() {
return this.key; return this.key;
} }
@@ -427,16 +453,17 @@ public class Criteria implements CriteriaDefinition {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
boolean not = false; boolean not = false;
for (String k : this.criteria.keySet()) { for (String k : this.criteria.keySet()) {
Object value = this.criteria.get(k);
if (not) { if (not) {
DBObject notDbo = new BasicDBObject(); DBObject notDbo = new BasicDBObject();
notDbo.put(k, this.criteria.get(k)); notDbo.put(k, value);
dbo.put("$not", notDbo); dbo.put("$not", notDbo);
not = false; not = false;
} else { } else {
if ("$not".equals(k)) { if ("$not".equals(k) && value == null) {
not = true; not = true;
} else { } else {
dbo.put(k, this.criteria.get(k)); dbo.put(k, value);
} }
} }
} }
@@ -462,11 +489,10 @@ public class Criteria implements CriteriaDefinition {
Object existing = dbo.get(key); Object existing = dbo.get(key);
if (existing == null) { if (existing == null) {
dbo.put(key, value); dbo.put(key, value);
} } else {
else { throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, "
throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, " + + "you can't add a second '" + key + "' expression specified as '" + key + " : " + value + "'. "
"you can't add a second '" + key + "' expression specified as '" + key + " : " + value + "'. " + + "Criteria already contains '" + key + " : " + existing + "'.");
"Criteria already contains '" + key + " : " + existing + "'.");
} }
} }

View File

@@ -56,11 +56,10 @@ public class Query {
String key = criteria.getKey(); String key = criteria.getKey();
if (existing == null) { if (existing == null) {
this.criteria.put(key, criteria); this.criteria.put(key, criteria);
} } else {
else { throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, "
throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, " + + "you can't add a second '" + key + "' criteria. " + "Query already contains '"
"you can't add a second '" + key + "' criteria. " + + existing.getCriteriaObject() + "'.");
"Query already contains '" + existing.getCriteriaObject() + "'.");
} }
return this; return this;
} }

View File

@@ -0,0 +1,106 @@
/*
* 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.mongodb.gridfs;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.util.Assert;
/**
* Value object to abstract Ant paths.
*
* @author Oliver Gierke
*/
class AntPath {
private static final String PREFIX_DELIMITER = ":";
private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\?|\\*\\*|\\*");
private final String path;
/**
* Creates a new {@link AntPath} from the given path.
*
* @param path must not be {@literal null}.
*/
public AntPath(String path) {
Assert.notNull(path);
this.path = path;
}
/**
* Returns whether the path is a pattern.
*
* @return
*/
public boolean isPattern() {
String path = stripPrefix(this.path);
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
}
private static String stripPrefix(String path) {
int index = path.indexOf(PREFIX_DELIMITER);
return (index > -1 ? path.substring(index + 1) : path);
}
/**
* Returns the regular expression equivalent of this Ant path.
*
* @return
*/
public String toRegex() {
StringBuilder patternBuilder = new StringBuilder();
Matcher m = WILDCARD_PATTERN.matcher(path);
int end = 0;
while (m.find()) {
patternBuilder.append(quote(path, end, m.start()));
String match = m.group();
if ("?".equals(match)) {
patternBuilder.append('.');
} else if ("**".equals(match)) {
patternBuilder.append(".*");
} else if ("*".equals(match)) {
patternBuilder.append("[^/]*");
}
end = m.end();
}
patternBuilder.append(quote(path, end, path.length()));
return patternBuilder.toString();
}
private static String quote(String s, int start, int end) {
if (start == end) {
return "";
}
return Pattern.quote(s.substring(start, end));
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return path;
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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.mongodb.gridfs;
import org.springframework.data.mongodb.core.query.Criteria;
/**
* GridFs-specific helper class to define {@link Criteria}s.
*
* @author Oliver Gierke
*/
public class GridFsCriteria extends Criteria {
/**
* Creates a new {@link GridFsCriteria} for the given key.
*
* @param key
*/
public GridFsCriteria(String key) {
super(key);
}
/**
* Creates a {@link GridFsCriteria} for restrictions on the file's metadata.
*
* @return
*/
public static GridFsCriteria whereMetaData() {
return new GridFsCriteria("metadata");
}
/**
* Creates a {@link GridFsCriteria} for restrictions on a single file's metadata item.
*
* @param metadataKey
* @return
*/
public static GridFsCriteria whereMetaData(String metadataKey) {
String extension = metadataKey == null ? "" : "." + metadataKey;
return new GridFsCriteria(String.format("metadata%s", extension));
}
/**
* Creates a {@link GridFsCriteria} for restrictions on the file's name.
*
* @return
*/
public static GridFsCriteria whereFilename() {
return new GridFsCriteria("filename");
}
/**
* Creates a {@link GridFsCriteria} for restrictions on the file's content type.
*
* @return
*/
public static GridFsCriteria whereContentType() {
return new GridFsCriteria("contentType");
}
}

View File

@@ -0,0 +1,105 @@
/*
* 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.mongodb.gridfs;
import java.io.InputStream;
import java.util.List;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
/**
* Collection of operations to store and read files from MongoDB GridFS.
*
* @author Oliver Gierke
*/
public interface GridFsOperations extends ResourcePatternResolver {
/**
* Stores the given content into a file with the given name.
*
* @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty.
* @return the {@link GridFSFile} just created
*/
GridFSFile store(InputStream content, String filename);
/**
* Stores the given content into a file with the given name using the given metadata. The metadata object will be
* marshalled before writing.
*
* @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty.
* @param metadata
* @return the {@link GridFSFile} just created
*/
GridFSFile store(InputStream content, String filename, Object metadata);
/**
* Stores the given content into a file with the given name using the given metadata.
*
* @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty.
* @param metadata must not be {@literal null}.
* @return the {@link GridFSFile} just created
*/
GridFSFile store(InputStream content, String filename, DBObject metadata);
/**
* Returns all files matching the given query.
*
* @param query
* @return
*/
List<GridFSDBFile> find(Query query);
/**
* Returns a single file matching the given query or {@literal null} in case no file matches.
*
* @param query
* @return
*/
GridFSDBFile findOne(Query query);
/**
* Deletes all files matching the given {@link Query}.
*
* @param query
*/
void delete(Query query);
/**
* Returns all {@link GridFsResource} with the given file name.
*
* @param filename
* @return
* @see ResourcePatternResolver#getResource(String)
*/
GridFsResource getResource(String filename);
/**
* Returns all {@link GridFsResource}s matching the given file name pattern.
*
* @param filenamePattern
* @return
* @see ResourcePatternResolver#getResources(String)
*/
GridFsResource[] getResources(String filenamePattern);
}

View File

@@ -0,0 +1,88 @@
/*
* 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.mongodb.gridfs;
import java.io.IOException;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import com.mongodb.gridfs.GridFSDBFile;
/**
* {@link GridFSDBFile} based {@link Resource} implementation.
*
* @author Oliver Gierke
*/
public class GridFsResource extends InputStreamResource {
private final GridFSDBFile file;
/**
* Creates a new {@link GridFsResource} from the given {@link GridFSDBFile}.
*
* @param file must not be {@literal null}.
*/
public GridFsResource(GridFSDBFile file) {
super(file.getInputStream());
this.file = file;
}
/*
* (non-Javadoc)
* @see org.springframework.core.io.AbstractResource#contentLength()
*/
@Override
public long contentLength() throws IOException {
return file.getLength();
}
/*
* (non-Javadoc)
* @see org.springframework.core.io.AbstractResource#getFilename()
*/
@Override
public String getFilename() throws IllegalStateException {
return file.getFilename();
}
/*
* (non-Javadoc)
* @see org.springframework.core.io.AbstractResource#lastModified()
*/
@Override
public long lastModified() throws IOException {
return file.getUploadDate().getTime();
}
/**
* Returns the {@link Resource}'s id.
*
* @return
*/
public Object getId() {
return file.getId();
}
/**
* Returns the {@link Resource}'s content type.
*
* @return
*/
public String getContentType() {
return file.getContentType();
}
}

View File

@@ -0,0 +1,194 @@
/*
* 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.mongodb.gridfs;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import com.mongodb.gridfs.GridFSInputFile;
/**
* {@link GridFsOperations} implementation to store content into MongoDB GridFS.
*
* @author Oliver Gierke
*/
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
private final MongoDbFactory dbFactory;
private final String bucket;
private final MongoConverter converter;
private final QueryMapper queryMapper;
/**
* Creates a new {@link GridFsTemplate} using the given {@link MongoDbFactory} and {@link MongoConverter}.
*
* @param dbFactory must not be {@literal null}.
* @param converter must not be {@literal null}.
*/
public GridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter) {
this(dbFactory, converter, null);
}
/**
* Creates a new {@link GridFsTemplate} using the given {@link MongoDbFactory} and {@link MongoConverter}.
*
* @param dbFactory must not be {@literal null}.
* @param converter must not be {@literal null}.
* @param bucket
*/
public GridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter, String bucket) {
Assert.notNull(dbFactory);
Assert.notNull(converter);
this.dbFactory = dbFactory;
this.converter = converter;
this.bucket = bucket;
this.queryMapper = new QueryMapper(converter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String)
*/
public GridFSFile store(InputStream content, String filename) {
return store(content, filename, (Object) null);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.Object)
*/
public GridFSFile store(InputStream content, String filename, Object metadata) {
DBObject dbObject = new BasicDBObject();
converter.write(metadata, dbObject);
return store(content, filename, dbObject);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, com.mongodb.DBObject)
*/
public GridFSFile store(InputStream content, String filename, DBObject metadata) {
Assert.notNull(content);
Assert.hasText(filename);
Assert.notNull(metadata);
GridFSInputFile file = getGridFs().createFile(content);
file.setFilename(filename);
file.setMetaData(metadata);
file.save();
return file;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#find(com.mongodb.DBObject)
*/
public List<GridFSDBFile> find(Query query) {
return getGridFs().find(getMappedQuery(query));
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#findOne(com.mongodb.DBObject)
*/
public GridFSDBFile findOne(Query query) {
return getGridFs().findOne(getMappedQuery(query));
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#delete(org.springframework.data.mongodb.core.query.Query)
*/
public void delete(Query query) {
getGridFs().remove(getMappedQuery(query));
}
/*
* (non-Javadoc)
* @see org.springframework.core.io.ResourceLoader#getClassLoader()
*/
public ClassLoader getClassLoader() {
return dbFactory.getClass().getClassLoader();
}
/*
* (non-Javadoc)
* @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String)
*/
public GridFsResource getResource(String location) {
return new GridFsResource(findOne(query(whereFilename().is(location))));
}
/*
* (non-Javadoc)
* @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String)
*/
public GridFsResource[] getResources(String locationPattern) {
if (!StringUtils.hasText(locationPattern)) {
return new GridFsResource[0];
}
AntPath path = new AntPath(locationPattern);
if (path.isPattern()) {
List<GridFSDBFile> files = find(query(whereFilename().regex(path.toRegex())));
List<GridFsResource> resources = new ArrayList<GridFsResource>(files.size());
for (GridFSDBFile file : files) {
resources.add(new GridFsResource(file));
}
return resources.toArray(new GridFsResource[resources.size()]);
}
return new GridFsResource[] { getResource(locationPattern) };
}
private DBObject getMappedQuery(Query query) {
return query == null ? null : queryMapper.getMappedObject(query.getQueryObject(), null);
}
private GridFS getGridFs() {
DB db = dbFactory.getDb();
return bucket == null ? new GridFS(db) : new GridFS(db, bucket);
}
}

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,13 +15,15 @@
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.core.MongoDbUtils;
import com.mongodb.CommandResult; import com.mongodb.CommandResult;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.Mongo; import com.mongodb.Mongo;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.core.MongoDbUtils;
/** /**
* Base class to encapsulate common configuration settings when connecting to a database * Base class to encapsulate common configuration settings when connecting to a database
@@ -30,7 +32,7 @@ import org.springframework.data.mongodb.core.MongoDbUtils;
*/ */
public abstract class AbstractMonitor { public abstract class AbstractMonitor {
private final Log logger = LogFactory.getLog(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
protected Mongo mongo; protected Mongo mongo;
private String username; private String username;
@@ -51,7 +53,6 @@ public abstract class AbstractMonitor {
* @param password The password to use * @param password The password to use
*/ */
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
@@ -65,6 +66,6 @@ public abstract class AbstractMonitor {
} }
public DB getDb(String databaseName) { public DB getDb(String databaseName) {
return MongoDbUtils.getDB(mongo, databaseName, username, password == null ? null : password.toCharArray()); return MongoDbUtils.getDB(mongo, databaseName, new UserCredentials(username, password));
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2010-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,21 +32,18 @@ public interface MongoRepository<T, ID extends Serializable> extends PagingAndSo
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
* @see org.springframework.data.repository.Repository#save(java.lang.Iterable)
*/ */
List<T> save(Iterable<? extends T> entites); <S extends T> List<S> save(Iterable<S> entites);
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#findAll()
* @see org.springframework.data.repository.Repository#findAll()
*/ */
List<T> findAll(); List<T> findAll();
/* /*
* (non-Javadoc) * (non-Javadoc)
*
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort) * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/ */
List<T> findAll(Sort sort); List<T> findAll(Sort sort);

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2012 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.mongodb.repository.cdi;
import java.lang.annotation.Annotation;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.repository.cdi.CdiRepositoryBean;
import org.springframework.util.Assert;
/**
* {@link CdiRepositoryBean} to create Mongo repository instances.
*
* @author Oliver Gierke
*/
public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
private final Bean<MongoOperations> operations;
/**
* Creates a new {@link MongoRepositoryBean}.
*
* @param operations must not be {@literal null}.
* @param qualifiers must not be {@literal null}.
* @param repositoryType must not be {@literal null}.
* @param beanManager must not be {@literal null}.
*/
public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType,
BeanManager beanManager) {
super(qualifiers, repositoryType, beanManager);
Assert.notNull(operations);
this.operations = operations;
}
/*
* (non-Javadoc)
* @see javax.enterprise.inject.spi.Bean#getScope()
*/
public Class<? extends Annotation> getScope() {
return operations.getScope();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class)
*/
@Override
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
MongoOperations mongoOperations = getDependencyInstance(operations, MongoOperations.class);
MongoRepositoryFactory factory = new MongoRepositoryFactory(mongoOperations);
return factory.getRepository(repositoryType);
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.mongodb.repository.cdi;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.ProcessBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
/**
* CDI extension to export Mongo repositories.
*
* @author Oliver Gierke
*/
public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport {
private static final Logger LOG = LoggerFactory.getLogger(MongoRepositoryExtension.class);
private final Map<Set<Annotation>, Bean<MongoOperations>> mongoOperations = new HashMap<Set<Annotation>, Bean<MongoOperations>>();
public MongoRepositoryExtension() {
LOG.info("Activating CDI extension for Spring Data MongoDB repositories.");
}
@SuppressWarnings("unchecked")
<X> void processBean(@Observes ProcessBean<X> processBean) {
Bean<X> bean = processBean.getBean();
for (Type type : bean.getTypes()) {
if (type instanceof Class<?> && MongoOperations.class.isAssignableFrom((Class<?>) type)) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Discovered %s with qualifiers %s.", MongoOperations.class.getName(),
bean.getQualifiers()));
}
// Store the EntityManager bean using its qualifiers.
mongoOperations.put(new HashSet<Annotation>(bean.getQualifiers()), (Bean<MongoOperations>) bean);
}
}
}
void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) {
Class<?> repositoryType = entry.getKey();
Set<Annotation> qualifiers = entry.getValue();
// Create the bean representing the repository.
Bean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager);
if (LOG.isInfoEnabled()) {
LOG.info(String.format("Registering bean for %s with qualifiers %s.", repositoryType.getName(), qualifiers));
}
// Register the bean to the container.
afterBeanDiscovery.addBean(repositoryBean);
}
}
/**
* Creates a {@link Bean}.
*
* @param <T> The type of the repository.
* @param repositoryType The class representing the repository.
* @param beanManager The BeanManager instance.
* @return The bean.
*/
private <T> Bean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, BeanManager beanManager) {
// Determine the MongoOperations bean which matches the qualifiers of the repository.
Bean<MongoOperations> mongoOperations = this.mongoOperations.get(qualifiers);
if (mongoOperations == null) {
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
MongoOperations.class.getName(), qualifiers));
}
// Construct and return the repository bean.
return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager);
}
}

View File

@@ -48,7 +48,7 @@ public class MongoRepositoryConfigParser extends
protected void postProcessBeanDefinition(MongoRepositoryConfiguration context, BeanDefinitionBuilder builder, protected void postProcessBeanDefinition(MongoRepositoryConfiguration context, BeanDefinitionBuilder builder,
BeanDefinitionRegistry registry, Object beanSource) { BeanDefinitionRegistry registry, Object beanSource) {
builder.addPropertyReference("template", context.getMongoTemplateRef()); builder.addPropertyReference("mongoOperations", context.getMongoTemplateRef());
builder.addPropertyValue("createIndexesForQueryMethods", context.getCreateQueryIndexes()); builder.addPropertyValue("createIndexesForQueryMethods", context.getCreateQueryIndexes());
} }
} }

View File

@@ -15,15 +15,15 @@
*/ */
package org.springframework.data.mongodb.repository.query; package org.springframework.data.mongodb.repository.query;
import static org.springframework.data.mongodb.repository.query.QueryUtils.applyPagination; import static org.springframework.data.mongodb.repository.query.QueryUtils.*;
import java.util.List; import java.util.List;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.geo.Distance; import org.springframework.data.mongodb.core.geo.Distance;
import org.springframework.data.mongodb.core.geo.GeoPage;
import org.springframework.data.mongodb.core.geo.GeoResult; import org.springframework.data.mongodb.core.geo.GeoResult;
import org.springframework.data.mongodb.core.geo.GeoResults; import org.springframework.data.mongodb.core.geo.GeoResults;
import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.geo.Point;
@@ -34,10 +34,6 @@ import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
/** /**
* Base class for {@link RepositoryQuery} implementations for Mongo. * Base class for {@link RepositoryQuery} implementations for Mongo.
* *
@@ -46,42 +42,47 @@ import com.mongodb.DBObject;
public abstract class AbstractMongoQuery implements RepositoryQuery { public abstract class AbstractMongoQuery implements RepositoryQuery {
private final MongoQueryMethod method; private final MongoQueryMethod method;
private final MongoOperations mongoOperations; private final MongoOperations operations;
/** /**
* Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}. * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}.
* *
* @param method * @param method must not be {@literal null}.
* @param template * @param operations must not be {@literal null}.
*/ */
public AbstractMongoQuery(MongoQueryMethod method, MongoOperations template) { public AbstractMongoQuery(MongoQueryMethod method, MongoOperations operations) {
Assert.notNull(template); Assert.notNull(operations);
Assert.notNull(method); Assert.notNull(method);
this.method = method; this.method = method;
this.mongoOperations = template; this.operations = operations;
} }
/* (non-Javadoc) /*
* (non-Javadoc)
* @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod() * @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod()
*/ */
public MongoQueryMethod getQueryMethod() { public MongoQueryMethod getQueryMethod() {
return method; return method;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
*
* @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[]) * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[])
*/ */
public Object execute(Object[] parameters) { public Object execute(Object[] parameters) {
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters); MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters);
Query query = createQuery(new ConvertingParameterAccessor(mongoOperations.getConverter(), accessor)); Query query = createQuery(new ConvertingParameterAccessor(operations.getConverter(), accessor));
if (method.isGeoNearQuery()) { if (method.isGeoNearQuery() && method.isPageQuery()) {
MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(method, parameters);
Query countQuery = createCountQuery(new ConvertingParameterAccessor(operations.getConverter(), countAccessor));
return new GeoNearExecution(accessor).execute(query, countQuery);
} else if (method.isGeoNearQuery()) {
return new GeoNearExecution(accessor).execute(query); return new GeoNearExecution(accessor).execute(query);
} else if (method.isCollectionQuery()) { } else if (method.isCollectionQuery()) {
return new CollectionExecution().execute(query); return new CollectionExecution().execute(query);
@@ -93,14 +94,25 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
} }
/** /**
* Create a {@link Query} instance using the given {@link ParameterAccessor} * Creates a {@link Query} instance using the given {@link ParameterAccessor}
* *
* @param accessor * @param accessor must not be {@literal null}.
* @param converter
* @return * @return
*/ */
protected abstract Query createQuery(ConvertingParameterAccessor accessor); protected abstract Query createQuery(ConvertingParameterAccessor accessor);
/**
* Creates a {@link Query} instance using the given {@link ConvertingParameterAccessor}. Will delegate to
* {@link #createQuery(ConvertingParameterAccessor)} by default but allows customization of the count query to be
* triggered.
*
* @param accessor must not be {@literal null}.
* @return
*/
protected Query createCountQuery(ConvertingParameterAccessor accessor) {
return createQuery(accessor);
}
private abstract class Execution { private abstract class Execution {
abstract Object execute(Query query); abstract Object execute(Query query);
@@ -110,7 +122,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
MongoEntityInformation<?, ?> metadata = method.getEntityInformation(); MongoEntityInformation<?, ?> metadata = method.getEntityInformation();
String collectionName = metadata.getCollectionName(); String collectionName = metadata.getCollectionName();
return mongoOperations.find(query, metadata.getJavaType(), collectionName); return operations.find(query, metadata.getJavaType(), collectionName);
} }
} }
@@ -123,12 +135,10 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.query.Query)
* @see org.springframework.data.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject)
*/ */
@Override @Override
public Object execute(Query query) { public Object execute(Query query) {
return readCollection(query); return readCollection(query);
} }
} }
@@ -162,24 +172,13 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
Object execute(Query query) { Object execute(Query query) {
MongoEntityInformation<?, ?> metadata = method.getEntityInformation(); MongoEntityInformation<?, ?> metadata = method.getEntityInformation();
int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count(); long count = operations.count(query, metadata.getCollectionName());
List<?> result = mongoOperations.find(applyPagination(query, pageable), metadata.getJavaType(), List<?> result = operations.find(applyPagination(query, pageable), metadata.getJavaType(),
metadata.getCollectionName()); metadata.getCollectionName());
return new PageImpl(result, pageable, count); return new PageImpl(result, pageable, count);
} }
private DBCursor getCollectionCursor(String collectionName, final DBObject query) {
return mongoOperations.execute(collectionName, new CollectionCallback<DBCursor>() {
public DBCursor doInCollection(DBCollection collection) {
return collection.find(query);
}
});
}
} }
/** /**
@@ -197,7 +196,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
Object execute(Query query) { Object execute(Query query) {
MongoEntityInformation<?, ?> entityInformation = method.getEntityInformation(); MongoEntityInformation<?, ?> entityInformation = method.getEntityInformation();
return mongoOperations.findOne(query, entityInformation.getJavaType()); return operations.findOne(query, entityInformation.getJavaType());
} }
} }
@@ -221,6 +220,28 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
@Override @Override
Object execute(Query query) { Object execute(Query query) {
GeoResults<?> results = doExecuteQuery(query);
return isListOfGeoResult() ? results.getContent() : results;
}
/**
* Executes the given {@link Query} to return a page.
*
* @param query must not be {@literal null}.
* @param countQuery must not be {@literal null}.
* @return
*/
Object execute(Query query, Query countQuery) {
MongoEntityInformation<?, ?> information = method.getEntityInformation();
long count = operations.count(countQuery, information.getCollectionName());
return new GeoPage<Object>(doExecuteQuery(query), accessor.getPageable(), count);
}
@SuppressWarnings("unchecked")
private GeoResults<Object> doExecuteQuery(Query query) {
Point nearLocation = accessor.getGeoNearLocation(); Point nearLocation = accessor.getGeoNearLocation();
NearQuery nearQuery = NearQuery.near(nearLocation); NearQuery nearQuery = NearQuery.near(nearLocation);
@@ -234,9 +255,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
} }
MongoEntityInformation<?, ?> entityInformation = method.getEntityInformation(); MongoEntityInformation<?, ?> entityInformation = method.getEntityInformation();
GeoResults<?> results = mongoOperations.geoNear(nearQuery, entityInformation.getJavaType(), entityInformation.getCollectionName()); return (GeoResults<Object>) operations.geoNear(nearQuery, entityInformation.getJavaType(),
entityInformation.getCollectionName());
return isListOfGeoResult() ? results.getContent() : results;
} }
private boolean isListOfGeoResult() { private boolean isListOfGeoResult() {

View File

@@ -22,6 +22,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.convert.MongoWriter; import org.springframework.data.mongodb.core.convert.MongoWriter;
import org.springframework.data.mongodb.core.geo.Distance; import org.springframework.data.mongodb.core.geo.Distance;
import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.geo.Point;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.repository.query.ParameterAccessor; import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -110,6 +111,14 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
return writer.convertToMongoType(value); return writer.convertToMongoType(value);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ParameterAccessor#hasBindableNullValue()
*/
public boolean hasBindableNullValue() {
return delegate.hasBindableNullValue();
}
/** /**
* Custom {@link Iterator} to convert items before returning them. * Custom {@link Iterator} to convert items before returning them.
* *
@@ -130,7 +139,6 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
/* /*
* (non-Javadoc) * (non-Javadoc)
*
* @see java.util.Iterator#hasNext() * @see java.util.Iterator#hasNext()
*/ */
public boolean hasNext() { public boolean hasNext() {
@@ -139,25 +147,22 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
/* /*
* (non-Javadoc) * (non-Javadoc)
*
* @see java.util.Iterator#next() * @see java.util.Iterator#next()
*/ */
public Object next() { public Object next() {
return delegate.next(); return delegate.next();
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ConvertingParameterAccessor.PotentiallConvertingIterator#nextConverted()
*/
public Object nextConverted() {
return getConvertedValue(next());
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.mongodb.repository.ConvertingParameterAccessor.PotentiallConvertingIterator#nextConverted()
*/
public Object nextConverted(MongoPersistentProperty property) {
return property.isAssociation() ? writer.toDBRef(next(), property) : getConvertedValue(next());
}
/*
* (non-Javadoc)
* @see java.util.Iterator#remove() * @see java.util.Iterator#remove()
*/ */
public void remove() { public void remove() {
@@ -177,6 +182,6 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
* *
* @return * @return
*/ */
Object nextConverted(); Object nextConverted(MongoPersistentProperty property);
} }
} }

View File

@@ -17,7 +17,6 @@ package org.springframework.data.mongodb.repository.query;
import java.io.Serializable; import java.io.Serializable;
/** /**
* Interface for components being able to provide {@link EntityInformationCreator} for a given {@link Class}. * Interface for components being able to provide {@link EntityInformationCreator} for a given {@link Class}.
* *

View File

@@ -89,7 +89,8 @@ public class MongoParameters extends Parameters {
if (this.nearIndex == null && mongoParameter.isManuallyAnnotatedNearParameter()) { if (this.nearIndex == null && mongoParameter.isManuallyAnnotatedNearParameter()) {
this.nearIndex = mongoParameter.getIndex(); this.nearIndex = mongoParameter.getIndex();
} else if (mongoParameter.isManuallyAnnotatedNearParameter()) { } else if (mongoParameter.isManuallyAnnotatedNearParameter()) {
throw new IllegalStateException(String.format("Found multiple @Near annotations ond method %s! Only one allowed!", parameter.getMethod().toString())); throw new IllegalStateException(String.format(
"Found multiple @Near annotations ond method %s! Only one allowed!", parameter.getMethod().toString()));
} }
return mongoParameter; return mongoParameter;
@@ -142,8 +143,7 @@ public class MongoParameters extends Parameters {
*/ */
@Override @Override
public boolean isSpecialParameter() { public boolean isSpecialParameter() {
return super.isSpecialParameter() || getType().equals(Distance.class) return super.isSpecialParameter() || getType().equals(Distance.class) || isNearParameter();
|| isNearParameter();
} }
private boolean isNearParameter() { private boolean isNearParameter() {

View File

@@ -20,8 +20,8 @@ import static org.springframework.data.mongodb.core.query.Criteria.*;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.logging.Log; import org.slf4j.Logger;
import org.apache.commons.logging.LogFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.context.PersistentPropertyPath; import org.springframework.data.mapping.context.PersistentPropertyPath;
@@ -31,7 +31,6 @@ import org.springframework.data.mongodb.core.geo.Shape;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition; import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.OrQuery;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.PotentiallyConvertingIterator; import org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.PotentiallyConvertingIterator;
import org.springframework.data.repository.query.parser.AbstractQueryCreator; import org.springframework.data.repository.query.parser.AbstractQueryCreator;
@@ -45,9 +44,9 @@ import org.springframework.util.Assert;
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
class MongoQueryCreator extends AbstractQueryCreator<Query, Query> { class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class); private static final Logger LOG = LoggerFactory.getLogger(MongoQueryCreator.class);
private final MongoParameterAccessor accessor; private final MongoParameterAccessor accessor;
private final boolean isGeoNearQuery; private final boolean isGeoNearQuery;
@@ -92,18 +91,19 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator) * @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator)
*/ */
@Override @Override
protected Query create(Part part, Iterator<Object> iterator) { protected Criteria create(Part part, Iterator<Object> iterator) {
if (isGeoNearQuery && part.getType().equals(Type.NEAR)) { if (isGeoNearQuery && part.getType().equals(Type.NEAR)) {
return null; return null;
} }
PersistentPropertyPath<MongoPersistentProperty> path = context.getPersistentPropertyPath(part.getProperty()); PersistentPropertyPath<MongoPersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
Criteria criteria = from(part.getType(), MongoPersistentProperty property = path.getLeafProperty();
Criteria criteria = from(part.getType(), property,
where(path.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)), where(path.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)),
(PotentiallyConvertingIterator) iterator); (PotentiallyConvertingIterator) iterator);
return new Query(criteria); return criteria;
} }
/* /*
@@ -111,18 +111,20 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#and(org.springframework.data.repository.query.parser.Part, java.lang.Object, java.util.Iterator) * @see org.springframework.data.repository.query.parser.AbstractQueryCreator#and(org.springframework.data.repository.query.parser.Part, java.lang.Object, java.util.Iterator)
*/ */
@Override @Override
protected Query and(Part part, Query base, Iterator<Object> iterator) { protected Criteria and(Part part, Criteria base, Iterator<Object> iterator) {
if (base == null) { if (base == null) {
return create(part, iterator); return create(part, iterator);
} }
PersistentPropertyPath<MongoPersistentProperty> path2 = context.getPersistentPropertyPath(part.getProperty()); PersistentPropertyPath<MongoPersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
MongoPersistentProperty property = path.getLeafProperty();
Criteria criteria = from(part.getType(), Criteria criteria = from(part.getType(), property,
where(path2.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)), where(path.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)),
(PotentiallyConvertingIterator) iterator); (PotentiallyConvertingIterator) iterator);
return base.addCriteria(criteria);
return criteria.andOperator(criteria);
} }
/* /*
@@ -133,8 +135,10 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
* #or(java.lang.Object, java.lang.Object) * #or(java.lang.Object, java.lang.Object)
*/ */
@Override @Override
protected Query or(Query base, Query query) { protected Criteria or(Criteria base, Criteria criteria) {
return new OrQuery(new Query[] { base, query });
Criteria result = new Criteria();
return result.orOperator(base, criteria);
} }
/* /*
@@ -145,12 +149,13 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
* #complete(java.lang.Object, org.springframework.data.domain.Sort) * #complete(java.lang.Object, org.springframework.data.domain.Sort)
*/ */
@Override @Override
protected Query complete(Query query, Sort sort) { protected Query complete(Criteria criteria, Sort sort) {
if (query == null) { if (criteria == null) {
return null; return null;
} }
Query query = new Query(criteria);
QueryUtils.applySorting(query, sort); QueryUtils.applySorting(query, sort);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
@@ -161,37 +166,44 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
} }
/** /**
* Populates the given {@link CriteriaDefinition} depending on the {@link Type} given. * Populates the given {@link CriteriaDefinition} depending on the {@link Part} given.
* *
* @param type * @param part
* @param property
* @param criteria * @param criteria
* @param parameters * @param parameters
* @return * @return
*/ */
private Criteria from(Type type, Criteria criteria, PotentiallyConvertingIterator parameters) { private Criteria from(Type type, MongoPersistentProperty property, Criteria criteria,
PotentiallyConvertingIterator parameters) {
switch (type) { switch (type) {
case AFTER:
case GREATER_THAN: case GREATER_THAN:
return criteria.gt(parameters.nextConverted()); return criteria.gt(parameters.nextConverted(property));
case GREATER_THAN_EQUAL: case GREATER_THAN_EQUAL:
return criteria.gte(parameters.nextConverted()); return criteria.gte(parameters.nextConverted(property));
case BEFORE:
case LESS_THAN: case LESS_THAN:
return criteria.lt(parameters.nextConverted()); return criteria.lt(parameters.nextConverted(property));
case LESS_THAN_EQUAL: case LESS_THAN_EQUAL:
return criteria.lte(parameters.nextConverted()); return criteria.lte(parameters.nextConverted(property));
case BETWEEN: case BETWEEN:
return criteria.gt(parameters.nextConverted()).lt(parameters.nextConverted()); return criteria.gt(parameters.nextConverted(property)).lt(parameters.nextConverted(property));
case IS_NOT_NULL: case IS_NOT_NULL:
return criteria.ne(null); return criteria.ne(null);
case IS_NULL: case IS_NULL:
return criteria.is(null); return criteria.is(null);
case NOT_IN: case NOT_IN:
return criteria.nin(nextAsArray(parameters)); return criteria.nin(nextAsArray(parameters, property));
case IN: case IN:
return criteria.in(nextAsArray(parameters)); return criteria.in(nextAsArray(parameters, property));
case LIKE: case LIKE:
case STARTING_WITH:
case ENDING_WITH:
case CONTAINING:
String value = parameters.next().toString(); String value = parameters.next().toString();
return criteria.regex(toLikeRegex(value)); return criteria.regex(toLikeRegex(value, type));
case REGEX: case REGEX:
return criteria.regex(parameters.next().toString()); return criteria.regex(parameters.next().toString());
case EXISTS: case EXISTS:
@@ -203,7 +215,8 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
case NEAR: case NEAR:
Distance distance = accessor.getMaxDistance(); Distance distance = accessor.getMaxDistance();
Point point = nextAs(parameters, Point.class); Point point = accessor.getGeoNearLocation();
point = point == null ? nextAs(parameters, Point.class) : point;
if (distance == null) { if (distance == null) {
return criteria.near(point); return criteria.near(point);
@@ -221,9 +234,9 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
Object parameter = parameters.next(); Object parameter = parameters.next();
return criteria.within((Shape) parameter); return criteria.within((Shape) parameter);
case SIMPLE_PROPERTY: case SIMPLE_PROPERTY:
return criteria.is(parameters.nextConverted()); return criteria.is(parameters.nextConverted(property));
case NEGATING_SIMPLE_PROPERTY: case NEGATING_SIMPLE_PROPERTY:
return criteria.not().is(parameters.nextConverted()); return criteria.not().is(parameters.nextConverted(property));
} }
throw new IllegalArgumentException("Unsupported keyword!"); throw new IllegalArgumentException("Unsupported keyword!");
@@ -249,8 +262,8 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
parameter.getClass())); parameter.getClass()));
} }
private Object[] nextAsArray(PotentiallyConvertingIterator iterator) { private Object[] nextAsArray(PotentiallyConvertingIterator iterator, MongoPersistentProperty property) {
Object next = iterator.nextConverted(); Object next = iterator.nextConverted(property);
if (next instanceof Collection) { if (next instanceof Collection) {
return ((Collection<?>) next).toArray(); return ((Collection<?>) next).toArray();
@@ -261,7 +274,19 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
return new Object[] { next }; return new Object[] { next };
} }
private String toLikeRegex(String source) { private String toLikeRegex(String source, Type type) {
switch (type) {
case STARTING_WITH:
source = source + "*";
break;
case ENDING_WITH:
source = "*" + source;
break;
case CONTAINING:
source = "*" + source + "*";
break;
}
return source.replaceAll("\\*", ".*"); return source.replaceAll("\\*", ".*");
} }

View File

@@ -38,8 +38,8 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
/** /**
* Creates a new {@link PartTreeMongoQuery} from the given {@link QueryMethod} and {@link MongoTemplate}. * Creates a new {@link PartTreeMongoQuery} from the given {@link QueryMethod} and {@link MongoTemplate}.
* *
* @param method * @param method must not be {@literal null}.
* @param template * @param template must not be {@literal null}.
*/ */
public PartTreeMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations) { public PartTreeMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations) {
@@ -50,6 +50,8 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
} }
/** /**
* Return the {@link PartTree} backing the query.
*
* @return the tree * @return the tree
*/ */
public PartTree getTree() { public PartTree getTree() {
@@ -58,10 +60,7 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor, boolean)
* @see
* org.springframework.data.mongodb.repository.AbstractMongoQuery#createQuery(org.springframework.data.
* document.mongodb.repository.ConvertingParameterAccessor)
*/ */
@Override @Override
protected Query createQuery(ConvertingParameterAccessor accessor) { protected Query createQuery(ConvertingParameterAccessor accessor) {
@@ -69,4 +68,13 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isGeoNearQuery); MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isGeoNearQuery);
return creator.createQuery(); return creator.createQuery();
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createCountQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor)
*/
@Override
protected Query createCountQuery(ConvertingParameterAccessor accessor) {
return new MongoQueryCreator(tree, accessor, context, false).createQuery();
}
} }

View File

@@ -18,13 +18,14 @@ package org.springframework.data.mongodb.repository.query;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.logging.Log; import org.slf4j.Logger;
import org.apache.commons.logging.LogFactory; import org.slf4j.LoggerFactory;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.util.JSON;
/** /**
* Query to use a plain JSON String to create the {@link Query} to actually execute. * Query to use a plain JSON String to create the {@link Query} to actually execute.
* *
@@ -33,7 +34,7 @@ import org.springframework.data.mongodb.core.query.Query;
public class StringBasedMongoQuery extends AbstractMongoQuery { public class StringBasedMongoQuery extends AbstractMongoQuery {
private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)"); private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)");
private static final Log LOG = LogFactory.getLog(StringBasedMongoQuery.class); private static final Logger LOG = LoggerFactory.getLogger(StringBasedMongoQuery.class);
private final String query; private final String query;
private final String fieldSpec; private final String fieldSpec;
@@ -56,10 +57,7 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor)
* @see
* org.springframework.data.mongodb.repository.AbstractMongoQuery#createQuery(org.springframework.data.
* repository.query.SimpleParameterAccessor, org.springframework.data.mongodb.core.core.support.convert.MongoConverter)
*/ */
@Override @Override
protected Query createQuery(ConvertingParameterAccessor accessor) { protected Query createQuery(ConvertingParameterAccessor accessor) {
@@ -99,13 +97,6 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
} }
private String getParameterWithIndex(ConvertingParameterAccessor accessor, int index) { private String getParameterWithIndex(ConvertingParameterAccessor accessor, int index) {
Object parameter = accessor.getBindableValue(index); return JSON.serialize(accessor.getBindableValue(index));
if (parameter instanceof String || parameter.getClass().isEnum()) {
return String.format("\"%s\"", parameter);
} else if (parameter instanceof ObjectId) {
return String.format("{ '$oid' : '%s' }", parameter);
}
return parameter.toString();
} }
} }

View File

@@ -25,7 +25,8 @@ import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Simple {@link EntityInformationCreator} to to create {@link MongoEntityInformation} instances based on a {@link MappingContext}. * Simple {@link EntityInformationCreator} to to create {@link MongoEntityInformation} instances based on a
* {@link MappingContext}.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */

View File

@@ -19,8 +19,8 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log; import org.slf4j.Logger;
import org.apache.commons.logging.LogFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.Index;
@@ -43,7 +43,7 @@ import org.springframework.util.Assert;
class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> { class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> {
private static final Set<Type> GEOSPATIAL_TYPES = new HashSet<Type>(Arrays.asList(Type.NEAR, Type.WITHIN)); private static final Set<Type> GEOSPATIAL_TYPES = new HashSet<Type>(Arrays.asList(Type.NEAR, Type.WITHIN));
private static final Log LOG = LogFactory.getLog(IndexEnsuringQueryCreationListener.class); private static final Logger LOG = LoggerFactory.getLogger(IndexEnsuringQueryCreationListener.class);
private final MongoOperations operations; private final MongoOperations operations;

View File

@@ -81,7 +81,7 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
protected Object getTargetRepository(RepositoryMetadata metadata) { protected Object getTargetRepository(RepositoryMetadata metadata) {
Class<?> repositoryInterface = metadata.getRepositoryInterface(); Class<?> repositoryInterface = metadata.getRepositoryInterface();
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainClass()); MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
if (isQueryDslRepository(repositoryInterface)) { if (isQueryDslRepository(repositoryInterface)) {
return new QueryDslMongoRepository(entityInformation, mongoOperations); return new QueryDslMongoRepository(entityInformation, mongoOperations);

View File

@@ -17,7 +17,7 @@ package org.springframework.data.mongodb.repository.support;
import java.io.Serializable; import java.io.Serializable;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
@@ -32,17 +32,17 @@ import org.springframework.util.Assert;
public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
RepositoryFactoryBeanSupport<T, S, ID> { RepositoryFactoryBeanSupport<T, S, ID> {
private MongoTemplate template; private MongoOperations operations;
private boolean createIndexesForQueryMethods = false; private boolean createIndexesForQueryMethods = false;
/** /**
* Configures the {@link MongoTemplate} to be used. * Configures the {@link MongoOperations} to be used.
* *
* @param template the template to set * @param operations the operations to set
*/ */
public void setTemplate(MongoTemplate template) { public void setMongoOperations(MongoOperations operations) {
this.template = template; this.operations = operations;
} }
/** /**
@@ -64,10 +64,10 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
@Override @Override
protected final RepositoryFactorySupport createRepositoryFactory() { protected final RepositoryFactorySupport createRepositoryFactory() {
RepositoryFactorySupport factory = getFactoryInstance(template); RepositoryFactorySupport factory = getFactoryInstance(operations);
if (createIndexesForQueryMethods) { if (createIndexesForQueryMethods) {
factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(template)); factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(operations));
} }
return factory; return factory;
@@ -76,11 +76,11 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
/** /**
* Creates and initializes a {@link RepositoryFactorySupport} instance. * Creates and initializes a {@link RepositoryFactorySupport} instance.
* *
* @param template * @param operations
* @return * @return
*/ */
protected RepositoryFactorySupport getFactoryInstance(MongoTemplate template) { protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
return new MongoRepositoryFactory(template); return new MongoRepositoryFactory(operations);
} }
/* /*
@@ -94,6 +94,6 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
public void afterPropertiesSet() { public void afterPropertiesSet() {
super.afterPropertiesSet(); super.afterPropertiesSet();
Assert.notNull(template, "MongoTemplate must not be null!"); Assert.notNull(operations, "MongoTemplate must not be null!");
} }
} }

View File

@@ -19,7 +19,6 @@ import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.collections15.Transformer;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@@ -38,7 +37,6 @@ import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.repository.core.EntityMetadata; import org.springframework.data.repository.core.EntityMetadata;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.DBCollection;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mysema.query.mongodb.MongodbQuery; import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.mongodb.MongodbSerializer; import com.mysema.query.mongodb.MongodbSerializer;
@@ -164,13 +162,9 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/ */
private MongodbQuery<T> createQueryFor(Predicate predicate) { private MongodbQuery<T> createQueryFor(Predicate predicate) {
DBCollection collection = getMongoOperations().getCollection(getEntityInformation().getCollectionName()); Class<T> domainType = getEntityInformation().getJavaType();
MongodbQuery<T> query = new MongodbQuery<T>(collection, new Transformer<DBObject, T>() {
public T transform(DBObject input) { MongodbQuery<T> query = new SpringDataMongodbQuery<T>(getMongoOperations(), serializer, domainType);
Class<T> type = getEntityInformation().getJavaType();
return getMongoOperations().getConverter().read(type, input);
}
}, serializer);
return query.where(predicate); return query.where(predicate);
} }
@@ -252,7 +246,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
Path<?> parent = metadata.getParent(); Path<?> parent = metadata.getParent();
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(parent.getType()); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(parent.getType());
MongoPersistentProperty property = entity.getPersistentProperty(metadata.getExpression().toString()); MongoPersistentProperty property = entity.getPersistentProperty(metadata.getExpression().toString());
return property.getFieldName(); return property == null ? super.getKeyForPath(expr, metadata) : property.getFieldName();
} }
@Override @Override

View File

@@ -15,14 +15,11 @@
*/ */
package org.springframework.data.mongodb.repository.support; package org.springframework.data.mongodb.repository.support;
import org.apache.commons.collections15.Transformer;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mysema.query.mongodb.MongodbQuery; import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.mongodb.MongodbSerializer; import com.mysema.query.mongodb.MongodbSerializer;
import com.mysema.query.types.EntityPath; import com.mysema.query.types.EntityPath;
@@ -75,11 +72,6 @@ public abstract class QuerydslRepositorySupport {
Assert.notNull(path); Assert.notNull(path);
Assert.hasText(collection); Assert.hasText(collection);
DBCollection dbCollection = template.getCollection(collection); return new SpringDataMongodbQuery<T>(template, serializer, path.getType(), collection);
return new MongodbQuery<T>(dbCollection, new Transformer<DBObject, T>() {
public T transform(DBObject input) {
return template.getConverter().read(path.getType(), input);
}
}, serializer);
} }
} }

View File

@@ -20,7 +20,9 @@ import static org.springframework.data.mongodb.core.query.Criteria.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
@@ -30,9 +32,9 @@ import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation; import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.query.QueryUtils; import org.springframework.data.mongodb.repository.query.QueryUtils;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@@ -40,7 +42,7 @@ import org.springframework.util.Assert;
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> { public class SimpleMongoRepository<T, ID extends Serializable> implements MongoRepository<T, ID> {
private final MongoOperations mongoOperations; private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation; private final MongoEntityInformation<T, ID> entityInformation;
@@ -61,11 +63,9 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
* @see
* org.springframework.data.repository.Repository#save(java.lang.Object)
*/ */
public T save(T entity) { public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null!"); Assert.notNull(entity, "Entity must not be null!");
@@ -75,17 +75,15 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
* @see
* org.springframework.data.repository.Repository#save(java.lang.Iterable)
*/ */
public List<T> save(Iterable<? extends T> entities) { public <S extends T> List<S> save(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities not be null!"); Assert.notNull(entities, "The given Iterable of entities not be null!");
List<T> result = new ArrayList<T>(); List<S> result = new ArrayList<S>();
for (T entity : entities) { for (S entity : entities) {
save(entity); save(entity);
result.add(entity); result.add(entity);
} }
@@ -95,10 +93,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
* @see
* org.springframework.data.repository.Repository#findById(java.io.Serializable
* )
*/ */
public T findOne(ID id) { public T findOne(ID id) {
Assert.notNull(id, "The given id must not be null!"); Assert.notNull(id, "The given id must not be null!");
@@ -115,10 +110,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
* @see
* org.springframework.data.repository.Repository#exists(java.io.Serializable
* )
*/ */
public boolean exists(ID id) { public boolean exists(ID id) {
@@ -129,8 +121,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#count()
* @see org.springframework.data.repository.Repository#count()
*/ */
public long count() { public long count() {
@@ -139,7 +130,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.repository.Repository#delete(java.io.Serializable) * @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
*/ */
public void delete(ID id) { public void delete(ID id) {
Assert.notNull(id, "The given id must not be null!"); Assert.notNull(id, "The given id must not be null!");
@@ -148,9 +139,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
* @see
* org.springframework.data.repository.Repository#delete(java.lang.Object)
*/ */
public void delete(T entity) { public void delete(T entity) {
Assert.notNull(entity, "The given entity must not be null!"); Assert.notNull(entity, "The given entity must not be null!");
@@ -159,9 +148,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
* @see
* org.springframework.data.repository.Repository#delete(java.lang.Iterable)
*/ */
public void delete(Iterable<? extends T> entities) { public void delete(Iterable<? extends T> entities) {
@@ -174,8 +161,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#deleteAll()
* @see org.springframework.data.repository.Repository#deleteAll()
*/ */
public void deleteAll() { public void deleteAll() {
@@ -184,20 +170,29 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#findAll()
* @see org.springframework.data.repository.Repository#findAll()
*/ */
public List<T> findAll() { public List<T> findAll() {
return findAll(new Query()); return findAll(new Query());
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
* @see */
* org.springframework.data.repository.PagingAndSortingRepository#findAll public Iterable<T> findAll(Iterable<ID> ids) {
* (org.springframework.data.domain.Pageable)
Set<ID> parameters = new HashSet<ID>();
for (ID id : ids) {
parameters.add(id);
}
return findAll(new Query(new Criteria(entityInformation.getIdAttribute()).in(parameters)));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
*/ */
public Page<T> findAll(final Pageable pageable) { public Page<T> findAll(final Pageable pageable) {
@@ -209,43 +204,13 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/* /*
* (non-Javadoc) * (non-Javadoc)
* * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
* @see
* org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Sort)
*/ */
public List<T> findAll(final Sort sort) { public List<T> findAll(final Sort sort) {
return findAll(QueryUtils.applySorting(new Query(), sort)); return findAll(QueryUtils.applySorting(new Query(), sort));
} }
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#findAll(java.lang.Iterable
* )
*/
public List<T> findAll(Iterable<ID> ids) {
Query query = null;
//TODO: verify intent
// for (ID id : ids) {
// if (query == null) {
// query = getIdQuery(id);
// } else {
// query = new Query().or(getIdQuery(id));
// }
// }
List<ID> idList = new ArrayList<ID>();
for (ID id : ids) {
idList.add(id);
}
query = new Query(Criteria.where(entityInformation.getIdAttribute()).in(idList));
return findAll(query);
}
private List<T> findAll(Query query) { private List<T> findAll(Query query) {
if (query == null) { if (query == null) {

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2012 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.mongodb.repository.support;
import org.springframework.data.mongodb.core.MongoOperations;
import com.google.common.base.Function;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.mongodb.MongodbSerializer;
/**
* Spring Data specfic {@link MongodbQuery} implementation.
*
* @author Oliver Gierke
*/
class SpringDataMongodbQuery<T> extends MongodbQuery<T> {
private final MongoOperations operations;
/**
* Creates a new {@link SpringDataMongodbQuery}.
*
* @param operations must not be {@literal null}.
* @param serializer must not be {@literal null}.
* @param type must not be {@literal null}.
*/
public SpringDataMongodbQuery(final MongoOperations operations, final MongodbSerializer serializer,
final Class<? extends T> type) {
this(operations, serializer, type, operations.getCollectionName(type));
}
/**
* Creates a new {@link SpringDataMongodbQuery} to query the given collection.
*
* @param operations must not be {@literal null}.
* @param serializer must not be {@literal null}.
* @param type must not be {@literal null}.
* @param collectionName must not be {@literal null} or empty.
*/
public SpringDataMongodbQuery(final MongoOperations operations, final MongodbSerializer serializer,
final Class<? extends T> type, String collectionName) {
super(operations.getCollection(collectionName), new Function<DBObject, T>() {
public T apply(DBObject input) {
return operations.getConverter().read(type, input);
}
}, serializer);
this.operations = operations;
}
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbQuery#getCollection(java.lang.Class)
*/
@Override
protected DBCollection getCollection(Class<?> type) {
return operations.getCollection(operations.getCollectionName(type));
}
}

View File

@@ -0,0 +1 @@
org.springframework.data.mongodb.repository.cdi.MongoRepositoryExtension

View File

@@ -1,2 +1,3 @@
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd=org/springframework/data/mongodb/config/spring-mongo-1.1.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/springframework/data/mongodb/config/spring-mongo-1.0.xsd http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/springframework/data/mongodb/config/spring-mongo-1.0.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.0.xsd http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.1.xsd

View File

@@ -7,9 +7,7 @@
xmlns:context="http://www.springframework.org/schema/context" xmlns:context="http://www.springframework.org/schema/context"
xmlns:repository="http://www.springframework.org/schema/data/repository" xmlns:repository="http://www.springframework.org/schema/data/repository"
targetNamespace="http://www.springframework.org/schema/data/mongo" targetNamespace="http://www.springframework.org/schema/data/mongo"
elementFormDefault="qualified" attributeFormDefault="unqualified" elementFormDefault="qualified" attributeFormDefault="unqualified">
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" /> <xsd:import namespace="http://www.springframework.org/schema/tool" />

View File

@@ -0,0 +1,479 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.springframework.org/schema/data/mongo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:repository="http://www.springframework.org/schema/data/repository"
targetNamespace="http://www.springframework.org/schema/data/mongo"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:import namespace="http://www.springframework.org/schema/context"
schemaLocation="http://www.springframework.org/schema/context/spring-context.xsd" />
<xsd:import namespace="http://www.springframework.org/schema/data/repository"
schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd" />
<xsd:element name="mongo" type="mongoType">
<xsd:annotation>
<xsd:documentation source="org.springframework.data.mongodb.core.core.MongoFactoryBean"><![CDATA[
Defines a Mongo instance used for accessing MongoDB'.
]]></xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:exports type="com.mongodb.Mongo"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="db-factory">
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines a MongoDbFactory for connecting to a specific database
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="id" type="xsd:ID" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the mongo definition (by default "mongoDbFactory").]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mongo-ref" type="mongoRef" use="optional">
<xsd:annotation>
<xsd:documentation>
The reference to a Mongo. Will default to 'mongo'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="dbname" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the database to connect to. Default is 'db'.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="port" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The port to connect to MongoDB server. Default is 27017
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="host" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The host to connect to a MongoDB server. Default is localhost
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="username" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The username to use when connecting to a MongoDB server.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="password" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The password to use when connecting to a MongoDB server.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="uri" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The Mongo URI string.]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="write-concern">
<xsd:annotation>
<xsd:documentation>
The WriteConcern that will be the default value used when asking the MongoDbFactory for a DB object
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="writeConcernEnumeration xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="mongo-repository">
<xsd:complexContent>
<xsd:extension base="repository:repository">
<xsd:attributeGroup ref="mongo-repository-attributes"/>
<xsd:attributeGroup ref="repository:repository-attributes"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:attributeGroup name="mongo-repository-attributes">
<xsd:attribute name="mongo-template-ref" type="mongoTemplateRef" default="mongoTemplate">
<xsd:annotation>
<xsd:documentation>
The reference to a MongoTemplate. Will default to 'mongoTemplate'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="create-query-indexes" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation>
Enables creation of indexes for queries that get derived from the method name
and thus reference domain class properties. Defaults to false.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:attributeGroup>
<xsd:element name="repositories">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="repository:repositories">
<xsd:sequence>
<xsd:element name="repository" minOccurs="0" maxOccurs="unbounded" type="mongo-repository"/>
</xsd:sequence>
<xsd:attributeGroup ref="mongo-repository-attributes"/>
<xsd:attributeGroup ref="repository:repository-attributes"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="mapping-converter">
<xsd:annotation>
<xsd:documentation><![CDATA[Defines a MongoConverter for getting rich mapping functionality.]]></xsd:documentation>
<xsd:appinfo>
<tool:exports type="org.springframework.data.mongodb.core.convert.MappingMongoConverter" />
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="custom-converters" minOccurs="0">
<xsd:annotation>
<xsd:documentation><![CDATA[
Top-level element that contains one or more custom converters to be used for mapping
domain objects to and from Mongo's DBObject]]>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="converter" type="customConverterType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="base-package" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the MappingMongoConverter instance (by default "mappingConverter").]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="base-package" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The base package in which to scan for entities annotated with @Document
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="db-factory-ref" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>
The reference to a DbFactory.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to type="org.springframework.data.mongodb.MongoDbFactory" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mongo-ref" type="mongoRef" use="optional">
<xsd:annotation>
<xsd:documentation>
The reference to a Mongo. Will default to 'mongo'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapping-context-ref" type="mappingContextRef" use="optional">
<xsd:annotation>
<xsd:documentation source="org.springframework.data.mapping.model.MappingContext">
The reference to a MappingContext. Will default to 'mappingContext'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mongo-template-ref" type="mongoTemplateRef" use="optional">
<xsd:annotation>
<xsd:documentation source="org.springframework.data.mongodb.core.core.MongoTemplate">
The reference to a MongoTemplate. Will default to 'mongoTemplate'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="disable-validation" use="optional">
<xsd:annotation>
<xsd:documentation source="org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener">
Disables JSR-303 validation on MongoDB documents before they are saved. By default it is set to false.
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="xsd:boolean xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="jmx">
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines a JMX Model MBeans for monitoring a MongoDB server'.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="mongo-ref" type="mongoRef" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the Mongo object that determines what server to monitor. (by default "mongo").]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="mappingContextRef">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to type="org.springframework.data.mapping.model.MappingContext"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:union memberTypes="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="mongoTemplateRef">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to type="org.springframework.data.mongodb.core.core.MongoTemplate"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:union memberTypes="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="mongoRef">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to type="org.springframework.data.mongodb.core.core.MongoFactoryBean"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:union memberTypes="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="writeConcernEnumeration">
<xsd:restriction base="xsd:token">
<xsd:enumeration value="NONE" />
<xsd:enumeration value="NORMAL" />
<xsd:enumeration value="SAFE" />
<xsd:enumeration value="FSYNC_SAFE" />
<xsd:enumeration value="REPLICAS_SAFE" />
<xsd:enumeration value="JOURNAL_SAFE" />
<xsd:enumeration value="MAJORITY" />
</xsd:restriction>
</xsd:simpleType>
<!-- MLP
<xsd:attributeGroup name="writeConcern">
<xsd:attribute name="write-concern">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="NONE" />
<xsd:enumeration value="NORMAL" />
<xsd:enumeration value="SAFE" />
<xsd:enumeration value="FSYNC_SAFE" />
<xsd:enumeration value="REPLICA_SAFE" />
<xsd:enumeration value="JOURNAL_SAFE" />
<xsd:enumeration value="MAJORITY" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:attributeGroup>
-->
<xsd:complexType name="mongoType">
<xsd:sequence minOccurs="0" maxOccurs="1">
<xsd:element name="options" type="optionsType">
<xsd:annotation>
<xsd:documentation><![CDATA[
The Mongo driver options
]]></xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:exports type="com.mongodb.MongoOptions"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="write-concern">
<xsd:annotation>
<xsd:documentation>
The WriteConcern that will be the default value used when asking the MongoDbFactory for a DB object
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="writeConcernEnumeration xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
<!-- MLP
<xsd:attributeGroup ref="writeConcern" />
-->
<xsd:attribute name="id" type="xsd:ID" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the mongo definition (by default "mongo").]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="port" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The port to connect to MongoDB server. Default is 27017
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="host" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The host to connect to a MongoDB server. Default is localhost
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="replica-set" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The comma delimited list of host:port entries to use for replica set/pairs.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="optionsType">
<xsd:attribute name="connections-per-host" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The number of connections allowed per host. Will block if run out. Default is 10. System property MONGO.POOLSIZE can override
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="threads-allowed-to-block-for-connection-multiplier" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The multiplier for connectionsPerHost for # of threads that can block. Default is 5.
If connectionsPerHost is 10, and threadsAllowedToBlockForConnectionMultiplier is 5,
then 50 threads can block more than that and an exception will be thrown.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="max-wait-time" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The max wait time of a blocking thread for a connection. Default is 12000 ms (2 minutes)
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="connect-timeout" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The connect timeout in milliseconds. 0 is default and infinite.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="socket-timeout" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The socket timeout. 0 is default and infinite.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="socket-keep-alive" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The keep alive flag, controls whether or not to have socket keep alive timeout. Defaults to false.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="auto-connect-retry" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
This controls whether or not on a connect, the system retries automatically. Default is false.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="max-auto-connect-retry-time" type="xsd:long">
<xsd:annotation>
<xsd:documentation><![CDATA[
The maximum amount of time in millisecons to spend retrying to open connection to the same server. Default is 0, which means to use the default 15s if autoConnectRetry is on.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="write-number" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
This specifies the number of servers to wait for on the write operation, and exception raising behavior. The 'w' option to the getlasterror command. Defaults to 0.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="write-timeout" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
This controls timeout for write operations in milliseconds. The 'wtimeout' option to the getlasterror command. Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="write-fsync" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
This controls whether or not to fsync. The 'fsync' option to the getlasterror command. Defaults to false.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="slave-ok" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
This controls if the driver is allowed to read from secondaries or slaves. Defaults to false.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:group name="beanElementGroup">
<xsd:choice>
<xsd:element ref="beans:bean"/>
<xsd:element ref="beans:ref"/>
</xsd:choice>
</xsd:group>
<xsd:complexType name="customConverterType">
<xsd:annotation>
<xsd:documentation><![CDATA[
Element defining a custom converterr.
]]></xsd:documentation>
</xsd:annotation>
<xsd:group ref="beanElementGroup" minOccurs="0" maxOccurs="1"/>
<xsd:attribute name="ref" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
A reference to a custom converter.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:schema>

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ import org.springframework.stereotype.Component;
import com.mongodb.DBObject; import com.mongodb.DBObject;
/** /**
* Integration tests for {@link MongoParser}. * Integration tests for {@link MappingMongoConverterParser}.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@@ -45,7 +45,6 @@ public class MappingMongoConverterParserIntegrationTests {
DefaultListableBeanFactory factory; DefaultListableBeanFactory factory;
@Before @Before
public void setUp() { public void setUp() {
factory = new DefaultListableBeanFactory(); factory = new DefaultListableBeanFactory();

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2012 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.mongodb.config;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
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;
/**
* Integration test for creation of instance of
* {@link org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener} by defining
* {@code <mongo:mapping-converter />} in context XML.
*
* @see DATAMONGO-36
* @author Maciej Walkowiak
*/
public class MappingMongoConverterParserValidationIntegrationTests {
DefaultListableBeanFactory factory;
BeanDefinitionReader reader;
@Before
public void setUp() {
factory = new DefaultListableBeanFactory();
reader = new XmlBeanDefinitionReader(factory);
}
@Test
public void validatingEventListenerCreatedWithDefaultConfig() {
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-default.xml"));
assertThat(factory.getBean(BeanNames.VALIDATING_EVENT_LISTENER), is(not(nullValue())));
}
@Test
public void validatingEventListenerCreatedWhenValidationEnabled() {
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-validation-enabled.xml"));
assertThat(factory.getBean(BeanNames.VALIDATING_EVENT_LISTENER), is(not(nullValue())));
}
@Test(expected = NoSuchBeanDefinitionException.class)
public void validatingEventListenersIsNotCreatedWhenDisabled() {
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-validation-disabled.xml"));
factory.getBean(BeanNames.VALIDATING_EVENT_LISTENER);
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.Mongo; import com.mongodb.Mongo;
@@ -59,7 +60,7 @@ public class MongoDbFactoryParserIntegrationTests {
SimpleMongoDbFactory dbFactory = new SimpleMongoDbFactory(new Mongo("localhost"), "database"); SimpleMongoDbFactory dbFactory = new SimpleMongoDbFactory(new Mongo("localhost"), "database");
dbFactory.setWriteConcern(WriteConcern.SAFE); dbFactory.setWriteConcern(WriteConcern.SAFE);
dbFactory.getDb(); dbFactory.getDb();
assertThat(WriteConcern.SAFE, is(dbFactory.getWriteConcern())); assertThat(ReflectionTestUtils.getField(dbFactory, "writeConcern"), is((Object) WriteConcern.SAFE));
} }
@Test @Test
@@ -70,7 +71,8 @@ public class MongoDbFactoryParserIntegrationTests {
@Test @Test
public void parsesCustomWriteConcern() { public void parsesCustomWriteConcern() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("namespace/db-factory-bean-custom-write-concern.xml"); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"namespace/db-factory-bean-custom-write-concern.xml");
assertWriteConcern(ctx, new WriteConcern("rack1")); assertWriteConcern(ctx, new WriteConcern("rack1"));
} }
@@ -90,16 +92,17 @@ public class MongoDbFactoryParserIntegrationTests {
private void assertWriteConcern(ClassPathXmlApplicationContext ctx, WriteConcern expectedWriteConcern) { private void assertWriteConcern(ClassPathXmlApplicationContext ctx, WriteConcern expectedWriteConcern) {
SimpleMongoDbFactory dbFactory = ctx.getBean("first", SimpleMongoDbFactory.class); SimpleMongoDbFactory dbFactory = ctx.getBean("first", SimpleMongoDbFactory.class);
DB db = dbFactory.getDb(); DB db = dbFactory.getDb();
assertThat("db", is(db.getName())); assertThat(db.getName(), is("db"));
MyWriteConcern myDbFactoryWriteConcern = new MyWriteConcern(dbFactory.getWriteConcern()); WriteConcern configuredConcern = (WriteConcern) ReflectionTestUtils.getField(dbFactory, "writeConcern");
MyWriteConcern myDbFactoryWriteConcern = new MyWriteConcern(configuredConcern);
MyWriteConcern myDbWriteConcern = new MyWriteConcern(db.getWriteConcern()); MyWriteConcern myDbWriteConcern = new MyWriteConcern(db.getWriteConcern());
MyWriteConcern myExpectedWriteConcern = new MyWriteConcern(expectedWriteConcern); MyWriteConcern myExpectedWriteConcern = new MyWriteConcern(expectedWriteConcern);
assertThat(myDbFactoryWriteConcern, is(myExpectedWriteConcern));
assertThat(myDbFactoryWriteConcern, equalTo(myExpectedWriteConcern)); assertThat(myDbWriteConcern, is(myExpectedWriteConcern));
assertThat(myDbWriteConcern, equalTo(myExpectedWriteConcern)); assertThat(myDbWriteConcern, is(myDbFactoryWriteConcern));
assertThat(myDbWriteConcern, equalTo(myDbFactoryWriteConcern));
} }
// This test will fail since equals in WriteConcern uses == for _w and not .equals // This test will fail since equals in WriteConcern uses == for _w and not .equals
@@ -108,11 +111,9 @@ public class MongoDbFactoryParserIntegrationTests {
String s2 = new String("rack1"); String s2 = new String("rack1");
WriteConcern wc1 = new WriteConcern(s1); WriteConcern wc1 = new WriteConcern(s1);
WriteConcern wc2 = new WriteConcern(s2); WriteConcern wc2 = new WriteConcern(s2);
assertThat(wc1, equalTo(wc2)); assertThat(wc1, is(wc2));
} }
@Test @Test
public void createsDbFactoryBean() { public void createsDbFactoryBean() {

View File

@@ -16,17 +16,19 @@
package org.springframework.data.mongodb.config; package org.springframework.data.mongodb.config;
import static org.springframework.test.util.ReflectionTestUtils.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoFactoryBean; import org.springframework.data.mongodb.core.MongoFactoryBean;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.Mongo; import com.mongodb.Mongo;
import com.mongodb.MongoOptions; import com.mongodb.MongoOptions;
@@ -62,8 +64,7 @@ public class MongoNamespaceTests {
Mongo mongo = (Mongo) getField(dbf, "mongo"); Mongo mongo = (Mongo) getField(dbf, "mongo");
assertEquals("localhost", mongo.getAddress().getHost()); assertEquals("localhost", mongo.getAddress().getHost());
assertEquals(27017, mongo.getAddress().getPort()); assertEquals(27017, mongo.getAddress().getPort());
assertEquals("joe", getField(dbf, "username")); assertEquals(new UserCredentials("joe", "secret"), getField(dbf, "credentials"));
assertEquals("secret", getField(dbf, "password"));
assertEquals("database", getField(dbf, "databaseName")); assertEquals("database", getField(dbf, "databaseName"));
} }

View File

@@ -17,6 +17,7 @@ public class MyWriteConcern {
boolean _fsync = false; boolean _fsync = false;
boolean _j = false; boolean _j = false;
boolean _continueOnErrorForInsert = false; boolean _continueOnErrorForInsert = false;
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@@ -28,6 +29,7 @@ public class MyWriteConcern {
result = prime * result + _wtimeout; result = prime * result + _wtimeout;
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)

View File

@@ -20,7 +20,6 @@ import java.lang.reflect.Field;
public class NamespaceTestSupport { public class NamespaceTestSupport {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public static <T> T readField(String name, Object target) throws Exception { public static <T> T readField(String name, Object target) throws Exception {
Field field = null; Field field = null;

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2012 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.mongodb.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import com.mongodb.WriteConcern;
/**
* Unit tests for {@link StringToWriteConcernConverter}.
*
* @author Oliver Gierke
*/
public class StringToWriteConcernConverterUnitTest {
StringToWriteConcernConverter converter = new StringToWriteConcernConverter();
@Test
public void createsWellKnownConstantsCorrectly() {
assertThat(converter.convert("SAFE"), is(WriteConcern.SAFE));
}
@Test
public void createsWriteConcernForUnknownValue() {
assertThat(converter.convert("-1"), is(new WriteConcern("-1")));
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2012 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.mongodb.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.WriteConcern;
/**
* Unit tests for {@link WriteConcernPropertyEditor}.
*
* @author Oliver Gierke
*/
public class WriteConcernPropertyEditorUnitTests {
WriteConcernPropertyEditor editor;
@Before
public void setUp() {
editor = new WriteConcernPropertyEditor();
}
@Test
public void createsWriteConcernForWellKnownConstants() {
editor.setAsText("SAFE");
assertThat(editor.getValue(), is((Object) WriteConcern.SAFE));
}
@Test
public void createsWriteConcernForUnknownConstants() {
editor.setAsText("-1");
assertThat(editor.getValue(), is((Object) new WriteConcern("-1")));
}
}

View File

@@ -15,7 +15,6 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
public class Friend { public class Friend {
private String id; private String id;

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2012 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.mongodb.core;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.data.mongodb.config.WriteConcernPropertyEditor;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.WriteConcern;
/**
* Integration tests for {@link MongoFactoryBean}.
*
* @author Oliver Gierke
*/
public class MongoFactoryBeanIntegrationTest {
/**
* @see DATAMONGO-408
*/
@Test
public void convertsWriteConcernCorrectly() {
RootBeanDefinition definition = new RootBeanDefinition(MongoFactoryBean.class);
definition.getPropertyValues().addPropertyValue("writeConcern", "SAFE");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerCustomEditor(WriteConcern.class, WriteConcernPropertyEditor.class);
factory.registerBeanDefinition("factory", definition);
MongoFactoryBean bean = factory.getBean("&factory", MongoFactoryBean.class);
assertThat(ReflectionTestUtils.getField(bean, "writeConcern"), is((Object) WriteConcern.SAFE));
}
}

View File

@@ -37,6 +37,7 @@ import org.springframework.data.mongodb.core.query.NearQuery;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.DBRef;
/** /**
* Abstract base class for unit tests to specify behaviour we expect from {@link MongoOperations}. Subclasses return * Abstract base class for unit tests to specify behaviour we expect from {@link MongoOperations}. Subclasses return
@@ -80,6 +81,10 @@ public abstract class MongoOperationsUnitTests {
public Object convertToMongoType(Object obj) { public Object convertToMongoType(Object obj) {
return null; return null;
} }
public DBRef toDBRef(Object object, MongoPersistentProperty referingProperty) {
return null;
}
}; };
} }

View File

@@ -47,6 +47,7 @@ import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.Index.Duplicates; import org.springframework.data.mongodb.core.index.Index.Duplicates;
import org.springframework.data.mongodb.core.index.IndexField;
import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
@@ -130,6 +131,7 @@ public class MongoTemplateTests {
template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeLong.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeLong.class));
template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfPrimitiveLong.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfPrimitiveLong.class));
template.dropCollection(template.getCollectionName(TestClass.class)); template.dropCollection(template.getCollectionName(TestClass.class));
template.dropCollection(Sample.class);
} }
@Test @Test
@@ -195,8 +197,11 @@ public class MongoTemplateTests {
assertThat(ii.isUnique(), is(true)); assertThat(ii.isUnique(), is(true));
assertThat(ii.isDropDuplicates(), is(true)); assertThat(ii.isDropDuplicates(), is(true));
assertThat(ii.isSparse(), is(false)); assertThat(ii.isSparse(), is(false));
assertThat(ii.getFieldSpec().containsKey("age"), is(true));
assertThat(ii.getFieldSpec().containsValue(Order.DESCENDING), is(true)); List<IndexField> indexFields = ii.getIndexFields();
IndexField field = indexFields.get(0);
assertThat(field, is(IndexField.create("age", Order.DESCENDING)));
} }
@Test @Test
@@ -1041,7 +1046,7 @@ public class MongoTemplateTests {
List<TestClass> testClassList = mappingTemplate.find(new Query(Criteria.where("myDate").is(dateTime.toDate())), List<TestClass> testClassList = mappingTemplate.find(new Query(Criteria.where("myDate").is(dateTime.toDate())),
TestClass.class); TestClass.class);
assertThat(testClassList.size(), is(1)); assertThat(testClassList.size(), is(1));
assertThat(testClassList.get(0).getMyDate(), is(testClass.getMyDate())); assertThat(testClassList.get(0).myDate, is(testClass.myDate));
} }
/** /**
@@ -1080,23 +1085,42 @@ public class MongoTemplateTests {
assertThat(template.findOne(query(where("id").is(id)), Sample.class), is(nullValue())); assertThat(template.findOne(query(where("id").is(id)), Sample.class), is(nullValue()));
} }
public class Sample { /**
* @see DATAMONGO-423
*/
@Test
public void executesQueryWithNegatedRegexCorrectly() {
Sample first = new Sample();
first.field = "Matthews";
Sample second = new Sample();
second.field = "Beauford";
template.save(first);
template.save(second);
Query query = query(where("field").not().regex("Matthews"));
System.out.println(query.getQueryObject());
List<Sample> result = template.find(query, Sample.class);
assertThat(result.size(), is(1));
assertThat(result.get(0).field, is("Beauford"));
}
public static class Sample {
@Id @Id
String id; String id;
String field;
} }
public class TestClass { static class TestClass {
private DateTime myDate; DateTime myDate;
@PersistenceConstructor @PersistenceConstructor
public TestClass(DateTime date) { TestClass(DateTime myDate) {
this.myDate = date; this.myDate = myDate;
}
public DateTime getMyDate() {
return myDate;
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,9 +17,11 @@ package org.springframework.data.mongodb.core;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Collections;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.junit.Before; import org.junit.Before;
@@ -29,10 +31,17 @@ import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.DB; import com.mongodb.DB;
@@ -51,20 +60,24 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
MongoTemplate template; MongoTemplate template;
@Mock
MongoDbFactory factory;
@Mock @Mock
Mongo mongo; Mongo mongo;
@Mock @Mock
DB db; DB db;
@Mock @Mock
DBCollection collection; DBCollection collection;
MappingMongoConverter converter;
@Before @Before
public void setUp() { public void setUp() {
this.template = new MongoTemplate(mongo, "database");
when(mongo.getDB("database")).thenReturn(db); this.converter = new MappingMongoConverter(factory, new MongoMappingContext());
this.template = new MongoTemplate(factory, converter);
when(factory.getDb()).thenReturn(db);
when(db.getCollection(Mockito.any(String.class))).thenReturn(collection); when(db.getCollection(Mockito.any(String.class))).thenReturn(collection);
} }
@@ -126,6 +139,8 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
@Test @Test
public void autogeneratesIdForEntityWithAutogeneratableId() { public void autogeneratesIdForEntityWithAutogeneratableId() {
this.converter.afterPropertiesSet();
MongoTemplate template = spy(this.template); MongoTemplate template = spy(this.template);
doReturn(new ObjectId()).when(template).saveDBObject(Mockito.any(String.class), Mockito.any(DBObject.class), doReturn(new ObjectId()).when(template).saveDBObject(Mockito.any(String.class), Mockito.any(DBObject.class),
Mockito.any(Class.class)); Mockito.any(Class.class));
@@ -136,6 +151,27 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
assertThat(entity.id, is(notNullValue())); assertThat(entity.id, is(notNullValue()));
} }
/**
* @see DATAMONGO-374
*/
@Test
public void convertsUpdateConstraintsUsingConverters() {
CustomConversions conversions = new CustomConversions(Collections.singletonList(MyConverter.INSTANCE));
this.converter.setCustomConversions(conversions);
this.converter.afterPropertiesSet();
Query query = new Query();
Update update = new Update().set("foo", new AutogenerateableId());
template.updateFirst(query, update, Wrapper.class);
QueryMapper queryMapper = new QueryMapper(converter);
DBObject reference = queryMapper.getMappedObject(update.getUpdateObject(), null);
verify(collection, times(1)).update(Mockito.any(DBObject.class), eq(reference), anyBoolean(), anyBoolean());
}
class AutogenerateableId { class AutogenerateableId {
@Id @Id
@@ -148,6 +184,20 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
Integer id; Integer id;
} }
enum MyConverter implements Converter<AutogenerateableId, String> {
INSTANCE;
public String convert(AutogenerateableId source) {
return source.toString();
}
}
class Wrapper {
AutogenerateableId foo;
}
/** /**
* Mocks out the {@link MongoTemplate#getDb()} method to return the {@link DB} mock instead of executing the actual * Mocks out the {@link MongoTemplate#getDb()} method to return the {@link DB} mock instead of executing the actual
* behaviour. * behaviour.

View File

@@ -15,8 +15,8 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
@@ -70,8 +71,8 @@ public class SimpleMongoDbFactoryUnitTests {
MongoURI mongoURI = new MongoURI("mongodb://myUsername:myPassword@localhost/myDatabase.myCollection"); MongoURI mongoURI = new MongoURI("mongodb://myUsername:myPassword@localhost/myDatabase.myCollection");
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoURI); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoURI);
assertThat(ReflectionTestUtils.getField(mongoDbFactory, "username").toString(), is("myUsername")); assertThat(ReflectionTestUtils.getField(mongoDbFactory, "credentials"), is((Object) new UserCredentials(
assertThat(ReflectionTestUtils.getField(mongoDbFactory, "password").toString(), is("myPassword")); "myUsername", "myPassword")));
assertThat(ReflectionTestUtils.getField(mongoDbFactory, "databaseName").toString(), is("myDatabase")); assertThat(ReflectionTestUtils.getField(mongoDbFactory, "databaseName").toString(), is("myDatabase"));
assertThat(ReflectionTestUtils.getField(mongoDbFactory, "databaseName").toString(), is("myDatabase")); assertThat(ReflectionTestUtils.getField(mongoDbFactory, "databaseName").toString(), is("myDatabase"));
} }

Some files were not shown because too many files have changed in this diff Show More