diff --git a/.vscode/launch.json b/.vscode/launch.json index 974a88c..466a6cd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ "stopOnEntry": false, "mainClass": "com.loafle.overflow.container.general.GeneralContainer", "projectName": "container_general", - "args": "${workspaceFolder}/general.pid" + "args": "60000" }, { "type": "java", diff --git a/pom.xml b/pom.xml index 3519c0f..b8810bd 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ 1.0.0-SNAPSHOT 4.1.17.Final - 2.8.2 + 1.9.13 5.0.5.RELEASE 6.0.6 @@ -76,9 +76,9 @@ ${netty.version} - com.google.code.gson - gson - ${gson.version} + org.codehaus.jackson + jackson-mapper-asl + ${jackson.mapper.version} org.springframework diff --git a/src/main/java/com/loafle/overflow/container/general/GeneralContainer.java b/src/main/java/com/loafle/overflow/container/general/GeneralContainer.java index 0954aa3..4dfe120 100644 --- a/src/main/java/com/loafle/overflow/container/general/GeneralContainer.java +++ b/src/main/java/com/loafle/overflow/container/general/GeneralContainer.java @@ -2,8 +2,8 @@ package com.loafle.overflow.container.general; import com.loafle.overflow.container.Container; import com.loafle.overflow.container.configuration.ContainerConfiguration; -import com.loafle.overflow.container.general.server.GeneralContainerConfiguration; -import com.loafle.overflow.container.general.server.GeneralContainerServer; +import com.loafle.overflow.container.general.client.GeneralContainerClient; +import com.loafle.overflow.container.general.client.GeneralContainerConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -17,12 +17,22 @@ public class GeneralContainer { String pidFilePath = args[0]; try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { - context.getBeanFactory().registerSingleton(Container.PIDFILE_PATH, pidFilePath); - context.register(ContainerConfiguration.class, GeneralContainerConfiguration.class); + context.getBeanFactory().registerSingleton(Container.PORT_NUMBER, pidFilePath); + context.register(GeneralContainerConfiguration.class, ContainerConfiguration.class); context.refresh(); context.registerShutdownHook(); - GeneralContainerServer server = context.getBean(GeneralContainerServer.class); - server.start(); + GeneralContainerClient client = context.getBean(GeneralContainerClient.class); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + try { + client.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + client.start(); } } } diff --git a/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java b/src/main/java/com/loafle/overflow/container/general/client/GeneralContainerClient.java similarity index 84% rename from src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java rename to src/main/java/com/loafle/overflow/container/general/client/GeneralContainerClient.java index 786c2d4..992d1e6 100644 --- a/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java +++ b/src/main/java/com/loafle/overflow/container/general/client/GeneralContainerClient.java @@ -1,10 +1,10 @@ -package com.loafle.overflow.container.general.server; +package com.loafle.overflow.container.general.client; import java.util.HashMap; import java.util.Map; import com.loafle.overflow.container.general.service.Service; -import com.loafle.overflow.container.server.ContainerServer; +import com.loafle.overflow.container.client.ContainerClient; import com.loafle.overflow.core.annotation.RPCService; import org.springframework.beans.factory.annotation.Autowired; @@ -14,17 +14,17 @@ import org.springframework.stereotype.Component; import static com.loafle.overflow.core.interfaces.Service.execServices; import static com.loafle.overflow.core.interfaces.Service.ServiceMethodType; +/** + * GeneralContainerClient + */ @Component -public class GeneralContainerServer extends ContainerServer { +public class GeneralContainerClient extends ContainerClient { @Autowired private ApplicationContext applicationContext; private Map, Object> services; - public GeneralContainerServer() { - } - - @Override + protected void init() throws Exception { super.init(); @@ -39,26 +39,21 @@ public class GeneralContainerServer extends ContainerServer { }); execServices(this.services, ServiceMethodType.INIT, Service.OrderedServices, false); - } - @Override + } protected void onStart() throws Exception { super.onStart(); execServices(this.services, ServiceMethodType.START, Service.OrderedServices, false); } - - @Override protected void onStop() throws Exception { execServices(this.services, ServiceMethodType.STOP, Service.OrderedServices, true); super.onStop(); } - - @Override protected void destroy() throws Exception { execServices(this.services, ServiceMethodType.DESTROY, Service.OrderedServices, true); super.destroy(); } -} +} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/container/general/client/GeneralContainerConfiguration.java b/src/main/java/com/loafle/overflow/container/general/client/GeneralContainerConfiguration.java new file mode 100644 index 0000000..7cb59be --- /dev/null +++ b/src/main/java/com/loafle/overflow/container/general/client/GeneralContainerConfiguration.java @@ -0,0 +1,57 @@ +package com.loafle.overflow.container.general.client; + +import java.net.URI; +import java.util.Map; + +import com.loafle.commons.server.websocket.client.Client; +import com.loafle.overflow.config.container.ContainerProtocol; +import com.loafle.overflow.config.probe.ContainerType; +import com.loafle.overflow.container.Container; +import com.loafle.overflow.container.general.crawler.Crawlers; +import com.loafle.overflow.crawler.Crawler; + +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import io.netty.handler.codec.http.DefaultHttpHeaders; +import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; +import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; +import io.netty.handler.codec.http.websocketx.WebSocketVersion; + +/** + * GeneralContainerConfiguration + */ +@Configuration +@ComponentScan(basePackages = { "com.loafle.overflow" }) +public class GeneralContainerConfiguration { + @Autowired + @Qualifier(Client.SERVER_URI) + protected URI serverURI; + + @Bean(Client.HANDSHAKER) + public WebSocketClientHandshaker handshaker() { + HttpHeaders customHeaders = new DefaultHttpHeaders(); + customHeaders.add(ContainerProtocol.HTTPRequestHeaderKey_Container_Method, ContainerProtocol.HTTPRequestHeaderValue_Container_Method_Connect); + customHeaders.add(ContainerProtocol.HTTPRequestHeaderKey_Container_Type, ContainerType.GENERNAL.toString()); + + return WebSocketClientHandshakerFactory.newHandshaker(serverURI, WebSocketVersion.V13, null, false, customHeaders); + } + + @Bean(Container.CRAWLERS) + public Map crawlers() { + return Crawlers.getCrawlers(); + } + + @Bean + public ObjectMapper objectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return objectMapper; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawler.java b/src/main/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawler.java index 11f2f3c..bd8d34b 100644 --- a/src/main/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawler.java +++ b/src/main/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawler.java @@ -1,15 +1,16 @@ package com.loafle.overflow.container.general.crawler.impl.wmi; -import com.google.gson.Gson; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.crawler.Crawler; import com.loafle.overflow.model.sensorconfig.ResultSet; import com.loafle.overflow.model.sensorconfig.SensorConfig; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; @@ -17,10 +18,11 @@ import java.util.List; import java.util.Map; public class WMICrawler implements Crawler { - private Gson gson; + private ObjectMapper objectMapper; public WMICrawler() { - this.gson = new Gson(); + this.objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); } @Override @@ -87,7 +89,7 @@ public class WMICrawler implements Crawler { SensorConfig metaConfig = null; for (String fi : metaFiles) { if (fi.indexOf("meta_") > -1) { - metaConfig = new Gson().fromJson(new InputStreamReader(new FileInputStream(new File(path + "/" + fi))), SensorConfig.class); + metaConfig = this.objectMapper.readValue(new FileInputStream(new File(path + "/" + fi)), SensorConfig.class); if (metaConfig != null) { retList.add(metaConfig); } diff --git a/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerConfiguration.java b/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerConfiguration.java deleted file mode 100644 index c8e18a2..0000000 --- a/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.loafle.overflow.container.general.server; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import com.loafle.overflow.container.Container; -import com.loafle.overflow.container.general.crawler.Crawlers; -import com.loafle.overflow.crawler.Crawler; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -import io.netty.channel.ChannelHandler; - -/** - * GeneralContainerConfiguration - */ -@Configuration -@ComponentScan(basePackages = { "com.loafle.overflow" }) -public class GeneralContainerConfiguration { - - @Bean(Container.PIPELINE_CHANNEL_HANDLERS) - public List pipelineChannelHandlers() { - return new ArrayList<>(); - } - - @Bean(Container.CRAWLERS) - public Map crawlers() { - return Crawlers.getCrawlers(); - } - -} \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/CrawlerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/CrawlerTest.java index 2bd9481..a12f37c 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/CrawlerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/CrawlerTest.java @@ -1,10 +1,16 @@ package com.loafle.overflow.container.general.crawler; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Before; public abstract class CrawlerTest { + protected ObjectMapper objectMapper; + @Before public void setUp() throws Exception { + this.objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); } } diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/impl/database/DatabaseCrawlerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/impl/database/DatabaseCrawlerTest.java index 801df72..21362df 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/impl/database/DatabaseCrawlerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/impl/database/DatabaseCrawlerTest.java @@ -1,6 +1,5 @@ package com.loafle.overflow.container.general.crawler.impl.database; -import com.google.gson.Gson; import com.loafle.overflow.container.general.crawler.CrawlerTest; import com.loafle.overflow.model.sensorconfig.SensorConfig; @@ -38,9 +37,8 @@ public abstract class DatabaseCrawlerTest extends CrawlerTest { protected SensorConfig getSensorConfig(String path) throws IOException { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource(path).getFile(); - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); + SensorConfig c = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); - SensorConfig c = new Gson().fromJson(inputStreamReader, SensorConfig.class); return c; } diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/impl/jmx/tomcat/TomcatCrawlerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/impl/jmx/tomcat/TomcatCrawlerTest.java index d510b6d..923247d 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/impl/jmx/tomcat/TomcatCrawlerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/impl/jmx/tomcat/TomcatCrawlerTest.java @@ -1,6 +1,5 @@ package com.loafle.overflow.container.general.crawler.impl.jmx.tomcat; -import com.google.gson.Gson; import com.loafle.overflow.container.general.crawler.impl.jmx.JMXCrawler; import com.loafle.overflow.container.general.crawler.impl.jmx.JMXCrawlerTest; import com.loafle.overflow.model.sensorconfig.SensorConfig; @@ -10,7 +9,6 @@ import org.junit.Test; import java.io.File; import java.io.FileInputStream; -import java.io.InputStreamReader; import java.net.URL; import java.util.Map; @@ -30,10 +28,7 @@ public class TomcatCrawlerTest extends JMXCrawlerTest { ClassLoader classLoader = getClass().getClassLoader(); URL url = classLoader.getResource("config/tomcat/"); String filePath = classLoader.getResource("config/tomcat/example1.json").getFile(); - - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig c = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig c = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); JMXCrawler cr = new JMXCrawler(); Map result = cr.get(c); diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/impl/mongodb/MongoDBCrawlerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/impl/mongodb/MongoDBCrawlerTest.java index beb108d..3f018c6 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/impl/mongodb/MongoDBCrawlerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/impl/mongodb/MongoDBCrawlerTest.java @@ -1,6 +1,5 @@ package com.loafle.overflow.container.general.crawler.impl.mongodb; -import com.google.gson.Gson; import com.loafle.overflow.container.general.crawler.CrawlerTest; import com.loafle.overflow.model.sensorconfig.SensorConfig; @@ -9,7 +8,6 @@ import org.junit.Test; import java.io.File; import java.io.FileInputStream; -import java.io.InputStreamReader; import java.util.Map; import static org.junit.Assert.*; @@ -23,9 +21,7 @@ public class MongoDBCrawlerTest extends CrawlerTest { // read test resources config/example.json ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/mongodb/example.json").getFile(); - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig c = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig c = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); MongoDBCrawler cr = new MongoDBCrawler(); Map m = cr.get(c); diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/impl/redis/RedisCralwerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/impl/redis/RedisCralwerTest.java index 755aece..4b71959 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/impl/redis/RedisCralwerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/impl/redis/RedisCralwerTest.java @@ -1,6 +1,5 @@ package com.loafle.overflow.container.general.crawler.impl.redis; -import com.google.gson.Gson; import com.loafle.overflow.container.general.crawler.CrawlerTest; import com.loafle.overflow.model.sensorconfig.SensorConfig; @@ -9,7 +8,6 @@ import org.junit.Test; import java.io.File; import java.io.FileInputStream; -import java.io.InputStreamReader; import java.util.Map; import static org.junit.Assert.*; @@ -21,10 +19,7 @@ public class RedisCralwerTest extends CrawlerTest { public void collectMetric() throws Exception { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/redis/example.json").getFile(); - - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig c = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig c = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); RedisCralwer rc = new RedisCralwer(); print("", rc.get(c)); diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/impl/snmp/SNMPCrawlerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/impl/snmp/SNMPCrawlerTest.java index 42055a5..56266c7 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/impl/snmp/SNMPCrawlerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/impl/snmp/SNMPCrawlerTest.java @@ -1,6 +1,5 @@ package com.loafle.overflow.container.general.crawler.impl.snmp; -import com.google.gson.Gson; import com.loafle.overflow.container.general.crawler.CrawlerTest; import com.loafle.overflow.model.sensorconfig.SensorConfig; @@ -34,10 +33,7 @@ public class SNMPCrawlerTest extends CrawlerTest { SNMPCrawler c = new SNMPCrawler(); ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/snmp/examplev3.json").getFile(); - - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); Map result = c.get(config); @@ -52,10 +48,7 @@ public class SNMPCrawlerTest extends CrawlerTest { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/snmp/examplev2.json").getFile(); - - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); Map result = c.get(config); diff --git a/src/test/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawlerTest.java b/src/test/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawlerTest.java index 03cc24b..2c3036e 100644 --- a/src/test/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawlerTest.java +++ b/src/test/java/com/loafle/overflow/container/general/crawler/impl/wmi/WMICrawlerTest.java @@ -1,6 +1,5 @@ package com.loafle.overflow.container.general.crawler.impl.wmi; -import com.google.gson.Gson; import com.loafle.overflow.container.general.crawler.CrawlerTest; import com.loafle.overflow.model.sensorconfig.ResultSet; import com.loafle.overflow.model.sensorconfig.SensorConfig; @@ -13,7 +12,6 @@ import org.junit.Test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.net.URL; import java.util.*; @@ -74,9 +72,7 @@ public class WMICrawlerTest extends CrawlerTest { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/wmi").getFile(); System.out.println(filePath); - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); // Config c = mapper.readValue(new File(classLoader.getResource("config").getFile()),Config.class); @@ -104,9 +100,7 @@ public class WMICrawlerTest extends CrawlerTest { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/wmi/example.json").getFile(); System.out.println(filePath); - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); System.out.println(config); } @@ -117,8 +111,7 @@ public class WMICrawlerTest extends CrawlerTest { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/wmi/meta/meta_network.json").getFile(); - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); // Map map = new HashMap<>(); // map.put("id", "administrator"); @@ -159,8 +152,7 @@ public class WMICrawlerTest extends CrawlerTest { ClassLoader classLoader = getClass().getClassLoader(); String filePath = classLoader.getResource("config/wmi/test.json").getFile(); - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath))); - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(filePath)), SensorConfig.class); List metaConfigList = loadMetaConfig(); @@ -198,8 +190,7 @@ public class WMICrawlerTest extends CrawlerTest { for (String fi : metaFiles) { if (fi.indexOf("meta_") > -1) { - InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(path + "/" + fi))); - SensorConfig config = new Gson().fromJson(inputStreamReader, SensorConfig.class); + SensorConfig config = this.objectMapper.readValue(new FileInputStream(new File(path + "/" + fi)), SensorConfig.class); if (config != null) { retList.add(config); }