提交 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();
//原先文件的新地址
Path originalPath = Paths.get(basePath +storePath);
File parentFile = originalPath.toFile().getParentFile();
if(!parentFile.exists()||!parentFile.isDirectory())parentFile.mkdirs();
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();
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());
}
}
package com.zjty.adaptationmaster.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class FileUtil {
private static final int BUFFEREDSIZE = 1024;
static int BUFFER_SIZE = 1024;
/**
* 解压zip格式的压缩文件到当前文件夹
*
* @param zipFileName
* @throws Exception
* zip解压
* @param srcFile zip源文件
* @param destDirPath 解压后的目标文件夹
* @throws RuntimeException 解压失败会抛出运行时异常
*/
@SuppressWarnings("unchecked")
public synchronized void unzipFile(String zipFileName) throws Exception {
try {
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName);
if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(f.getParent());
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + "/" + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else {
//读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + "/" + gbkPath;
//建目录
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + "/" + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = bis.read()) != -1) {
bos.write((byte) c);
}
bos.close();
fos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 解压zip格式的压缩文件到指定位置
*
* @param zipFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
*/
@SuppressWarnings("unchecked")
public synchronized void unzip(String zipFileName, String extPlace) throws Exception {
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName);
if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else {
//读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;
//建目录
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = bis.read()) != -1) {
bos.write((byte) c);
}
bos.close();
fos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
}
/**
* 解压zip格式的压缩文件到指定位置
*
* @param zipFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
*/
@SuppressWarnings("unchecked")
public synchronized void unzip(String zipFileName, String extPlace, boolean whether) throws Exception {
// 开始解压
ZipFile zipFile = null;
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName);
if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
continue;
} else {
//读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;
//建目录
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = bis.read()) != -1) {
bos.write((byte) c);
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
bos.close();
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
long end = System.currentTimeMillis();
System.out.println("解压完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 压缩zip格式的压缩文件
*
* @param inputFilename 压缩的文件或文件夹及详细路径
* @param zipFilename 输出文件名称及详细路径
* @throws IOException
*/
public synchronized void zip(String inputFilename, String zipFilename) throws IOException {
zip(new File(inputFilename), zipFilename);
}
/**
* 压缩zip格式的压缩文件
*
* @param inputFile 需压缩文件
* @param zipFilename 输出文件及详细路径
* @throws IOException
*/
public synchronized void zip(File inputFile, String zipFilename) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
try {
zip(inputFile, out, "");
} catch (IOException e) {
throw e;
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
out.close();
}
}
/**
* 压缩zip格式的压缩文件
*
* @param inputFile 需压缩文件
* @param out 输出压缩文件
* @param base 结束标识
* @throws IOException
*/
@SuppressWarnings("unused")
private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
/**
* 解压tar格式的压缩文件到指定目录下
*
* @param tarFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
*/
public synchronized void untar(String tarFileName, String extPlace) throws Exception {
}
/**
* 压缩tar格式的压缩文件
*
* @param inputFilename 压缩文件
* @param tarFilename 输出路径
* @throws IOException
*/
public synchronized void tar(String inputFilename, String tarFilename) throws IOException {
tar(new File(inputFilename), tarFilename);
}
public static void main(String[] args) {
/**
* 压缩tar格式的压缩文件
*
* @param inputFile 压缩文件
* @param tarFilename 输出路径
* @throws IOException
*/
public synchronized void tar(File inputFile, String tarFilename) throws IOException {
TarOutputStream out = new TarOutputStream(new FileOutputStream(tarFilename));
String filePath = "C:\\Users\\wyl\\Desktop\\alllib.zip";
String path = "C:\\Users\\wyl\\Desktop\\unZip";
try {
tar(inputFile, out, "");
} catch (IOException e) {
throw e;
} finally {
out.close();
}
}
/**
* 压缩tar格式的压缩文件
*
* @param inputFile 压缩文件
* @param out 输出文件
* @param base 结束标识
* @throws IOException
*/
@SuppressWarnings("unused")
private synchronized void tar(File inputFile, TarOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new TarEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
tar(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new TarEntry(base));
} else {
out.putNextEntry(new TarEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
unZip(new File(filePath),path);
} catch (Exception e) {
e.printStackTrace();
}
}
/* <!-- ant -->
maven 坐标
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.7</version>
</dependency>
*/
}
......@@ -4,7 +4,7 @@ import com.zjty.adaptationmaster.adaptor.entity.Project;
import com.zjty.adaptationmaster.adaptor.entity.Report;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
......@@ -13,7 +13,6 @@ import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.regex.Matcher;
/**
* 项目体检,根据既定特征值,
......@@ -25,47 +24,45 @@ public class Inspector {
public static void main(String[] args) {
Project project = new Project();
project.setCodeUrl("");
Map<String,Report.Language> suffixLanguageMapping = new HashMap<>();
Map<String, Report.Separate> suffixSeparateMapping = new HashMap<>();
List<String> configFileSuffixList = new ArrayList<>();
Inspector inspector = new Inspector(project,suffixLanguageMapping,suffixSeparateMapping/*,configFileSuffixList*/);
inspector.inspect();
project.setCodeUrl("C:\\home\\project\\rsc\\hrmbclient");
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);
Inspector inspector = new Inspector(project,suffixLanguageMapping);
System.out.println(inspector.inspect());
}
private Project project;
private Report report;
private Map<String, Counter> languageMatchMap;
private Map<String, Counter> separateMatchMap;
private Map<String, Report.Language> suffixLanguageMapping;
private Map<String, Report.Separate> suffixSeparateMapping;
private Map<String,List<Path>> configFileTypePathsMapping;
public Inspector(Project project,Map<String,Report.Language> suffixLanguageMapping,Map<String, Report.Separate> suffixSeparateMapping/*,List<String> configFileSuffixList*/){
public Inspector(Project project,Map<String,Report.Language> suffixLanguageMapping){
this.project = project;
this.suffixLanguageMapping = suffixLanguageMapping;
this.suffixSeparateMapping = suffixSeparateMapping;
this.languageMatchMap = new HashMap<>();
for(String s:suffixLanguageMapping.keySet()){
languageMatchMap.put(s,new Counter());
}
this.separateMatchMap = new HashMap<>();
for(String s:suffixSeparateMapping.keySet()){
separateMatchMap.put(s,new Counter());
}
/**
* 如果存在html文件,不一定是前端代码,
* 如果只存在html文件,不存在其他类型文件,大概率是前端代码
* 策略:
* 先检测是否存在html文件,如果存在,假定为前端项目
* 然后检测其他条件,如果符合,将假定冲掉
*/
languageMatchMap.put("html",new Counter());
this.configFileTypePathsMapping = new HashMap<>();
// for(String s:configFileSuffixList){
// configFileTypePathsMapping.put(s,new ArrayList<>());
// }
configFileTypePathsMapping.put("xml",new ArrayList<>());
configFileTypePathsMapping.put("properties",new ArrayList<>());
configFileTypePathsMapping.put("yml",new ArrayList<>());
this.report = new Report();
}
private List<Path> propertiesConfigPaths = new ArrayList<>();
private List<Path> ymlConfigPaths = new ArrayList<>();
private List<Path> xmlConfigPaths = new ArrayList<>();
/**
* FileVisitResult.CONTINUE 继续遍历
* FileVisitResult.TERMINATE 中止访问
......@@ -79,11 +76,6 @@ public class Inspector {
for(String s:suffixLanguageMapping.keySet()){
languageSuffixMatcherMapping.put(s,aDefault.getPathMatcher("glob:**/*."+s));
}
Map<String,PathMatcher> separateSuffixMatcherMapping = new HashMap<>();
for(String s:suffixLanguageMapping.keySet()){
separateSuffixMatcherMapping.put(s,aDefault.getPathMatcher("glob:**/*."+s));
}
Map<PathMatcher,String> configFileMatcherSuffixMapping = new HashMap<>();
for(String s:configFileTypePathsMapping.keySet()){
configFileMatcherSuffixMapping.put(aDefault.getPathMatcher("glob:**/*."+s),s);
......@@ -99,26 +91,26 @@ public class Inspector {
*/
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
for(Map.Entry<String,PathMatcher> entry:languageSuffixMatcherMapping.entrySet()){
if(entry.getValue().matches(dir)){
languageMatchMap.get(entry.getKey()).plus();
}
}
for(Map.Entry<String,PathMatcher> entry:separateSuffixMatcherMapping.entrySet()){
if(entry.getValue().matches(dir)){
separateMatchMap.get(entry.getKey()).plus();
}
}
/**
* 这里是对于路径(文件夹)的过滤,在这里读不到文件
* 如果能判断,可以返回FileVisitResult.SKIP_SUBTREE 不访问子目录
*/
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
for(Map.Entry<String,PathMatcher> entry:languageSuffixMatcherMapping.entrySet()){
if(entry.getValue().matches(file)){
languageMatchMap.get(entry.getKey()).plus();
}
}
for(Map.Entry<PathMatcher,String> entry:configFileMatcherSuffixMapping.entrySet()){
if(entry.getKey().matches(file)){
configFileTypePathsMapping.get(entry.getValue()).add(file);
}
}
//System.out.println(file);
return FileVisitResult.CONTINUE;
}
......@@ -139,21 +131,32 @@ public class Inspector {
}
public Report analysis(){
report.setLanguage(Report.Language.ONLYVIEW);
for(Map.Entry<String,Counter> entry:languageMatchMap.entrySet()){
if(entry.getValue().getNumber()>0){
report.setLanguage(suffixLanguageMapping.get(entry.getKey()));
}
}
for(Map.Entry<String,Counter> entry:separateMatchMap.entrySet()){
if(entry.getValue().getNumber()>0){
report.setIsSeparate(suffixSeparateMapping.get(entry.getKey()));
if(!entry.getKey().equals("html")){
report.setLanguage(suffixLanguageMapping.get(entry.getKey()));
}
}
}
/**
* 对于具体的配置文件对应的处理方法
* 增加要处理的文件类型需要在这里增加相应的处理方法
*/
for(Map.Entry<String,List<Path>> entry:configFileTypePathsMapping.entrySet()){
//System.out.println(entry.getKey());
switch (entry.getKey()){
case ".xml":
/**
* 配置文件的一个类型,xml文件
*/
case "xml":
System.out.println(entry.getValue().size());
for(Path path:entry.getValue()){
if(path.getFileName().equals("pom.xml")){
/**
* 对于maven工程,可以在maven配置文件中得到一这些信息
* 编译方式打包方式和打好的包的路径
*/
if(path.getFileName().endsWith("pom.xml")){
report.setDependenceManagement(Report.DependenceManagement.MAVEN);
report.setCompileFilePath(path.toString());
......@@ -164,50 +167,61 @@ public class Inspector {
} catch (DocumentException e) {
e.printStackTrace();
}
//Node dependences = document.selectSingleNode("dependencies");
//report.setPackagePath();
Element elementProject = document.getRootElement();
String projectMavenName = elementProject.elementText("name");
String projectMavenVersion = elementProject.elementText("version");
report.setPackagePath(path.getParent()+"/target/"+projectMavenName+"-"+projectMavenVersion+".war");
}
//PathMatcher matcher = FileSystems.getDefault().getPathMatcher("");
if(path.getFileName().toString().equals("application.properties")){
}
break;
/**
* 配置文件的一个类型,properties文件
*
*/
case "properties":
for(Path path:entry.getValue()) {
/**
* springboot项目的配置文件一般是application.properties或application.yml
*/
if (path.getFileName().endsWith("application.properties")) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(path.toFile()));
} catch (IOException e) {
e.printStackTrace();
}
String datasourceDriver = properties.getProperty("spring.datasource.driver-class-name");
String active = properties.getProperty("spring.profiles.active");
System.out.println(path.getParent());
File file = new File(path.getParent().toString()+"/application-"+active+".properties");
if(file.exists()){
File file = new File(path.getParent().toString() + "/application-" + active + ".properties");
if (file.exists()) {
Properties properties1 = new Properties();
try {
properties1.load(new FileInputStream(file));
String driver = properties1.getProperty("spring.datasource.driver-class-name");
if(driver!=null)datasourceDriver = driver;
if (driver != null) datasourceDriver = driver;
} catch (IOException e) {
e.printStackTrace();
}
}else {
} else {
System.out.println("没有找到active配置文件");
}
if(datasourceDriver!=null){
if(datasourceDriver.contains(Report.DatabaseType.MYSQL.name().toLowerCase())){
if (datasourceDriver != null) {
if (datasourceDriver.contains(Report.DatabaseType.MYSQL.name().toLowerCase())) {
report.setDatabaseType(Report.DatabaseType.MYSQL);
} else if(datasourceDriver.contains(Report.DatabaseType.POSTGRE.name().toLowerCase())){
} else if (datasourceDriver.contains(Report.DatabaseType.POSTGRE.name().toLowerCase())) {
report.setDatabaseType(Report.DatabaseType.POSTGRE);
} else if(datasourceDriver.contains(Report.DatabaseType.ORACLE.name().toLowerCase())){
} else if (datasourceDriver.contains(Report.DatabaseType.ORACLE.name().toLowerCase())) {
report.setDatabaseType(Report.DatabaseType.ORACLE);
} else if(datasourceDriver.contains(Report.DatabaseType.SQLSERVER.name().toLowerCase())){
} else if (datasourceDriver.contains(Report.DatabaseType.SQLSERVER.name().toLowerCase())) {
report.setDatabaseType(Report.DatabaseType.SQLSERVER);
}
}
}
}
break;
case "yml":
break;
}
}
// for(Path path:propertiesConfigPaths){
......@@ -253,7 +267,7 @@ public class Inspector {
// e.printStackTrace();
// }
// }
return null;
return report;
}
public class Counter {
private int i = 0;
......
......@@ -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());
}
}
}
//container.add(readedFile.getPath().getFileName()+"文本替换:"+entity.getTextMatching()+"|"+entity.getReplacing());
} else {
//template.convertAndSend("===="+readedFile.getPath().getFileName()+"////"+entity.getTextMatching()+"||||"+entity.getReplacing(),"1L");
//responseWriter.write("===="+readedFile.getPath().getFileName()+"////"+entity.getTextMatching()+"||||"+entity.getReplacing());
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;
}
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论