Project

General

Profile

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

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

    
20
import javax.xml.parsers.DocumentBuilder;
21
import javax.xml.parsers.DocumentBuilderFactory;
22
import javax.xml.parsers.ParserConfigurationException;
23

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

    
31
/**
32
 * @author a.kohlbecker
33
 * @date 30.03.2010
34
 *
35
 */
36
public class DataSourcePropertyParser {
37

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

    
40
    public static List<CdmInstanceProperties> parseDataSourceConfigs(File datasourcesFile){
41

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

    
56

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

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

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

    
73
                conf.setUrl(getXMLNodeProperty(beanNode, "url"));
74
                if(conf.getUrl() == null){
75
                    conf.setUrl(getXMLNodeProperty(beanNode, "jdbcUrl"));
76
                }
77

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

    
85
            }
86

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

    
99
    }
100

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

    
113

    
114

    
115
}
(4-4/6)