2 * Copyright (C) 2007 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
.util
.Enumeration
;
13 import java
.util
.Properties
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.hibernate
.cache
.CacheProvider
;
17 import org
.hibernate
.cache
.NoCacheProvider
;
18 import org
.springframework
.beans
.MutablePropertyValues
;
19 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
20 import org
.springframework
.beans
.factory
.config
.PropertiesFactoryBean
;
21 import org
.springframework
.beans
.factory
.support
.AbstractBeanDefinition
;
22 import org
.springframework
.beans
.factory
.support
.RootBeanDefinition
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
25 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
26 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
32 public class CdmDataSource
extends CdmDataSourceBase
{
33 @SuppressWarnings("unused")
34 private static final Logger logger
= Logger
.getLogger(CdmDataSource
.class);
36 private DatabaseTypeEnum dbType
;
37 private String server
;
38 private String database
;
39 private int port
= -1;
40 private String username
;
41 private String password
;
43 private String filePath
;
46 private boolean isLazy
= true;
47 private String initMethodName
= null;
48 private String destroyMethodName
= null;
49 private DbSchemaValidation hbm2dll
= DbSchemaValidation
.VALIDATE
;
50 private boolean showSql
= false;
51 private boolean formatSql
= false;
52 private boolean registerSearchListener
= false;
53 private Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;;
55 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
){
56 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null);
59 static public CdmDataSource
NewMySqlInstance(String server
, String database
, int port
, String username
, String password
){
60 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, port
, username
, password
, null, null);
63 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, String username
, String password
){
64 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, -1, username
, password
, null, null);
67 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
){
68 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null);
72 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
){
75 H2Mode mode
= H2Mode
.EMBEDDED
;
76 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, null, mode
);
81 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, String filePath
){
84 H2Mode mode
= H2Mode
.EMBEDDED
;
85 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, filePath
, mode
);
90 static public CdmDataSource
NewH2InMemoryInstance(){
93 H2Mode mode
= H2Mode
.IN_MEMORY
;
94 String username
= "sa";
96 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, null, port
, username
, password
, null, mode
);
106 protected CdmDataSource(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, String filePath
, H2Mode mode
) {
108 this.dbType
= dbType
;
109 this.server
= server
;
110 this.database
= database
;
112 this.username
= username
;
113 this.password
= password
;
114 this.initMethodName
= dbType
.getInitMethod();
115 this.destroyMethodName
= dbType
.getDestroyMethod();
116 this.filePath
= filePath
;
122 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
123 * A CdmDataSource does not have a name representation therefor the database name is returned
125 public String
getName() {
130 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
132 @SuppressWarnings("unchecked")
133 public BeanDefinition
getDatasourceBean(){
134 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDriverManagerDataSourceClass());
136 bd
.setLazyInit(isLazy
);
137 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
138 bd
.setInitMethodName(initMethodName
);
140 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
141 bd
.setInitMethodName(destroyMethodName
);
145 MutablePropertyValues props
= new MutablePropertyValues();
146 Properties persistentProperties
= getDatasourceProperties();
147 Enumeration
<String
> keys
= (Enumeration
)persistentProperties
.keys();
148 while (keys
.hasMoreElements()){
149 String key
= (String
)keys
.nextElement();
150 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
153 bd
.setPropertyValues(props
);
158 * Returns the list of properties that are defined in the datasource
161 private Properties
getDatasourceProperties(){
162 Properties result
= new Properties();
163 result
.put("driverClassName", dbType
.getDriverClassName());
164 String connectionString
= dbType
.getConnectionString(this);
165 result
.put("url", connectionString
);
166 result
.put("username", username
);
167 result
.put("password", password
);
173 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
175 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
176 boolean showSql
= false;
177 boolean formatSql
= false;
178 boolean registerSearchListener
= false;
179 Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;
180 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, registerSearchListener
, cacheProviderClass
);
184 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
186 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Boolean registerSearchListener
, Class
<?
extends CacheProvider
> cacheProviderClass
){
187 //Hibernate default values
188 if (hbm2dll
== null){
189 hbm2dll
= this.hbm2dll
;
191 if (showSql
== null){
192 showSql
= this.showSql
;
194 if (formatSql
== null){
195 formatSql
= this.formatSql
;
197 if (cacheProviderClass
== null){
198 cacheProviderClass
= this.cacheProviderClass
;
200 if(registerSearchListener
== null){
201 registerSearchListener
= this.registerSearchListener
;
204 DatabaseTypeEnum dbtype
= dbType
;
205 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
206 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
208 Properties props
= new Properties();
209 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
210 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
211 props
.setProperty("hibernate.cache.provider_class", cacheProviderClass
.getName());
212 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
213 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
214 props
.setProperty("hibernate.search.autoregister_listeners", String
.valueOf(registerSearchListener
));
216 hibernateProps
.addPropertyValue("properties",props
);
217 bd
.setPropertyValues(hibernateProps
);
221 public String
getInitMethodName() {
222 return initMethodName
;
225 public void setInitMethodName(String initMethodName
) {
226 this.initMethodName
= initMethodName
;
229 public String
getDestroyMethodName() {
230 return destroyMethodName
;
233 public void setDestroyMethodName(String destroyMethodName
) {
234 this.destroyMethodName
= destroyMethodName
;
237 public String
getDatabase() {
241 public DatabaseTypeEnum
getDatabaseType() {
245 public String
getFilePath() {
250 public int getPort() {
254 public String
getServer() {
258 public H2Mode
getMode() {
263 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
265 public String
getPassword() {
270 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
272 public String
getUsername() {