explaining the deprecation of this class
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / UpdatableRoutingDataSource.java
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 * @deprecated<b>NOTICE:</b>
44 * <em>This class is related to the switchable database infrastructure which allows to serve
45 * multiple databases with only a single instance of the cdm-remote-webapp.
46 * This concept however is deprecated due to several problems of which the most severe is the term loading issue.
47 * This class should however not deleted since we once might wish to switch back to this concept when we are
48 * able to deal with the implicated issues.
49 *
50 * See http://dev.e-taxonomy.eu/trac/wiki/CdmServerSwitchableDataSources for more information.</em>
51 */
52 @Deprecated
53 public class UpdatableRoutingDataSource extends AbstractRoutingDataSource {
54
55
56 private String defaultDatasourceName = "default";
57
58 @Override
59 protected Object determineCurrentLookupKey() {
60 return NamedContextHolder.getContextKey();
61 }
62
63 @Override
64 public void afterPropertiesSet() {
65 updateDataSources();
66 // super.afterPropertiesSet() is called by updateRoutingDataSource()
67 }
68
69 public void setDefaultDatasourceName(String name){
70 this.defaultDatasourceName = name;
71 }
72
73
74
75
76 public Map<String, DataSourceInfo> updateDataSources() {
77
78 logger.info("loading & testing datasources .. ");
79 Map<String,SimpleDriverDataSource> dataSources = loadDataSources();
80 Map<String, DataSourceInfo> dataSourceInfos = testDataSources(dataSources);
81
82 setTargetDataSources((Map)dataSources);
83 DataSource defaultDatasource = dataSources.get(defaultDatasourceName);
84 if(defaultDatasource == null) {
85 logger.error("Defaultdatasource '" +defaultDatasourceName + "' not found.");
86 }
87 setDefaultTargetDataSource(defaultDatasource);
88 super.afterPropertiesSet();
89
90 return dataSourceInfos;
91 }
92
93 protected Map<String, SimpleDriverDataSource> loadDataSources(){
94 return DataSourceBeanLoader.loadDataSources(SimpleDriverDataSource.class);
95 }
96
97 /**
98 * @param dataSources
99 * @return
100 */
101 protected Map<String, DataSourceInfo> testDataSources(Map<String, SimpleDriverDataSource> dataSources) {
102
103 Map<String, DataSourceInfo> dataSourceInfos = new HashMap<String, DataSourceInfo>();
104
105 for(String key : dataSources.keySet()){
106 SimpleDriverDataSource datasource = dataSources.get(key);
107 DataSourceInfo dsi = new DataSourceInfo(datasource);
108 Connection connection = null;
109 String sqlerror = null;
110 try {
111 connection = datasource.getConnection();
112 connection.close();
113 } catch (SQLException e) {
114 sqlerror = e.getMessage() + "["+ e.getSQLState() + "]";
115 dsi.getProblems().add(sqlerror);
116 if(connection != null){
117 try {connection.close();} catch (SQLException e1) { /* IGNORE */ }
118 }
119 }
120 logger.info(" /" + key + " => "+ datasource.getUrl() + "[ "+(sqlerror == null ? "OK" : "ERROR: " + sqlerror) + " ]");
121 dataSourceInfos.put(key, dsi);
122 }
123
124 return dataSourceInfos;
125 }
126
127 }