|
@@ -4,6 +4,8 @@ import com.mongodb.client.MongoCollection;
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.bson.Document;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.ssssssss.magicapi.config.MagicModule;
|
|
|
import org.ssssssss.script.reflection.AbstractReflection;
|
|
@@ -17,16 +19,30 @@ import java.util.HashMap;
|
|
|
*/
|
|
|
public class MongoModule extends HashMap<String, Object> implements MagicModule {
|
|
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(MongoModule.class);
|
|
|
+
|
|
|
private final MongoTemplate mongoTemplate;
|
|
|
|
|
|
private JavaInvoker<Method> invoker;
|
|
|
|
|
|
+ private JavaInvoker<Method> mongoDbFactoryInvoker;
|
|
|
+
|
|
|
public MongoModule(MongoTemplate mongoTemplate) {
|
|
|
this.mongoTemplate = mongoTemplate;
|
|
|
- Object factory = mongoTemplate.getMongoDbFactory();
|
|
|
- invoker = AbstractReflection.getInstance().getMethod(factory, "getDb", StringUtils.EMPTY);
|
|
|
- if (invoker == null) {
|
|
|
- invoker = AbstractReflection.getInstance().getMethod(factory, "getMongoDatabase", StringUtils.EMPTY);
|
|
|
+ AbstractReflection reflection = AbstractReflection.getInstance();
|
|
|
+ mongoDbFactoryInvoker = reflection.getMethod(this.mongoTemplate, "getMongoDbFactory");
|
|
|
+ if (mongoDbFactoryInvoker != null) {
|
|
|
+ try {
|
|
|
+ Object factory = mongoDbFactoryInvoker.invoke0(this.mongoTemplate, null);
|
|
|
+ invoker = reflection.getMethod(factory, "getDb", StringUtils.EMPTY);
|
|
|
+ if (invoker == null) {
|
|
|
+ invoker = reflection.getMethod(factory, "getMongoDatabase", StringUtils.EMPTY);
|
|
|
+ }
|
|
|
+ } catch (Throwable e) {
|
|
|
+ logger.error("mongo模块初始化失败", e);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("mongo模块初始化失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -39,7 +55,8 @@ public class MongoModule extends HashMap<String, Object> implements MagicModule
|
|
|
return null;
|
|
|
}
|
|
|
try {
|
|
|
- MongoDatabase database = (MongoDatabase) invoker.invoke0(mongoTemplate.getMongoDbFactory(), null, databaseName.toString());
|
|
|
+ Object factory = mongoDbFactoryInvoker.invoke0(mongoTemplate, null);
|
|
|
+ MongoDatabase database = (MongoDatabase) invoker.invoke0(factory, null, databaseName.toString());
|
|
|
return database.getCollection(collection.toString());
|
|
|
} catch (Throwable throwable) {
|
|
|
throw new RuntimeException(throwable);
|