#
Junjie
3 天以前 1b8a4677f362d234d834120deac4880d7ae89a50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.zy.ai.config;
 
import com.zy.ai.service.AiMcpMountService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
 
@Slf4j
@Component
public class AiMcpMountInitializer {
 
    private final DataSource dataSource;
    private final AiMcpMountService aiMcpMountService;
 
    public AiMcpMountInitializer(DataSource dataSource, AiMcpMountService aiMcpMountService) {
        this.dataSource = dataSource;
        this.aiMcpMountService = aiMcpMountService;
    }
 
    @PostConstruct
    public void init() {
        try (Connection connection = dataSource.getConnection()) {
            if (!hasTable(connection, "sys_ai_mcp_mount")) {
                log.warn("Skip AI MCP mount initialization because mount table does not exist");
                return;
            }
            int changed = aiMcpMountService.initDefaultsIfMissing();
            log.info("AI MCP mounts initialized, insertedOrRecovered={}", changed);
        } catch (Exception e) {
            log.warn("Failed to initialize AI MCP mounts", e);
        }
    }
 
    private boolean hasTable(Connection connection, String tableName) throws Exception {
        DatabaseMetaData metaData = connection.getMetaData();
        try (ResultSet resultSet = metaData.getTables(connection.getCatalog(), null, tableName, new String[]{"TABLE"})) {
            while (resultSet.next()) {
                if (tableName.equalsIgnoreCase(resultSet.getString("TABLE_NAME"))) {
                    return true;
                }
            }
        }
        return false;
    }
}