Project

General

Profile

Download (4.06 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
 */
35
public class DataSourcePropertyParser {
36

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

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

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

    
55

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

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

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

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

    
77
                if(idSet.add(conf.getInstanceName())) {
78
                    logger.debug("adding instanceName '"+ conf.getInstanceName() + "'");
79
                    configList.add(conf);
80
                } else {
81
                    logger.error("instance with name '"+ conf.getInstanceName() + "' alreaddy exists");
82
                }
83

    
84
            }
85

    
86
        } catch (SAXException e) {
87
            // TODO Auto-generated catch block
88
            e.printStackTrace();
89
        } catch (IOException e) {
90
            // TODO Auto-generated catch block
91
            e.printStackTrace();
92
        } catch (ParserConfigurationException e) {
93
            // TODO Auto-generated catch block
94
            e.printStackTrace();
95
        }
96
        return configList;
97

    
98
    }
99

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

    
112

    
113

    
114
}
(3-3/7)