change version to 3.3.0-SNAPSHOT in cdm-3.3 branch
[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.sql.SQLFeatureNotSupportedException;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.logging.Logger;
18
19 import javax.sql.DataSource;
20
21 import org.springframework.jdbc.datasource.SimpleDriverDataSource;
22 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
23
24
25 /**
26 * A simple RoutingDataSource.
27 * Bean definitions must set the key of the default datasource to "default"
28 * This String is defined in the contant <code>DEFAULT_DATASOURCE_KEY</code> and will
29 * be used when the RoutingDataSource is beeing updated with a new <code>Map</code>
30 * of data sources.
31 * <p>
32 * <b>Example of bean definition:</b>
33 * <pre>
34 &lt;bean id="dataSource" lazy-init="true" class="eu.etaxonomy.cdm.remote.service.BasepathRoutingDataSource"&gt;
35 &lt;property name="targetDataSources"&gt;
36 &lt;map key-type="java.lang.String"&gt;
37 &lt;entry key="default" value-ref="defaultDataSource"/&gt;
38 &lt;/map&gt;
39 &lt;/property&gt;
40 &lt;property name="defaultTargetDataSource" ref="defaultDataSource"/&gt;
41 &lt;/bean&gt;
42 </pre>
43 *
44 * @author a.kohlbecker
45 * @deprecated<b>NOTICE:</b>
46 * <em>This class is related to the switchable database infrastructure which allows to serve
47 * multiple databases with only a single instance of the cdm-remote-webapp.
48 * This concept however is deprecated due to several problems of which the most severe is the term loading issue.
49 * This class should however not deleted since we once might wish to switch back to this concept when we are
50 * able to deal with the implicated issues.
51 *
52 * See http://dev.e-taxonomy.eu/trac/wiki/CdmServerSwitchableDataSources for more information.</em>
53 */
54 @Deprecated
55 public class UpdatableRoutingDataSource extends AbstractRoutingDataSource {
56
57
58 private String defaultDatasourceName = "default";
59
60 @Override
61 protected Object determineCurrentLookupKey() {
62 return NamedContextHolder.getContextKey();
63 }
64
65 @Override
66 public void afterPropertiesSet() {
67 updateDataSources();
68 // super.afterPropertiesSet() is called by updateRoutingDataSource()
69 }
70
71 public void setDefaultDatasourceName(String name){
72 this.defaultDatasourceName = name;
73 }
74
75
76
77
78 public Map<String, DataSourceInfo> updateDataSources() {
79
80 logger.info("loading & testing datasources .. ");
81 Map<String,SimpleDriverDataSource> dataSources = loadDataSources();
82 Map<String, DataSourceInfo> dataSourceInfos = testDataSources(dataSources);
83
84 setTargetDataSources((Map)dataSources);
85 DataSource defaultDatasource = dataSources.get(defaultDatasourceName);
86 if(defaultDatasource == null) {
87 logger.error("Defaultdatasource '" +defaultDatasourceName + "' not found.");
88 }
89 setDefaultTargetDataSource(defaultDatasource);
90 super.afterPropertiesSet();
91
92 return dataSourceInfos;
93 }
94
95 protected Map<String, SimpleDriverDataSource> loadDataSources(){
96 return DataSourceBeanLoader.loadDataSources(SimpleDriverDataSource.class);
97 }
98
99 /**
100 * @param dataSources
101 * @return
102 */
103 protected Map<String, DataSourceInfo> testDataSources(Map<String, SimpleDriverDataSource> dataSources) {
104
105 Map<String, DataSourceInfo> dataSourceInfos = new HashMap<String, DataSourceInfo>();
106
107 for(String key : dataSources.keySet()){
108 SimpleDriverDataSource datasource = dataSources.get(key);
109 DataSourceInfo dsi = new DataSourceInfo(datasource);
110 Connection connection = null;
111 String sqlerror = null;
112 try {
113 connection = datasource.getConnection();
114 connection.close();
115 } catch (SQLException e) {
116 sqlerror = e.getMessage() + "["+ e.getSQLState() + "]";
117 dsi.getProblems().add(sqlerror);
118 if(connection != null){
119 try {connection.close();} catch (SQLException e1) { /* IGNORE */ }
120 }
121 }
122 logger.info(" /" + key + " => "+ datasource.getUrl() + "[ "+(sqlerror == null ? "OK" : "ERROR: " + sqlerror) + " ]");
123 dataSourceInfos.put(key, dsi);
124 }
125
126 return dataSourceInfos;
127 }
128
129 // added for compatibility with Java 7
130 public Logger getParentLogger() /* throws SQLFeatureNotSupportedException (is not compatibel with parent class in Java 6)*/ {
131 // TODO Auto-generated method stub
132 return null;
133 }
134
135 }