Project

General

Profile

Download (7.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.util.Enumeration;
13
import java.util.Properties;
14

    
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;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
26
import eu.etaxonomy.cdm.database.DbSchemaValidation;
27

    
28
/**
29
 * @author a.mueller
30
 *
31
 */
32
public class CdmDataSource implements ICdmDataSource {
33
	private static final Logger logger = Logger.getLogger(CdmDataSource.class);
34
	
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
	
42
	private String filePath;
43
	private H2Mode mode;
44

    
45
	private boolean isLazy = true;
46
	private String initMethodName = null;
47
	private String destroyMethodName = null;
48
	private DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
49
	private boolean showSql = false;
50
	private boolean formatSql = false;
51
	private Class<? extends CacheProvider> cacheProviderClass = NoCacheProvider.class;;
52

    
53
	static public CdmDataSource  NewMySqlInstance(String server, String database, String username, String password){
54
		return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null);
55
	}
56
	
57
	static public CdmDataSource  NewMySqlInstance(String server, String database, int port, String username, String password){
58
		return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, port, username, password, null, null);
59
	}
60

    
61
	static public CdmDataSource  NewSqlServer2005Instance(String server, String database, String username, String password){
62
		return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, -1, username, password, null, null);
63
	}
64
	
65
	static public CdmDataSource  NewSqlServer2005Instance(String server, String database, int port, String username, String password){
66
		return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null);
67
	}
68

    
69
	/** in work */
70
	static public CdmDataSource  NewH2EmbeddedInstance(String database, String username, String password){
71
		//FIXME in work
72
		int port = -1;
73
		H2Mode mode = H2Mode.EMBEDDED;
74
		CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, null, mode);
75
		return dataSource;
76
	}
77

    
78
	/** in work */
79
	static public CdmDataSource  NewH2InMemoryInstance(){
80
		//FIXME in work
81
		int port = -1;
82
		H2Mode mode = H2Mode.IN_MEMORY;
83
		String username = "sa";
84
		String password = "";
85
		CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode);
86
		return dataSource;
87
	}
88

    
89
	
90
	/**
91
	 * @param server
92
	 * @param database
93
	 * @param port
94
	 */
95
	protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode) {
96
		super();
97
		this.dbType = dbType;
98
		this.server = server;
99
		this.database = database;
100
		this.port = port;
101
		this.username = username;
102
		this.password = password;
103
		this.initMethodName = dbType.getInitMethod();
104
		this.destroyMethodName = dbType.getDestroyMethod();
105
		this.filePath = filePath;
106
		this.mode = mode;
107
	}
108
	
109
	
110
	/* (non-Javadoc)
111
	 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
112
	 */
113
	public String getName() {
114
		return database;
115
	}
116
	
117
	/* (non-Javadoc)
118
	 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
119
	 */
120
	public BeanDefinition getDatasourceBean(){
121
		AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDriverManagerDataSourceClass());
122
		//attributes
123
		bd.setLazyInit(isLazy);
124
		if (! CdmUtils.Nz(initMethodName).trim().equals("") ){
125
			bd.setInitMethodName(initMethodName);
126
		}
127
		if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){
128
			bd.setInitMethodName(destroyMethodName);
129
		}
130
		
131
		//properties
132
		MutablePropertyValues props = new MutablePropertyValues();
133
		Properties persistentProperties = getDatasourceProperties();
134
		Enumeration<String> keys = (Enumeration)persistentProperties.keys(); 
135
		while (keys.hasMoreElements()){
136
			String key = (String)keys.nextElement();
137
			props.addPropertyValue(key, persistentProperties.getProperty(key));
138
		}
139

    
140
		bd.setPropertyValues(props);
141
		return bd;
142
	}
143
	
144
	/**
145
	 * Returns the list of properties that are defined in the datasource    
146
	 * @return 
147
	 */
148
	private Properties getDatasourceProperties(){
149
		Properties result = new Properties();
150
		result.put("driverClassName", dbType.getDriverClassName());
151
		String connectionString = dbType.getConnectionString(this);
152
		result.put("url", connectionString);
153
		result.put("username", username);
154
		result.put("password", password);
155
		return result;
156
	}
157
	
158

    
159
	/* (non-Javadoc)
160
	 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
161
	 */
162
	public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){
163
		boolean showSql = false;
164
		boolean formatSql = false;
165
		Class<? extends CacheProvider> cacheProviderClass = NoCacheProvider.class;
166
		return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, cacheProviderClass);
167
	}
168
	
169
	/* (non-Javadoc)
170
	 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
171
	 */
172
	public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Class<? extends CacheProvider> cacheProviderClass){
173
		//Hibernate default values
174
		if (hbm2dll == null){
175
			hbm2dll = this.hbm2dll;
176
		}
177
		if (showSql == null){
178
			showSql = this.showSql;
179
		}
180
		if (formatSql == null){
181
			formatSql = this.formatSql;
182
		}
183
		if (cacheProviderClass == null){
184
			cacheProviderClass = this.cacheProviderClass;
185
		}
186
		
187
		DatabaseTypeEnum dbtype = dbType;
188
		AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
189
		MutablePropertyValues hibernateProps = new MutablePropertyValues();
190

    
191
		Properties props = new Properties();
192
		props.setProperty("hibernate.hbm2ddl.auto", hbm2dll.toString());
193
		props.setProperty("hibernate.dialect", dbtype.getHibernateDialect());
194
		props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());
195
		props.setProperty("hibernate.show_sql", String.valueOf(showSql));
196
		props.setProperty("hibernate.format_sql", String.valueOf(formatSql));
197

    
198
		hibernateProps.addPropertyValue("properties",props);
199
		bd.setPropertyValues(hibernateProps);
200
		return bd;
201
	}
202

    
203
	public String getInitMethodName() {
204
		return initMethodName;
205
	}
206

    
207
	public void setInitMethodName(String initMethodName) {
208
		this.initMethodName = initMethodName;
209
	}
210

    
211
	public String getDestroyMethodName() {
212
		return destroyMethodName;
213
	}
214

    
215
	public void setDestroyMethodName(String destroyMethodName) {
216
		this.destroyMethodName = destroyMethodName;
217
	}
218

    
219
	public String getDatabase() {
220
		return database;
221
	}
222

    
223
	public DatabaseTypeEnum getDatabaseType() {
224
		return dbType;
225
	}
226
	
227
	public String getFilePath() {
228
		return filePath;
229
	}
230

    
231

    
232
	public int getPort() {
233
		return port;
234
	}
235

    
236
	public String getServer() {
237
		return server;
238
	}
239

    
240
	public H2Mode getMode() {
241
		return mode;
242
	}	
243
}
244

    
(1-1/11)