Project

General

Profile

Download (3.66 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

    
10
package eu.etaxonomy.cdm.database;
11

    
12
import java.sql.Connection;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.Map;
16

    
17
import javax.sql.DataSource;
18

    
19
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
20
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
21

    
22

    
23
/**
24
 * A simple RoutingDataSource.
25
 * Bean definitions must set the key of the default datasource to "default" 
26
 * This String is defined in the contant <code>DEFAULT_DATASOURCE_KEY</code> and will
27
 * be used when the RoutingDataSource is beeing updated with a new <code>Map</code> 
28
 * of data sources.
29
 * <p>
30
 * <b>Example of bean definition:</b>
31
 * <pre>
32
   &lt;bean id="dataSource"  lazy-init="true" class="eu.etaxonomy.cdm.remote.service.BasepathRoutingDataSource"&gt;
33
    	&lt;property name="targetDataSources"&gt;
34
	      &lt;map key-type="java.lang.String"&gt;
35
	         &lt;entry key="default" value-ref="defaultDataSource"/&gt;
36
	      &lt;/map&gt;
37
   		&lt;/property&gt;
38
   		&lt;property name="defaultTargetDataSource" ref="defaultDataSource"/&gt;
39
   &lt;/bean&gt;
40
   </pre>
41
 * 
42
 * @author a.kohlbecker
43
 */
44
@Deprecated
45
public class UpdatableRoutingDataSource extends AbstractRoutingDataSource {
46
	
47

    
48
	private String defaultDatasourceName = "default";
49

    
50
	@Override
51
	protected Object determineCurrentLookupKey() {
52
		return NamedContextHolder.getContextKey();
53
	}
54
	
55
	@Override
56
	public void afterPropertiesSet() {
57
		updateDataSources();
58
		// super.afterPropertiesSet() is called by updateRoutingDataSource()
59
	}
60
	
61
	public void setDefaultDatasourceName(String name){
62
		this.defaultDatasourceName = name;
63
	}
64
	
65
	
66

    
67
	
68
	public Map<String, DataSourceInfo> updateDataSources() {
69
		
70
		logger.info("loading & testing datasources .. ");
71
		Map<String,SimpleDriverDataSource> dataSources = loadDataSources();
72
		Map<String, DataSourceInfo> dataSourceInfos = testDataSources(dataSources);
73
		
74
		setTargetDataSources((Map)dataSources);
75
		DataSource defaultDatasource = dataSources.get(defaultDatasourceName);
76
		if(defaultDatasource == null) {
77
			logger.error("Defaultdatasource '" +defaultDatasourceName + "' not found.");
78
		}
79
		setDefaultTargetDataSource(defaultDatasource);
80
		super.afterPropertiesSet();
81
		
82
		return dataSourceInfos;
83
	}
84
	
85
	protected Map<String, SimpleDriverDataSource> loadDataSources(){
86
		return DataSourceBeanLoader.loadDataSources(SimpleDriverDataSource.class);
87
	}
88

    
89
	/**
90
	 * @param dataSources
91
	 * @return
92
	 */
93
	protected Map<String, DataSourceInfo> testDataSources(Map<String, SimpleDriverDataSource> dataSources) {
94
		
95
		Map<String, DataSourceInfo> dataSourceInfos = new HashMap<String, DataSourceInfo>();
96

    
97
		for(String key : dataSources.keySet()){
98
			SimpleDriverDataSource datasource = dataSources.get(key);
99
			DataSourceInfo dsi = new DataSourceInfo(datasource);
100
			Connection connection = null;
101
			String sqlerror = null;
102
			try {
103
				connection = datasource.getConnection();
104
				connection.close();
105
			} catch (SQLException e) {
106
				sqlerror = e.getMessage() + "["+ e.getSQLState() + "]";
107
				dsi.getProblems().add(sqlerror);
108
				if(connection !=  null){
109
					try {connection.close();} catch (SQLException e1) { /* IGNORE */ }
110
				}
111
			}
112
			logger.info("    /" + key + " => "+ datasource.getUrl() + "[ "+(sqlerror == null ? "OK" : "ERROR: " + sqlerror) + " ]");
113
			dataSourceInfos.put(key, dsi);
114
		}
115
		
116
		return dataSourceInfos;
117
	}
118

    
119
}
(18-18/18)