file type engine type

This commit is contained in:
Captain.B 2020-03-16 16:11:37 +08:00
parent 4e5e8c9809
commit a1311ca540
11 changed files with 58 additions and 38 deletions

View File

@ -0,0 +1,5 @@
package io.metersphere.commons.constants;
public enum FileType {
JMX
}

View File

@ -1,13 +1,13 @@
package io.metersphere.engine; package io.metersphere.engine;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class EngineContext { public class EngineContext {
private String engineId; private String engineId;
private String engineType; private String engineType;
private InputStream inputStream; private String fileType;
private String content;
private Map<String, Object> properties = new HashMap<>(); private Map<String, Object> properties = new HashMap<>();
public String getEngineId() { public String getEngineId() {
@ -26,14 +26,6 @@ public class EngineContext {
this.engineType = engineType; this.engineType = engineType;
} }
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public void addProperty(String key, Object value) { public void addProperty(String key, Object value) {
this.properties.put(key, value); this.properties.put(key, value);
} }
@ -41,4 +33,20 @@ public class EngineContext {
public Object getProperty(String key) { public Object getProperty(String key) {
return this.properties.get(key); return this.properties.get(key);
} }
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
} }

View File

@ -6,6 +6,7 @@ import io.metersphere.base.domain.FileContent;
import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.commons.constants.EngineType; import io.metersphere.commons.constants.EngineType;
import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.engine.docker.DockerTestEngine; import io.metersphere.engine.docker.DockerTestEngine;
import io.metersphere.engine.kubernetes.KubernetesTestEngine; import io.metersphere.engine.kubernetes.KubernetesTestEngine;
@ -14,7 +15,6 @@ import io.metersphere.parse.EngineSourceParserFactory;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class EngineFactory { public class EngineFactory {
public static Engine createEngine(String engineType) { public static Engine createEngine(String engineType) {
@ -32,8 +32,8 @@ public class EngineFactory {
public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, FileContent fileContent) throws Exception { public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, FileContent fileContent) throws Exception {
final EngineContext engineContext = new EngineContext(); final EngineContext engineContext = new EngineContext();
engineContext.setEngineId(loadTest.getId()); engineContext.setEngineId(loadTest.getId());
engineContext.setInputStream(new ByteArrayInputStream(fileContent.getFile())); engineContext.setEngineType(fileMetadata.getEngine());
engineContext.setEngineType(fileMetadata.getType()); engineContext.setFileType(fileMetadata.getType());
if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) { if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) {
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration()); final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());
@ -44,14 +44,14 @@ public class EngineFactory {
} }
} }
final EngineSourceParser engineSourceParser = EngineSourceParserFactory.createEngineSourceParser(engineContext.getEngineType()); final EngineSourceParser engineSourceParser = EngineSourceParserFactory.createEngineSourceParser(engineContext.getFileType());
if (engineSourceParser == null) { if (engineSourceParser == null) {
MSException.throwException("未知的文件类型!"); MSException.throwException("未知的文件类型!");
} }
final InputStream inputStream = engineSourceParser.parse(engineContext, engineContext.getInputStream()); String content = engineSourceParser.parse(engineContext, new ByteArrayInputStream(fileContent.getFile()));
engineContext.setInputStream(inputStream); engineContext.setContent(content);
return engineContext; return engineContext;
} }

View File

@ -4,9 +4,13 @@ import io.metersphere.engine.Engine;
import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineContext;
public class DockerTestEngine implements Engine { public class DockerTestEngine implements Engine {
private EngineContext context;
@Override @Override
public boolean init(EngineContext context) { public boolean init(EngineContext context) {
return false; // todo 初始化操作
this.context = context;
return true;
} }
@Override @Override

View File

@ -4,11 +4,16 @@ import io.metersphere.engine.Engine;
import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineContext;
public class KubernetesTestEngine implements Engine { public class KubernetesTestEngine implements Engine {
private EngineContext context;
@Override @Override
public boolean init(EngineContext context) { public boolean init(EngineContext context) {
return false; // todo 初始化操作
this.context = context;
return true;
} }
@Override @Override
public void start() { public void start() {

View File

@ -5,5 +5,5 @@ import io.metersphere.engine.EngineContext;
import java.io.InputStream; import java.io.InputStream;
public interface EngineSourceParser { public interface EngineSourceParser {
InputStream parse(EngineContext context, InputStream source) throws Exception; String parse(EngineContext context, InputStream source) throws Exception;
} }

View File

@ -1,13 +1,13 @@
package io.metersphere.parse; package io.metersphere.parse;
import io.metersphere.commons.constants.EngineType; import io.metersphere.commons.constants.FileType;
import io.metersphere.parse.xml.XmlEngineSourceParse; import io.metersphere.parse.xml.XmlEngineSourceParse;
public class EngineSourceParserFactory { public class EngineSourceParserFactory {
public static EngineSourceParser createEngineSourceParser(String type) { public static EngineSourceParser createEngineSourceParser(String type) {
final EngineType engineType = EngineType.valueOf(type); final FileType engineType = FileType.valueOf(type);
if (EngineType.JMX.equals(engineType)) { if (FileType.JMX.equals(engineType)) {
return new XmlEngineSourceParse(); return new XmlEngineSourceParse();
} }

View File

@ -13,7 +13,7 @@ import java.io.InputStream;
public class XmlEngineSourceParse implements EngineSourceParser { public class XmlEngineSourceParse implements EngineSourceParser {
@Override @Override
public InputStream parse(EngineContext context, InputStream source) throws Exception { public String parse(EngineContext context, InputStream source) throws Exception {
final InputSource inputSource = new InputSource(source); final InputSource inputSource = new InputSource(source);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@ -21,7 +21,7 @@ public class XmlEngineSourceParse implements EngineSourceParser {
DocumentBuilder docBuilder = factory.newDocumentBuilder(); DocumentBuilder docBuilder = factory.newDocumentBuilder();
final Document document = docBuilder.parse(inputSource); final Document document = docBuilder.parse(inputSource);
final DocumentParser documentParser = createDocumentParser(context.getEngineType()); final DocumentParser documentParser = createDocumentParser(context.getFileType());
return documentParser.parse(context, document); return documentParser.parse(context, document);
} }

View File

@ -6,5 +6,5 @@ import org.w3c.dom.Document;
import java.io.InputStream; import java.io.InputStream;
public interface DocumentParser { public interface DocumentParser {
InputStream parse(EngineContext context, Document document) throws Exception; String parse(EngineContext context, Document document) throws Exception;
} }

View File

@ -1,16 +1,18 @@
package io.metersphere.parse.xml.reader; package io.metersphere.parse.xml.reader;
import io.metersphere.commons.constants.EngineType; import io.metersphere.commons.constants.FileType;
import io.metersphere.parse.xml.reader.jmx.JmeterDocumentParser; import io.metersphere.parse.xml.reader.jmx.JmeterDocumentParser;
public class DocumentParserFactory { public class DocumentParserFactory {
public static DocumentParser createDocumentParser(String type) { public static DocumentParser createDocumentParser(String type) {
final EngineType engineType = EngineType.valueOf(type); final FileType fileType = FileType.valueOf(type);
if (EngineType.JMX.equals(engineType)) { switch (fileType) {
case JMX:
return new JmeterDocumentParser(); return new JmeterDocumentParser();
default:
break;
} }
return null; return null;
} }
} }

View File

@ -15,10 +15,7 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
public class JmeterDocumentParser implements DocumentParser { public class JmeterDocumentParser implements DocumentParser {
private final static String HASH_TREE_ELEMENT = "hashTree"; private final static String HASH_TREE_ELEMENT = "hashTree";
@ -32,7 +29,7 @@ public class JmeterDocumentParser implements DocumentParser {
private EngineContext context; private EngineContext context;
@Override @Override
public InputStream parse(EngineContext context, Document document) throws Exception { public String parse(EngineContext context, Document document) throws Exception {
this.context = context; this.context = context;
final Element jmeterTestPlan = document.getDocumentElement(); final Element jmeterTestPlan = document.getDocumentElement();
@ -49,18 +46,17 @@ public class JmeterDocumentParser implements DocumentParser {
} }
} }
return documentToInputStream(document); return documentToString(document);
} }
private InputStream documentToInputStream(Document document) throws TransformerException { private String documentToString(Document document) throws TransformerException {
DOMSource domSource = new DOMSource(document); DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer); StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance(); TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer(); Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result); transformer.transform(domSource, result);
final String resultStr = writer.toString(); return writer.toString();
return new ByteArrayInputStream(resultStr.getBytes(StandardCharsets.UTF_8));
} }
private void parseHashTree(Element hashTree) { private void parseHashTree(Element hashTree) {