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
|
private static final String DATA_SOURCE_PROPERTIES = "dataSourceProperties";
|
39
|
public static final Logger logger = Logger.getLogger(DataSourcePropertyParser.class);
|
40
|
|
41
|
public static List<CdmInstanceProperties> parseDataSourceConfigs(File datasourcesFile){
|
42
|
|
43
|
logger.info("loading bean definition file: " + datasourcesFile.getAbsolutePath());
|
44
|
List<CdmInstanceProperties> configList = new ArrayList<CdmInstanceProperties>();
|
45
|
Set<String> idSet = new HashSet<String>();
|
46
|
try {
|
47
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
48
|
Document doc = builder.parse(datasourcesFile);
|
49
|
NodeList beanNodes = doc.getElementsByTagName("bean");
|
50
|
for(int i=0; i < beanNodes.getLength(); i++){
|
51
|
CdmInstanceProperties conf = new CdmInstanceProperties();
|
52
|
Node beanNode = beanNodes.item(i);
|
53
|
// ATTRIBUTE_DATASOURCE_NAME
|
54
|
NamedNodeMap namedNodeMap = beanNode.getAttributes();
|
55
|
String beanId = namedNodeMap.getNamedItem("id").getNodeValue();
|
56
|
|
57
|
// skip the dataSourceProperties bean
|
58
|
if(beanId.equals(DATA_SOURCE_PROPERTIES)){
|
59
|
continue;
|
60
|
}
|
61
|
|
62
|
conf.setDataSourceName(beanId);
|
63
|
// ATTRIBUTE_DATASOURCE_DRIVERCLASS
|
64
|
conf.setDriverClass(getXMLNodeProperty(beanNode, "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.setUrl(getXMLNodeProperty(beanNode, "url"));
|
72
|
if(conf.getUrl() == null){
|
73
|
conf.setUrl(getXMLNodeProperty(beanNode, "jdbcUrl"));
|
74
|
}
|
75
|
|
76
|
if(idSet.add(conf.getDataSourceName())) {
|
77
|
logger.debug("adding instanceName '"+ conf.getDataSourceName() + "'");
|
78
|
configList.add(conf);
|
79
|
} else {
|
80
|
logger.error("instance with name '"+ conf.getDataSourceName() + "' alreaddy exists");
|
81
|
}
|
82
|
|
83
|
}
|
84
|
|
85
|
} catch (SAXException e) {
|
86
|
// TODO Auto-generated catch block
|
87
|
e.printStackTrace();
|
88
|
} catch (IOException e) {
|
89
|
// TODO Auto-generated catch block
|
90
|
e.printStackTrace();
|
91
|
} catch (ParserConfigurationException e) {
|
92
|
// TODO Auto-generated catch block
|
93
|
e.printStackTrace();
|
94
|
}
|
95
|
return configList;
|
96
|
|
97
|
}
|
98
|
|
99
|
private static String getXMLNodeProperty(Node beanNode, String name){
|
100
|
NodeList children = beanNode.getChildNodes();
|
101
|
for(int i=0; i < children.getLength(); i++){
|
102
|
Node p = children.item(i);
|
103
|
if(p.getNodeName().equals("property")
|
104
|
&& p.getAttributes().getNamedItem("name").getNodeValue().equals(name)){
|
105
|
return p.getAttributes().getNamedItem("value").getNodeValue();
|
106
|
}
|
107
|
}
|
108
|
return null;
|
109
|
}
|
110
|
|
111
|
|
112
|
|
113
|
}
|