Browse Source

Merge pull request #1162 in DESIGN/design from ~VITO/c-design:feature/10.0 to feature/10.0

* commit '11efe5ba934c8a6d5b357abe52a04d5438202b4e':
  REPORT-22377 收集jar版本信息&release部分代码传feature
research/11.0
vito 5 years ago
parent
commit
403bfdd3a9
  1. 644
      designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java
  2. 9
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/StartupMessageCollector.java

644
designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java

@ -30,6 +30,7 @@ import com.fr.stable.xml.XMLTools;
import com.fr.stable.xml.XMLWriter; import com.fr.stable.xml.XMLWriter;
import com.fr.stable.xml.XMLableReader; import com.fr.stable.xml.XMLableReader;
import com.fr.third.javax.xml.stream.XMLStreamException; import com.fr.third.javax.xml.stream.XMLStreamException;
import com.sun.management.OperatingSystemMXBean;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -43,11 +44,14 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -58,309 +62,339 @@ import java.util.concurrent.TimeUnit;
*/ */
public class InformationCollector implements XMLReadable, XMLWriter { public class InformationCollector implements XMLReadable, XMLWriter {
// 24小时上传一次 /**
private static final long DELTA = 24 * 3600 * 1000L; * 24小时上传一次
private static final long SEND_DELAY = 300 * 1000L; */
private static final String FILE_NAME = "fr.info"; private static final long DELTA = 24 * 3600 * 1000L;
private static final String XML_START_STOP_LIST = "StartStopList"; private static final long SEND_DELAY = 300 * 1000L;
private static final String XML_START_STOP = "StartStop"; private static final int BYTE_TO_MB = 1024 * 1024;
private static final String XML_LAST_TIME = "LastTime"; private static final String FILE_NAME = "fr.info";
private static final String ATTR_START = "start"; private static final String XML_START_STOP_LIST = "StartStopList";
private static final String ATTR_STOP = "stop"; private static final String XML_START_STOP = "StartStop";
private static final String XML_JAR = "JarInfo"; private static final String XML_LAST_TIME = "LastTime";
private static final String XML_VERSION = "Version"; private static final String ATTR_START = "start";
private static final String XML_USERNAME = "Username"; private static final String ATTR_STOP = "stop";
private static final String XML_UUID = "UUID"; private static final String XML_JAR = "JarInfo";
private static final String XML_KEY = "ActiveKey"; private static final String XML_VERSION = "Version";
private static final String XML_OS = "OS"; private static final String XML_USERNAME = "Username";
private static final String XML_UUID = "UUID";
private static InformationCollector collector; private static final String XML_KEY = "ActiveKey";
private static final String XML_OS = "OS";
//启动时间与关闭时间列表 private static final String XML_ARCH = "arch";
private List<StartStopTime> startStop = new ArrayList<StartStopTime>(); private static final String XML_AVAILABLE_PROCESSORS = "cpu";
//上一次的发送时间 private static final String XML_PHYSICAL_MEMORY = "systemMemory";
private String lastTime;
private StartStopTime current = new StartStopTime(); private static InformationCollector collector;
public static InformationCollector getInstance() { /**
if (collector == null) { * 启动时间与关闭时间列表
collector = new InformationCollector(); */
private List<StartStopTime> startStop = new ArrayList<StartStopTime>();
readEncodeXMLFile(collector, collector.getInfoFile());
} /**
* 上一次的发送时间
return collector; */
} private String lastTime;
private StartStopTime current = new StartStopTime();
private static void readEncodeXMLFile(XMLReadable xmlReadable, File xmlFile) {
if (xmlFile == null || !xmlFile.exists()) { public static InformationCollector getInstance() {
return; if (collector == null) {
} collector = new InformationCollector();
String charset = EncodeConstants.ENCODING_UTF_8;
try { readEncodeXMLFile(collector, collector.getInfoFile());
String decodeContent = getDecodeFileContent(xmlFile); }
InputStream xmlInputStream = new ByteArrayInputStream(decodeContent.getBytes(charset));
InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset); return collector;
}
XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader);
private static void readEncodeXMLFile(XMLReadable xmlReadable, File xmlFile) {
if (xmlReader != null) { if (xmlFile == null || !xmlFile.exists()) {
xmlReader.readXMLObject(xmlReadable); return;
} }
xmlInputStream.close(); String charset = EncodeConstants.ENCODING_UTF_8;
} catch (FileNotFoundException e) { try {
FineLoggerFactory.getLogger().error(e.getMessage(), e); String decodeContent = getDecodeFileContent(xmlFile);
} catch (IOException e) { InputStream xmlInputStream = new ByteArrayInputStream(decodeContent.getBytes(charset));
FineLoggerFactory.getLogger().error(e.getMessage(), e); InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset);
} catch (XMLStreamException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e); XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader);
}
if (xmlReader != null) {
} xmlReader.readXMLObject(xmlReadable);
}
private static String getDecodeFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { xmlInputStream.close();
InputStream encodeInputStream = new FileInputStream(xmlFile); } catch (FileNotFoundException e) {
String encodeContent = IOUtils.inputStream2String(encodeInputStream); FineLoggerFactory.getLogger().error(e.getMessage(), e);
return DesUtils.getDecString(encodeContent); } catch (IOException e) {
} FineLoggerFactory.getLogger().error(e.getMessage(), e);
} catch (XMLStreamException e) {
private long getLastTimeMillis() { FineLoggerFactory.getLogger().error(e.getMessage(), e);
if (StringUtils.isEmpty(this.lastTime)) { }
return 0;
} }
try { private static String getDecodeFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException {
return DateUtils.string2Date(this.lastTime, true).getTime(); InputStream encodeInputStream = new FileInputStream(xmlFile);
} catch (Exception e) { String encodeContent = IOUtils.inputStream2String(encodeInputStream);
return -1; return DesUtils.getDecString(encodeContent);
} }
} private long getLastTimeMillis() {
if (StringUtils.isEmpty(this.lastTime)) {
private JSONObject getJSONContentAsByte() { return 0;
JSONObject content = new JSONObject(); }
JSONArray startStopArray = new JSONArray(); try {
for (int i = 0; i < startStop.size(); i++) { return Objects.requireNonNull(DateUtils.string2Date(this.lastTime, true)).getTime();
JSONObject jo = new JSONObject(); } catch (Exception e) {
jo.put(ATTR_START, startStop.get(i).getStartDate()); return -1;
jo.put(ATTR_STOP, startStop.get(i).getStopDate()); }
startStopArray.put(jo);
DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); }
content.put(XML_START_STOP, startStopArray);
content.put(XML_UUID, envManager.getUUID()); private JSONObject getJSONContentAsByte() {
content.put(XML_JAR, GeneralUtils.readBuildNO()); JSONObject content = new JSONObject();
content.put(XML_VERSION, ProductConstants.RELEASE_VERSION);
content.put(XML_USERNAME, MarketConfig.getInstance().getBbsUsername()); JSONArray startStopArray = new JSONArray();
content.put(XML_KEY, envManager.getActivationKey()); for (StartStopTime startStopTime : startStop) {
content.put(XML_OS, System.getProperty("os.name")); JSONObject jo = new JSONObject();
} jo.put(ATTR_START, startStopTime.getStartDate());
return content; jo.put(ATTR_STOP, startStopTime.getStopDate());
} startStopArray.put(jo);
}
private void sendUserInfo() { DesignerEnvManager envManager = DesignerEnvManager.getEnvManager();
long currentTime = new Date().getTime(); content.put(XML_START_STOP, startStopArray);
long lastTime = getLastTimeMillis(); content.put(XML_UUID, envManager.getUUID());
content.put(XML_JAR, GeneralUtils.readBuildNO());
if (currentTime - lastTime <= DELTA) { content.put(XML_VERSION, ProductConstants.RELEASE_VERSION);
return; content.put(XML_USERNAME, MarketConfig.getInstance().getBbsUsername());
} content.put(XML_KEY, envManager.getActivationKey());
JSONObject content = getJSONContentAsByte(); content.put(XML_OS, System.getProperty("os.name"));
String url = CloudCenter.getInstance().acquireUrlByKind("user.info.v10"); content.put(XML_ARCH, System.getProperty("os.arch"));
boolean success = false; content.put(XML_AVAILABLE_PROCESSORS, Runtime.getRuntime().availableProcessors());
try { content.put(XML_PHYSICAL_MEMORY, getTotalPhysicalMemorySize());
HashMap<String, Object> para = new HashMap<>(); return content;
para.put("token", SiteCenterToken.generateToken()); }
para.put("content", content);
String res = HttpToolbox.post(url, para); /**
success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); * 获取物理内存单位GB
} catch (Exception e) { *
FineLoggerFactory.getLogger().error(e.getMessage(), e); * @return 物理内存
} */
if (success) { private static long getTotalPhysicalMemorySize() {
this.reset(); OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
} return bean.getTotalPhysicalMemorySize() / BYTE_TO_MB;
} }
/**
* 收集开始使用时间发送信息 private void sendUserInfo() {
*/ long currentTime = System.currentTimeMillis();
public void collectStartTime() { long lastTime = getLastTimeMillis();
this.current.setStartDate(dateToString());
if (currentTime - lastTime <= DELTA) {
sendUserInfoInOtherThread(); return;
} }
JSONObject content = getJSONContentAsByte();
private void sendUserInfoInOtherThread() { String url = CloudCenter.getInstance().acquireUrlByKind("user.info.v10");
if (!DesignerEnvManager.getEnvManager().isJoinProductImprove() || !FRContext.isChineseEnv()) { boolean success = false;
return; try {
} HashMap<String, Object> para = new HashMap<>();
para.put("token", SiteCenterToken.generateToken());
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("InformationCollector")); para.put("content", content);
service.schedule(new Runnable() { String res = HttpToolbox.post(url, para);
@Override success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success");
public void run() { } catch (Exception e) {
sendUserInfo(); FineLoggerFactory.getLogger().error(e.getMessage(), e);
FocusPointMessageUploader.getInstance().sendToCloudCenter(); }
TemplateInfoCollector.getInstance().sendTemplateInfo(); if (success) {
ErrorInfoUploader.getInstance().sendErrorInfo(); this.reset();
} }
}, SEND_DELAY, TimeUnit.MILLISECONDS); }
}
/**
/** * 收集开始使用时间发送信息
* 收集结束使用时间 */
*/ public void collectStartTime() {
public void collectStopTime() { this.current.setStartDate(dateToString());
this.current.setStopDate(dateToString());
} sendUserInfoInOtherThread();
}
private String dateToString() {
DateFormat df = FRContext.getDefaultValues().getDateTimeFormat(); private void sendUserInfoInOtherThread() {
return df.format(new Date()); if (!DesignerEnvManager.getEnvManager().isJoinProductImprove() || !FRContext.isChineseEnv()) {
} return;
}
private void reset() {
this.startStop.clear(); ScheduledExecutorService service = Executors
this.lastTime = dateToString(); .newSingleThreadScheduledExecutor(new NamedThreadFactory("InformationCollector"));
} service.schedule(new Runnable() {
@Override
private File getInfoFile() { public void run() {
return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME)); sendUserInfo();
} FocusPointMessageUploader.getInstance().sendToCloudCenter();
TemplateInfoCollector.getInstance().sendTemplateInfo();
/** ErrorInfoUploader.getInstance().sendErrorInfo();
* 保存xml文件 }
*/ }, SEND_DELAY, TimeUnit.MILLISECONDS);
public void saveXMLFile() { service.shutdown();
File xmlFile = this.getInfoFile(); }
try {
ByteArrayOutputStream out = new ByteArrayOutputStream(); /**
XMLTools.writeOutputStreamXML(this, out); * 收集结束使用时间
out.flush(); */
out.close(); public void collectStopTime() {
String fileContent = new String(out.toByteArray(), EncodeConstants.ENCODING_UTF_8); this.current.setStopDate(dateToString());
String encodeCotent = DesUtils.getEncString(fileContent); }
writeEncodeContentToFile(encodeCotent, xmlFile);
} catch (Exception e) { private String dateToString() {
FineLoggerFactory.getLogger().error(e.getMessage(), e); DateFormat df = FRContext.getDefaultValues().getDateTimeFormat();
} return df.format(new Date());
} }
private void reset() {
/** this.startStop.clear();
* 将文件内容写到输出流中 this.lastTime = dateToString();
*/ }
private static void writeEncodeContentToFile(String fileContent, File file) {
try (FileOutputStream fos = new FileOutputStream(file); private File getInfoFile() {
OutputStreamWriter osw = new OutputStreamWriter(fos, EncodeConstants.ENCODING_UTF_8); return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME));
BufferedWriter bw = new BufferedWriter(osw)) { }
bw.write(fileContent);
} catch (Exception e) { /**
FineLoggerFactory.getLogger().error(e.getMessage(), e); * 保存xml文件
} */
} public void saveXMLFile() {
File xmlFile = this.getInfoFile();
@Override try {
public void writeXML(XMLPrintWriter writer) { ByteArrayOutputStream out = new ByteArrayOutputStream();
startStop.add(current); XMLTools.writeOutputStreamXML(this, out);
writer.startTAG("Info"); out.flush();
//启停信息 out.close();
writeStartStopList(writer); String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
//上一次更新的时间 String encodeCotent = DesUtils.getEncString(fileContent);
writeTag(XML_LAST_TIME, this.lastTime, writer); writeEncodeContentToFile(encodeCotent, xmlFile);
} catch (Exception e) {
writer.end(); FineLoggerFactory.getLogger().error(e.getMessage(), e);
} }
}
private void writeStartStopList(XMLPrintWriter writer) {
//启停
writer.startTAG(XML_START_STOP_LIST); /**
for (int i = 0; i < startStop.size(); i++) { * 将文件内容写到输出流中
startStop.get(i).writeXML(writer); */
} private static void writeEncodeContentToFile(String fileContent, File file) {
writer.end(); try (FileOutputStream fos = new FileOutputStream(file);
} OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(osw)) {
private void writeTag(String tag, String content, XMLPrintWriter writer) { bw.write(fileContent);
if (StringUtils.isEmpty(content)) { } catch (Exception e) {
return; FineLoggerFactory.getLogger().error(e.getMessage(), e);
} }
}
writer.startTAG(tag);
writer.textNode(content); @Override
writer.end(); public void writeXML(XMLPrintWriter writer) {
} startStop.add(current);
writer.startTAG("Info");
@Override //启停信息
public void readXML(XMLableReader reader) { writeStartStopList(writer);
if (reader.isChildNode()) { //上一次更新的时间
String name = reader.getTagName(); writeTag(XML_LAST_TIME, this.lastTime, writer);
if (XML_START_STOP_LIST.equals(name)) {
readStartStopList(reader); writer.end();
} else if (XML_LAST_TIME.equals(name)) { }
readLastTime(reader);
} private void writeStartStopList(XMLPrintWriter writer) {
} //启停
} writer.startTAG(XML_START_STOP_LIST);
for (StartStopTime startStopTime : startStop) {
private void readLastTime(XMLableReader reader) { startStopTime.writeXML(writer);
String tmpVal; }
if (StringUtils.isNotBlank(tmpVal = reader.getElementValue())) { writer.end();
this.lastTime = tmpVal; }
}
} private void writeTag(String tag, String content, XMLPrintWriter writer) {
if (StringUtils.isEmpty(content)) {
private void readStartStopList(XMLableReader reader) { return;
startStop.clear(); }
reader.readXMLObject(new XMLReadable() { writer.startTAG(tag);
public void readXML(XMLableReader reader) { writer.textNode(content);
if (XML_START_STOP.equals(reader.getTagName())) { writer.end();
StartStopTime startStopTime = new StartStopTime(); }
reader.readXMLObject(startStopTime);
startStop.add(startStopTime); @Override
} public void readXML(XMLableReader reader) {
} if (reader.isChildNode()) {
}); String name = reader.getTagName();
} if (XML_START_STOP_LIST.equals(name)) {
readStartStopList(reader);
private class StartStopTime implements XMLReadable, XMLWriter { } else if (XML_LAST_TIME.equals(name)) {
readLastTime(reader);
private String startDate; }
private String stopDate; }
}
public String getStartDate() {
return startDate; private void readLastTime(XMLableReader reader) {
} String tmpVal;
if (StringUtils.isNotBlank(tmpVal = reader.getElementValue())) {
public void setStartDate(String startDate) { this.lastTime = tmpVal;
this.startDate = startDate; }
} }
public String getStopDate() { private void readStartStopList(XMLableReader reader) {
return stopDate; startStop.clear();
}
reader.readXMLObject(new XMLReadable() {
public void setStopDate(String endDate) { @Override
this.stopDate = endDate; public void readXML(XMLableReader reader) {
} if (XML_START_STOP.equals(reader.getTagName())) {
StartStopTime startStopTime = new StartStopTime();
public void writeXML(XMLPrintWriter writer) { reader.readXMLObject(startStopTime);
writer.startTAG(XML_START_STOP); startStop.add(startStopTime);
if (StringUtils.isNotEmpty(startDate)) { }
writer.attr(ATTR_START, this.startDate); }
} });
if (StringUtils.isNotEmpty(stopDate)) { }
writer.attr(ATTR_STOP, this.stopDate);
} private class StartStopTime implements XMLReadable, XMLWriter {
writer.end();
} private String startDate;
private String stopDate;
public void readXML(XMLableReader reader) {
this.startDate = reader.getAttrAsString(ATTR_START, StringUtils.EMPTY); public String getStartDate() {
this.stopDate = reader.getAttrAsString(ATTR_STOP, StringUtils.EMPTY); return startDate;
} }
} public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getStopDate() {
return stopDate;
}
public void setStopDate(String endDate) {
this.stopDate = endDate;
}
@Override
public void writeXML(XMLPrintWriter writer) {
writer.startTAG(XML_START_STOP);
if (StringUtils.isNotEmpty(startDate)) {
writer.attr(ATTR_START, this.startDate);
}
if (StringUtils.isNotEmpty(stopDate)) {
writer.attr(ATTR_STOP, this.stopDate);
}
writer.end();
}
@Override
public void readXML(XMLableReader reader) {
this.startDate = reader.getAttrAsString(ATTR_START, StringUtils.EMPTY);
this.stopDate = reader.getAttrAsString(ATTR_STOP, StringUtils.EMPTY);
}
}
} }

9
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/StartupMessageCollector.java

@ -8,6 +8,7 @@ import com.fr.event.EventDispatcher;
import com.fr.event.Listener; import com.fr.event.Listener;
import com.fr.general.CloudCenter; import com.fr.general.CloudCenter;
import com.fr.general.ComparatorUtils; import com.fr.general.ComparatorUtils;
import com.fr.general.GeneralUtils;
import com.fr.general.http.HttpToolbox; import com.fr.general.http.HttpToolbox;
import com.fr.json.JSONObject; import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
@ -30,14 +31,15 @@ public class StartupMessageCollector {
private static final String XML_STARTUP_TIME = "t"; private static final String XML_STARTUP_TIME = "t";
private static final String XML_STARTUP_LOG = "startupLog"; private static final String XML_STARTUP_LOG = "startupLog";
private static final String XML_STARTUP_Memory = "designerMemory"; private static final String XML_STARTUP_MEMORY = "designerMemory";
private static final String XML_STARTUP_COST = "cost"; private static final String XML_STARTUP_COST = "cost";
private static final String XML_UUID = "UUID"; private static final String XML_UUID = "UUID";
private static final String XML_BUILD_NO = "buildNO";
private static final String STARTUP_URL_KEY = "user.info.v10.startup"; private static final String STARTUP_URL_KEY = "user.info.v10.startup";
private static final String LOG_TYPE = "single"; private static final String LOG_TYPE = "single";
private static final int BYTE_TO_MB = 1024 * 1024; private static final int BYTE_TO_MB = 1024 * 1024;
public static final StartupMessageCollector INSTANCE = new StartupMessageCollector(); private static final StartupMessageCollector INSTANCE = new StartupMessageCollector();
private StartupMessageCollector() { private StartupMessageCollector() {
} }
@ -67,10 +69,11 @@ public class StartupMessageCollector {
} }
JSONObject json = JSONObject.create() JSONObject json = JSONObject.create()
.put(XML_UUID, DesignerEnvManager.getEnvManager().getUUID()) .put(XML_UUID, DesignerEnvManager.getEnvManager().getUUID())
.put(XML_BUILD_NO, GeneralUtils.readBuildNO())
.put(XML_STARTUP_TIME, FineRuntime.getAppStartTime() + FineRuntime.getStartingTime()) .put(XML_STARTUP_TIME, FineRuntime.getAppStartTime() + FineRuntime.getStartingTime())
.put(XML_STARTUP_COST, FineRuntime.getStartingTime()) .put(XML_STARTUP_COST, FineRuntime.getStartingTime())
.put(XML_STARTUP_LOG, profile) .put(XML_STARTUP_LOG, profile)
.put(XML_STARTUP_Memory, Runtime.getRuntime().totalMemory() / BYTE_TO_MB); .put(XML_STARTUP_MEMORY, Runtime.getRuntime().maxMemory() / BYTE_TO_MB);
sendInfo(json, url + LOG_TYPE); sendInfo(json, url + LOG_TYPE);
} }
}); });

Loading…
Cancel
Save