2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
;
12 import java
.sql
.Connection
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
16 import java
.util
.logging
.Logger
;
18 import javax
.sql
.DataSource
;
20 import org
.springframework
.jdbc
.datasource
.SimpleDriverDataSource
;
21 import org
.springframework
.jdbc
.datasource
.lookup
.AbstractRoutingDataSource
;
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>
31 * <b>Example of bean definition:</b>
33 <bean id="dataSource" lazy-init="true" class="eu.etaxonomy.cdm.remote.service.BasepathRoutingDataSource">
34 <property name="targetDataSources">
35 <map key-type="java.lang.String">
36 <entry key="default" value-ref="defaultDataSource"/>
39 <property name="defaultTargetDataSource" ref="defaultDataSource"/>
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.
51 * See http://dev.e-taxonomy.eu/trac/wiki/CdmServerSwitchableDataSources for more information.</em>
54 public class UpdatableRoutingDataSource
extends AbstractRoutingDataSource
{
57 private String defaultDatasourceName
= "default";
60 protected Object
determineCurrentLookupKey() {
61 return NamedContextHolder
.getContextKey();
65 public void afterPropertiesSet() {
67 // super.afterPropertiesSet() is called by updateRoutingDataSource()
70 public void setDefaultDatasourceName(String name
){
71 this.defaultDatasourceName
= name
;
77 public Map
<String
, DataSourceInfo
> updateDataSources() {
79 logger
.info("loading & testing datasources .. ");
80 Map
<String
,SimpleDriverDataSource
> dataSources
= loadDataSources();
81 Map
<String
, DataSourceInfo
> dataSourceInfos
= testDataSources(dataSources
);
83 setTargetDataSources((Map
)dataSources
);
84 DataSource defaultDatasource
= dataSources
.get(defaultDatasourceName
);
85 if(defaultDatasource
== null) {
86 logger
.error("Defaultdatasource '" +defaultDatasourceName
+ "' not found.");
88 setDefaultTargetDataSource(defaultDatasource
);
89 super.afterPropertiesSet();
91 return dataSourceInfos
;
94 protected Map
<String
, SimpleDriverDataSource
> loadDataSources(){
95 return DataSourceBeanLoader
.loadDataSources(SimpleDriverDataSource
.class);
102 protected Map
<String
, DataSourceInfo
> testDataSources(Map
<String
, SimpleDriverDataSource
> dataSources
) {
104 Map
<String
, DataSourceInfo
> dataSourceInfos
= new HashMap
<String
, DataSourceInfo
>();
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;
112 connection
= datasource
.getConnection();
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 */ }
121 logger
.info(" /" + key
+ " => "+ datasource
.getUrl() + "[ "+(sqlerror
== null ?
"OK" : "ERROR: " + sqlerror
) + " ]");
122 dataSourceInfos
.put(key
, dsi
);
125 return dataSourceInfos
;
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