Project

General

Profile

Download (4.03 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
/**
31
 * @author a.kohlbecker
32
 * @date 30.03.2010
33
 */
34
public class DataSourcePropertyParser {
35

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

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

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

    
54

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

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

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

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

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

    
95
    }
96

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