4
4
*/
5
5
package com .alipay .muagent .service .tool .loader .impl ;
6
6
7
+ import com .alipay .muagent .model .enums .tool .ToolDataTypeEnum ;
7
8
import com .alipay .muagent .model .tool .meta .Tool ;
8
9
import com .alipay .muagent .service .mybatisplus .dto .ToolConverter ;
9
10
import com .alipay .muagent .service .mybatisplus .dto .ToolDO ;
10
11
import com .alipay .muagent .service .mybatisplus .mapper .TooDOlMapper ;
11
12
import com .alipay .muagent .service .tool .loader .ToolLoader ;
13
+ import com .alipay .muagent .util .GsonUtils ;
14
+ import com .baomidou .mybatisplus .core .conditions .query .QueryWrapper ;
12
15
import com .google .common .collect .Lists ;
13
16
import org .springframework .beans .factory .annotation .Autowired ;
17
+ import org .springframework .beans .factory .annotation .Value ;
18
+ import org .springframework .core .io .Resource ;
14
19
import org .springframework .core .io .ResourceLoader ;
15
20
import org .springframework .stereotype .Service ;
16
21
import org .springframework .util .CollectionUtils ;
22
+ import java .io .BufferedInputStream ;
17
23
import java .util .List ;
18
24
19
25
/**
@@ -26,32 +32,94 @@ public class LocalToolLoader implements ToolLoader {
26
32
@ Autowired
27
33
private ResourceLoader resourceLoader ;
28
34
35
+ @ Value ("${runtime.tool.datatype:local}" )
36
+ private String toolType ;
37
+
29
38
@ Autowired
30
39
private TooDOlMapper tooDOlMapper ;
31
40
41
+ /**
42
+ * Query tool by id tool.
43
+ *
44
+ * @param id the id
45
+ * @return the tool
46
+ */
32
47
@ Override
33
- public Tool queryToolById (String id ) {
34
- return queryToolByKey (id );
48
+ 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
+ };
35
54
}
36
55
56
+ /**
57
+ * Query tools by id list list.
58
+ *
59
+ * @param ids the ids
60
+ * @return the list
61
+ */
37
62
@ Override
38
- public List <Tool > queryToolsByIdList (List <String > ids ) {
63
+ public List <Tool > queryToolsByIdList (List <Long > ids ) {
39
64
if (CollectionUtils .isEmpty (ids )) {
40
65
return Lists .newArrayList ();
41
66
}
42
67
return ids .stream ().map (this ::queryToolById ).toList ();
43
68
}
44
69
70
+ /**
71
+ * Query tool by key tool.
72
+ *
73
+ * @param id the id
74
+ * @return the tool
75
+ */
45
76
@ Override
46
77
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
99
try {
48
- ToolDO toolDO = tooDOlMapper .selectById (id );
49
- return new ToolConverter ().convertFromDto (toolDO );
100
+ Resource resource = resourceLoader .getResource ("classpath:" + fileName );
101
+ BufferedInputStream bufferedReader = new BufferedInputStream (resource .getInputStream ());
102
+ byte [] buffer = new byte [1024 ]; // 设置缓冲区大小
103
+ int bytesRead = 0 ;
104
+ StringBuffer sBuffer = new StringBuffer ();
105
+ while ((bytesRead = bufferedReader .read (buffer )) != -1 ) {
106
+ sBuffer .append (new String (buffer , 0 , bytesRead ));
107
+ }
108
+ bufferedReader .close ();
109
+
110
+ Tool tool = GsonUtils .fromString (Tool .class , sBuffer .toString ());
111
+ return tool ;
50
112
} catch (Exception e ) {
51
- throw new RuntimeException (String .format ("loadToolFailed:%s" , id ), e );
113
+ throw new RuntimeException (String .format ("loadToolFailed:%s" , key ), e );
52
114
}
53
115
}
54
116
117
+ /**
118
+ * Query tools by key list list.
119
+ *
120
+ * @param keys the keys
121
+ * @return the list
122
+ */
55
123
@ Override
56
124
public List <Tool > queryToolsByKeyList (List <String > keys ) {
57
125
if (CollectionUtils .isEmpty (keys )) {
0 commit comments