Project

General

Profile

Download (4.02 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.server.instance;
10

    
11
import java.io.File;
12
import java.io.IOException;
13
import java.util.ArrayList;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import javax.xml.parsers.DocumentBuilder;
19
import javax.xml.parsers.DocumentBuilderFactory;
20
import javax.xml.parsers.ParserConfigurationException;
21

    
22
import org.apache.log4j.Logger;
23
import org.w3c.dom.Document;
24
import org.w3c.dom.NamedNodeMap;
25
import org.w3c.dom.Node;
26
import org.w3c.dom.NodeList;
27
import org.xml.sax.SAXException;
28

    
29
/**
30
 * @author a.kohlbecker
31
 * @date 30.03.2010
32
 */
33
public class DataSourcePropertyParser {
34

    
35
    public static final Logger logger = Logger.getLogger(DataSourcePropertyParser.class);
36

    
37
    public static List<Configuration> parseDataSourceConfigs(File datasourcesFile){
38

    
39
        logger.info("loading bean definition file: " + datasourcesFile.getAbsolutePath());
40
        List<Configuration> configList = new ArrayList<>();
41
        Set<String> idSet = new HashSet<>();
42
        try {
43
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
44
            Document doc = builder.parse(datasourcesFile);
45
            NodeList beanNodes  = doc.getElementsByTagName("bean");
46
            for(int i=0; i < beanNodes.getLength(); i++){
47
                Configuration conf = new Configuration();
48
                Node beanNode = beanNodes.item(i);
49
                // ATTRIBUTE_DATASOURCE_NAME
50
                NamedNodeMap namedNodeMap = beanNode.getAttributes();
51
                String beanId = namedNodeMap.getNamedItem("id").getNodeValue();
52

    
53

    
54
                conf.setInstanceName(beanId);
55
                // ATTRIBUTE_DATASOURCE_DRIVERCLASS
56
                String driverClass = getXMLNodeProperty(beanNode, "driverClass");
57

    
58
                if(driverClass == null || driverClass.isEmpty()){
59
                    // not a data source bean
60
                    continue;
61
                }
62

    
63
                conf.setDriverClass(driverClass);
64
                conf.setUsername(getXMLNodeProperty(beanNode, "username"));
65
                if(conf.getUsername() == null){
66
                    conf.setUsername(getXMLNodeProperty(beanNode, "user"));
67
                }
68
                conf.setPassword(getXMLNodeProperty(beanNode, "password"));
69

    
70
                conf.setDataSourceUrl(getXMLNodeProperty(beanNode, "url"));
71
                if(conf.getDataSourceUrl() == null){
72
                    conf.setDataSourceUrl(getXMLNodeProperty(beanNode, "jdbcUrl"));
73
                }
74

    
75
                if(idSet.add(conf.getInstanceName())) {
76
                    logger.debug("adding instanceName '"+ conf.getInstanceName() + "'");
77
                    configList.add(conf);
78
                } else {
79
                    logger.error("instance with name '"+ conf.getInstanceName() + "' alreaddy exists");
80
                }
81
            }
82
        } catch (SAXException e) {
83
            // TODO Auto-generated catch block
84
            e.printStackTrace();
85
        } catch (IOException e) {
86
            // TODO Auto-generated catch block
87
            e.printStackTrace();
88
        } catch (ParserConfigurationException e) {
89
            // TODO Auto-generated catch block
90
            e.printStackTrace();
91
        }
92
        return configList;
93

    
94
    }
95

    
96
    private static String getXMLNodeProperty(Node beanNode, String name){
97
        NodeList children = beanNode.getChildNodes();
98
        for(int i=0; i < children.getLength(); i++){
99
            Node p = children.item(i);
100
            if(p.getNodeName().equals("property")
101
                    && p.getAttributes().getNamedItem("name").getNodeValue().equals(name)){
102
                return p.getAttributes().getNamedItem("value").getNodeValue();
103
            }
104
        }
105
        return null;
106
    }
107
}
(3-3/7)