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