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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package foundation.data.loader;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import foundation.persist.source.DataSourceManager;
import foundation.persist.source.NamedDataSource;
import foundation.server.Initializer;
import foundation.server.config.Configer;
import foundation.server.config.ServerAssembleLoader;
 
public class DatasuorceLoader extends Initializer {
 
    private static final String Node_Defination = "defination";
    private static final String Node_Defination_URL = "url";
    private static final String Node_Defination_UserName = "username";
    private static final String Node_Defination_Password = "password";
    private Set<String> activeDataSourceList;
    
    public DatasuorceLoader() {
        activeDataSourceList = ServerAssembleLoader.activeDataSourceList;
    }
    
    @Override
    public void startUp() throws Exception {
        File path = new File(Configer.getPath_Config());
        File[] files = path.listFiles();
        
        for (File file: files) {
            String name = getShortName(file);
            
            if (!activeDataSourceList.contains(name)) {
                continue;
            };
            
            loadOneFile(name, file);
        }
    }
 
    @Override
    public void shutDown() throws Exception {
        
    }
    
    private void loadOneFile(String name, File file) {
        try {
            logger.debug("load datasource file:" + file);
            InputStream inputStream = new FileInputStream(file);
            
            try {
                SAXReader reader = new SAXReader();
                reader.setValidation(false);
                    
                Document doc = reader.read(inputStream);
                Element root = doc.getRootElement();
                    
                loadDefination(name, root);
                    
            } catch (DocumentException e) {
                logger.error("can not load sql file: " + file);
                logger.error(e);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        catch (Exception e) {
            logger.error(e);
        }        
    }
    
    private void loadDefination(String name, Element root) throws Exception {
        Element element = root.element(Node_Defination);
            
        String url = element.attributeValue(Node_Defination_URL);
        String username = element.attributeValue(Node_Defination_UserName);
        String password = element.attributeValue(Node_Defination_Password);
        
        NamedDataSource dataSource = new NamedDataSource(name);
        
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        
        DataSourceManager.appendDataSource(dataSource);
    }
    
    private String getShortName(File file) {
        String result = file.getName();
        
        int pos = result.lastIndexOf(".");
        if (pos > 0) {
            result = result.substring(0, pos);
        }
        
        return result;
    }
 
}