Skip to content

Commit a410df6

Browse files
author
尘绝
committed
version3
mysqlToolLoader & init by local
1 parent f65e5aa commit a410df6

File tree

4 files changed

+158
-136
lines changed

4 files changed

+158
-136
lines changed

runtime/model/src/main/java/com/alipay/muagent/model/enums/tool/ToolDataTypeEnum.java

-72
This file was deleted.

runtime/service/src/main/java/com/alipay/muagent/service/mybatisplus/mapper/TooDOlMapper.java renamed to runtime/service/src/main/java/com/alipay/muagent/service/mybatisplus/mapper/ToolDoMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
* @author chenjue.wwp
1212
* @version $Id: ToolMapper.java, v 0.1 2024-11-21 20:53 Exp $$
1313
*/
14-
public interface TooDOlMapper extends BaseMapper<ToolDO> {
14+
public interface ToolDoMapper extends BaseMapper<ToolDO> {
1515
}

runtime/service/src/main/java/com/alipay/muagent/service/tool/loader/impl/LocalToolLoader.java

+6-63
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
*/
55
package com.alipay.muagent.service.tool.loader.impl;
66

7-
import com.alipay.muagent.model.enums.tool.ToolDataTypeEnum;
87
import com.alipay.muagent.model.tool.meta.Tool;
9-
import com.alipay.muagent.service.mybatisplus.dto.ToolConverter;
10-
import com.alipay.muagent.service.mybatisplus.dto.ToolDO;
11-
import com.alipay.muagent.service.mybatisplus.mapper.TooDOlMapper;
128
import com.alipay.muagent.service.tool.loader.ToolLoader;
139
import com.alipay.muagent.util.GsonUtils;
14-
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
1510
import com.google.common.collect.Lists;
1611
import org.springframework.beans.factory.annotation.Autowired;
17-
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1813
import org.springframework.core.io.Resource;
1914
import org.springframework.core.io.ResourceLoader;
2015
import org.springframework.stereotype.Service;
2116
import org.springframework.util.CollectionUtils;
17+
2218
import java.io.BufferedInputStream;
2319
import java.util.List;
2420

@@ -27,38 +23,17 @@
2723
* @version : LocalToolLoader.java, v 0.1 2024年10月11日 下午7:23 renmao.rm Exp $
2824
*/
2925
@Service
26+
@ConditionalOnProperty(name = "runtime.tool.datatype", havingValue = "local")
3027
public class LocalToolLoader implements ToolLoader {
3128

3229
@Autowired
3330
private ResourceLoader resourceLoader;
3431

35-
@Value("${runtime.tool.datatype:local}")
36-
private String toolType;
37-
38-
@Autowired
39-
private TooDOlMapper tooDOlMapper;
40-
41-
/**
42-
* Query tool by id tool.
43-
*
44-
* @param id the id
45-
* @return the tool
46-
*/
4732
@Override
4833
public Tool queryToolById(Long id) {
49-
ToolDataTypeEnum toolDataTypeEnum = ToolDataTypeEnum.getByName(toolType);
50-
return switch (toolDataTypeEnum) {
51-
case LOCAL -> queryLocalToolByKey(String.valueOf(id));
52-
case MYSQL -> queryMysqlToolById(id);
53-
};
34+
return queryToolByKey(String.valueOf(id));
5435
}
5536

56-
/**
57-
* Query tools by id list list.
58-
*
59-
* @param ids the ids
60-
* @return the list
61-
*/
6237
@Override
6338
public List<Tool> queryToolsByIdList(List<Long> ids) {
6439
if (CollectionUtils.isEmpty(ids)) {
@@ -67,35 +42,9 @@ public List<Tool> queryToolsByIdList(List<Long> ids) {
6742
return ids.stream().map(this::queryToolById).toList();
6843
}
6944

70-
/**
71-
* Query tool by key tool.
72-
*
73-
* @param id the id
74-
* @return the tool
75-
*/
7645
@Override
7746
public Tool queryToolByKey(String id) {
78-
ToolDataTypeEnum toolDataTypeEnum = ToolDataTypeEnum.getByName(toolType);
79-
return switch (toolDataTypeEnum) {
80-
case LOCAL -> queryLocalToolByKey(id);
81-
case MYSQL -> queryMysqlToolByKey(id);
82-
};
83-
}
84-
85-
private Tool queryMysqlToolById(Long id) {
86-
ToolDO toolDO = tooDOlMapper.selectById(id);
87-
return new ToolConverter().convertFromDto(toolDO);
88-
}
89-
90-
private Tool queryMysqlToolByKey(String key) {
91-
QueryWrapper<ToolDO> queryWrapper = new QueryWrapper<>();
92-
queryWrapper.eq("tool_key", key);
93-
ToolDO toolDO = tooDOlMapper.selectOne(queryWrapper);
94-
return new ToolConverter().convertFromDto(toolDO);
95-
}
96-
97-
private Tool queryLocalToolByKey(String key) {
98-
String fileName = String.format("tools/%s.json", key);
47+
String fileName = String.format("tools/%s.json", id);
9948
try {
10049
Resource resource = resourceLoader.getResource("classpath:" + fileName);
10150
BufferedInputStream bufferedReader = new BufferedInputStream(resource.getInputStream());
@@ -110,16 +59,10 @@ private Tool queryLocalToolByKey(String key) {
11059
Tool tool = GsonUtils.fromString(Tool.class, sBuffer.toString());
11160
return tool;
11261
} catch (Exception e) {
113-
throw new RuntimeException(String.format("loadToolFailed:%s", key), e);
62+
throw new RuntimeException(String.format("loadToolFailed:%s", id), e);
11463
}
11564
}
11665

117-
/**
118-
* Query tools by key list list.
119-
*
120-
* @param keys the keys
121-
* @return the list
122-
*/
12366
@Override
12467
public List<Tool> queryToolsByKeyList(List<String> keys) {
12568
if (CollectionUtils.isEmpty(keys)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* Alipay.com Inc.
3+
* Copyright (c) 2004-2024 All Rights Reserved.
4+
*/
5+
package com.alipay.muagent.service.tool.loader.impl;
6+
7+
import java.io.BufferedInputStream;
8+
import java.io.IOException;
9+
import java.util.List;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
14+
import org.springframework.core.io.Resource;
15+
import org.springframework.core.io.ResourceLoader;
16+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
17+
import org.springframework.dao.DataIntegrityViolationException;
18+
import org.springframework.stereotype.Service;
19+
import org.springframework.util.CollectionUtils;
20+
import com.alipay.muagent.model.tool.meta.Tool;
21+
import com.alipay.muagent.service.mybatisplus.dto.ToolConverter;
22+
import com.alipay.muagent.service.mybatisplus.dto.ToolDO;
23+
import com.alipay.muagent.service.mybatisplus.mapper.ToolDoMapper;
24+
import com.alipay.muagent.service.tool.loader.ToolLoader;
25+
import com.alipay.muagent.util.GsonUtils;
26+
import com.alipay.muagent.util.LoggerUtil;
27+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
28+
import com.google.common.collect.Lists;
29+
30+
import jakarta.annotation.PostConstruct;
31+
32+
/**
33+
* @author chenjue.wwp
34+
* @version : LocalToolLoader.java, v 0.1 2024年12月10日 下午7:23 chenjue.wwp Exp $
35+
*/
36+
@Service
37+
@ConditionalOnProperty(name = "runtime.tool.datatype", havingValue = "mysql")
38+
public class MysqlToolLoader implements ToolLoader {
39+
40+
private static final Logger LOGGER = LoggerFactory.getLogger(MysqlToolLoader.class);
41+
42+
@Autowired
43+
private ResourceLoader resourceLoader;
44+
45+
46+
@Autowired
47+
private ToolDoMapper toolDoMapper;
48+
49+
/**
50+
* Query tool by id tool.
51+
*
52+
* @param id the id
53+
* @return the tool
54+
*/
55+
@Override
56+
public Tool queryToolById(Long id) {
57+
return queryMysqlToolById(id);
58+
}
59+
60+
/**
61+
* Query tools by id list list.
62+
*
63+
* @param ids the ids
64+
* @return the list
65+
*/
66+
@Override
67+
public List<Tool> queryToolsByIdList(List<Long> ids) {
68+
if (CollectionUtils.isEmpty(ids)) {
69+
return Lists.newArrayList();
70+
}
71+
return ids.stream().map(this::queryToolById).toList();
72+
}
73+
74+
/**
75+
* Query tool by key tool.
76+
*
77+
* @param id the id
78+
* @return the tool
79+
*/
80+
@Override
81+
public Tool queryToolByKey(String id) {
82+
return queryMysqlToolByKey(id);
83+
}
84+
85+
private Tool queryMysqlToolById(Long id) {
86+
ToolDO toolDO = toolDoMapper.selectById(id);
87+
return new ToolConverter().convertFromDto(toolDO);
88+
}
89+
90+
private Tool queryMysqlToolByKey(String key) {
91+
QueryWrapper<ToolDO> queryWrapper = new QueryWrapper<>();
92+
queryWrapper.eq("tool_key", key);
93+
ToolDO toolDO = toolDoMapper.selectOne(queryWrapper);
94+
return new ToolConverter().convertFromDto(toolDO);
95+
}
96+
97+
/**
98+
* Query tools by key list list.
99+
*
100+
* @param keys the keys
101+
* @return the list
102+
*/
103+
@Override
104+
public List<Tool> queryToolsByKeyList(List<String> keys) {
105+
if (CollectionUtils.isEmpty(keys)) {
106+
return Lists.newArrayList();
107+
}
108+
return keys.stream().map(this::queryToolByKey).toList();
109+
}
110+
111+
112+
@PostConstruct
113+
public void initTools() {
114+
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
115+
try {
116+
// 注意:这里的路径以斜杠开头,并且需要包含双星号 ** 来匹配子目录
117+
Resource[] resources = resolver.getResources("classpath:/tools/**");
118+
for (Resource resource : resources) {
119+
if (resource.isReadable()) { // 确保资源可读
120+
String filename = resource.getFilename();
121+
insertToMysql(filename);
122+
}
123+
}
124+
} catch (IOException e) {
125+
e.printStackTrace();
126+
}
127+
}
128+
129+
private void insertToMysql(String fileName) throws IOException {
130+
StringBuilder sBuffer;
131+
Resource resource = resourceLoader.getResource("classpath:tools/" + fileName);
132+
BufferedInputStream bufferedReader = new BufferedInputStream(resource.getInputStream());
133+
byte[] buffer = new byte[1024];
134+
int bytesRead = 0;
135+
sBuffer = new StringBuilder();
136+
while ((bytesRead = bufferedReader.read(buffer)) != -1) {
137+
sBuffer.append(new String(buffer, 0, bytesRead));
138+
}
139+
bufferedReader.close();
140+
141+
Tool tool = GsonUtils.fromString(Tool.class, sBuffer.toString());
142+
ToolDO toolDO = new ToolConverter().convertFromEntity(tool);
143+
toolDO.setId(null);
144+
try {
145+
toolDoMapper.insert(toolDO);
146+
} catch (DataIntegrityViolationException e) {
147+
LoggerUtil.info(LOGGER, "insert tool failed, tool key: {}", toolDO.getToolKey());
148+
}
149+
150+
}
151+
}

0 commit comments

Comments
 (0)