Project

General

Profile

Download (3.09 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.HashSet;
15
import java.util.Set;
16

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

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

    
28
/**
29
 * @author a.kohlbecker
30
 * @date 30.03.2010
31
 *
32
 */
33
public class DataSourcePropertyParser {
34
	
35
	public static final Logger logger = Logger.getLogger(DataSourcePropertyParser.class);
36
	
37
	public static Set<CdmInstanceProperties> parseDataSourceConfigs(File datasourcesFile){
38

    
39
		logger.info("loading bean definition file: " + datasourcesFile.getAbsolutePath());
40
		Set<CdmInstanceProperties> configSet = new HashSet<CdmInstanceProperties>();
41
    	try {
42
    		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
43
			Document doc = builder.parse(datasourcesFile);
44
			NodeList beanNodes  = doc.getElementsByTagName("bean");
45
			for(int i=0; i < beanNodes.getLength(); i++){
46
				CdmInstanceProperties conf = new CdmInstanceProperties();
47
				Node beanNode = beanNodes.item(i);
48
				// ATTRIBUTE_DATASOURCE_NAME
49
				NamedNodeMap namedNodeMap = beanNode.getAttributes();
50
				conf.setDataSourceName(namedNodeMap.getNamedItem("id").getNodeValue());
51
				// ATTRIBUTE_DATASOURCE_DRIVERCLASS
52
				conf.setDriverClass(getXMLNodeProperty(beanNode, "driverClass"));
53
				conf.setUsername(getXMLNodeProperty(beanNode, "username"));
54
				if(conf.getUsername() == null){
55
					conf.setUsername(getXMLNodeProperty(beanNode, "user"));
56
				}
57
				conf.setPassword(getXMLNodeProperty(beanNode, "password"));
58
				
59
				conf.setUrl(getXMLNodeProperty(beanNode, "url"));
60
				if(conf.getUrl() == null){
61
					conf.setUrl(getXMLNodeProperty(beanNode, "jdbcUrl"));
62
				}
63
				
64
				logger.debug("adding instanceName: "+ conf.getDataSourceName());
65
				configSet.add(conf);
66
			}
67
			
68
		} catch (SAXException e) {
69
			// TODO Auto-generated catch block
70
			e.printStackTrace();
71
		} catch (IOException e) {
72
			// TODO Auto-generated catch block
73
			e.printStackTrace();
74
		} catch (ParserConfigurationException e) {
75
			// TODO Auto-generated catch block
76
			e.printStackTrace();
77
		}
78
		return configSet;
79

    
80
    }
81
	
82
    private static String getXMLNodeProperty(Node beanNode, String name){
83
    	NodeList children = beanNode.getChildNodes();
84
    	for(int i=0; i < children.getLength(); i++){
85
    		Node p = children.item(i);
86
    		if(p.getNodeName().equals("property")
87
    				&& p.getAttributes().getNamedItem("name").getNodeValue().equals(name)){
88
    			return p.getAttributes().getNamedItem("value").getNodeValue();
89
    		}
90
    	}
91
		return null;
92
	}
93
    
94
    
95

    
96
}
(4-4/5)