Project

General

Profile

Download (23.1 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 static eu.etaxonomy.cdm.common.XmlHelp.getBeansRoot;
13
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlBean;
14
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlValueProperty;
15
import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;
16

    
17
import java.io.File;
18
import java.io.FileInputStream;
19
import java.io.FileNotFoundException;
20
import java.io.FileOutputStream;
21
import java.io.IOException;
22
import java.util.ArrayList;
23
import java.util.Enumeration;
24
import java.util.Iterator;
25
import java.util.List;
26
import java.util.Properties;
27

    
28
import javax.sql.DataSource;
29

    
30
import org.apache.log4j.Logger;
31
import org.hibernate.cache.internal.NoCachingRegionFactory;
32
import org.hibernate.cache.spi.RegionFactory;
33
import org.jdom.Attribute;
34
import org.jdom.Document;
35
import org.jdom.Element;
36
import org.jdom.output.Format;
37
import org.springframework.beans.MutablePropertyValues;
38
import org.springframework.beans.factory.config.BeanDefinition;
39
import org.springframework.beans.factory.config.PropertiesFactoryBean;
40
import org.springframework.beans.factory.support.AbstractBeanDefinition;
41
import org.springframework.beans.factory.support.RootBeanDefinition;
42

    
43
import com.mchange.v2.c3p0.ComboPooledDataSource;
44

    
45
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
46
import eu.etaxonomy.cdm.common.CdmUtils;
47
import eu.etaxonomy.cdm.common.XmlHelp;
48
import eu.etaxonomy.cdm.database.types.IDatabaseType;
49
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
50

    
51

    
52
/**
53
 * class to access an CdmDataSource
54
 */
55
public class CdmPersistentDataSource extends CdmDataSourceBase{
56
	private static final Logger logger = Logger.getLogger(CdmPersistentDataSource.class);
57
	
58
	public static final String DATASOURCE_BEAN_POSTFIX = "DataSource";
59
	public final static String DATASOURCE_FILE_NAME = "cdm.datasources.xml";
60
	public final static String DATASOURCE_PATH = "/eu/etaxonomy/cdm/";
61
	
62
	private final static Format format = Format.getPrettyFormat(); 
63

    
64
	public enum DbProperties{
65
		DRIVER_CLASS,
66
		URL,
67
		SERVER, 
68
		DATABASE,
69
		PORT,
70
		MODE,
71
		FILEPATH,
72
		USERNAME,
73
		PASSWORD, 
74
		NOMENCLATURAL_CODE;
75

    
76
		@Override
77
		public String toString(){
78
			switch (this){
79
				case DRIVER_CLASS:
80
					return "driverClassName";
81
				case URL:
82
					return "url";
83
				case SERVER:
84
					return "server";
85
				case DATABASE:
86
					return "database";
87
				case PORT:
88
					return "port";
89
				case MODE:
90
					return "mode";
91
				case FILEPATH:
92
					return "filePath";
93
				case USERNAME:
94
					return "username";
95
				case PASSWORD:
96
					return "password";
97
				case NOMENCLATURAL_CODE:
98
					return "nomenclaturalCode";
99
				default: 
100
					throw new IllegalArgumentException( "Unknown enumeration type" );
101
			}
102
		}
103
	}
104

    
105
	/**
106
	 * The Datasource class that Spring will use to set up the connection to the database
107
	 */
108
	private static String dataSourceClassName = ComboPooledDataSource.class.getName();
109
	// we used dbcps BasicDataSource before
110
//	private static String dataSourceClassName = BasicDataSource.class.getName();
111
	
112
	//name
113
	protected String dataSourceName;
114

    
115
	
116
	/**
117
	 * Returns the default CdmDataSource
118
	 * @return the default CdmDataSource
119
	 * @throws DataSourceNotFoundException 
120
	 */
121
	public final static CdmPersistentDataSource NewDefaultInstance() throws DataSourceNotFoundException {
122
		return NewInstance("default");
123
	}
124
	
125
	
126
	/**
127
	 * Returns the default CdmDataSource
128
	 * @return the default CdmDataSource
129
	 * @throws DataSourceNotFoundException 
130
	 */
131
	public final static CdmPersistentDataSource NewLocalHsqlInstance() throws DataSourceNotFoundException{
132
		return NewInstance("localDefaultHsql");
133
	}
134
	
135
	/**
136
	 * Returns the CdmDataSource named by strDataSource
137
	 * @param strDataSource
138
	 * @return
139
	 */
140
	public final static CdmPersistentDataSource NewInstance(String dataSourceName) throws DataSourceNotFoundException{
141
		if (exists(dataSourceName)){
142
			return new CdmPersistentDataSource(dataSourceName);
143
		}else{
144
			throw new DataSourceNotFoundException("Datasource not found: " + dataSourceName);
145
		}
146
	}
147

    
148
	/**
149
	 * Private Constructor. Use NewXXX factory methods for creating a new instance of CdmDataSource!
150
	 * @param strDataSource
151
	 */
152
	private CdmPersistentDataSource(String strDataSource){
153
		dataSourceName = strDataSource;
154
	}
155
	
156
	/**
157
	 * Returns the name of the bean.
158
	 * @return
159
	 */
160
	public String getName(){
161
		return dataSourceName;
162
	}
163
	
164
	
165
	/**
166
	 * Returns the name of the bean Element in the xml config file.
167
	 * @return bean name
168
	 */
169
	private static String getBeanName(String name){
170
		return name == null? null : name + DATASOURCE_BEAN_POSTFIX;
171
	}
172

    
173

    
174
	
175
	public String getDatabase() {
176
		return getDatabaseProperty(DbProperties.DATABASE);
177
	}
178

    
179

    
180
	public String getFilePath() {
181
		//TODO null
182
		return getDatabaseProperty(DbProperties.FILEPATH);
183
	}
184

    
185

    
186
	public H2Mode getMode() {
187
		//TODO null
188
		return H2Mode.fromString(getDatabaseProperty(DbProperties.MODE));
189
	}
190
	
191

    
192
	/* (non-Javadoc)
193
	 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getNomenclaturalCode()
194
	 */
195
	public NomenclaturalCode getNomenclaturalCode() {
196
		// TODO null
197
		return NomenclaturalCode.fromString(getDatabaseProperty(DbProperties.NOMENCLATURAL_CODE));
198
	}
199

    
200
	public int getPort() {
201
		String port = CdmUtils.Nz(getDatabaseProperty(DbProperties.PORT));
202
		if ("".equals(port)){
203
			return -1;
204
		}else{
205
			//TODO exception if non integer
206
			return Integer.valueOf(port);
207
		}
208
	}
209

    
210

    
211
	public String getServer() {
212
		return getDatabaseProperty(DbProperties.SERVER);
213
	}
214

    
215
	/**
216
	 * Returns the database type of the data source. 
217
	 * @return the database type of the data source. Null if the bean or the driver class property does not exist or the driver class is unknown.
218
	 */
219
	public DatabaseTypeEnum getDatabaseType(){
220
		Element bean = getDatasourceBeanXml(this.dataSourceName);
221
		if (bean == null){
222
			return null;
223
		}else{
224
			Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", "driverClassName");
225
			if (driverProp == null){
226
				logger.warn("Unknown property driverClass");
227
		    	return null;
228
			}else{
229
				String strDriverClass = driverProp.getAttributeValue("value");
230
				DatabaseTypeEnum dbType = DatabaseTypeEnum.getDatabaseEnumByDriverClass(strDriverClass);
231
				return dbType;
232
			}
233
		}
234
	}
235
	
236
	
237
	/**
238
	 * Returns the database type of the data source. 
239
	 * @return the database type of the data source. Null if the bean or the driver class property does not exist or the driver class is unknown.
240
	 */
241
	protected String getDatabaseProperty(DbProperties property){
242
		Element bean = getDatasourceBeanXml(this.dataSourceName);
243
		String url;
244
		String result = null;
245
		if (bean != null){
246
			result = getPropertyValue(bean, property.toString());
247
			if (result == null){  //test if property is database, server or port which are included in the url
248
				url = getPropertyValue(bean, DbProperties.URL.toString());
249
				DatabaseTypeEnum dbTypeEnum = getDatabaseType();
250
				if (dbTypeEnum != null){
251
					IDatabaseType dbType = dbTypeEnum.getDatabaseType();
252
					if (property.equals(DbProperties.DATABASE)){
253
						result = dbType.getDatabaseNameByConnectionString(url);
254
					}else if(property.equals(DbProperties.SERVER)){
255
						result = dbType.getServerNameByConnectionString(url);
256
					}else if(property.equals(DbProperties.PORT)){
257
						result = String.valueOf(dbType.getPortByConnectionString(url));
258
					}else{
259
						logger.debug("Unknown property: " + property);
260
					}
261
				}
262
			}
263
		}
264
		return result;	
265
	}
266
	
267
	private String getPropertyValue(Element bean, String property){
268
		Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", property);
269
		if (driverProp == null){
270
			logger.debug("Unknown property: " + property);
271
	    	return null;
272
		}else{
273
			String strProperty = driverProp.getAttributeValue("value");
274
			return strProperty;
275
		}
276
	}
277
	
278

    
279

    
280
	/**
281
	 * Returns the list of properties that are defined in the datasource    
282
	 * @return 
283
	 */
284
	@SuppressWarnings("unchecked")
285
	public List<Attribute> getDatasourceAttributes(){
286
		List<Attribute> result = new ArrayList<Attribute>();
287
		Element bean = getDatasourceBeanXml(this.dataSourceName);
288
		if (bean == null){
289
			return null;
290
		}else{
291
			result = bean.getAttributes();
292
		}
293
		return result;
294
	}	
295

    
296
	/**
297
	 * Returns a defined property of the datasource
298
	 * @return the property of the data source. NULL if the datasource bean or the property does not exist.
299
	 */
300
	public String getDatasourceProperty(DbProperties dbProp){
301
		Element bean = getDatasourceBeanXml(this.dataSourceName);
302
		if (bean == null){
303
			return null;
304
		}else{
305
			Element elProperty = XmlHelp.getFirstAttributedChild(bean, "property", "name", dbProp.toString());
306
			if (elProperty == null){
307
				logger.warn("Unknown property: " + dbProp.toString());
308
		    	return null;
309
			}else{
310
				String strValue = elProperty.getAttributeValue("value");
311
				return strValue;
312
			}
313
		}
314
	}
315

    
316
	
317
	/**
318
	 * Returns the list of properties that are defined in the datasource    
319
	 * @return 
320
	 */
321
	public Properties getDatasourceProperties(){
322
		Properties result = new Properties();
323
		Element bean = getDatasourceBeanXml(this.dataSourceName);
324
		if (bean == null){
325
			return null;
326
		}else{
327
			List<Element> elProperties = XmlHelp.getAttributedChildList(bean, "property", "name");
328
			Iterator<Element> iterator = elProperties.iterator();
329
			while(iterator.hasNext()){
330
				Element next = iterator.next();
331
				String strName = next.getAttributeValue("name");
332
				String strValue = next.getAttributeValue("value");
333
				result.put(strName, strValue);
334
			}
335
		}
336
		return result;
337
	}
338
	
339
	/**
340
	 * Returns a BeanDefinition object of type DataSource that contains
341
	 * datsource properties (url, username, password, ...)
342
	 * @return
343
	 */
344
	@SuppressWarnings("unchecked")
345
	public BeanDefinition getDatasourceBean(){
346
		DatabaseTypeEnum dbtype = DatabaseTypeEnum.getDatabaseEnumByDriverClass(getDatasourceProperty(DbProperties.DRIVER_CLASS));
347
		
348
		AbstractBeanDefinition bd = new RootBeanDefinition(dbtype.getDataSourceClass());
349
		//attributes
350
		Iterator<Attribute> iterator = getDatasourceAttributes().iterator();
351
		while(iterator.hasNext()){
352
			Attribute attribute = iterator.next();
353
			if (attribute.getName().equals("lazy-init")){
354
				bd.setLazyInit(Boolean.valueOf(attribute.getValue()));
355
			}
356
			if (attribute.getName().equals("init-method")){
357
				bd.setInitMethodName(attribute.getValue());
358
			}
359
			if (attribute.getName().equals("destroy-method")){
360
				bd.setDestroyMethodName(attribute.getValue());
361
			}
362
			//Attribute attribute = iterator.next();
363
			//bd.setAttribute(attribute.getName(), attribute.getValue());
364
		}
365
		
366
		//properties
367
		MutablePropertyValues props = new MutablePropertyValues();
368
		Properties persistentProperties = getDatasourceProperties();
369
		Enumeration<String> keys = (Enumeration)persistentProperties.keys(); 
370
		while (keys.hasMoreElements()){
371
			String key = (String)keys.nextElement();
372
			props.addPropertyValue(key, persistentProperties.getProperty(key));
373
		}
374

    
375
		bd.setPropertyValues(props);
376
		return bd;
377
	}
378
	
379
	/**
380
	 * @param hbm2dll
381
	 * @param showSql
382
	 * @return
383
	 */
384
	public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){
385
		boolean showSql = false;
386
		boolean formatSql = false;
387
		boolean registerSearchListener = false;
388
		Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
389
		return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);
390
	}
391
	
392
	
393
	/**
394
	 * @param hbm2dll
395
	 * @param showSql
396
	 * @return
397
	 */
398
	public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Boolean registerSearchListener, Class<? extends RegionFactory> cacheProviderClass){
399
		//Hibernate default values
400
		if (hbm2dll == null){
401
			hbm2dll = DbSchemaValidation.VALIDATE;
402
		}
403
		if (showSql == null){
404
			showSql = false;
405
		}
406
		if (formatSql == null){
407
			formatSql = false;
408
		}
409
		if (cacheProviderClass == null){
410
			cacheProviderClass = NoCachingRegionFactory.class;
411
		}
412
		if(registerSearchListener == null){
413
			registerSearchListener = false;
414
		}
415
				
416
		DatabaseTypeEnum dbtype = getDatabaseType();
417
		AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
418
		MutablePropertyValues hibernateProps = new MutablePropertyValues();
419

    
420
		Properties props = new Properties();
421
		props.setProperty("hibernate.hbm2ddl.auto", hbm2dll.toString());
422
		props.setProperty("hibernate.dialect", dbtype.getHibernateDialect());
423
		props.setProperty("hibernate.cache.region.factory_class", cacheProviderClass.getName());
424
		props.setProperty("hibernate.show_sql", String.valueOf(showSql));
425
		props.setProperty("hibernate.format_sql", String.valueOf(formatSql));
426
		props.setProperty("hibernate.search.autoregister_listeners", String.valueOf(registerSearchListener));
427

    
428
		hibernateProps.addPropertyValue("properties",props);
429
		bd.setPropertyValues(hibernateProps);
430
		return bd;
431
	}
432
	
433
	
434
	/**
435
	 * Tests existing of the datsource in the according config  file.
436
	 * @return true if a datasource with the given name exists in the according datasource config file.
437
	 */
438
	public static boolean exists(String strDataSourceName){
439
		Element bean = getDatasourceBeanXml(strDataSourceName);
440
		return (bean != null);
441
	}
442

    
443
	
444
	/**
445
	 * 
446
	 * @param strDataSourceName
447
	 * @param databaseTypeEnum
448
	 * @param server
449
	 * @param database
450
	 * @param port
451
	 * @param username
452
	 * @param password
453
	 * @param dataSourceClass
454
	 * @param initMethod
455
	 * @param destroyMethod
456
	 * @param startSilent
457
	 * @param startServer
458
	 * @param filePath
459
	 * @param mode
460
	 * @return
461
	 */
462
	private static CdmPersistentDataSource save(String strDataSourceName, 
463
			DatabaseTypeEnum databaseTypeEnum, 
464
			String server, 
465
			String database, 
466
			String port, 
467
			String username, 
468
			String password, 
469
			Class<? extends DataSource> dataSourceClass,
470
			String initMethod,
471
			String destroyMethod,
472
			Boolean startSilent,
473
			Boolean startServer, 
474
			String filePath,
475
			H2Mode mode,
476
			NomenclaturalCode code
477
		){
478
		
479
		int portNumber = "".equals(port) ? databaseTypeEnum.getDefaultPort() : Integer.valueOf(port);
480
		
481
		ICdmDataSource dataSource = new CdmDataSource(databaseTypeEnum, server, database, portNumber, username, password, filePath, mode, code);
482
				
483
		//root
484
		Element root = getBeansRoot(getDataSourceInputStream());
485
		if (root == null){
486
			return null;
487
		}
488
		//bean
489
		Element bean = XmlHelp.getFirstAttributedChild(root, "bean", "id", getBeanName(strDataSourceName));
490
		if (bean != null){
491
			bean.detach();  //delete old version if necessary
492
		}
493
		bean = insertXmlBean(root, getBeanName(strDataSourceName), dataSourceClass.getName());
494
		//attributes
495
		bean.setAttribute("lazy-init", "true");
496
		if (initMethod != null) {bean.setAttribute("init-method", initMethod);}
497
		if (destroyMethod != null) {bean.setAttribute("destroy-method", destroyMethod);}
498
		
499
		//set properties
500
		insertXmlValueProperty(bean, "driverClassName", databaseTypeEnum.getDriverClassName());
501
		
502
		insertXmlValueProperty(bean, "url", databaseTypeEnum.getConnectionString(dataSource));
503
		if (username != null) {insertXmlValueProperty(bean, "username", username );}
504
		if (password != null) {insertXmlValueProperty(bean, "password", password );}
505
		if (startSilent != null) {insertXmlValueProperty(bean, "startSilent", startSilent.toString() );}
506
		if (startServer != null) {insertXmlValueProperty(bean, "startServer", startServer.toString() );}
507
		if (filePath != null) {insertXmlValueProperty(bean, "filePath", filePath );}
508
		if (mode != null) {insertXmlValueProperty(bean, "mode", mode.toString() );}
509
		if (code != null) {insertXmlValueProperty(bean, "nomenclaturalCode", code.name());}
510
		
511
		//save
512
		saveToXml(root.getDocument(), getResourceDirectory(), DATASOURCE_FILE_NAME, format );
513
		try {
514
			return NewInstance(strDataSourceName) ;
515
		} catch (DataSourceNotFoundException e) {
516
			logger.error("Error when saving datasource");
517
			return null;
518
		}
519
	}
520
	
521
	/**
522
	 * @param strDataSourceName
523
	 * @param dataSource
524
	 * @param code 
525
	 * @return
526
	 * 			the updated dataSource, null if not succesful
527
	 */
528
	public static CdmPersistentDataSource update(String strDataSourceName,
529
			ICdmDataSource dataSource) throws DataSourceNotFoundException, IllegalArgumentException{
530
		delete(CdmPersistentDataSource.NewInstance(strDataSourceName));
531
		return save(strDataSourceName, dataSource);
532
	}
533

    
534
	/**
535
	 * Saves a datasource to the datasource config file. If strDataSourceName differs a new dataSource
536
	 * will be created in config file. Use update() of real update functionality.
537
	 * 
538
	 * @param strDataSourceName
539
	 * @param dataSource
540
	 * @return
541
	 */
542
	public static CdmPersistentDataSource save(String strDataSourceName,
543
			ICdmDataSource dataSource)  throws IllegalArgumentException{
544
		
545
		if(dataSource.getDatabaseType() == null){
546
			new IllegalArgumentException("Database type not specified");
547
		}
548
		
549
		if(dataSource.getDatabaseType().equals(DatabaseTypeEnum.H2)){
550
			Class<? extends DataSource> dataSourceClass =  LocalH2.class;
551
			if(dataSource.getMode() == null){
552
				new IllegalArgumentException("H2 mode not specified");
553
			}
554
			return save(
555
					strDataSourceName, 
556
					dataSource.getDatabaseType(), 
557
					"localhost", 
558
					getCheckedDataSourceParameter(dataSource.getDatabase()), 
559
					dataSource.getDatabaseType().getDefaultPort() + "", 
560
					getCheckedDataSourceParameter(dataSource.getUsername()), 
561
					getCheckedDataSourceParameter(dataSource.getPassword()), 
562
					dataSourceClass, 
563
					null, null, null, null, 
564
					getCheckedDataSourceParameter(dataSource.getFilePath()), 
565
					dataSource.getMode(),
566
					dataSource.getNomenclaturalCode());
567
		}else{
568
			
569
			Class<? extends DataSource> dataSourceClass;
570
			try {
571
				dataSourceClass = (Class<? extends DataSource>) Class.forName(dataSourceClassName);
572
				
573
				CdmPersistentDataSource persistendDatasource =  save(
574
					strDataSourceName, 
575
					dataSource.getDatabaseType(), 
576
					getCheckedDataSourceParameter(dataSource.getServer()), 
577
					getCheckedDataSourceParameter(dataSource.getDatabase()), 
578
					dataSource.getPort() + "", 
579
					getCheckedDataSourceParameter(dataSource.getUsername()), 
580
					getCheckedDataSourceParameter(dataSource.getPassword()), 
581
					dataSourceClass, 
582
					null, null, null, null, null, null,
583
					dataSource.getNomenclaturalCode());
584
				
585
				return persistendDatasource;
586
			} catch (ClassNotFoundException e) {
587
				logger.error("DataSourceClass not found - stopping application", e);
588
				System.exit(-1);
589
			}
590
			// will never be reached
591
			return null;
592
		}
593
	}
594

    
595
	private static String getCheckedDataSourceParameter(String parameter) throws IllegalArgumentException{		
596
		if(parameter != null){
597
			return parameter;
598
		}else{
599
			new IllegalArgumentException("Non obsolete paramater was assigned a null value: " + parameter);
600
			return null;
601
		}
602
	}
603

    
604
	/**
605
	 * Deletes a dataSource
606
	 * @param dataSource
607
	 */
608
	public static void delete (CdmPersistentDataSource dataSource){
609
		Element bean = getDatasourceBeanXml(dataSource.getName());
610
		if (bean != null){
611
			Document doc = bean.getDocument();
612
			bean.detach();
613
			saveToXml(doc, getDataSourceOutputStream(), format );
614
		}
615
	}
616
	
617
	
618
	/**
619
	 * Returns a list of all datasources stored in the datasource config file
620
	 * @return all existing data sources
621
	 */
622
	@SuppressWarnings("unchecked")
623
	static public List<CdmPersistentDataSource> getAllDataSources(){
624
		List<CdmPersistentDataSource> dataSources = new ArrayList<CdmPersistentDataSource>();
625
		
626
		Element root = getBeansRoot(getDataSourceInputStream());
627
		if (root == null){
628
			return null;
629
		}else{
630
	    	List<Element> lsChildren  = root.getChildren("bean", root.getNamespace());
631
	    	
632
	    	for (Element elBean : lsChildren){
633
	    		String strId = elBean.getAttributeValue("id");
634
	    		if (strId != null && strId.endsWith(DATASOURCE_BEAN_POSTFIX)){
635
	    			strId = strId.replace(DATASOURCE_BEAN_POSTFIX, "");
636
	    			dataSources.add(new CdmPersistentDataSource(strId));
637
	    		}
638
	    	}
639
		}
640
		return dataSources;
641
	}
642
	
643
	public String getUsername(){
644
		return getDatasourceProperty(DbProperties.USERNAME);
645
	}
646
	
647
	public String getPassword(){
648
		return getDatasourceProperty(DbProperties.PASSWORD);
649
	}
650

    
651

    
652
	/* (non-Javadoc)
653
	 * @see java.lang.Object#toString()
654
	 */
655
	public String toString(){
656
		if (this.dataSourceName != null){
657
			return dataSourceName;
658
		}else{
659
			return null;
660
		}
661
	}
662

    
663

    
664
	
665
	/**
666
	 * Returns the datasource config file input stream.
667
	 * @return data source config file input stream
668
	 */
669
	static protected FileInputStream getDataSourceInputStream(){
670
		String dir = getResourceDirectory();
671
		File file = new File(dir + File.separator +  DATASOURCE_FILE_NAME);
672
		return fileInputStream(file);
673
	}
674
	
675
	
676
	/**
677
	 * Returns the datasource config file outputStream.
678
	 * @return data source config file outputStream
679
	 */
680
	static protected FileOutputStream getDataSourceOutputStream(){
681
		String dir = getResourceDirectory();
682
		File file = new File(dir + File.separator +  DATASOURCE_FILE_NAME);
683
		return fileOutputStream(file);
684
	}
685

    
686
	/**
687
	 * Returns the jdom Element representing the data source bean in the config file.
688
	 * @return
689
	 */
690
	private static Element getDatasourceBeanXml(String strDataSourceName){
691
		FileInputStream inStream = getDataSourceInputStream();
692
		Element root = getBeansRoot(inStream);
693
		if (root == null){
694
			return null;
695
		}else{
696
	    	Element xmlBean = XmlHelp.getFirstAttributedChild(root, "bean", "id", getBeanName(strDataSourceName));
697
			if (xmlBean == null){
698
				//TODO warn or info
699
				logger.debug("Unknown Element 'bean id=" +strDataSourceName + "' ");
700
			}
701
			return xmlBean;
702
		}
703
	}
704
	
705
	// returns the directory containing the resources 
706
	private static String getResourceDirectory(){
707
		try {
708
			File f = CdmApplicationUtils.getWritableResourceDir();
709
			return f.getPath();
710
		} catch (IOException e) {
711
			logger.error(e);
712
			throw new RuntimeException(e);
713
		}
714
	}
715
	
716
	static private FileInputStream fileInputStream(File file){
717
		try {
718
			FileInputStream fis = new FileInputStream(file);
719
			return fis;
720
		} catch (FileNotFoundException e) {
721
			logger.warn("File " + file == null?"null":file.getAbsolutePath() + " does not exist in the file system");
722
			return null;
723
		}
724
	}
725
	
726
	static private FileOutputStream fileOutputStream(File file){
727
		try {
728
			FileOutputStream fos = new FileOutputStream(file);
729
			return fos;
730
		} catch (FileNotFoundException e) {
731
			logger.warn("File " + (file == null?"null":file.getAbsolutePath()) + " does not exist in the file system");
732
			return null;
733
		}
734
	}
735
	
736
	public boolean equals(Object obj){
737
		if (obj == null){
738
			return false;
739
		}else if (! CdmPersistentDataSource.class.isAssignableFrom(obj.getClass())){
740
			return false;
741
		}else{
742
			CdmPersistentDataSource dataSource = (CdmPersistentDataSource)obj;
743
			return (this.dataSourceName == dataSource.dataSourceName);
744
		}
745

    
746
	}
747
}
(3-3/20)