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