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 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, String username
, String password
){
56 return new CdmDataSource(dbType
, server
, database
, -1, username
, password
, null, null, null);
59 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
){
60 return new CdmDataSource(dbType
, server
, database
, port
, username
, password
, null, null, null);
63 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, String username
, String password
, NomenclaturalCode code
){
64 return new CdmDataSource(dbType
, server
, database
, -1, username
, password
, null, null, code
);
67 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
68 return new CdmDataSource(dbType
, server
, database
, port
, username
, password
, null, null, code
);
72 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
){
73 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null, null);
76 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
, NomenclaturalCode code
){
77 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null, code
);
80 static public CdmDataSource
NewMySqlInstance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
81 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, port
, username
, password
, null, null, code
);
84 static public CdmDataSource
NewPostgreSQLInstance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
85 return new CdmDataSource(DatabaseTypeEnum
.PostgreSQL
, server
, database
, port
, username
, password
, null, null, code
);
88 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
89 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null, code
);
92 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
/*, NomenclaturalCode code*/){
93 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null, null);
99 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
){
100 return NewH2EmbeddedInstance(database
, username
, password
, null, null);
105 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, NomenclaturalCode code
){
106 return NewH2EmbeddedInstance(database
, username
, password
, null, code
);
111 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, String filePath
, NomenclaturalCode code
){
114 H2Mode mode
= H2Mode
.EMBEDDED
;
115 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, null, mode
, code
);
120 static public CdmDataSource
NewH2InMemoryInstance(){
123 H2Mode mode
= H2Mode
.IN_MEMORY
;
124 String username
= "sa";
125 String password
= "";
126 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, null, port
, username
, password
, null, mode
, null);
136 protected CdmDataSource(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, String filePath
, H2Mode mode
, NomenclaturalCode code
) {
138 this.dbType
= dbType
;
139 this.server
= server
;
140 this.database
= database
;
142 this.username
= username
;
143 this.password
= password
;
144 this.initMethodName
= dbType
.getInitMethod();
145 this.destroyMethodName
= dbType
.getDestroyMethod();
146 this.filePath
= filePath
;
148 this.nomenclaturalCode
= code
;
153 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
154 * A CdmDataSource does not have a name representation therefor the database name is returned
156 public String
getName() {
161 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
163 @SuppressWarnings("unchecked")
164 public BeanDefinition
getDatasourceBean(){
165 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDataSourceClass());
167 bd
.setLazyInit(isLazy
);
168 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
169 bd
.setInitMethodName(initMethodName
);
171 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
172 bd
.setInitMethodName(destroyMethodName
);
176 MutablePropertyValues props
= new MutablePropertyValues();
177 Properties persistentProperties
= getDatasourceProperties();
178 Enumeration
<String
> keys
= (Enumeration
)persistentProperties
.keys();
179 while (keys
.hasMoreElements()){
180 String key
= (String
)keys
.nextElement();
181 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
184 bd
.setPropertyValues(props
);
189 * Returns the list of properties that are defined in the datasource
192 private Properties
getDatasourceProperties(){
193 Properties result
= new Properties();
194 result
.put("driverClassName", dbType
.getDriverClassName());
195 String connectionString
= dbType
.getConnectionString(this);
196 result
.put("url", connectionString
);
197 result
.put("username", username
);
198 result
.put("password", password
);
204 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
206 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
207 boolean showSql
= false;
208 boolean formatSql
= false;
209 boolean registerSearchListener
= false;
210 Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;
211 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, registerSearchListener
, cacheProviderClass
);
215 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
217 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Boolean registerSearchListener
, Class
<?
extends CacheProvider
> cacheProviderClass
){
218 //Hibernate default values
219 if (hbm2dll
== null){
220 hbm2dll
= this.hbm2dll
;
222 if (showSql
== null){
223 showSql
= this.showSql
;
225 if (formatSql
== null){
226 formatSql
= this.formatSql
;
228 if (cacheProviderClass
== null){
229 cacheProviderClass
= this.cacheProviderClass
;
231 if(registerSearchListener
== null){
232 registerSearchListener
= this.registerSearchListener
;
235 DatabaseTypeEnum dbtype
= dbType
;
236 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
237 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
239 Properties props
= new Properties();
240 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
241 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
242 props
.setProperty("hibernate.cache.provider_class", cacheProviderClass
.getName());
243 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
244 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
245 props
.setProperty("hibernate.search.autoregister_listeners", String
.valueOf(registerSearchListener
));
247 hibernateProps
.addPropertyValue("properties",props
);
248 bd
.setPropertyValues(hibernateProps
);
252 public String
getInitMethodName() {
253 return initMethodName
;
256 public void setInitMethodName(String initMethodName
) {
257 this.initMethodName
= initMethodName
;
260 public String
getDestroyMethodName() {
261 return destroyMethodName
;
264 public void setDestroyMethodName(String destroyMethodName
) {
265 this.destroyMethodName
= destroyMethodName
;
268 public String
getDatabase() {
272 public DatabaseTypeEnum
getDatabaseType() {
276 public String
getFilePath() {
281 public int getPort() {
285 public String
getServer() {
289 public H2Mode
getMode() {
294 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
296 public String
getPassword() {
301 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
303 public String
getUsername() {
308 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getNomenclaturalCode()
310 public NomenclaturalCode
getNomenclaturalCode() {
311 return nomenclaturalCode
;