1
1
/*
2
- * Copyright 2014 the original author or authors.
2
+ * Copyright 2014-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
28
28
import org .bson .types .ObjectId ;
29
29
import org .springframework .dao .DataAccessException ;
30
- import org .springframework .data .mongodb .core .script .CallableMongoScript ;
31
- import org .springframework .data .mongodb .core .script .ServerSideJavaScript ;
30
+ import org .springframework .data .mongodb .core .script .ExecutableMongoScript ;
31
+ import org .springframework .data .mongodb .core .script .NamedMongoScript ;
32
32
import org .springframework .util .Assert ;
33
33
import org .springframework .util .CollectionUtils ;
34
34
import org .springframework .util .ObjectUtils ;
41
41
* Default implementation of {@link ScriptOperations} capable of saving and executing {@link ServerSideJavaScript}.
42
42
*
43
43
* @author Christoph Strobl
44
+ * @author Oliver Gierke
44
45
* @since 1.7
45
46
*/
46
- public class DefaultScriptOperations implements ScriptOperations {
47
+ class DefaultScriptOperations implements ScriptOperations {
47
48
48
49
private static final String SCRIPT_COLLECTION_NAME = "system.js" ;
49
50
private static final String SCRIPT_NAME_PREFIX = "func_" ;
51
+
50
52
private final MongoOperations mongoOperations ;
51
53
52
54
/**
@@ -63,39 +65,39 @@ public DefaultScriptOperations(MongoOperations mongoOperations) {
63
65
64
66
/*
65
67
* (non-Javadoc)
66
- * @see org.springframework.data.mongodb.core.ScriptOperations#save(org.springframework.data.mongodb.core.script.MongoScript)
68
+ * @see org.springframework.data.mongodb.core.ScriptOperations#register(org.springframework.data.mongodb.core.script.ExecutableMongoScript)
69
+ */
70
+ @ Override
71
+ public NamedMongoScript register (ExecutableMongoScript script ) {
72
+ return register (new NamedMongoScript (generateScriptName (), script ));
73
+ }
74
+
75
+ /*
76
+ * (non-Javadoc)
77
+ * @see org.springframework.data.mongodb.core.ScriptOperations#register(org.springframework.data.mongodb.core.script.NamedMongoScript)
67
78
*/
68
79
@ Override
69
- public CallableMongoScript register (ServerSideJavaScript script ) {
80
+ public NamedMongoScript register (NamedMongoScript script ) {
70
81
71
82
Assert .notNull (script , "Script must not be null!" );
72
83
73
- CallableMongoScript callableScript = (script instanceof CallableMongoScript ) ? (CallableMongoScript ) script
74
- : new CallableMongoScript (generateScriptName (), script );
75
- mongoOperations .save (callableScript , SCRIPT_COLLECTION_NAME );
76
- return callableScript ;
84
+ mongoOperations .save (script , SCRIPT_COLLECTION_NAME );
85
+ return script ;
77
86
}
78
87
79
88
/*
80
89
* (non-Javadoc)
81
- * @see org.springframework.data.mongodb.core.ScriptOperations#execute(org.springframework.data.mongodb.core.script.MongoScript , java.lang.Object[])
90
+ * @see org.springframework.data.mongodb.core.ScriptOperations#execute(org.springframework.data.mongodb.core.script.ExecutableMongoScript , java.lang.Object[])
82
91
*/
83
92
@ Override
84
- public Object execute (final ServerSideJavaScript script , final Object ... args ) {
93
+ public Object execute (final ExecutableMongoScript script , final Object ... args ) {
85
94
86
95
Assert .notNull (script , "Script must not be null!" );
87
96
88
- if (script instanceof CallableMongoScript ) {
89
- return call (((CallableMongoScript ) script ).getName (), args );
90
- }
91
-
92
97
return mongoOperations .execute (new DbCallback <Object >() {
93
98
94
99
@ Override
95
100
public Object doInDB (DB db ) throws MongoException , DataAccessException {
96
-
97
- Assert .notNull (script .getCode (), "Script.code must not be null!" );
98
-
99
101
return db .eval (script .getCode (), convertScriptArgs (args ));
100
102
}
101
103
});
@@ -114,9 +116,7 @@ public Object call(final String scriptName, final Object... args) {
114
116
115
117
@ Override
116
118
public Object doInDB (DB db ) throws MongoException , DataAccessException {
117
-
118
- String evalString = scriptName + "(" + convertAndJoinScriptArgs (args ) + ")" ;
119
- return db .eval (evalString );
119
+ return db .eval (String .format ("%s(%s)" , scriptName , convertAndJoinScriptArgs (args )));
120
120
}
121
121
});
122
122
}
@@ -126,43 +126,33 @@ public Object doInDB(DB db) throws MongoException, DataAccessException {
126
126
* @see org.springframework.data.mongodb.core.ScriptOperations#exists(java.lang.String)
127
127
*/
128
128
@ Override
129
- public Boolean exists (String scriptName ) {
129
+ public boolean exists (String scriptName ) {
130
130
131
131
Assert .hasText (scriptName , "ScriptName must not be null or empty!" );
132
132
133
- return mongoOperations .exists (query (where ("name" ).is (scriptName )), CallableMongoScript .class ,
134
- SCRIPT_COLLECTION_NAME );
133
+ return mongoOperations .exists (query (where ("name" ).is (scriptName )), NamedMongoScript .class , SCRIPT_COLLECTION_NAME );
135
134
}
136
135
137
136
/*
138
137
* (non-Javadoc)
139
- * @see org.springframework.data.mongodb.core.ScriptOperations#scriptNames ()
138
+ * @see org.springframework.data.mongodb.core.ScriptOperations#getScriptNames ()
140
139
*/
141
140
@ Override
142
- public Set <String > scriptNames () {
141
+ public Set <String > getScriptNames () {
143
142
144
- List <CallableMongoScript > scripts = ( mongoOperations .findAll (CallableMongoScript .class , SCRIPT_COLLECTION_NAME ) );
143
+ List <NamedMongoScript > scripts = mongoOperations .findAll (NamedMongoScript .class , SCRIPT_COLLECTION_NAME );
145
144
146
145
if (CollectionUtils .isEmpty (scripts )) {
147
146
return Collections .emptySet ();
148
147
}
149
148
150
149
Set <String > scriptNames = new HashSet <String >();
151
- for (CallableMongoScript script : scripts ) {
150
+
151
+ for (NamedMongoScript script : scripts ) {
152
152
scriptNames .add (script .getName ());
153
153
}
154
- return scriptNames ;
155
- }
156
154
157
- /**
158
- * Generate a valid name for the {@literal JavaScript}. MongoDB requires an id of type String for scripts. Calling
159
- * scripts having {@link ObjectId} as id fails. Therefore we create a random UUID without {@code -} (as this won't
160
- * work) an prefix the result with {@link #SCRIPT_NAME_PREFIX}.
161
- *
162
- * @return
163
- */
164
- private String generateScriptName () {
165
- return SCRIPT_NAME_PREFIX + randomUUID ().toString ().replaceAll ("-" , "" );
155
+ return scriptNames ;
166
156
}
167
157
168
158
private Object [] convertScriptArgs (Object ... args ) {
@@ -172,23 +162,27 @@ private Object[] convertScriptArgs(Object... args) {
172
162
}
173
163
174
164
List <Object > convertedValues = new ArrayList <Object >(args .length );
165
+
175
166
for (Object arg : args ) {
176
- if (arg instanceof String ) {
177
- convertedValues .add ("'" + arg + "'" );
178
- } else {
179
- convertedValues .add (this .mongoOperations .getConverter ().convertToMongoType (arg ));
180
- }
167
+ convertedValues .add (arg instanceof String ? String .format ("'%s'" , arg ) : this .mongoOperations .getConverter ()
168
+ .convertToMongoType (arg ));
181
169
}
170
+
182
171
return convertedValues .toArray ();
183
172
}
184
173
185
174
private String convertAndJoinScriptArgs (Object ... args ) {
186
-
187
- if (ObjectUtils .isEmpty (args )) {
188
- return "" ;
189
- }
190
-
191
- return StringUtils .arrayToCommaDelimitedString (convertScriptArgs (args ));
175
+ return ObjectUtils .isEmpty (args ) ? "" : StringUtils .arrayToCommaDelimitedString (convertScriptArgs (args ));
192
176
}
193
177
178
+ /**
179
+ * Generate a valid name for the {@literal JavaScript}. MongoDB requires an id of type String for scripts. Calling
180
+ * scripts having {@link ObjectId} as id fails. Therefore we create a random UUID without {@code -} (as this won't
181
+ * work) an prefix the result with {@link #SCRIPT_NAME_PREFIX}.
182
+ *
183
+ * @return
184
+ */
185
+ private static String generateScriptName () {
186
+ return SCRIPT_NAME_PREFIX + randomUUID ().toString ().replaceAll ("-" , "" );
187
+ }
194
188
}
0 commit comments