|
@@ -1,10 +1,9 @@
|
|
package cn.com.qmth.examcloud.web.mongodb;
|
|
package cn.com.qmth.examcloud.web.mongodb;
|
|
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
|
-import java.net.Inet4Address;
|
|
|
|
-import java.net.InetAddress;
|
|
|
|
-import java.util.Map;
|
|
|
|
-
|
|
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
+import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
|
+import com.google.common.collect.Maps;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.boot.ApplicationArguments;
|
|
import org.springframework.boot.ApplicationArguments;
|
|
@@ -12,11 +11,10 @@ import org.springframework.boot.ApplicationRunner;
|
|
import org.springframework.core.annotation.Order;
|
|
import org.springframework.core.annotation.Order;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import com.google.common.collect.Maps;
|
|
|
|
-
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
|
-import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
-import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+import java.net.Inet4Address;
|
|
|
|
+import java.net.InetAddress;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* mongodb 侦察器
|
|
* mongodb 侦察器
|
|
@@ -29,56 +27,52 @@ import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
@Order(99)
|
|
@Order(99)
|
|
public class MongodbDetector implements ApplicationRunner {
|
|
public class MongodbDetector implements ApplicationRunner {
|
|
|
|
|
|
- private static final Logger LOG = LoggerFactory.getLogger(MongodbDetector.class);
|
|
|
|
-
|
|
|
|
- public void start() {
|
|
|
|
- String appName = PropertyHolder.getString("spring.application.name");
|
|
|
|
-
|
|
|
|
- String ip = null;
|
|
|
|
- try {
|
|
|
|
- InetAddress localHost = Inet4Address.getLocalHost();
|
|
|
|
- ip = localHost.getHostAddress();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- // ingnore
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Class<?> mongoTemplateClass = null;
|
|
|
|
- Object mongoTemplateObject = null;
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- mongoTemplateClass = Class
|
|
|
|
- .forName("org.springframework.data.mongodb.core.MongoTemplate");
|
|
|
|
- mongoTemplateObject = SpringContextHolder.getBean(mongoTemplateClass);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- // ingnore
|
|
|
|
- LOG.info("mongodb useless");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- Method getDbMethod = mongoTemplateClass.getMethod("getDb");
|
|
|
|
- Object mongoDatabaseObject = getDbMethod.invoke(mongoTemplateObject);
|
|
|
|
- Method getNameMethod = mongoDatabaseObject.getClass().getMethod("getName");
|
|
|
|
- Object dbName = getNameMethod.invoke(mongoDatabaseObject);
|
|
|
|
- LOG.info("mongo.db=" + dbName);
|
|
|
|
-
|
|
|
|
- Map<String, String> data = Maps.newHashMap();
|
|
|
|
- data.put("startupTime", DateUtil.chinaNow());
|
|
|
|
- data.put("appName", appName);
|
|
|
|
- data.put("ip", ip);
|
|
|
|
-
|
|
|
|
- Method insertMethod = mongoTemplateClass.getMethod("insert", Object.class,
|
|
|
|
- String.class);
|
|
|
|
- insertMethod.invoke(mongoTemplateObject, data, "startup");
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- LOG.error("unexpected mongodb exception");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void run(ApplicationArguments args) throws Exception {
|
|
|
|
- start();
|
|
|
|
- }
|
|
|
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger(MongodbDetector.class);
|
|
|
|
+
|
|
|
|
+ public void start() {
|
|
|
|
+ Class<?> mongoTemplateClass;
|
|
|
|
+ Object mongoTemplateObject;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ mongoTemplateClass = Class.forName("org.springframework.data.mongodb.core.MongoTemplate");
|
|
|
|
+ mongoTemplateObject = SpringContextHolder.getBean(mongoTemplateClass);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ LOG.info("mongodb disabled...");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String ip = null;
|
|
|
|
+ try {
|
|
|
|
+ InetAddress localHost = Inet4Address.getLocalHost();
|
|
|
|
+ ip = localHost.getHostAddress();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ // ignore
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String appName = PropertyHolder.getString("spring.application.name");
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Method getDbMethod = mongoTemplateClass.getMethod("getDb");
|
|
|
|
+ Object mongoDatabaseObject = getDbMethod.invoke(mongoTemplateObject);
|
|
|
|
+ Method getNameMethod = mongoDatabaseObject.getClass().getMethod("getName");
|
|
|
|
+ Object dbName = getNameMethod.invoke(mongoDatabaseObject);
|
|
|
|
+ LOG.info("mongodb database is " + dbName);
|
|
|
|
+
|
|
|
|
+ Map<String, String> data = Maps.newHashMap();
|
|
|
|
+ data.put("startupTime", DateUtil.chinaNow());
|
|
|
|
+ data.put("appName", appName);
|
|
|
|
+ data.put("ip", ip);
|
|
|
|
+
|
|
|
|
+ Method insertMethod = mongoTemplateClass.getMethod("insert", Object.class, String.class);
|
|
|
|
+ insertMethod.invoke(mongoTemplateObject, data, "startup");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ LOG.error("mongodb detector fail...");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void run(ApplicationArguments args) throws Exception {
|
|
|
|
+ start();
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|