import java.io.FileOutputStream;\r
import java.io.FilenameFilter;\r
import java.util.ArrayList;\r
+import java.util.HashMap;\r
import java.util.List;\r
+import java.util.Map;\r
+import java.util.Properties;\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.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
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
\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
}\r
\r
+ public BeanDefinition getDatasourceBean(){\r
+ AbstractBeanDefinition bd = new RootBeanDefinition(DriverManagerDataSource.class);\r
+ DatabaseTypeEnum dbtype = getDatabaseType();\r
+ //TODO: read real values\r
+ MutablePropertyValues props = new MutablePropertyValues();\r
+ props.addPropertyValue("driverClassName", dbtype.getDriverClassName());\r
+ props.addPropertyValue("url", "jdbc:mysql://192.168.2.10/cdm_build");\r
+ props.addPropertyValue("username", "edit");\r
+ props.addPropertyValue("password", "wp5");\r
+ bd.setPropertyValues(props);\r
+ return bd;\r
+ }\r
+ public BeanDefinition getHibernatePropertiesBean(HBM2DDL hbm2dll, boolean showSql){\r
+ DatabaseTypeEnum dbtype = getDatabaseType();\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.getHibernateString());\r
+ props.setProperty("hibernate.dialect", dbtype.getHibernateDialect());\r
+ props.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");\r
+ props.setProperty("hibernate.show_sql", String.valueOf(showSql));\r
+ props.setProperty("hibernate.format_sql", String.valueOf(false));\r
+\r
+ hibernateProps.addPropertyValue("properties",props);\r
+ bd.setPropertyValues(hibernateProps);\r
+ return bd;\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
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
+// * Returns the session factory config file input stream.\r
+// * @return session factory config file\r
+// */\r
+// private FileInputStream get22SessionFactoryInputStream(){\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 get22SessionFactoryOutputStream(){\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