提交 cd8ae5ed authored 作者: wyl's avatar wyl

adapt done

上级 ca8f77f7
......@@ -139,7 +139,13 @@
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.2</version>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
......
......@@ -66,12 +66,12 @@ public class WebSocketServer {
}
public static void sendInfo(String sessionId,String msg,String action,String state,String projectName){
try {
map.get(sessionId).sendMessage(LocalDateTime.now()+"\t"+projectName+"\t"+action+"\t"+state+"\t"+msg);
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(LocalDateTime.now()+"\t"+projectName+"\t"+action+"\t"+state+"\t"+msg);
// try {
// //map.get(sessionId).sendMessage(LocalDateTime.now()+"\t"+projectName+"\t"+action+"\t"+state+"\t"+msg);
// } catch (IOException e) {
// e.printStackTrace();
// }
}
......
......@@ -37,6 +37,6 @@ public class Report {
MYSQL,ORACLE,SQLSERVER,POSTGRE
}
public enum Language{
JAVA,PYTHON,CPP
JAVA,PYTHON,CPP,JSP,ONLYVIEW
}
}
......@@ -33,6 +33,7 @@ public class Rule {
private MatchType pathMatchType;//文件匹配方式 路径/文件名/后缀
private String path;//文件路径匹配
private TextMatch textMatching;//文本匹配方式 全文匹配/正则匹配
private DealWay dealWay;//替换,在匹配字之前插入,在匹配字之后插入
/**
* AREA
......@@ -42,14 +43,14 @@ public class Rule {
private String replacing;//更改方式 全文替换/正则替换
public enum MatchType{
PATH,NAME,SUFFIX
PATH,NAME,SUFFIX,GLOB
}
public enum TextMatch{
AREA
AREA,CONTENT
}
public enum DealWay{
REPLACE,INSERTAFTER,INSERTBEFORE
REPLACE,INSERTAFTER,INSERTBEFORE,InsertBetween
}
}
......@@ -64,7 +64,7 @@ public class AdaptationServiceImpl implements AdaptationService {
FileUtil fileUtil = new FileUtil();
project.setCodeUrl(Const.UPLOAD_LOCATION+File.separator+project.getProjectName());
try {
fileUtil.unzip(path,project.getCodeUrl());
fileUtil.unZip(new File(path),project.getCodeUrl());
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -7,6 +7,9 @@ import com.zjty.adaptationmaster.adaptor.entity.OriginalFile;
import com.zjty.adaptationmaster.adaptor.entity.Rule;
import com.zjty.adaptationmaster.base.enums.Const;
import com.zjty.adaptationmaster.adaptor.repository.OriginalFileDao;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -59,7 +62,9 @@ public class Adaptor {
public void doAdapt(){
//String regular = preprocesAdaptation();
//List<String> strings1 = preprocessRegular();
Map<String, Rule> strings1 = preprocessRegular();
List<MatchAndRule> strings1 = preprocessRegular();
System.out.println(strings1.size()+"规则数量");
if(strings1.size()<1){
System.out.println("检测到匹配规则为空");
return;
......@@ -70,33 +75,36 @@ public class Adaptor {
//记录实际具体适配内容
//List<AdaptationDetailsLogEntity> detailsLogEntities = new ArrayList<>();
ReadedFileRepository repository = new ReadedFileRepository();
String storePathParent ="originalFile/"+ UUID.randomUUID().toString();
try {
Files.walkFileTree(Paths.get(project.getCodeUrl()),new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// System.out.println(file);
// String pathString = file.toString();
// pathString.indexOf(System.getProperty("file.separator"),basePath.length());
boolean match = false;
List<Rule> thisFileMatched = null;
//不符合规则的路径过滤掉
for(String regular:strings1.keySet()) {
if (FileSystems.getDefault().getPathMatcher("glob:"+regular).matches(file)) {
if(thisFileMatched==null)
thisFileMatched = new ArrayList<>();
thisFileMatched.add(strings1.get(regular));
for(MatchAndRule regular:strings1) {
if (FileSystems.getDefault().getPathMatcher("glob:"+regular.getMath()).matches(file)) {
if(thisFileMatched==null)thisFileMatched = new ArrayList<>();
thisFileMatched.add(regular.getRule());
match = true;
}
}
String storePath ="originalFile/"+ UUID.randomUUID().toString()+"/"+file.getFileName();
if(match) {
//System.out.println("match");
String storePath = storePathParent+"/"+UUID.randomUUID().toString()+"/"+file.getFileName();
//原先文件的新地址
Path originalPath = Paths.get(basePath +storePath);
File parentFile = originalPath.toFile().getParentFile();
if(!parentFile.exists()||!parentFile.isDirectory())parentFile.mkdirs();
if(match) {
OriginalFile originalFile = new OriginalFile(file.toString(),originalPath.toString(),new Date());
originalFiles.add(originalFile);
//Files.copy(file,originalPath);
Files.move(file, originalPath);
//splitedFile.setDeal(true);
WebSocketServer.sendInfo(uuid,"过滤得到符合规则的文件" + file,"替换","过滤文件",project.getProjectName());
......@@ -143,6 +151,7 @@ public class Adaptor {
return FileVisitResult.CONTINUE;
}
});
System.out.println("遍历结束");
ReadedFileTask apacheTask = new ReadedFileTask(repository.getReadedFiles(),project.getProjectName(),uuid);
pool.submit(apacheTask);
pool.shutdown();
......@@ -151,7 +160,7 @@ public class Adaptor {
} catch (InterruptedException e) {
e.printStackTrace();
}
originalFileDao.saveAll(originalFiles);
//originalFileDao.saveAll(originalFiles);
//adaptationDetailLogEntityDao.saveAll(detailsLogEntities);
} catch (IOException e) {
e.printStackTrace();
......@@ -162,20 +171,26 @@ public class Adaptor {
* 预处理适配规则,将规则实体类处理成glob表达式
* @return
*/
private Map<String,Rule> preprocessRegular() {
private List<MatchAndRule> preprocessRegular() {
//List<String> result = new ArrayList<>();
Map<String,Rule> result = new HashMap<>();
List<MatchAndRule> result = new ArrayList<>();
for (Rule adaptorEntity : ruleList) {
System.out.println(adaptorEntity.getPath());
// System.out.println(adaptorEntity.getTarget()+"11");
// System.out.println(adaptorEntity.getPathMatchType());
// System.out.println(adaptorEntity);
switch (adaptorEntity.getPathMatchType()) {
case SUFFIX:
result.put("**/**" + adaptorEntity.getPath(),adaptorEntity);
result.add(new MatchAndRule("**/**" + adaptorEntity.getPath(),adaptorEntity));
break;
case PATH:
result.put("" + adaptorEntity.getPath(),adaptorEntity);
result.add(new MatchAndRule("" + adaptorEntity.getPath(),adaptorEntity));
break;
case NAME:
result.put("**/" + adaptorEntity.getPath(),adaptorEntity);
result.add(new MatchAndRule("**/" + adaptorEntity.getPath(),adaptorEntity));
break;
case GLOB:
result.add(new MatchAndRule(adaptorEntity.getPath(),adaptorEntity));
}
}
return result;
......@@ -267,5 +282,11 @@ public class Adaptor {
}
}
@NoArgsConstructor
@AllArgsConstructor
@Data
private class MatchAndRule{
private String math;
private Rule rule;
}
}
package com.zjty.adaptationmaster.adaptor.service.Impl;
import com.zjty.adaptationmaster.adaptor.entity.Project;
import com.zjty.adaptationmaster.adaptor.entity.Report;
import com.zjty.adaptationmaster.adaptor.repository.ProjectDao;
import com.zjty.adaptationmaster.adaptor.service.InspectService;
import com.zjty.adaptationmaster.base.response.ServerResponse;
......@@ -11,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Service
public class InspectServiceImpl implements InspectService {
......@@ -21,6 +23,13 @@ public class InspectServiceImpl implements InspectService {
@Override
public ServerResponse inspect(Project project) {
//Project project = projectDao.getOne(projectId);
return ServerResponse.success(new Inspector(project,new HashMap<>(),new HashMap<>()/*,new ArrayList<>()*/).inspect());
Map<String, Report.Language> suffixLanguageMapping = new HashMap<>();
suffixLanguageMapping.put("java",Report.Language.JAVA);
suffixLanguageMapping.put("cpp",Report.Language.CPP);
suffixLanguageMapping.put("py",Report.Language.PYTHON);
suffixLanguageMapping.put("jsp",Report.Language.JSP);
//suffixLanguageMapping.put("html",Report.Language.ONLYVIEW);
return ServerResponse.success(new Inspector(project,suffixLanguageMapping).inspect());
}
}
......@@ -25,7 +25,7 @@ public class MavenCompiler {
public void compiler(){
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File(project.getReport().getCompileFilePath()));
request.setGoals(Collections.singletonList("compile"));
request.setGoals(Collections.singletonList("package"));
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(mavenHome));
......
......@@ -11,6 +11,7 @@ import java.io.*;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.Properties;
public class ReadedFileTask implements Runnable {
......@@ -20,7 +21,11 @@ public class ReadedFileTask implements Runnable {
private List<ReadedFile> readedFiles;
//private Writer responseWriter;
public ReadedFileTask(List<ReadedFile> readedFiles,String projectName,String uuid) {
//System.out.println("一个线程");
this.readedFiles = readedFiles;
for(ReadedFile file:readedFiles){
System.out.println(file.path);
}
this.projectName = projectName;
this.uuid = uuid;
}
......@@ -29,6 +34,7 @@ public class ReadedFileTask implements Runnable {
private int index;
public void setBySort(Adaptor.WriterBySort bySort) {
System.out.println("线程遇到大文件");
this.bySort = bySort;
}
......@@ -38,42 +44,90 @@ public class ReadedFileTask implements Runnable {
@Override
public void run() {
for(ReadedFile readedFile:readedFiles){
System.out.println("进来线程");
for(ReadedFile readedFile:readedFiles) {
if(readedFile.path.toString().endsWith(".properties")){
Properties properties = new Properties();
OutputStream stream =null;
try {
properties.load(new ByteArrayInputStream(readedFile.content.getBytes()));
for (Rule entity : readedFile.getThisMatchedRule()) {
//if(properties.getProperty(entity.getTarget())!=null) {
properties.setProperty(entity.getTarget(), entity.getReplacing());
//}
System.out.println("属性修改后"+properties.getProperty(entity.getTarget()));
}
readedFile.getPath().toFile().createNewFile();
stream = new FileOutputStream(readedFile.getPath().toFile());
System.out.println(properties);
properties.store(stream,"111");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
continue;
}
String content = readedFile.getContent();
System.out.println("规则数量"+readedFile.getThisMatchedRule().size());
for(Rule entity:readedFile.getThisMatchedRule()){
System.out.println("文本匹配方式"+entity.getTextMatching());
switch (entity.getTextMatching()){
System.out.println("规则数量" + readedFile.getThisMatchedRule().size());
for (Rule entity : readedFile.getThisMatchedRule()) {
System.out.println("文本匹配方式" + entity.getTextMatching());
switch (entity.getTextMatching()) {
case AREA:
System.out.println("case");
String[] split = entity.getTarget().split("\\|\\|");
String begin = split[0];
String matching = split[1];
String end = split[2];
System.out.println(begin+"||"+matching+"||"+end);
System.out.println(begin + "||" + matching + "||" + end);
int i = content.indexOf(matching);
if(i>-1){
WebSocketServer.sendInfo(uuid,readedFile.getPath().getFileName()+":"+matching+" 替换为 "+entity.getReplacing(),"替换","正在替换",projectName);
if (i > -1) {
System.out.println("i" + i);
WebSocketServer.sendInfo(uuid, readedFile.getPath().getFileName() + ":" + matching + " 替换为 " + entity.getReplacing(), "替换", "正在替换", projectName);
int beginIndex = content.lastIndexOf(begin, i);
int endIndex = content.indexOf(end, i + 1);
if(beginIndex>-1&&endIndex>-1){
content = content.substring(0,beginIndex)+entity.getReplacing()+content.substring(endIndex+end.length());
if (beginIndex > -1 && endIndex > -1) {
content = content.substring(0, beginIndex) + entity.getReplacing() + content.substring(endIndex + end.length());
}
} else {
System.out.println("-i" + i);
}
break;
case CONTENT:
switch (entity.getDealWay()) {
case REPLACE:
content = content.replaceAll(entity.getTarget(), entity.getReplacing());
break;
case INSERTAFTER:
//判断是不是本来就有,如果已有,就不用新增了
//if(content.indexOf(entity.getReplacing(),content.indexOf(entity.getTarget()))==-1) {
int insertPlace = content.indexOf(entity.getTarget()) + entity.getTarget().length();
content = content.substring(0, insertPlace) + entity.getReplacing() + content.substring(insertPlace);
//}
break;
case INSERTBEFORE:
break;
case InsertBetween:
break;
}
//container.add(readedFile.getPath().getFileName()+"文本替换:"+entity.getTextMatching()+"|"+entity.getReplacing());
//template.convertAndSend("===="+readedFile.getPath().getFileName()+"////"+entity.getTextMatching()+"||||"+entity.getReplacing(),"1L");
//responseWriter.write("===="+readedFile.getPath().getFileName()+"////"+entity.getTextMatching()+"||||"+entity.getReplacing());
break;
}
}
try {
if(bySort!=null){
System.out.println("toBySort");
bySort.insert(index,content);
if (bySort != null) {
bySort.insert(index, content);
} else {
System.out.println("writerFile");
WebSocketServer.sendInfo(uuid,readedFile.getPath().getFileName()+"","替换","正在写出",projectName);
WebSocketServer.sendInfo(uuid, readedFile.getPath().getFileName() + "", "替换", "正在写出", projectName);
File file = readedFile.getPath().toFile();
file.createNewFile();
OutputStreamWriter contentWriter = new OutputStreamWriter(new FileOutputStream(file));
......@@ -83,6 +137,7 @@ public class ReadedFileTask implements Runnable {
} catch (IOException e) {
e.printStackTrace();
}
}
}
......
package com.zjty.adaptationmaster;
import com.zjty.adaptationmaster.adaptor.entity.Project;
import com.zjty.adaptationmaster.adaptor.entity.Rule;
import com.zjty.adaptationmaster.adaptor.service.Impl.Adaptor;
import org.junit.Test;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class TestAdapt {
public static void main(String[] args) {
List ruleList = new ArrayList();
Rule rule = new Rule();
rule.setPathMatchType(Rule.MatchType.NAME);
rule.setPath("pom.xml");
rule.setTextMatching(Rule.TextMatch.CONTENT);
rule.setDealWay(Rule.DealWay.REPLACE);
rule.setTarget("<packaging>jar</packaging>");
rule.setReplacing("<packaging>war</packaging>");
ruleList.add(rule);
Rule rule11 = new Rule();
rule11.setPathMatchType(Rule.MatchType.NAME);
rule11.setPath("pom.xml");
rule11.setTextMatching(Rule.TextMatch.CONTENT);
rule11.setDealWay(Rule.DealWay.INSERTAFTER);
rule11.setTarget("<dependencies>");
rule11.setReplacing("<dependency>\n" +
" <groupId>org.postgresql</groupId>\n" +
" <artifactId>postgresql</artifactId>\n" +
" <scope>runtime</scope>\n" +
" </dependency>");
ruleList.add(rule11);
Rule rule1 = new Rule();
rule1.setPathMatchType(Rule.MatchType.GLOB);
rule1.setPath("**/application**.properties");
rule1.setTextMatching(Rule.TextMatch.CONTENT);
rule1.setDealWay(Rule.DealWay.REPLACE);
rule1.setTarget("spring.datasource.driver-class-name");
rule1.setReplacing("org.postgresql.Driver");
ruleList.add(rule1);
Rule rule2 = new Rule();
rule2.setPathMatchType(Rule.MatchType.GLOB);
rule2.setPath("**/application**.properties");
rule2.setTextMatching(Rule.TextMatch.CONTENT);
rule2.setDealWay(Rule.DealWay.REPLACE);
rule2.setTarget("spring.datasource.url");
rule2.setReplacing("jdbc:postgresql://localhost:5866/hrm");
ruleList.add(rule2);
Rule rule3 = new Rule();
rule3.setPathMatchType(Rule.MatchType.GLOB);
rule3.setPath("**/application**.properties");
rule3.setTextMatching(Rule.TextMatch.CONTENT);
rule3.setDealWay(Rule.DealWay.REPLACE);
rule3.setTarget("spring.datasource.username");
rule3.setReplacing("sysdba");
ruleList.add(rule3);
Rule rule4 = new Rule();
rule4.setPathMatchType(Rule.MatchType.GLOB);
rule4.setPath("**/application**.properties");
rule4.setTextMatching(Rule.TextMatch.CONTENT);
rule4.setDealWay(Rule.DealWay.REPLACE);
rule4.setTarget("spring.datasource.password");
rule4.setReplacing("highgo@123");
ruleList.add(rule4);
Rule rule5 = new Rule();
rule5.setPathMatchType(Rule.MatchType.GLOB);
rule5.setPath("**/application**.properties");
rule5.setTextMatching(Rule.TextMatch.CONTENT);
rule5.setDealWay(Rule.DealWay.REPLACE);
rule5.setTarget("spring.jpa.database-platform");
rule5.setReplacing("org.hibernate.dialect.PostgreSQLDialect");
ruleList.add(rule5);
Adaptor adaptor = new Adaptor();
adaptor.setRuleList(ruleList);
adaptor.setUuid("111");
Project project = new Project();
project.setCodeUrl("C:\\home\\project\\rsc\\hrmbclient");
project.setProjectName("hrm");
adaptor.setProject(project);
adaptor.doAdapt();
}
@Test
public void test(){
Path path = Paths.get("D:/aaa.properties");
System.out.println(path.toString().endsWith(".properties"));
}
}
package com.zjty.adaptationmaster;
import com.zjty.adaptationmaster.adaptor.controller.WebSocketServer;
import java.io.*;
public class TestProcess {
public static void main(String[] args) {
try {
Process exec = Runtime.getRuntime().exec("C:/Users/wyl/Desktop/national/AAS-V9.0/bin/appctl.cmd install -p Qwer123!@# hrm C://home/project/rsc/hrmbclient/target/hrmanager-0.0.1-SNAPSHOT.war");
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(exec.getOutputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.write("@Qwer123!@#");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论