(no commit message)
authorAndreas Müller <a.mueller@bgbm.org>
Wed, 23 Jan 2008 17:34:54 +0000 (17:34 +0000)
committerAndreas Müller <a.mueller@bgbm.org>
Wed, 23 Jan 2008 17:34:54 +0000 (17:34 +0000)
.gitattributes
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/CdmDataSource.java [new file with mode: 0644]
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/LocalHsqldb.java [new file with mode: 0644]

index ad16227bc0a6fe6d5a5f1cba43c40581046cb1e8..c3faf426b354a14335771dc53b8f12141eec40da 100644 (file)
@@ -253,8 +253,10 @@ cdmlib-model/src/test/java/eu/etaxonomy/cdm/test/function/TestModel.java -text
 cdmlib-model/src/test/java/eu/etaxonomy/cdm/test/suite/CdmTestSuite.java -text
 cdmlib-model/src/test/java/eu/etaxonomy/cdm/test/unit/CdmUnitTestBase.java -text
 cdmlib-persistence/pom.xml -text
+cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/CdmDataSource.java -text
 cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/DataSourceNotFoundException.java -text
 cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/DatabaseTypeEnum.java -text
+cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/LocalHsqldb.java -text
 cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/types/AbstractDatabaseType.java -text
 cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/types/Db2DatabaseType.java -text
 cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/types/HSqlDbDatabaseType.java -text
diff --git a/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/CdmDataSource.java b/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/CdmDataSource.java
new file mode 100644 (file)
index 0000000..761b76d
--- /dev/null
@@ -0,0 +1,434 @@
+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.List;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.jdom.Attribute;\r
+import org.jdom.Document;\r
+import org.jdom.Element;\r
+import org.jdom.output.Format;\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
+\r
+/**\r
+ * class to access an CdmDataSource\r
+ */\r
+public class CdmDataSource {\r
+       private static final Logger logger = Logger.getLogger(CdmDataSource.class);\r
+       \r
+       public static final String DATASOURCE_BEAN_POSTFIX = "DataSource";\r
+       public static final String SESSION_FACTORY_FILE = "sessionfactory.xml";\r
+       public final static String DATASOURCE_FILE_NAME = "cdm.datasource.xml";\r
+       public final static String APPLICATION_CONTEXT_FILE_NAME = "applicationContext.xml";\r
+       private final static Format format = Format.getPrettyFormat(); \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
+       }\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
+       \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
+       /**\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
+       }\r
+       \r
+       /**\r
+        * Returns the name of the bean.\r
+        * @return\r
+        */\r
+       public String getName(){\r
+               return dataSourceName;\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
+\r
+\r
+       \r
+       /**\r
+        * Updates the session factory config file for using this database.\r
+        * Writes the datasource property and the dialect property into the session factory.\r
+        * @param hibernateHbm2ddlAuto value for the hibernate property hibernate.hbm2dll.auto . If null the properties is not changed. Possible values are 'validate', 'create', 'update' and 'create-drop'.\r
+        * @return true if successful.\r
+        */\r
+       public boolean updateSessionFactory(String hibernateHbm2ddlAuto){\r
+               Element root = getRoot(getSessionFactoryInputStream());\r
+               if (root == null){\r
+                       return false;\r
+               }\r
+               //get sessionFactory bean\r
+               Element sessionFactoryBean = getFirstAttributedChild(root, "bean", "id", "sessionFactory");\r
+               //sessionFactory must exist \r
+               if  (sessionFactoryBean == null){\r
+                       return false;\r
+               }\r
+               \r
+               //set dataSource property\r
+               Element dataSourceProperty = getFirstAttributedChild(sessionFactoryBean, "property", "name", "dataSource");\r
+               if (dataSourceProperty == null){\r
+                       dataSourceProperty = insertXmlRefProperty(sessionFactoryBean, "dataSource", getBeanName(this.dataSourceName));\r
+               }\r
+               Attribute attrRef = dataSourceProperty.getAttribute("ref");\r
+               if (attrRef == null){\r
+                       dataSourceProperty.setAttribute("ref", getBeanName(this.dataSourceName));\r
+               }else{\r
+                       attrRef.setValue(getBeanName(this.dataSourceName));\r
+               }\r
+               \r
+               //set dialect\r
+               Element elHibernateProperties = getOrAddChild(sessionFactoryBean, "property", "name", "hibernateProperties");\r
+               Element props = getOrAddChild(elHibernateProperties, "props", null, null);\r
+               Element elDialectProp = getOrAddChild(props, "prop", "key", "hibernate.dialect");\r
+               elDialectProp.setText(this.getDatabaseType().getHibernateDialect());\r
+               \r
+               //set hibernateHbm2ddlAuto\r
+               if (hibernateHbm2ddlAuto != null){\r
+                       if (hibernateHbm2ddlAuto != "validate" && hibernateHbm2ddlAuto != "create"  && hibernateHbm2ddlAuto != "update "  && hibernateHbm2ddlAuto != "create-drop"  ){\r
+                               logger.warn("Invalid value " + hibernateHbm2ddlAuto + " for property hibernate.hbm2ddl.auto");\r
+                       }\r
+                       Element elHbm2ddlAutoProp = getOrAddChild(props, "prop", "key", "hibernate.hbm2ddl.auto");\r
+                       elHbm2ddlAutoProp.setText(hibernateHbm2ddlAuto);\r
+               }\r
+               \r
+               //save\r
+               saveToXml(root.getDocument(), getSessionFactoryOutputStream() , format );\r
+               return true;\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
+       }\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
+\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
+       }\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
+       \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
+       \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 (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
+       \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
+       \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
+       \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
+\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
+       }\r
+       \r
+       \r
+       /**\r
+        * Returns the datasource config file outputStream.\r
+        * @return data source config file outputStream\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
+       }\r
+\r
+       /**\r
+        * Returns the jdom Element representing the data source bean in the config file.\r
+        * @return\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){logger.warn("Unknown Element 'bean' ");};\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
+       }\r
+       \r
+       /**\r
+        * Returns the session factory config file input stream.\r
+        * @return session factory config file\r
+        */\r
+       private FileInputStream getSessionFactoryInputStream(){\r
+               String dir = getResourceDirectory();\r
+               File file = new File(dir + File.separator +  SESSION_FACTORY_FILE);\r
+               return fileInputStream(file);\r
+       }\r
+       \r
+       /**\r
+        * Returns the session factory output stream.\r
+        * @return \r
+        */\r
+       private FileOutputStream getSessionFactoryOutputStream(){\r
+               String dir = getResourceDirectory();\r
+               File file = new File(dir + File.separator +  SESSION_FACTORY_FILE);\r
+               return fileOutputStream(file);\r
+       }\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
+       \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
+       }\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
+}
\ No newline at end of file
diff --git a/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/LocalHsqldb.java b/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/LocalHsqldb.java
new file mode 100644 (file)
index 0000000..0c40aee
--- /dev/null
@@ -0,0 +1,256 @@
+/**\r
+ * \r
+ */\r
+package eu.etaxonomy.cdm.database;\r
+\r
+import java.io.File;\r
+import java.sql.Connection;\r
+import java.sql.Driver;\r
+import java.sql.DriverManager;\r
+import java.sql.SQLException;\r
+import java.util.Properties;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.hsqldb.Server;\r
+import org.springframework.jdbc.CannotGetJdbcConnectionException;\r
+import org.springframework.jdbc.datasource.DriverManagerDataSource;\r
+\r
+import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;\r
+import eu.etaxonomy.cdm.common.CdmUtils;\r
+\r
+/**\r
+ * @author a.mueller\r
+ *\r
+ */\r
+\r
+public class LocalHsqldb extends DriverManagerDataSource {\r
+       private static final Logger logger = Logger.getLogger(LocalHsqldb.class);\r
+       \r
+       private String sep = System.getProperty("file.separator");\r
+       \r
+       /** url without database name */\r
+       protected String pureUrl = "jdbc:hsqldb:hsql://localhost/";\r
+       /** database name */\r
+       protected String dbName = "cdm";\r
+       /** path, where database should be stored in the file system */\r
+       protected String databasePath = getDefaultPath();\r
+       /** Server instance */\r
+       protected Server hsqldbServer;\r
+       /** if true starts server on init() */\r
+       protected boolean isStartServer = true;\r
+       /** makes the Server silent (no messages) */\r
+       protected boolean isSilent = true;\r
+       /** default driver class name */\r
+       protected String DEFAULT_DRIVER_CLASS_NAME = "org.hsqldb.jdbcDriver";\r
+       \r
+       \r
+       /**\r
+        * \r
+        */\r
+       public LocalHsqldb() {\r
+               setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);\r
+               setComposedUrl();\r
+       }\r
+\r
+       /**\r
+        * @param url\r
+        * @throws CannotGetJdbcConnectionException\r
+        */\r
+       public LocalHsqldb(String url) throws CannotGetJdbcConnectionException {\r
+               super(url);\r
+               setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);\r
+       }\r
+\r
+       /**\r
+        * @param url\r
+        * @param username\r
+        * @param password\r
+        * @throws CannotGetJdbcConnectionException\r
+        */\r
+       public LocalHsqldb(String url, String username, String password)\r
+                       throws CannotGetJdbcConnectionException {\r
+               super(url, username, password);\r
+               this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);\r
+       }\r
+\r
+       /**\r
+        * @param driverClassName\r
+        * @param url\r
+        * @param username\r
+        * @param password\r
+        * @throws CannotGetJdbcConnectionException\r
+        */\r
+       public LocalHsqldb(String driverClassName, String url, String username,\r
+                       String password) throws CannotGetJdbcConnectionException {\r
+               super(driverClassName, url, username, password);\r
+       }\r
+\r
+       public void init(){\r
+               if (isStartServer){\r
+                       this.startHsqldbServer();\r
+               }\r
+       }\r
+       \r
+       public void destroy(){\r
+               this.stopHsqldbServer();\r
+       }\r
+       \r
+\r
+       /* (non-Javadoc)\r
+        * @see org.springframework.jdbc.datasource.DriverManagerDataSource#getUrl()\r
+        */\r
+       @Override\r
+       public String getUrl() {\r
+               return super.getUrl();\r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see org.springframework.jdbc.datasource.DriverManagerDataSource#setUrl(java.lang.String)\r
+        */\r
+       @Override\r
+       public void setUrl(String url) {\r
+               super.setUrl(url);\r
+       }\r
+\r
+       /**\r
+        * @return the pureUrl\r
+        */\r
+       public String getPureUrl() {\r
+               return pureUrl;\r
+       }\r
+\r
+       /**\r
+        * @param pureUrl the pureUrl to set\r
+        */\r
+       public void setPureUrl(String pureUrl) {\r
+               this.pureUrl = pureUrl;\r
+               if (dbName != null){\r
+                       setComposedUrl();\r
+               }\r
+       }\r
+\r
+       /**\r
+        * @return the dbName\r
+        */\r
+       public String getDbName() {\r
+               return dbName;\r
+       }\r
+\r
+       /**\r
+        * @param dbName the dbName to set\r
+        */\r
+       public void setDbName(String dbName) {\r
+               this.dbName = dbName;\r
+               if (pureUrl != null){\r
+                       setComposedUrl();\r
+               }\r
+       }\r
+       \r
+       private void setComposedUrl(){\r
+               setUrl(getPureUrl() + getDbName());\r
+       }\r
+       \r
+       //checks if hsqldb-server is started, if not it will be started \r
+       private void startHsqldbServer(){\r
+               try {\r
+                       Driver driver = DriverManager.getDriver(getUrl());\r
+                       Properties prop = new Properties();\r
+                       prop.setProperty("user", this.getUsername());\r
+                       prop.setProperty("password", this.getPassword());\r
+                       Connection con = driver.connect(getUrl(),  prop);\r
+                       if (con == null) {\r
+                               logger.warn("Connection to URL " +  getUrl() +  " could not be established");\r
+                               throw new SQLException();\r
+                       }\r
+               } catch (SQLException e) {\r
+                       try {\r
+                               //server is probably not runing on the url (or login is wrong !!)\r
+                               logger.warn("Start HsqldbServer"); //TODO make it .info\r
+                               hsqldbServer = new Server();\r
+                               hsqldbServer.setSilent(this.isSilent);\r
+                               for (int i = 0; i < 10; i++){\r
+                                       logger.info("DatabaseName " + i + ": " + hsqldbServer.getDatabaseName(i, true));\r
+                                       logger.info("DatabaseName " + i + ": " + hsqldbServer.getDatabaseName(i, false));\r
+                                       logger.info("DatabasePath " + i + ": " + hsqldbServer.getDatabasePath(i, true));\r
+                                       logger.info("DatabasePath " + i + ": " + hsqldbServer.getDatabasePath(i, false));\r
+                                       logger.info("DatabaseType " + i + ": " + hsqldbServer.getDatabaseType(i));\r
+                               }\r
+                               hsqldbServer.setDatabaseName(0, getDbName());\r
+                               hsqldbServer.setDatabasePath(0,  getDatabasePath());\r
+                               hsqldbServer.start();\r
+                               hsqldbServer.checkRunning(true);\r
+                       } catch (RuntimeException e1) {\r
+                               logger.error("Local hsqlServer could not be started or connection to existing server could not be established.");\r
+                       }\r
+               }\r
+       }\r
+       \r
+       \r
+       /**\r
+        * stops the Hsqldb Server\r
+        */\r
+       private void stopHsqldbServer(){\r
+               if (hsqldbServer != null){\r
+                       logger.info("stop HsqldbServer");\r
+                       hsqldbServer.stop();\r
+               }\r
+       }\r
+       \r
+       private static final String getDefaultPath(){\r
+               //String path = System.getProperty("user.dir");\r
+               File path = CdmApplicationUtils.getWritableResourceDir();\r
+               String supPath = File.separator + "db" + File.separator + "LocalHsqldb"; \r
+               return  path + supPath;\r
+       }\r
+\r
+       /**\r
+        * @return the dbPath\r
+        */\r
+       public String getDatabasePath() {\r
+               return databasePath;\r
+       }\r
+\r
+       /**\r
+        * @param dbPath the dbPath to set\r
+        */\r
+       public void setDatabasePath(String databasePath) {\r
+               if (databasePath.endsWith(sep)){\r
+                       databasePath = databasePath + "localCdm";\r
+               }\r
+               this.databasePath = databasePath;\r
+       }\r
+\r
+       /**\r
+        * @return the isStartServer\r
+        */\r
+       public boolean isStartServer() {\r
+               return isStartServer;\r
+       }\r
+\r
+       /**\r
+        * @param isStartServer the isStartServer to set\r
+        */\r
+       public void setStartServer(boolean isStartServer) {\r
+               this.isStartServer = isStartServer;\r
+       }\r
+\r
+       /**\r
+        * @return the isSilent\r
+        */\r
+       public boolean isSilent() {\r
+               return isSilent;\r
+       }\r
+\r
+       /**\r
+        * @param isSilent the isSilent to set\r
+        */\r
+       public void setSilent(boolean isSilent) {\r
+               if (this.hsqldbServer != null){\r
+                       this.hsqldbServer.setSilent(isSilent);\r
+               }\r
+               this.isSilent = isSilent;\r
+       }\r
+       \r
+       \r
+\r
+}\r