Schema update for referenceType persistence #2615
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / CdmDataSource.java
index 943a4efeb403e3a9175af9624b6ed9e3cc4f1b52..3ab21af96cbcc8dd7ca6a4ba4b17ce972cc15a45 100644 (file)
+/**\r
+* Copyright (C) 2007 EDIT\r
+* European Distributed Institute of Taxonomy\r
+* http://www.e-taxonomy.eu\r
+*\r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/\r
+\r
 package eu.etaxonomy.cdm.database;\r
 \r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileOutputStream;\r
-import java.io.FilenameFilter;\r
-import java.util.ArrayList;\r
 import java.util.Enumeration;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
 import java.util.Properties;\r
 \r
+import org.apache.commons.lang.StringUtils;\r
 import org.apache.log4j.Logger;\r
-import org.hibernate.cache.CacheProvider;\r
-import org.hibernate.cache.NoCacheProvider;\r
-import org.jdom.Attribute;\r
-import org.jdom.Document;\r
-import org.jdom.Element;\r
-import org.jdom.output.Format;\r
+import org.hibernate.cache.internal.NoCachingRegionFactory;\r
+import org.hibernate.cache.spi.RegionFactory;\r
+import org.hibernate.cfg.Environment;\r
 import org.springframework.beans.MutablePropertyValues;\r
 import org.springframework.beans.factory.config.BeanDefinition;\r
 import org.springframework.beans.factory.config.PropertiesFactoryBean;\r
 import org.springframework.beans.factory.support.AbstractBeanDefinition;\r
 import org.springframework.beans.factory.support.RootBeanDefinition;\r
-import org.springframework.jdbc.datasource.DriverManagerDataSource;\r
 \r
-import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;\r
 import eu.etaxonomy.cdm.common.CdmUtils;\r
-import eu.etaxonomy.cdm.common.XmlHelp;\r
-\r
-import static eu.etaxonomy.cdm.common.XmlHelp.getFirstAttributedChild;\r
-import static eu.etaxonomy.cdm.common.XmlHelp.getOrAddChild;\r
-import static eu.etaxonomy.cdm.common.XmlHelp.getRoot;\r
-import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlBean;\r
-import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlRefProperty;\r
-import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlValueProperty;\r
-import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;\r
-\r
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;\r
 \r
 /**\r
- * class to access an CdmDataSource\r
+ * @author a.mueller\r
+ *\r
  */\r
-public class CdmDataSource {\r
+public class CdmDataSource extends CdmDataSourceBase {\r
+       @SuppressWarnings("unused")\r
        private static final Logger logger = Logger.getLogger(CdmDataSource.class);\r
-       \r
-       public static final String DATASOURCE_BEAN_POSTFIX = "DataSource";\r
-       public final static String DATASOURCE_FILE_NAME = "cdm.datasources.xml";\r
-       private final static Format format = Format.getPrettyFormat(); \r
-\r
-       public enum DbProperties{\r
-               DRIVER_CLASS,\r
-               URL,\r
-               USERNAME,\r
-               PASSWORD;\r
-\r
-               @Override\r
-               public String toString(){\r
-                       switch (this){\r
-                               case DRIVER_CLASS:\r
-                                       return "driverClassName";\r
-                               case URL:\r
-                                       return "url";\r
-                               case USERNAME:\r
-                                       return "username";\r
-                               case PASSWORD:\r
-                                       return "password";\r
-                               default: \r
-                                       throw new IllegalArgumentException( "Unknown enumeration type" );\r
-                       }\r
-               }\r
+\r
+       private DatabaseTypeEnum dbType;\r
+       private String server;\r
+       private String database;\r
+       private int port = -1;\r
+       private String username;\r
+       private String password;\r
+       private NomenclaturalCode nomenclaturalCode;\r
+\r
+       private String filePath;\r
+       private H2Mode mode;\r
+\r
+       private boolean isLazy = true;\r
+       private String initMethodName = null;\r
+       private String destroyMethodName = null;\r
+       private DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;\r
+       private boolean showSql = false;\r
+       private boolean formatSql = false;\r
+       private boolean registerSearchListener = false;\r
+       private Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;\r
+\r
+       public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, String username, String password){\r
+               return new CdmDataSource(dbType, server, database, -1, username, password, null, null, null);\r
        }\r
-       \r
-       public enum HBM2DDL{\r
-               VALIDATE,\r
-               UPDATE,\r
-               CREATE,\r
-               CREATE_DROP;\r
-\r
-               @Override\r
-               public String toString(){\r
-                       switch (this){\r
-                               case VALIDATE:\r
-                                       return "validate";\r
-                               case UPDATE:\r
-                                       return "update";\r
-                               case CREATE:\r
-                                       return "create";\r
-                               case CREATE_DROP:\r
-                                       return "create-drop";\r
-                               default: \r
-                                       throw new IllegalArgumentException( "Unknown enumeration type" );\r
-                       }\r
-               }\r
+\r
+       public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password){\r
+               return new CdmDataSource(dbType, server, database, port, username, password, null, null, null);\r
        }\r
-       \r
-       //name\r
-       protected String dataSourceName;\r
 \r
-       \r
-       /**\r
-        * Returns the default CdmDataSource\r
-        * @return the default CdmDataSource\r
-        */\r
-       public final static CdmDataSource NewDefaultInstance(){\r
-               try {\r
-                       return NewInstance("default");\r
-               } catch (DataSourceNotFoundException e) {\r
-                       logger.error("Default datasource does not exist in config file");\r
-                       return null;\r
-               }\r
+       public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, String username, String password , NomenclaturalCode code){\r
+               return new CdmDataSource(dbType, server, database, -1, username, password, null, null, code);\r
        }\r
-       \r
-       \r
-       /**\r
-        * Returns the default CdmDataSource\r
-        * @return the default CdmDataSource\r
-        */\r
-       public final static CdmDataSource NewLocalHsqlInstance(){\r
-               try {\r
-                       return NewInstance("localDefaultHsql");\r
-               } catch (DataSourceNotFoundException e) {\r
-                       logger.error("Local datasource does not exist in config file");\r
-                       return null;\r
-               }\r
+\r
+       public static CdmDataSource NewInstance(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password , NomenclaturalCode code){\r
+               return new CdmDataSource(dbType, server, database, port, username, password, null, null, code);\r
        }\r
-       \r
-       /**\r
-        * Returns the CdmDataSource named by strDataSource\r
-        * @param strDataSource\r
-        * @return\r
-        */\r
-       public final static CdmDataSource NewInstance(String dataSourceName) \r
-                               throws DataSourceNotFoundException{\r
-               if (exists(dataSourceName)){\r
-                       return new CdmDataSource(dataSourceName);\r
-               }else{\r
-                       throw new DataSourceNotFoundException("Datasource not found: " + dataSourceName);\r
-               }\r
+\r
+\r
+       static public CdmDataSource  NewMySqlInstance(String server, String database, String username, String password ){\r
+               return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null, null);\r
        }\r
 \r
-       /**\r
-        * Private Constructor. Use NewXXX factory methods for creating a new instance of CdmDataSource!\r
-        * @param strDataSource\r
-        */\r
-       private CdmDataSource(String strDataSource){\r
-               dataSourceName = strDataSource;\r
+       static public CdmDataSource  NewMySqlInstance(String server, String database, String username, String password , NomenclaturalCode code){\r
+               return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, -1, username, password, null, null, code);\r
        }\r
-       \r
-       /**\r
-        * Returns the name of the bean.\r
-        * @return\r
-        */\r
-       public String getName(){\r
-               return dataSourceName;\r
+\r
+       static public CdmDataSource  NewMySqlInstance(String server, String database, int port, String username, String password, NomenclaturalCode code){\r
+               return new CdmDataSource(DatabaseTypeEnum.MySQL, server, database, port, username, password, null, null, code);\r
        }\r
-       \r
-       \r
-       /**\r
-        * Returns the name of the bean Element in the xml config file.\r
-        * @return bean name\r
-        */\r
-       private static String getBeanName(String name){\r
-               return name == null? null : name + DATASOURCE_BEAN_POSTFIX;\r
+\r
+       static public CdmDataSource  NewPostgreSQLInstance(String server, String database, int port, String username, String password, NomenclaturalCode code){\r
+               return new CdmDataSource(DatabaseTypeEnum.PostgreSQL, server, database, port, username, password, null, null, code);\r
        }\r
 \r
+       static public CdmDataSource  NewSqlServer2005Instance(String server, String database, int port, String username, String password, NomenclaturalCode code){\r
+               return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null, code);\r
+       }\r
 \r
-       \r
-       /**\r
-        * Returns the database type of the data source. \r
-        * @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.\r
-        */\r
-       public DatabaseTypeEnum getDatabaseType(){\r
-               Element bean = getDatasourceBeanXml(this.dataSourceName);\r
-               if (bean == null){\r
-                       return null;\r
-               }else{\r
-                       Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", "driverClassName");\r
-                       if (driverProp == null){\r
-                               logger.warn("Unknown property driverClass");\r
-                       return null;\r
-                       }else{\r
-                               String strDriverClass = driverProp.getAttributeValue("value");\r
-                               DatabaseTypeEnum dbType = DatabaseTypeEnum.getDatabaseEnumByDriverClass(strDriverClass);\r
-                               return dbType;\r
-                       }\r
-               }\r
+       static public CdmDataSource  NewSqlServer2005Instance(String server, String database, int port, String username, String password /*, NomenclaturalCode code*/){\r
+               return new CdmDataSource(DatabaseTypeEnum.SqlServer2005, server, database, port, username, password, null, null, null);\r
+       }\r
+\r
+\r
+       /** in work\r
+        * @param code TODO*/\r
+       static public CdmDataSource  NewH2EmbeddedInstance(String database, String username, String password){\r
+               return NewH2EmbeddedInstance(database, username, password, null,  null);\r
+       }\r
+\r
+       /** in work\r
+        * @param code TODO*/\r
+       static public CdmDataSource  NewH2EmbeddedInstance(String database, String username, String password, NomenclaturalCode code){\r
+               return NewH2EmbeddedInstance(database, username, password, null, code);\r
+       }\r
+\r
+       /** in work\r
+        * @param code TODO*/\r
+       static public CdmDataSource  NewH2EmbeddedInstance(String database, String username, String password, String filePath, NomenclaturalCode code){\r
+               //FIXME in work\r
+               int port = -1;\r
+               H2Mode mode = H2Mode.EMBEDDED;\r
+               CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, null, mode, code);\r
+               return dataSource;\r
+       }\r
+\r
+       /** in work */\r
+       static public CdmDataSource  NewH2InMemoryInstance(){\r
+               //FIXME in work\r
+               int port = -1;\r
+               H2Mode mode = H2Mode.IN_MEMORY;\r
+               String username = "sa";\r
+               String password = "";\r
+               CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode, null);\r
+               return dataSource;\r
        }\r
 \r
-       \r
 \r
        /**\r
-        * Returns a defined property of the datasource\r
-        * @return the property of the data source. NULL if the datasource bean or the property does not exist.\r
+        * @param server\r
+        * @param database\r
+        * @param port\r
         */\r
-       public String getDbProperty(DbProperties dbProp){\r
-               Element bean = getDatasourceBeanXml(this.dataSourceName);\r
-               if (bean == null){\r
-                       return null;\r
-               }else{\r
-                       Element elProperty = XmlHelp.getFirstAttributedChild(bean, "property", "name", dbProp.toString());\r
-                       if (elProperty == null){\r
-                               logger.warn("Unknown property: " + dbProp.toString());\r
-                       return null;\r
-                       }else{\r
-                               String strValue = elProperty.getAttributeValue("value");\r
-                               return strValue;\r
-                       }\r
-               }\r
+       protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode, NomenclaturalCode code) {\r
+               super();\r
+               this.dbType = dbType;\r
+               this.server = server;\r
+               this.database = database;\r
+               this.port = port;\r
+               this.username = username;\r
+               this.password = password;\r
+               this.initMethodName = dbType.getInitMethod();\r
+               this.destroyMethodName = dbType.getDestroyMethod();\r
+               this.filePath = filePath;\r
+               this.mode = mode;\r
+               this.nomenclaturalCode = code;\r
        }\r
-       \r
-       \r
-       /**\r
-        * Returns the list of properties that are defined in the datasource    \r
-        * @return \r
+\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()\r
+        * A CdmDataSource does not have a name representation therefor the database name is returned\r
         */\r
-       public Properties getDbProperties(){\r
-               Properties result = new Properties();\r
-               Element bean = getDatasourceBeanXml(this.dataSourceName);\r
-               if (bean == null){\r
-                       return null;\r
-               }else{\r
-                       List<Element> elProperties = XmlHelp.getAttributedChildList(bean, "property", "name");\r
-                       Iterator<Element> iterator = elProperties.iterator();\r
-                       while(iterator.hasNext()){\r
-                               Element next = iterator.next();\r
-                               String strName = next.getAttributeValue("name");\r
-                               String strValue = next.getAttributeValue("value");\r
-                               result.put(strName, strValue);\r
-                       }\r
-               }\r
-               return result;\r
+       public String getName() {\r
+               return database;\r
        }\r
-       \r
-       /**\r
-        * Returns a BeanDefinition object of type  DriverManagerDataSource that contains\r
-        * datsource properties (url, username, password, ...)\r
-        * @return\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()\r
         */\r
        public BeanDefinition getDatasourceBean(){\r
-               DatabaseTypeEnum dbtype = DatabaseTypeEnum.getDatabaseEnumByDriverClass(getDbProperty(DbProperties.DRIVER_CLASS));\r
-               \r
-               AbstractBeanDefinition bd = new RootBeanDefinition(dbtype.getDriverManagerDataSourceClass());\r
-               \r
+               AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDataSourceClass());\r
+               //attributes\r
+               bd.setLazyInit(isLazy);\r
+               if (! CdmUtils.Nz(initMethodName).trim().equals("") ){\r
+                       bd.setInitMethodName(initMethodName);\r
+               }\r
+               if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){\r
+                       bd.setInitMethodName(destroyMethodName);\r
+               }\r
+\r
+               //properties\r
                MutablePropertyValues props = new MutablePropertyValues();\r
-               Properties persistentProperties = getDbProperties();\r
-               Enumeration<String> keys = (Enumeration)persistentProperties.keys(); \r
+               Properties persistentProperties = getDatasourceProperties();\r
+               Enumeration<Object> keys = (Enumeration<Object>)persistentProperties.keys();\r
                while (keys.hasMoreElements()){\r
                        String key = (String)keys.nextElement();\r
                        props.addPropertyValue(key, persistentProperties.getProperty(key));\r
+                       Properties a = Environment.getProperties();\r
                }\r
-//             props.addPropertyValue("driverClassName", dbtype.getDriverClassName());\r
-//             props.addPropertyValue("url", getDbProperty(DbProperties.URL));\r
-//             props.addPropertyValue("username", getDbProperty(DbProperties.USERNAME));\r
-//             props.addPropertyValue("password", getDbProperty(DbProperties.PASSWORD));\r
+\r
                bd.setPropertyValues(props);\r
                return bd;\r
        }\r
-       \r
+\r
        /**\r
-        * @param hbm2dll\r
-        * @param showSql\r
+        * Returns the list of properties that are defined in the datasource\r
         * @return\r
         */\r
-       public BeanDefinition getHibernatePropertiesBean(HBM2DDL hbm2dll){\r
+       private Properties getDatasourceProperties(){\r
+               Properties result = new Properties();\r
+               result.put("driverClassName", dbType.getDriverClassName());\r
+               String connectionString = dbType.getConnectionString(this);\r
+               result.put("url", connectionString);\r
+               result.put("username", username);\r
+               result.put("password", password);\r
+               return result;\r
+       }\r
+\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)\r
+        */\r
+       public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll){\r
                boolean showSql = false;\r
                boolean formatSql = false;\r
-               Class<? extends CacheProvider> cacheProviderClass = NoCacheProvider.class;\r
-               return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, cacheProviderClass);\r
+               boolean registerSearchListener = false;\r
+               Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;\r
+               return getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);\r
        }\r
-       \r
-       \r
-       /**\r
-        * @param hbm2dll\r
-        * @param showSql\r
-        * @return\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)\r
         */\r
-       public BeanDefinition getHibernatePropertiesBean(HBM2DDL hbm2dll, Boolean showSql, Boolean formatSql, Class<? extends CacheProvider> cacheProviderClass){\r
+       public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Boolean registerSearchListener, Class<? extends RegionFactory> cacheProviderClass){\r
                //Hibernate default values\r
                if (hbm2dll == null){\r
-                       hbm2dll = HBM2DDL.VALIDATE;\r
+                       hbm2dll = this.hbm2dll;\r
                }\r
                if (showSql == null){\r
-                       showSql = false;\r
+                       showSql = this.showSql;\r
                }\r
                if (formatSql == null){\r
-                       formatSql = false;\r
+                       formatSql = this.formatSql;\r
                }\r
                if (cacheProviderClass == null){\r
-                       cacheProviderClass = NoCacheProvider.class;\r
+                       cacheProviderClass = this.cacheProviderClass;\r
                }\r
-               \r
-               DatabaseTypeEnum dbtype = getDatabaseType();\r
+               if(registerSearchListener == null){\r
+                       registerSearchListener = this.registerSearchListener;\r
+               }\r
+\r
+               DatabaseTypeEnum dbtype = dbType;\r
                AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);\r
                MutablePropertyValues hibernateProps = new MutablePropertyValues();\r
 \r
                Properties props = new Properties();\r
                props.setProperty("hibernate.hbm2ddl.auto", hbm2dll.toString());\r
                props.setProperty("hibernate.dialect", dbtype.getHibernateDialect());\r
-               props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());\r
+//             OLD:props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());\r
+               props.setProperty("hibernate.cache.region.factory_class", cacheProviderClass.getName());\r
                props.setProperty("hibernate.show_sql", String.valueOf(showSql));\r
                props.setProperty("hibernate.format_sql", String.valueOf(formatSql));\r
+               props.setProperty("hibernate.search.autoregister_listeners", String.valueOf(registerSearchListener));\r
 \r
                hibernateProps.addPropertyValue("properties",props);\r
                bd.setPropertyValues(hibernateProps);\r
                return bd;\r
        }\r
-       \r
-       \r
-       /**\r
-        * Tests existing of the datsource in the according config  file.\r
-        * @return true if a datasource with the given name exists in the according datasource config file.\r
-        */\r
-       public static boolean exists(String strDataSourceName){\r
-               Element bean = getDatasourceBeanXml(strDataSourceName);\r
-               return (bean != null);\r
+\r
+       public String getInitMethodName() {\r
+               return initMethodName;\r
        }\r
 \r
-       \r
-       /**\r
-        * Saves or updates the datasource to the datasource config file.\r
-        * Uses default port.\r
-        * @param strDataSourceName name of the datasource (without postfix DataSource)\r
-        * @param databaseTypeEnum\r
-        * @param server\r
-        * @param database\r
-        * @param username\r
-        * @param password\r
-        * @return the CdmDataSource, null if not successful.\r
-        */\r
-       public static CdmDataSource save(String strDataSourceName, DatabaseTypeEnum databaseTypeEnum, String server, String database, \r
-                       String username, String password){\r
-               return save(strDataSourceName, databaseTypeEnum, server, database, \r
-                               databaseTypeEnum.getDefaultPort(), username, password);\r
+       public void setInitMethodName(String initMethodName) {\r
+               this.initMethodName = initMethodName;\r
        }\r
-       \r
-       /**\r
-        * Saves or updates the datasource to the datasource config file.\r
-        * @param strDataSourceName name of the datasource (without postfix DataSource)\r
-        * @param databaseTypeEnum\r
-        * @param server\r
-        * @param database\r
-        * @param port\r
-        * @param username\r
-        * @param password\r
-        * @return the CdmDataSource, null if not successful.\r
-        */\r
-       public static CdmDataSource save(String strDataSourceName, DatabaseTypeEnum databaseTypeEnum, String server, String database, \r
-                               int port, String username, String password){\r
-               Class<? extends DriverManagerDataSource> driverManagerDataSource =  DriverManagerDataSource.class;\r
-               return save(strDataSourceName, databaseTypeEnum, server, database, port, username, password, driverManagerDataSource, null, null, null, null, null);\r
+\r
+       public String getDestroyMethodName() {\r
+               return destroyMethodName;\r
        }\r
-       \r
-       \r
-       public static CdmDataSource saveLocalHsqlDb(String strDataSourceName, String databasePath, String databaseName, String username, String password){\r
-               DatabaseTypeEnum databaseTypeEnum = DatabaseTypeEnum.HSqlDb;\r
-               Class<? extends DriverManagerDataSource> driverManagerDataSource =  LocalHsqldb.class;\r
-               String server = "localhost";\r
-               int port = databaseTypeEnum.getDefaultPort();\r
-               return save(strDataSourceName, databaseTypeEnum, server, databaseName, port, username, password, driverManagerDataSource, "init", "destroy", true, true, databasePath);\r
+\r
+       public void setDestroyMethodName(String destroyMethodName) {\r
+               this.destroyMethodName = destroyMethodName;\r
        }\r
-       \r
-       //\r
-       private static CdmDataSource save(String strDataSourceName, \r
-                       DatabaseTypeEnum databaseTypeEnum, \r
-                       String server, \r
-                       String database, \r
-                       int port, \r
-                       String username, \r
-                       String password, \r
-                       Class<? extends DriverManagerDataSource> driverManagerDataSource,\r
-                       String initMethod,\r
-                       String destroyMethod,\r
-                       Boolean startSilent,\r
-                       Boolean startServer, \r
-                       String databasePath\r
-               ){\r
-               //root\r
-               Element root = getRoot(getDataSourceInputStream());\r
-               if (root == null){\r
-                       return null;\r
-               }\r
-               //bean\r
-               Element bean = XmlHelp.getFirstAttributedChild(root, "bean", "id", getBeanName(strDataSourceName));\r
-               if (bean != null){\r
-                       bean.detach();  //delete old version if necessary\r
-               }\r
-               bean = insertXmlBean(root, getBeanName(strDataSourceName), driverManagerDataSource.getName());\r
-               //attributes\r
-               bean.setAttribute("lazy-init", "true");\r
-               if (initMethod != null) {bean.setAttribute("init-method", initMethod);}\r
-               if (destroyMethod != null) {bean.setAttribute("destroy-method", destroyMethod);}\r
-               \r
-               //set properties\r
-               insertXmlValueProperty(bean, "driverClassName", databaseTypeEnum.getDriverClassName());\r
-               insertXmlValueProperty(bean, "url", databaseTypeEnum.getConnectionString(server, database, port));\r
-               if (username != null) {insertXmlValueProperty(bean, "username", username );}\r
-               if (password != null) {insertXmlValueProperty(bean, "password", password );}\r
-               if (startSilent != null) {insertXmlValueProperty(bean, "startSilent", startSilent.toString() );}\r
-               if (startServer != null) {insertXmlValueProperty(bean, "startServer", startServer.toString() );}\r
-               if (startServer != null) {insertXmlValueProperty(bean, "databasePath", databasePath );}\r
-               \r
-               //save\r
-               saveToXml(root.getDocument(), getResourceDirectory(), DATASOURCE_FILE_NAME, format );\r
-               try {\r
-                       return NewInstance(strDataSourceName) ;\r
-               } catch (DataSourceNotFoundException e) {\r
-                       logger.error("Error when saving datasource");\r
-                       return null;\r
-               }\r
+\r
+       public String getDatabase() {\r
+               return database;\r
        }\r
-       \r
-       \r
-       /**\r
-        * Deletes a dataSource\r
-        * @param dataSource\r
-        */\r
-       public static void delete (CdmDataSource dataSource){\r
-               Element bean = getDatasourceBeanXml(dataSource.getName());\r
-               if (bean != null){\r
-                       Document doc = bean.getDocument();\r
-                       bean.detach();\r
-                       saveToXml(doc, getDataSourceOutputStream(), format );\r
-               }\r
+\r
+       public DatabaseTypeEnum getDatabaseType() {\r
+               return dbType;\r
        }\r
-       \r
-       \r
-       /**\r
-        * Returns a list of all datasources stored in the datasource config file\r
-        * @return all existing data sources\r
-        */\r
-       static public List<CdmDataSource> getAllDataSources(){\r
-               List<CdmDataSource> dataSources = new ArrayList<CdmDataSource>();\r
-               \r
-               Element root = getRoot(getDataSourceInputStream());\r
-               if (root == null){\r
-                       return null;\r
-               }else{\r
-               List<Element> lsChildren  = root.getChildren("bean", root.getNamespace());\r
-               \r
-               for (Element elBean : lsChildren){\r
-                       String strId = elBean.getAttributeValue("id");\r
-                       if (strId != null && strId.endsWith(DATASOURCE_BEAN_POSTFIX)){\r
-                               strId = strId.replace(DATASOURCE_BEAN_POSTFIX, "");\r
-                               dataSources.add(new CdmDataSource(strId));\r
-                       }\r
-               }\r
-               }\r
-               return dataSources;\r
+\r
+       public String getFilePath() {\r
+               return filePath;\r
        }\r
-       \r
-       \r
-       /* (non-Javadoc)\r
-        * @see java.lang.Object#toString()\r
-        */\r
-       public String toString(){\r
-               if (this.dataSourceName != null){\r
-                       return dataSourceName;\r
-               }else{\r
-                       return null;\r
-               }\r
+\r
+\r
+       public int getPort() {\r
+               return port;\r
        }\r
 \r
+       public String getServer() {\r
+               return server;\r
+       }\r
 \r
-       \r
-       /**\r
-        * Returns the datasource config file input stream.\r
-        * @return data source config file input stream\r
-        */\r
-       static protected FileInputStream getDataSourceInputStream(){\r
-               String dir = getResourceDirectory();\r
-               File file = new File(dir + File.separator +  DATASOURCE_FILE_NAME);\r
-               return fileInputStream(file);\r
+       public H2Mode getMode() {\r
+               return mode;\r
        }\r
-       \r
-       \r
-       /**\r
-        * Returns the datasource config file outputStream.\r
-        * @return data source config file outputStream\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()\r
         */\r
-       static protected FileOutputStream getDataSourceOutputStream(){\r
-               String dir = getResourceDirectory();\r
-               File file = new File(dir + File.separator +  DATASOURCE_FILE_NAME);\r
-               return fileOutputStream(file);\r
+       public String getPassword() {\r
+               return password;\r
        }\r
 \r
-       /**\r
-        * Returns the jdom Element representing the data source bean in the config file.\r
-        * @return\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()\r
         */\r
-       private static Element getDatasourceBeanXml(String strDataSourceName){\r
-               Element root = getRoot(getDataSourceInputStream());\r
-               if (root == null){\r
-                       return null;\r
-               }else{\r
-               Element xmlBean = XmlHelp.getFirstAttributedChild(root, "bean", "id", getBeanName(strDataSourceName));\r
-                       if (xmlBean == null){\r
-                               //TODO warn or info\r
-                               logger.debug("Unknown Element 'bean id=" +strDataSourceName + "' ");\r
-                       };\r
-                       return xmlBean;\r
-               }\r
-       }\r
-       \r
-       // returns the directory containing the resources \r
-       private static String getResourceDirectory(){\r
-               File f = CdmApplicationUtils.getWritableResourceDir();\r
-               return f.getPath();\r
+       public String getUsername() {\r
+               return username;\r
        }\r
-       \r
-       static private FileInputStream fileInputStream(File file){\r
-               try {\r
-                       FileInputStream fis = new FileInputStream(file);\r
-                       return fis;\r
-               } catch (FileNotFoundException e) {\r
-                       logger.warn("File " + file == null?"null":file.getAbsolutePath() + " does not exist in the file system");\r
-                       return null;\r
-               }\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.database.ICdmDataSource#getNomenclaturalCode()\r
+        */\r
+       public NomenclaturalCode getNomenclaturalCode() {\r
+               return nomenclaturalCode;\r
        }\r
-       \r
-       static private FileOutputStream fileOutputStream(File file){\r
-               try {\r
-                       FileOutputStream fos = new FileOutputStream(file);\r
-                       return fos;\r
-               } catch (FileNotFoundException e) {\r
-                       logger.warn("File " + (file == null?"null":file.getAbsolutePath()) + " does not exist in the file system");\r
-                       return null;\r
+\r
+       @Override\r
+       public String toString() {\r
+               if (StringUtils.isBlank(this.database)){\r
+                       return super.toString();\r
+               }else{\r
+                       String result = "DataSource<" + dbType.getConnectionString(this).replace(CdmUtils.Nz(password), "") + ">";\r
+                       return result;\r
                }\r
        }\r
        \r
        \r
-       /**\r
-        * Filter class to define datasource file format\r
-        */\r
-       private static class DataSourceFileNameFilter implements FilenameFilter{\r
-               public boolean accept(File dir, String name) {\r
-               return (name.endsWith(DATASOURCE_FILE_NAME));\r
-           }\r
-       }\r
-       \r
-       public boolean equals(Object obj){\r
-               if (obj == null){\r
-                       return false;\r
-               }else if (! CdmDataSource.class.isAssignableFrom(obj.getClass())){\r
-                       return false;\r
-               }else{\r
-                       CdmDataSource dataSource = (CdmDataSource)obj;\r
-                       return (this.dataSourceName == dataSource.dataSourceName);\r
-               }\r
 \r
-       }\r
-}
\ No newline at end of file
+}\r
+\r