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
.model
.name
.NomenclaturalCode
;
31 public class CdmDataSource
extends CdmDataSourceBase
{
32 @SuppressWarnings("unused")
33 private static final Logger logger
= Logger
.getLogger(CdmDataSource
.class);
35 private DatabaseTypeEnum dbType
;
36 private String server
;
37 private String database
;
38 private int port
= -1;
39 private String username
;
40 private String password
;
41 private NomenclaturalCode nomenclaturalCode
;
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 NomenclaturalCode code
= null;
57 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, String username
, String password
){
58 return new CdmDataSource(dbType
, server
, database
, -1, username
, password
, null, null, null);
61 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
){
62 return new CdmDataSource(dbType
, server
, database
, port
, username
, password
, null, null, null);
65 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, String username
, String password
, NomenclaturalCode code
){
66 return new CdmDataSource(dbType
, server
, database
, -1, username
, password
, null, null, code
);
69 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
70 return new CdmDataSource(dbType
, server
, database
, port
, username
, password
, null, null, code
);
74 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
){
75 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null, null);
78 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
, NomenclaturalCode code
){
79 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null, code
);
82 static public CdmDataSource
NewMySqlInstance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
83 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, port
, username
, password
, null, null, code
);
86 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, String username
, String password
, NomenclaturalCode code
){
87 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, -1, username
, password
, null, null, code
);
90 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
/*, NomenclaturalCode code*/){
91 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null, code
);
97 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
){
98 return NewH2EmbeddedInstance(database
, username
, password
, null, null);
103 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, NomenclaturalCode code
){
104 return NewH2EmbeddedInstance(database
, username
, password
, null, code
);
109 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, String filePath
, NomenclaturalCode code
){
112 H2Mode mode
= H2Mode
.EMBEDDED
;
113 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, null, mode
, code
);
118 static public CdmDataSource
NewH2InMemoryInstance(){
121 H2Mode mode
= H2Mode
.IN_MEMORY
;
122 String username
= "sa";
123 String password
= "";
124 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, null, port
, username
, password
, null, mode
, null);
134 protected CdmDataSource(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, String filePath
, H2Mode mode
, NomenclaturalCode code
) {
136 this.dbType
= dbType
;
137 this.server
= server
;
138 this.database
= database
;
140 this.username
= username
;
141 this.password
= password
;
142 this.initMethodName
= dbType
.getInitMethod();
143 this.destroyMethodName
= dbType
.getDestroyMethod();
144 this.filePath
= filePath
;
146 this.nomenclaturalCode
= code
;
151 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
152 * A CdmDataSource does not have a name representation therefor the database name is returned
154 public String
getName() {
159 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
161 @SuppressWarnings("unchecked")
162 public BeanDefinition
getDatasourceBean(){
163 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDriverManagerDataSourceClass());
165 bd
.setLazyInit(isLazy
);
166 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
167 bd
.setInitMethodName(initMethodName
);
169 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
170 bd
.setInitMethodName(destroyMethodName
);
174 MutablePropertyValues props
= new MutablePropertyValues();
175 Properties persistentProperties
= getDatasourceProperties();
176 Enumeration
<String
> keys
= (Enumeration
)persistentProperties
.keys();
177 while (keys
.hasMoreElements()){
178 String key
= (String
)keys
.nextElement();
179 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
182 bd
.setPropertyValues(props
);
187 * Returns the list of properties that are defined in the datasource
190 private Properties
getDatasourceProperties(){
191 Properties result
= new Properties();
192 result
.put("driverClassName", dbType
.getDriverClassName());
193 String connectionString
= dbType
.getConnectionString(this);
194 result
.put("url", connectionString
);
195 result
.put("username", username
);
196 result
.put("password", password
);
202 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
204 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
205 boolean showSql
= false;
206 boolean formatSql
= false;
207 boolean registerSearchListener
= false;
208 Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;
209 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, registerSearchListener
, cacheProviderClass
);
213 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
215 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Boolean registerSearchListener
, Class
<?
extends CacheProvider
> cacheProviderClass
){
216 //Hibernate default values
217 if (hbm2dll
== null){
218 hbm2dll
= this.hbm2dll
;
220 if (showSql
== null){
221 showSql
= this.showSql
;
223 if (formatSql
== null){
224 formatSql
= this.formatSql
;
226 if (cacheProviderClass
== null){
227 cacheProviderClass
= this.cacheProviderClass
;
229 if(registerSearchListener
== null){
230 registerSearchListener
= this.registerSearchListener
;
233 DatabaseTypeEnum dbtype
= dbType
;
234 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
235 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
237 Properties props
= new Properties();
238 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
239 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
240 props
.setProperty("hibernate.cache.provider_class", cacheProviderClass
.getName());
241 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
242 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
243 props
.setProperty("hibernate.search.autoregister_listeners", String
.valueOf(registerSearchListener
));
245 hibernateProps
.addPropertyValue("properties",props
);
246 bd
.setPropertyValues(hibernateProps
);
250 public String
getInitMethodName() {
251 return initMethodName
;
254 public void setInitMethodName(String initMethodName
) {
255 this.initMethodName
= initMethodName
;
258 public String
getDestroyMethodName() {
259 return destroyMethodName
;
262 public void setDestroyMethodName(String destroyMethodName
) {
263 this.destroyMethodName
= destroyMethodName
;
266 public String
getDatabase() {
270 public DatabaseTypeEnum
getDatabaseType() {
274 public String
getFilePath() {
279 public int getPort() {
283 public String
getServer() {
287 public H2Mode
getMode() {
292 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
294 public String
getPassword() {
299 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
301 public String
getUsername() {
306 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getNomenclaturalCode()
308 public NomenclaturalCode
getNomenclaturalCode() {
309 return nomenclaturalCode
;