P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package foundation.persist.source;
 
import java.sql.SQLException;
import java.util.Iterator;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.server.config.DBaseType;
import foundation.util.MapList;
 
 
public class DataSourceManager implements Iterable<NamedDataSource> {
 
    private static Logger logger;
    private static DataSourceManager instance;
    private static MapList<String, NamedDataSource> dataSourceList;
    private static NamedDataSource main;
    
    static {
        logger = LogManager.getLogger(DataSourceManager.class);
        dataSourceList = new MapList<String, NamedDataSource>();
    }
 
    public static synchronized DataSourceManager getInstance() {
        if (instance == null) {
            instance = new DataSourceManager();
        }
        
        return instance;
    }
    
    public static void appendDataSource(NamedDataSource dataSource) throws SQLException {
        String name = dataSource.getName();
        
        if (name == null) {
            return;
        }
        
        dataSourceList.add(name, dataSource);
        
        if (main == null) {
            main = dataSource;
            DBaseType.setMain(dataSource.getDBaseType());
        }
    }
    
    public static ConnectionAgent getConnection() throws SQLException {
        return new ConnectionAgent(main.getName(), main.getConnection(), main.getDBaseType());
    }
    
    public static ConnectionAgent getConnection(String name) {
        try {
            NamedDataSource dataSource = dataSourceList.get(name);
            
            if (dataSource == null) {
                return null;
            }
            
            return dataSource.getConnectionAgent();
        } 
        catch (SQLException e) {
            logger.error(e);
            return null;
        }
    }
 
    public static NamedDataSource getDataSource(String name) {
        return dataSourceList.get(name);
    }
 
    public static void setMain(NamedDataSource dataSource) {
        main = dataSource;
        DBaseType.setMain(dataSource.getDBaseType());
    }
    
    public static void setMain(String name) {
        main = dataSourceList.get(name);
        DBaseType.setMain(main.getDBaseType());
    }
 
    public static NamedDataSource getMain() {
        return main;
    }
 
    @Override
    public Iterator<NamedDataSource> iterator() {
        return dataSourceList.iterator();
    }
 
    public static DBaseType getDBaseType() {
        return main.getDBaseType();
    }
 
}