完成脚本执行方法的二次封装
This commit is contained in:
parent
709bdddee5
commit
2463095f77
|
|
@ -0,0 +1,14 @@
|
|||
package com.osredm.codescan.entity.Nicad;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class NicadResult {
|
||||
|
||||
private String line;
|
||||
private int re;
|
||||
}
|
||||
|
|
@ -6,8 +6,10 @@ import com.osredm.codescan.config.StoreConfig;
|
|||
import com.osredm.codescan.entity.Nicad.NicadClones;
|
||||
import com.osredm.codescan.entity.Nicad.NicadInfo;
|
||||
import com.osredm.codescan.entity.Nicad.NicadJson;
|
||||
import com.osredm.codescan.entity.Nicad.NicadResult;
|
||||
import com.osredm.codescan.service.NicadService;
|
||||
import com.osredm.codescan.utils.FileUploadUtil;
|
||||
import com.osredm.codescan.utils.ProcessUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
|
@ -33,94 +35,18 @@ public class NicadServiceImpl implements NicadService {
|
|||
String language = nicadInfo.getLanguage();
|
||||
switch (language) {
|
||||
case "Java":
|
||||
String filePath = storeConfig.getFilePath() + nicadInfo.getFileName();
|
||||
String time = String.valueOf(System.currentTimeMillis());
|
||||
String flag = storeConfig.getFilePath() + time;
|
||||
// 解压文件
|
||||
String[] arguments = new String[] {"unzip" , filePath, "-d", flag};
|
||||
Process proc;
|
||||
try {
|
||||
proc = Runtime.getRuntime().exec(arguments);// 执行解压文件
|
||||
//用输入输出流来截取结果
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
|
||||
String line = null;
|
||||
String line1 = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
line1 = line;
|
||||
}
|
||||
in.close();
|
||||
int re = proc.waitFor(); //waitFor是用来显示脚本是否运行成功,1表示失败,0表示成功,还有其他的表示其他错误
|
||||
|
||||
if (re == 0) {
|
||||
//成功
|
||||
|
||||
Process proc1;
|
||||
String[] arguments1 = new String[] {"nicad6" ,"functions","java", flag, "default-report"};
|
||||
proc1 = Runtime.getRuntime().exec(arguments1);// 执行解压文件
|
||||
//用输入输出流来截取结果
|
||||
BufferedReader in1 = new BufferedReader(new InputStreamReader(proc1.getInputStream()));
|
||||
|
||||
String line2 = null;
|
||||
String line22 = null;
|
||||
while ((line2 = in1.readLine()) != null) {
|
||||
System.out.println(line2);
|
||||
line22 = line2;
|
||||
}
|
||||
in1.close();
|
||||
int re1 = proc1.waitFor(); //waitFor是用来显示脚本是否运行成功,1表示失败,0表示成功,还有其他的表示其他错误
|
||||
if (re1 == 0) {
|
||||
//成功 读取文件
|
||||
Process proc2;
|
||||
String[] arguments2 = new String[] {"/usr/bin/python3.6", "/root/code-scan/nicad/main.py", storeConfig.getFilePath() + time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.xml"};
|
||||
proc2 = Runtime.getRuntime().exec(arguments2);// 执行解压文件
|
||||
//用输入输出流来截取结果
|
||||
BufferedReader in2 = new BufferedReader(new InputStreamReader(proc2.getInputStream()));
|
||||
|
||||
String line3 = null;
|
||||
String line33 = null;
|
||||
while ((line3 = in2.readLine()) != null) {
|
||||
System.out.println(line3);
|
||||
line33 = line3;
|
||||
}
|
||||
in2.close();
|
||||
int re2 = proc2.waitFor();
|
||||
if (re2 == 0) {
|
||||
//转换为实体类
|
||||
JSONObject jsonObject = JSONObject.parseObject(line33);
|
||||
System.out.println(jsonObject);
|
||||
NicadJson nicadJson = JSON.toJavaObject(jsonObject,NicadJson.class);
|
||||
map.put("status",SUCCESS);
|
||||
map.put("html","/api/files/"+time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.html");
|
||||
map.put("json",nicadJson);
|
||||
}else {
|
||||
map.put("status",FAIL);
|
||||
}
|
||||
|
||||
return map;
|
||||
}else {
|
||||
//失败
|
||||
map.put("status",FAIL);
|
||||
return map;
|
||||
}
|
||||
}else {
|
||||
//失败
|
||||
map.put("status",FAIL);
|
||||
return map;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return nicadScript(nicadInfo,map,"java");
|
||||
case "C":
|
||||
return nicadScript(nicadInfo,map,"c");
|
||||
|
||||
case "C#":
|
||||
return nicadScript(nicadInfo,map,"c#");
|
||||
|
||||
case "Ruby":
|
||||
return nicadScript(nicadInfo,map,"ruby");
|
||||
|
||||
case "JavaScript":
|
||||
return nicadScript(nicadInfo,map,"javascript");
|
||||
|
||||
default:
|
||||
map.put("status",FAIL);
|
||||
|
|
@ -128,4 +54,44 @@ public class NicadServiceImpl implements NicadService {
|
|||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
private HashMap<String,Object> nicadScript(NicadInfo nicadInfo,HashMap<String,Object> map,String language){
|
||||
String filePath = storeConfig.getFilePath() + nicadInfo.getFileName();
|
||||
String time = String.valueOf(System.currentTimeMillis());
|
||||
String flag = storeConfig.getFilePath() + time;
|
||||
// 解压文件
|
||||
String[] arguments = new String[] {"unzip" , filePath, "-d", flag};
|
||||
|
||||
NicadResult process = ProcessUtil.Process(arguments);
|
||||
if (process.getRe() == 0) {
|
||||
//成功
|
||||
String[] arguments1 = new String[] {"nicad6" ,"functions",language, flag, "default-report"};
|
||||
NicadResult process1 = ProcessUtil.Process(arguments1);
|
||||
if (process1.getRe() == 0) {
|
||||
String[] arguments2 = new String[] {"/usr/bin/python3.6", "/root/code-scan/nicad/main.py", storeConfig.getFilePath() + time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.xml"};
|
||||
NicadResult process2 = ProcessUtil.Process(arguments2);
|
||||
if (process2.getRe() == 0) {
|
||||
JSONObject jsonObject = JSONObject.parseObject(process2.getLine());
|
||||
System.out.println(jsonObject);
|
||||
NicadJson nicadJson = JSON.toJavaObject(jsonObject,NicadJson.class);
|
||||
map.put("status",SUCCESS);
|
||||
map.put("html","/api/files/"+time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.html");
|
||||
map.put("json",nicadJson);
|
||||
return map;
|
||||
}else {
|
||||
//失败
|
||||
map.put("status",FAIL);
|
||||
return map;
|
||||
}
|
||||
}else {
|
||||
//失败
|
||||
map.put("status",FAIL);
|
||||
return map;
|
||||
}
|
||||
}else {
|
||||
//失败
|
||||
map.put("status",FAIL);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
package com.osredm.codescan.utils;
|
||||
|
||||
import com.osredm.codescan.entity.Nicad.NicadResult;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class ProcessUtil {
|
||||
|
||||
public static NicadResult Process(String[] arg){
|
||||
|
||||
|
||||
NicadResult nicadResult = new NicadResult();
|
||||
|
||||
|
||||
|
||||
Process proc;
|
||||
try {
|
||||
proc = Runtime.getRuntime().exec(arg);// 执行解压文件
|
||||
//用输入输出流来截取结果
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
|
||||
String line = null;
|
||||
String line1 = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
line1 = line;
|
||||
}
|
||||
in.close();
|
||||
int re = proc.waitFor(); //waitFor是用来显示脚本是否运行成功,1表示失败,0表示成功,还有其他的表示其他错误
|
||||
|
||||
nicadResult.setRe(re);
|
||||
nicadResult.setLine(line1);
|
||||
}catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return nicadResult;
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue