Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
adaptation-master
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
912协同工作系统
项目监控管理工具
adaptation-master
Commits
c8209a5f
提交
c8209a5f
authored
12月 23, 2019
作者:
wyl
浏览文件
操作
浏览文件
下载
差异文件
合并分支 'wyl' 到 'master'
Wyl 查看合并请求 ty_wyl/adaptation-master1!41
上级
00536ec1
ddccadf2
隐藏空白字符变更
内嵌
并排
正在显示
13 个修改的文件
包含
1017 行增加
和
336 行删除
+1017
-336
pom.xml
pom.xml
+7
-1
Report.java
...java/com/zjty/adaptationmaster/adaptor/entity/Report.java
+1
-0
Rule.java
...n/java/com/zjty/adaptationmaster/adaptor/entity/Rule.java
+14
-13
AdaptationServiceImpl.java
...ionmaster/adaptor/service/Impl/AdaptationServiceImpl.java
+2
-5
Adaptor.java
...m/zjty/adaptationmaster/adaptor/service/Impl/Adaptor.java
+308
-283
Adaptor.java
src/main/java/com/zjty/adaptationmaster/utils/Adaptor.java
+309
-0
Inspector.java
src/main/java/com/zjty/adaptationmaster/utils/Inspector.java
+3
-0
ReadedFileTask.java
.../java/com/zjty/adaptationmaster/utils/ReadedFileTask.java
+49
-34
ReadedFileTask1.java
...java/com/zjty/adaptationmaster/utils/ReadedFileTask1.java
+152
-0
WriterBySort.java
...in/java/com/zjty/adaptationmaster/utils/WriterBySort.java
+45
-0
Test1.java
src/test/java/com/zjty/adaptationmaster/Test1.java
+6
-0
TestAdapt.java
src/test/java/com/zjty/adaptationmaster/TestAdapt.java
+96
-0
TestProcess.java
src/test/java/com/zjty/adaptationmaster/TestProcess.java
+25
-0
没有找到文件。
pom.xml
浏览文件 @
c8209a5f
...
...
@@ -144,7 +144,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>
...
...
src/main/java/com/zjty/adaptationmaster/adaptor/entity/Report.java
浏览文件 @
c8209a5f
...
...
@@ -26,6 +26,7 @@ public class Report {
@ElementCollection
public
List
<
String
>
jarPath
;
//依赖文件的地址列表,用于编译,如果不是用版本管理工具
private
String
packagePath
;
private
int
fileNum
;
public
enum
DependenceManagement
{
MAVEN
,
GRADLE
,
ANT
...
...
src/main/java/com/zjty/adaptationmaster/adaptor/entity/Rule.java
浏览文件 @
c8209a5f
...
...
@@ -30,9 +30,10 @@ public class Rule {
private
Integer
status
=
1
;
//状态(0:删除 1:未删除)
private
MatchType
pathMatchType
;
//文件匹配方式 路径/文件名/后缀
//
private MatchType pathMatchType;//文件匹配方式 路径/文件名/后缀
private
String
path
;
//文件路径匹配
private
TextMatch
textMatching
;
//文本匹配方式 全文匹配/正则匹配
//private TextMatch textMatching;//文本匹配方式 全文匹配/正则匹配
//private DealWay dealWay;//替换,在匹配字之前插入,在匹配字之后插入
/**
* AREA
...
...
@@ -41,15 +42,15 @@ public class Rule {
private
String
target
;
//文本匹配目标
private
String
replacing
;
//更改方式 全文替换/正则替换
public
enum
MatchType
{
PATH
,
NAME
,
SUFFIX
,
GLOB
}
public
enum
TextMatch
{
AREA
,
CONTENT
}
public
enum
DealWay
{
REPLACE
,
INSERTAFTER
,
INSERTBEFORE
}
//
public enum MatchType{
//
PATH,NAME,SUFFIX,GLOB
//
}
//
//
public enum TextMatch{
//
AREA,CONTENT
//
}
//
//
public enum DealWay{
// REPLACE,INSERTAFTER,INSERTBEFORE,InsertBetween
//
}
}
src/main/java/com/zjty/adaptationmaster/adaptor/service/Impl/AdaptationServiceImpl.java
浏览文件 @
c8209a5f
...
...
@@ -7,10 +7,7 @@ import com.zjty.adaptationmaster.adaptor.repository.ProjectDao;
import
com.zjty.adaptationmaster.base.enums.Const
;
import
com.zjty.adaptationmaster.base.response.ServerResponse
;
import
com.zjty.adaptationmaster.adaptor.service.AdaptationService
;
import
com.zjty.adaptationmaster.utils.ApusicDeployer
;
import
com.zjty.adaptationmaster.utils.FileUtil
;
import
com.zjty.adaptationmaster.utils.FileZip
;
import
com.zjty.adaptationmaster.utils.MavenCompiler
;
import
com.zjty.adaptationmaster.utils.*
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -87,7 +84,7 @@ public class AdaptationServiceImpl implements AdaptationService {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
ServerResponse
.
success
(
filter
(
project1
)
);
return
ServerResponse
.
success
(
project
);
}
@Override
...
...
src/main/java/com/zjty/adaptationmaster/adaptor/service/Impl/Adaptor.java
浏览文件 @
c8209a5f
package
com
.
zjty
.
adaptationmaster
.
adaptor
.
service
.
Impl
;
import
com.zjty.adaptationmaster.adaptor.controller.WebSocketServer
;
import
com.zjty.adaptationmaster.adaptor.entity.Project
;
import
com.zjty.adaptationmaster.adaptor.repository.ProjectDao
;
import
com.zjty.adaptationmaster.base.response.ServerResponse
;
import
com.zjty.adaptationmaster.utils.ReadedFileTask
;
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
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.io.*
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.*
;
import
java.nio.file.attribute.BasicFileAttributes
;
import
java.util.*
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
@Component
public
class
Adaptor
{
//@Autowired
//private AdaptationDetailLogEntityDao adaptationDetailLogEntityDao;
@Autowired
private
OriginalFileDao
originalFileDao
;
private
ProjectDao
projectDao
;
private
String
uuid
;
//@Value("${base.path}")
private
String
basePath
=
Const
.
CONSOLE
;
//线程池数量,合适的线程数量能让程序更快
private
static
final
int
poolSize
=
20
;
//为避免内存溢出,对于超大文件,切分成部分分别读入处理,最后拼接写出
private
static
final
long
LIMIT
=
200
*
1024
*
1024
;
List
<
Rule
>
ruleList
;
Project
project
;
//String projectPath;
//private List<Rule> adaptorEntities = new ArrayList<>();
//public void setProjectPath(String projectPath) {
// this.projectPath = projectPath;
//}
public
void
setProject
(
Project
project
)
{
this
.
project
=
project
;
}
public
void
setRuleList
(
List
<
Rule
>
ruleList
)
{
this
.
ruleList
=
ruleList
;
}
public
void
setUuid
(
String
uuid
)
{
this
.
uuid
=
uuid
;
}
public
ServerResponse
doAdapt
(){
//String regular = preprocesAdaptation();
//List<String> strings1 = preprocessRegular();
Map
<
String
,
Rule
>
strings1
=
preprocessRegular
();
if
(
strings1
.
size
()<
1
){
System
.
out
.
println
(
"检测到匹配规则为空"
);
return
ServerResponse
.
error
(
"检测到匹配规则为空"
);
}
if
(
project
.
getCodeUrl
()
==
null
||
project
.
getCodeUrl
().
equals
(
""
)){
System
.
out
.
println
(
"未上传源代码"
);
return
ServerResponse
.
error
(
"未上传源代码"
);
}
ThreadPoolExecutor
pool
=
new
ThreadPoolExecutor
(
poolSize
,
poolSize
,
10
,
TimeUnit
.
SECONDS
,
new
ArrayBlockingQueue
<>(
poolSize
*
2
));
pool
.
setRejectedExecutionHandler
(
new
ThreadPoolExecutor
.
CallerRunsPolicy
());
List
<
OriginalFile
>
originalFiles
=
new
ArrayList
<>();
//记录实际具体适配内容
//List<AdaptationDetailsLogEntity> detailsLogEntities = new ArrayList<>();
ReadedFileRepository
repository
=
new
ReadedFileRepository
();
try
{
Files
.
walkFileTree
(
Paths
.
get
(
project
.
getCodeUrl
()),
new
SimpleFileVisitor
<
Path
>()
{
@Override
public
FileVisitResult
visitFile
(
Path
file
,
BasicFileAttributes
attrs
)
throws
IOException
{
// 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
));
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
)
{
OriginalFile
originalFile
=
new
OriginalFile
(
file
.
toString
(),
originalPath
.
toString
(),
new
Date
());
originalFiles
.
add
(
originalFile
);
Files
.
move
(
file
,
originalPath
);
//splitedFile.setDeal(true);
WebSocketServer
.
sendInfo
(
uuid
,
"过滤得到符合规则的文件"
+
file
,
"替换"
,
"过滤文件"
,
project
.
getProjectName
());
//System.out.println();
if
(
attrs
.
size
()
>
LIMIT
)
{
System
.
out
.
println
(
"找到大文件"
+
file
);
ReadedLinesRepository
linesRepository
=
new
ReadedLinesRepository
();
int
i
=
0
;
WriterBySort
bySort
=
new
WriterBySort
();
File
newFile
=
file
.
toFile
();
newFile
.
createNewFile
();
OutputStreamWriter
writer
=
new
OutputStreamWriter
(
new
FileOutputStream
(
newFile
),
StandardCharsets
.
UTF_8
);
bySort
.
setWriter
(
writer
);
for
(
String
s
:
Files
.
readAllLines
(
originalPath
,
Charset
.
forName
(
"utf-8"
)))
{
if
(
linesRepository
.
put
(
s
))
{
List
<
ReadedFileTask
.
ReadedFile
>
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
new
ReadedFileTask
.
ReadedFile
(
linesRepository
.
getReadedFiles
(),
attrs
,
file
,
thisFileMatched
));
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
readedFiles
,
project
.
getProjectName
(),
uuid
);
apacheTask
.
setBySort
(
bySort
);
apacheTask
.
setIndex
(
i
);
pool
.
submit
(
apacheTask
);
i
++;
}
}
List
<
ReadedFileTask
.
ReadedFile
>
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
new
ReadedFileTask
.
ReadedFile
(
linesRepository
.
getReadedFiles
(),
attrs
,
file
,
thisFileMatched
));
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
readedFiles
,
project
.
getProjectName
(),
uuid
);
apacheTask
.
setBySort
(
bySort
);
apacheTask
.
setIndex
(
i
);
pool
.
submit
(
apacheTask
);
i
++;
bySort
.
setSize
(
i
);
}
else
{
String
s
=
new
String
(
Files
.
readAllBytes
(
originalPath
));
ReadedFileTask
.
ReadedFile
readedFile
=
new
ReadedFileTask
.
ReadedFile
(
s
,
attrs
,
file
,
thisFileMatched
);
if
(
repository
.
put
(
readedFile
))
{
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
repository
.
getReadedFiles
(),
project
.
getProjectName
(),
uuid
);
pool
.
submit
(
apacheTask
);
}
}
}
/*else {
Files.copy(file, Paths.get(newPath));
}*/
return
FileVisitResult
.
CONTINUE
;
}
});
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
repository
.
getReadedFiles
(),
project
.
getProjectName
(),
uuid
);
pool
.
submit
(
apacheTask
);
pool
.
shutdown
();
try
{
pool
.
awaitTermination
(
1
,
TimeUnit
.
HOURS
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
originalFileDao
.
saveAll
(
originalFiles
);
//adaptationDetailLogEntityDao.saveAll(detailsLogEntities);
Project
one
=
projectDao
.
getOne
(
project
.
getId
());
one
.
setStage
(
1
);
return
ServerResponse
.
success
(
"替换成功"
,
one
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
ServerResponse
.
error
(
"替换失败"
);
}
}
/**
* 预处理适配规则,将规则实体类处理成glob表达式
* @return
*/
private
Map
<
String
,
Rule
>
preprocessRegular
()
{
//List<String> result = new ArrayList<>();
Map
<
String
,
Rule
>
result
=
new
HashMap
<>();
for
(
Rule
adaptorEntity
:
ruleList
)
{
switch
(
adaptorEntity
.
getPathMatchType
())
{
case
SUFFIX:
result
.
put
(
"**/**"
+
adaptorEntity
.
getPath
(),
adaptorEntity
);
break
;
case
PATH:
result
.
put
(
""
+
adaptorEntity
.
getPath
(),
adaptorEntity
);
break
;
case
NAME:
result
.
put
(
"**/"
+
adaptorEntity
.
getPath
(),
adaptorEntity
);
break
;
}
}
return
result
;
}
/**
* 对于大文件,按行读取,积攒到一定大小去处理
* 这里put进来每行
* 大小不够时返回false
* 大小够时返回true
*/
private
class
ReadedLinesRepository
{
private
static
final
long
LIMIT
=
200
*
1024
*
1024
;
//字节*k*m
private
List
<
StringBuilder
>
readedFiles
;
private
long
count
;
public
String
getReadedFiles
()
{
return
readedFiles
.
toString
();
}
public
boolean
put
(
String
readedFile
){
if
(
readedFiles
==
null
)
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
new
StringBuilder
(
readedFile
));
count
+=
readedFile
.
length
();
return
count
>
LIMIT
;
}
}
/**
* 读取到的大量小文件,积累到一定的大小,才交给一个线程去处理
*/
private
class
ReadedFileRepository
{
private
List
<
ReadedFileTask
.
ReadedFile
>
readedFiles
;
private
long
count
;
public
List
<
ReadedFileTask
.
ReadedFile
>
getReadedFiles
()
{
return
readedFiles
;
}
public
boolean
put
(
ReadedFileTask
.
ReadedFile
readedFile
){
if
(
readedFiles
==
null
)
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
readedFile
);
count
+=
readedFile
.
getAttributes
().
size
();
return
count
>
LIMIT
;
}
}
/**
* 过大的文件,切割成几块,每块编号,分给每个线程处理,处理结束后,按编号顺序写出
* 这里按编号控制写出顺序
*/
public
class
WriterBySort
{
private
int
currentIndex
=
-
1
;
private
Map
<
Integer
,
String
>
map
=
new
TreeMap
<>();
private
Writer
contentWriter
;
private
int
size
;
public
void
setWriter
(
Writer
writer
)
{
this
.
contentWriter
=
writer
;
}
public
void
setSize
(
int
size
)
{
this
.
size
=
size
;
if
(
currentIndex
==
size
){
try
{
contentWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
public
void
insert
(
Integer
key
,
String
value
){
map
.
put
(
key
,
value
);
while
(
map
.
get
(
currentIndex
+
1
)!=
null
){
try
{
contentWriter
.
write
(
map
.
remove
(
currentIndex
+
1
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
currentIndex
++;
if
(
currentIndex
==
size
){
try
{
contentWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
}
//package com.zjty.adaptationmaster.adaptor.service.Impl;
//
//import com.zjty.adaptationmaster.adaptor.controller.WebSocketServer;
//import com.zjty.adaptationmaster.adaptor.entity.Project;
//import com.zjty.adaptationmaster.adaptor.repository.ProjectDao;
//import com.zjty.adaptationmaster.base.response.ServerResponse;
//import com.zjty.adaptationmaster.utils.ReadedFileTask;
//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 com.zjty.adaptationmaster.utils.WriterBySort;
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.*;
//import java.nio.charset.Charset;
//import java.nio.charset.StandardCharsets;
//import java.nio.file.*;
//import java.nio.file.attribute.BasicFileAttributes;
//import java.util.*;
//import java.util.concurrent.ArrayBlockingQueue;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
////适应四种规则的适配器,改成只适配java正则的,再util下面
////@Component
//public class Adaptor {
// //@Autowired
// //private AdaptationDetailLogEntityDao adaptationDetailLogEntityDao;
// @Autowired
// private OriginalFileDao originalFileDao;
//
// private ProjectDao projectDao;
//
// private String uuid;
// //@Value("${base.path}")
// private String basePath = Const.CONSOLE;
// //线程池数量,合适的线程数量能让程序更快
// private static final int poolSize = 20;
// //为避免内存溢出,对于超大文件,切分成部分分别读入处理,最后拼接写出
// private static final long LIMIT = 200*1024*1024;
// List<Rule> ruleList;
// Project project;
// //String projectPath;
// //private List<Rule> adaptorEntities = new ArrayList<>();
//
// //public void setProjectPath(String projectPath) {
// // this.projectPath = projectPath;
// //}
//
//
// public void setProject(Project project) {
// this.project = project;
// }
//
// public void setRuleList(List<Rule> ruleList) {
// this.ruleList = ruleList;
// }
//
// public void setUuid(String uuid) {
// this.uuid = uuid;
// }
//
// public ServerResponse doAdapt(){
// //String regular = preprocesAdaptation();
// //List<String> strings1 = preprocessRegular();
// List<MatchAndRule> strings1 = preprocessRegular();
// System.out.println(strings1.size()+"规则数量");
//
// if(strings1.size()<1){
// System.out.println("检测到匹配规则为空");
// return ServerResponse.error("检测到匹配规则为空");
// }
// if(project.getCodeUrl() == null || project.getCodeUrl().equals("")){
// System.out.println("未上传源代码");
// return ServerResponse.error("未上传源代码");
// }
// ThreadPoolExecutor pool = new ThreadPoolExecutor(poolSize,poolSize,10, TimeUnit.SECONDS,new ArrayBlockingQueue<>(poolSize*2));
// pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// List<OriginalFile> originalFiles = new ArrayList<>();
// //记录实际具体适配内容
// //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(MatchAndRule regular:strings1) {
// //if (FileSystems.getDefault().getPathMatcher("glob:"+regular.getMath()).matches(file)) {
// //直接用java正则
// if (FileSystems.getDefault().getPathMatcher(regular.getMath()).matches(file)) {
// if(thisFileMatched==null)thisFileMatched = new ArrayList<>();
// thisFileMatched.add(regular.getRule());
// match = true;
// }
// }
//
// 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());
// //System.out.println();
// if (attrs.size() > LIMIT) {
// System.out.println("找到大文件" + file);
// ReadedLinesRepository linesRepository = new ReadedLinesRepository();
// int i = 0;
// WriterBySort bySort = new WriterBySort();
// File newFile = file.toFile();
// newFile.createNewFile();
// OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(newFile), StandardCharsets.UTF_8);
// bySort.setWriter(writer);
// for (String s : Files.readAllLines(originalPath, Charset.forName("utf-8"))) {
// if (linesRepository.put(s)) {
// List<ReadedFileTask.ReadedFile> readedFiles = new ArrayList<>();
// readedFiles.add(new ReadedFileTask.ReadedFile(linesRepository.getReadedFiles(), attrs, file,thisFileMatched));
// ReadedFileTask apacheTask = new ReadedFileTask(readedFiles,project.getProjectName(),uuid);
// apacheTask.setBySort(bySort);
// apacheTask.setIndex(i);
// pool.submit(apacheTask);
// i++;
// }
// }
// List<ReadedFileTask.ReadedFile> readedFiles = new ArrayList<>();
// readedFiles.add(new ReadedFileTask.ReadedFile(linesRepository.getReadedFiles(), attrs, file,thisFileMatched));
// ReadedFileTask apacheTask = new ReadedFileTask(readedFiles,project.getProjectName(),uuid);
// apacheTask.setBySort(bySort);
// apacheTask.setIndex(i);
// pool.submit(apacheTask);
// i++;
// bySort.setSize(i);
// }else {
// String s = new String(Files.readAllBytes(originalPath));
// ReadedFileTask.ReadedFile readedFile = new ReadedFileTask.ReadedFile(s, attrs, file,thisFileMatched);
// if (repository.put(readedFile)) {
// ReadedFileTask apacheTask = new ReadedFileTask(repository.getReadedFiles(),project.getProjectName(),uuid);
// pool.submit(apacheTask);
// }
// }
// }/*else {
// Files.copy(file, Paths.get(newPath));
// }*/
// return FileVisitResult.CONTINUE;
// }
// });
// System.out.println("遍历结束");
// ReadedFileTask apacheTask = new ReadedFileTask(repository.getReadedFiles(),project.getProjectName(),uuid);
// pool.submit(apacheTask);
// pool.shutdown();
// try {
// pool.awaitTermination(1,TimeUnit.HOURS);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// //originalFileDao.saveAll(originalFiles);
// //adaptationDetailLogEntityDao.saveAll(detailsLogEntities);
// Project one = projectDao.getOne(project.getId());
// one.setStage(1);
// return ServerResponse.success("替换成功",one);
// } catch (IOException e) {
// e.printStackTrace();
// return ServerResponse.error("替换失败");
// }
// }
//
// /**
// * 预处理适配规则,将规则实体类处理成glob表达式
// * @return
// */
// private List<MatchAndRule> preprocessRegular() {
// //List<String> result = new ArrayList<>();
// 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.add(new MatchAndRule("**/**" + adaptorEntity.getPath(),adaptorEntity));
// break;
// case PATH:
// result.add(new MatchAndRule("" + adaptorEntity.getPath(),adaptorEntity));
// break;
// case NAME:
// result.add(new MatchAndRule("**/" + adaptorEntity.getPath(),adaptorEntity));
// break;
// case GLOB:
// result.add(new MatchAndRule(adaptorEntity.getPath(),adaptorEntity));
// }
// }
// return result;
// }
//
// /**
// * 对于大文件,按行读取,积攒到一定大小去处理
// * 这里put进来每行
// * 大小不够时返回false
// * 大小够时返回true
// */
// private class ReadedLinesRepository{
// private static final long LIMIT = 200*1024*1024;//字节*k*m
//
// private List<StringBuilder> readedFiles;
// private long count;
//
// public String getReadedFiles() {
// return readedFiles.toString();
// }
//
// public boolean put(String readedFile){
// if(readedFiles==null)readedFiles = new ArrayList<>();
// readedFiles.add(new StringBuilder(readedFile));
// count+=readedFile.length();
// return count > LIMIT;
// }
// }
//
// /**
// * 读取到的大量小文件,积累到一定的大小,才交给一个线程去处理
// */
// private class ReadedFileRepository{
// private List<ReadedFileTask.ReadedFile> readedFiles;
// private long count;
//
// public List<ReadedFileTask.ReadedFile> getReadedFiles() {
// return readedFiles;
// }
//
// public boolean put(ReadedFileTask.ReadedFile readedFile){
// if(readedFiles==null)readedFiles = new ArrayList<>();
// readedFiles.add(readedFile);
// count+=readedFile.getAttributes().size();
// return count > LIMIT;
// }
// }
//
// /**
// * 过大的文件,切割成几块,每块编号,分给每个线程处理,处理结束后,按编号顺序写出
// * 这里按编号控制写出顺序
// */
//// public class WriterBySort{
//// private int currentIndex = -1;
//// private Map<Integer,String> map = new TreeMap<>();
//// private Writer contentWriter;
//// private int size;
////
//// public void setWriter(Writer writer) {
//// this.contentWriter = writer;
//// }
//// public void setSize(int size) {
//// this.size = size;
//// if(currentIndex==size){
//// try {
//// contentWriter.close();
//// } catch (IOException e) {
//// e.printStackTrace();
//// }
//// }
//// }
//// public void insert(Integer key, String value){
//// map.put(key,value);
//// while (map.get(currentIndex+1)!=null){
//// try {
//// contentWriter.write(map.remove(currentIndex+1));
//// } catch (IOException e) {
//// e.printStackTrace();
//// }
//// currentIndex++;
//// if(currentIndex == size){
//// try {
//// contentWriter.close();
//// } catch (IOException e) {
//// e.printStackTrace();
//// }
//// }
//// }
//// }
//// }
//
// @NoArgsConstructor
// @AllArgsConstructor
// @Data
// private class MatchAndRule{
// private String math;
// private Rule rule;
// }
//}
src/main/java/com/zjty/adaptationmaster/utils/Adaptor.java
0 → 100644
浏览文件 @
c8209a5f
package
com
.
zjty
.
adaptationmaster
.
utils
;
import
com.zjty.adaptationmaster.adaptor.controller.WebSocketServer
;
import
com.zjty.adaptationmaster.adaptor.entity.OriginalFile
;
import
com.zjty.adaptationmaster.adaptor.entity.Project
;
import
com.zjty.adaptationmaster.adaptor.entity.Rule
;
import
com.zjty.adaptationmaster.adaptor.repository.OriginalFileDao
;
import
com.zjty.adaptationmaster.adaptor.repository.ProjectDao
;
import
com.zjty.adaptationmaster.base.enums.Const
;
import
com.zjty.adaptationmaster.base.response.ServerResponse
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.io.*
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.*
;
import
java.nio.file.attribute.BasicFileAttributes
;
import
java.util.*
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.regex.Pattern
;
@Component
public
class
Adaptor
{
//@Autowired
//private AdaptationDetailLogEntityDao adaptationDetailLogEntityDao;
@Autowired
private
OriginalFileDao
originalFileDao
;
private
ProjectDao
projectDao
;
private
String
uuid
;
//@Value("${base.path}")
private
String
basePath
=
Const
.
CONSOLE
;
//线程池数量,合适的线程数量能让程序更快
private
static
final
int
poolSize
=
20
;
//为避免内存溢出,对于超大文件,切分成部分分别读入处理,最后拼接写出
private
static
final
long
LIMIT
=
200
*
1024
*
1024
;
List
<
Rule
>
ruleList
;
Project
project
;
//String projectPath;
//private List<Rule> adaptorEntities = new ArrayList<>();
//public void setProjectPath(String projectPath) {
// this.projectPath = projectPath;
//}
public
void
setProject
(
Project
project
)
{
this
.
project
=
project
;
}
public
void
setRuleList
(
List
<
Rule
>
ruleList
)
{
this
.
ruleList
=
ruleList
;
}
public
void
setUuid
(
String
uuid
)
{
this
.
uuid
=
uuid
;
}
public
ServerResponse
doAdapt
(){
//String regular = preprocesAdaptation();
//List<String> strings1 = preprocessRegular();
List
<
MatchAndRule
>
strings1
=
preprocessRegular
();
System
.
out
.
println
(
strings1
.
size
()+
"规则数量"
);
if
(
strings1
.
size
()<
1
){
System
.
out
.
println
(
"检测到匹配规则为空"
);
return
ServerResponse
.
error
(
"检测到匹配规则为空"
);
}
if
(
project
.
getCodeUrl
()
==
null
||
project
.
getCodeUrl
().
equals
(
""
)){
System
.
out
.
println
(
"未上传源代码"
);
return
ServerResponse
.
error
(
"未上传源代码"
);
}
ThreadPoolExecutor
pool
=
new
ThreadPoolExecutor
(
poolSize
,
poolSize
,
10
,
TimeUnit
.
SECONDS
,
new
ArrayBlockingQueue
<>(
poolSize
*
2
));
pool
.
setRejectedExecutionHandler
(
new
ThreadPoolExecutor
.
CallerRunsPolicy
());
List
<
OriginalFile
>
originalFiles
=
new
ArrayList
<>();
//记录实际具体适配内容
//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
(
MatchAndRule
regular:
strings1
)
{
//if (FileSystems.getDefault().getPathMatcher("glob:"+regular.getMath()).matches(file)) {
//直接用java正则
String
s
=
regular
.
getMath
().
replaceAll
(
"\\?"
,
"."
).
replaceAll
(
"\\*"
,
".+"
);
if
(
Pattern
.
matches
(
s
,
file
.
getFileName
().
toString
()))
{
if
(
thisFileMatched
==
null
)
thisFileMatched
=
new
ArrayList
<>();
thisFileMatched
.
add
(
regular
.
getRule
());
match
=
true
;
}
}
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
());
//System.out.println();
if
(
attrs
.
size
()
>
LIMIT
)
{
System
.
out
.
println
(
"找到大文件"
+
file
);
ReadedLinesRepository
linesRepository
=
new
ReadedLinesRepository
();
int
i
=
0
;
WriterBySort
bySort
=
new
WriterBySort
();
File
newFile
=
file
.
toFile
();
newFile
.
createNewFile
();
OutputStreamWriter
writer
=
new
OutputStreamWriter
(
new
FileOutputStream
(
newFile
),
StandardCharsets
.
UTF_8
);
bySort
.
setWriter
(
writer
);
for
(
String
s
:
Files
.
readAllLines
(
originalPath
,
Charset
.
forName
(
"utf-8"
)))
{
if
(
linesRepository
.
put
(
s
))
{
List
<
ReadedFileTask
.
ReadedFile
>
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
new
ReadedFileTask
.
ReadedFile
(
linesRepository
.
getReadedFiles
(),
attrs
,
file
,
thisFileMatched
));
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
readedFiles
,
project
.
getProjectName
(),
uuid
);
apacheTask
.
setBySort
(
bySort
);
apacheTask
.
setIndex
(
i
);
pool
.
submit
(
apacheTask
);
i
++;
}
}
List
<
ReadedFileTask
.
ReadedFile
>
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
new
ReadedFileTask
.
ReadedFile
(
linesRepository
.
getReadedFiles
(),
attrs
,
file
,
thisFileMatched
));
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
readedFiles
,
project
.
getProjectName
(),
uuid
);
apacheTask
.
setBySort
(
bySort
);
apacheTask
.
setIndex
(
i
);
pool
.
submit
(
apacheTask
);
i
++;
bySort
.
setSize
(
i
);
}
else
{
String
s
=
new
String
(
Files
.
readAllBytes
(
originalPath
));
ReadedFileTask
.
ReadedFile
readedFile
=
new
ReadedFileTask
.
ReadedFile
(
s
,
attrs
,
file
,
thisFileMatched
);
if
(
repository
.
put
(
readedFile
))
{
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
repository
.
getReadedFiles
(),
project
.
getProjectName
(),
uuid
);
pool
.
submit
(
apacheTask
);
}
}
}
/*else {
Files.copy(file, Paths.get(newPath));
}*/
return
FileVisitResult
.
CONTINUE
;
}
});
System
.
out
.
println
(
"遍历结束"
);
ReadedFileTask
apacheTask
=
new
ReadedFileTask
(
repository
.
getReadedFiles
(),
project
.
getProjectName
(),
uuid
);
pool
.
submit
(
apacheTask
);
pool
.
shutdown
();
try
{
pool
.
awaitTermination
(
1
,
TimeUnit
.
HOURS
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
//originalFileDao.saveAll(originalFiles);
//adaptationDetailLogEntityDao.saveAll(detailsLogEntities);
Project
one
=
projectDao
.
getOne
(
project
.
getId
());
one
.
setStage
(
1
);
return
ServerResponse
.
success
(
"替换成功"
,
one
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
ServerResponse
.
error
(
"替换失败"
);
}
}
/**
* 预处理适配规则,将规则实体类处理成glob表达式
* @return
*/
private
List
<
MatchAndRule
>
preprocessRegular
()
{
//List<String> result = new ArrayList<>();
List
<
MatchAndRule
>
result
=
new
ArrayList
<>();
for
(
Rule
adaptorEntity
:
ruleList
)
{
System
.
out
.
println
(
adaptorEntity
.
getPath
());
adaptorEntity
.
setPath
(
adaptorEntity
.
getPath
().
replaceAll
(
"\\?"
,
"."
).
replaceAll
(
"\\*"
,
".+"
));
// System.out.println(adaptorEntity.getTarget()+"11");
// System.out.println(adaptorEntity.getPathMatchType());
// System.out.println(adaptorEntity);
// switch (adaptorEntity.getPathMatchType()) {
// case SUFFIX:
// result.add(new MatchAndRule("**/**" + adaptorEntity.getPath(),adaptorEntity));
// break;
// case PATH:
// result.add(new MatchAndRule("" + adaptorEntity.getPath(),adaptorEntity));
// break;
// case NAME:
// result.add(new MatchAndRule("**/" + adaptorEntity.getPath(),adaptorEntity));
// break;
// case GLOB:
// result.add(new MatchAndRule(adaptorEntity.getPath(),adaptorEntity));
// }
result
.
add
(
new
MatchAndRule
(
adaptorEntity
.
getPath
(),
adaptorEntity
));
}
return
result
;
}
/**
* 对于大文件,按行读取,积攒到一定大小去处理
* 这里put进来每行
* 大小不够时返回false
* 大小够时返回true
*/
private
class
ReadedLinesRepository
{
private
static
final
long
LIMIT
=
200
*
1024
*
1024
;
//字节*k*m
private
List
<
StringBuilder
>
readedFiles
;
private
long
count
;
public
String
getReadedFiles
()
{
return
readedFiles
.
toString
();
}
public
boolean
put
(
String
readedFile
){
if
(
readedFiles
==
null
)
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
new
StringBuilder
(
readedFile
));
count
+=
readedFile
.
length
();
return
count
>
LIMIT
;
}
}
/**
* 读取到的大量小文件,积累到一定的大小,才交给一个线程去处理
*/
private
class
ReadedFileRepository
{
private
List
<
ReadedFileTask
.
ReadedFile
>
readedFiles
;
private
long
count
;
public
List
<
ReadedFileTask
.
ReadedFile
>
getReadedFiles
()
{
return
readedFiles
;
}
public
boolean
put
(
ReadedFileTask
.
ReadedFile
readedFile
){
if
(
readedFiles
==
null
)
readedFiles
=
new
ArrayList
<>();
readedFiles
.
add
(
readedFile
);
count
+=
readedFile
.
getAttributes
().
size
();
return
count
>
LIMIT
;
}
}
/**
* 过大的文件,切割成几块,每块编号,分给每个线程处理,处理结束后,按编号顺序写出
* 这里按编号控制写出顺序
*/
// public class WriterBySort{
// private int currentIndex = -1;
// private Map<Integer,String> map = new TreeMap<>();
// private Writer contentWriter;
// private int size;
//
// public void setWriter(Writer writer) {
// this.contentWriter = writer;
// }
// public void setSize(int size) {
// this.size = size;
// if(currentIndex==size){
// try {
// contentWriter.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// public void insert(Integer key, String value){
// map.put(key,value);
// while (map.get(currentIndex+1)!=null){
// try {
// contentWriter.write(map.remove(currentIndex+1));
// } catch (IOException e) {
// e.printStackTrace();
// }
// currentIndex++;
// if(currentIndex == size){
// try {
// contentWriter.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// }
// }
@NoArgsConstructor
@AllArgsConstructor
@Data
private
class
MatchAndRule
{
private
String
math
;
private
Rule
rule
;
}
}
src/main/java/com/zjty/adaptationmaster/utils/Inspector.java
浏览文件 @
c8209a5f
...
...
@@ -63,6 +63,7 @@ public class Inspector {
this
.
report
=
new
Report
();
}
int
fileNum
=
0
;
/**
* FileVisitResult.CONTINUE 继续遍历
* FileVisitResult.TERMINATE 中止访问
...
...
@@ -100,6 +101,7 @@ public class Inspector {
@Override
public
FileVisitResult
visitFile
(
Path
file
,
BasicFileAttributes
attrs
)
throws
IOException
{
fileNum
++;
for
(
Map
.
Entry
<
String
,
PathMatcher
>
entry:
languageSuffixMatcherMapping
.
entrySet
()){
if
(
entry
.
getValue
().
matches
(
file
)){
languageMatchMap
.
get
(
entry
.
getKey
()).
plus
();
...
...
@@ -131,6 +133,7 @@ public class Inspector {
}
public
Report
analysis
(){
report
.
setFileNum
(
fileNum
);
report
.
setLanguage
(
Report
.
Language
.
ONLYVIEW
);
for
(
Map
.
Entry
<
String
,
Counter
>
entry:
languageMatchMap
.
entrySet
()){
if
(
entry
.
getValue
().
getNumber
()>
0
){
...
...
src/main/java/com/zjty/adaptationmaster/utils/ReadedFileTask.java
浏览文件 @
c8209a5f
...
...
@@ -2,7 +2,6 @@ package com.zjty.adaptationmaster.utils;
import
com.zjty.adaptationmaster.adaptor.controller.WebSocketServer
;
import
com.zjty.adaptationmaster.adaptor.entity.Rule
;
import
com.zjty.adaptationmaster.adaptor.service.Impl.Adaptor
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
...
...
@@ -11,6 +10,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,15 +20,20 @@ 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
;
}
private
Adaptor
.
WriterBySort
bySort
;
private
WriterBySort
bySort
;
private
int
index
;
public
void
setBySort
(
Adaptor
.
WriterBySort
bySort
)
{
public
void
setBySort
(
WriterBySort
bySort
)
{
System
.
out
.
println
(
"线程遇到大文件"
);
this
.
bySort
=
bySort
;
}
...
...
@@ -38,42 +43,51 @@ public class ReadedFileTask implements Runnable {
@Override
public
void
run
()
{
for
(
ReadedFile
readedFile:
readedFiles
){
String
content
=
readedFile
.
getContent
();
System
.
out
.
println
(
"规则数量"
+
readedFile
.
getThisMatchedRule
().
size
());
for
(
Rule
entity:
readedFile
.
getThisMatchedRule
()){
System
.
out
.
println
(
"文本匹配方式"
+
entity
.
getTextMatching
());
switch
(
entity
.
getTextMatching
()){
case
AREA:
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
(
"进来线程"
);
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
())
{
int
i
=
content
.
indexOf
(
matching
);
if
(
i
>-
1
){
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(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
();
}
}
//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());
continue
;
}
String
content
=
readedFile
.
getContent
();
System
.
out
.
println
(
"规则数量"
+
readedFile
.
getThisMatchedRule
().
size
());
for
(
Rule
entity
:
readedFile
.
getThisMatchedRule
())
{
//在改动的地方加日志
// int i = content.indexOf(entity.getTarget());
// int i1 = content.lastIndexOf("\\r\\n", i);
//
content
=
content
.
replaceAll
(
entity
.
getTarget
(),
entity
.
getReplacing
());
}
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 +97,7 @@ public class ReadedFileTask implements Runnable {
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
...
...
src/main/java/com/zjty/adaptationmaster/utils/ReadedFileTask1.java
0 → 100644
浏览文件 @
c8209a5f
//package com.zjty.adaptationmaster.utils;
//
//import com.zjty.adaptationmaster.adaptor.controller.WebSocketServer;
//import com.zjty.adaptationmaster.adaptor.entity.Rule;
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//
//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 {
//
// private String projectName;
// private String uuid;
// 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;
// }
//
// private WriterBySort bySort;
// private int index;
//
// public void setBySort(WriterBySort bySort) {
// System.out.println("线程遇到大文件");
// this.bySort = bySort;
// }
//
// public void setIndex(int index) {
// this.index = index;
// }
//
// @Override
// public void run() {
// 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()) {
// 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);
//
// int i = content.indexOf(matching);
// 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());
// }
// } 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;
// }
//
// break;
// }
// }
// try {
// if (bySort != null) {
// bySort.insert(index, content);
// } else {
// WebSocketServer.sendInfo(uuid, readedFile.getPath().getFileName() + "", "替换", "正在写出", projectName);
// File file = readedFile.getPath().toFile();
// file.createNewFile();
// OutputStreamWriter contentWriter = new OutputStreamWriter(new FileOutputStream(file));
// contentWriter.write(content);
// contentWriter.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// }
// }
//
// @Data
// @NoArgsConstructor
// @AllArgsConstructor
// public static class ReadedFile {
// private String content;
// private BasicFileAttributes attributes;
// private Path path;
// private List<Rule> thisMatchedRule;
// }
//}
src/main/java/com/zjty/adaptationmaster/utils/WriterBySort.java
0 → 100644
浏览文件 @
c8209a5f
package
com
.
zjty
.
adaptationmaster
.
utils
;
import
java.io.IOException
;
import
java.io.Writer
;
import
java.util.Map
;
import
java.util.TreeMap
;
public
class
WriterBySort
{
private
int
currentIndex
=
-
1
;
private
Map
<
Integer
,
String
>
map
=
new
TreeMap
<>();
private
Writer
contentWriter
;
private
int
size
;
public
void
setWriter
(
Writer
writer
)
{
this
.
contentWriter
=
writer
;
}
public
void
setSize
(
int
size
)
{
this
.
size
=
size
;
if
(
currentIndex
==
size
){
try
{
contentWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
public
void
insert
(
Integer
key
,
String
value
){
map
.
put
(
key
,
value
);
while
(
map
.
get
(
currentIndex
+
1
)!=
null
){
try
{
contentWriter
.
write
(
map
.
remove
(
currentIndex
+
1
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
currentIndex
++;
if
(
currentIndex
==
size
){
try
{
contentWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
src/test/java/com/zjty/adaptationmaster/Test1.java
浏览文件 @
c8209a5f
...
...
@@ -9,6 +9,7 @@ import java.nio.file.PathMatcher;
import
java.nio.file.Paths
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.regex.Pattern
;
public
class
Test1
{
@Test
...
...
@@ -684,4 +685,9 @@ public class Test1 {
System
.
out
.
println
(
sb
.
toString
());
}
@Test
public
void
test5
(){
System
.
out
.
println
(
Pattern
.
matches
(
".+22.+"
,
"2223"
));
}
}
src/test/java/com/zjty/adaptationmaster/TestAdapt.java
0 → 100644
浏览文件 @
c8209a5f
//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"));
// }
//}
src/test/java/com/zjty/adaptationmaster/TestProcess.java
0 → 100644
浏览文件 @
c8209a5f
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论