Author: tolusha
Date: 2011-07-01 04:57:52 -0400 (Fri, 01 Jul 2011)
New Revision: 4571
Modified:
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBConnectionInfo.java
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java
Log:
EXOJCR-1398: Allow to initialize DBCreator with custome set of parameters
Modified:
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBConnectionInfo.java
===================================================================
---
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBConnectionInfo.java 2011-07-01
08:50:58 UTC (rev 4570)
+++
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBConnectionInfo.java 2011-07-01
08:57:52 UTC (rev 4571)
@@ -30,13 +30,16 @@
{
private final Map<String, String> connectionProperties;
+ private final String dbName;
+
/**
* DBConnectionInfo constructor.
* @param connectionProperties
* connection properties
*/
- public DBConnectionInfo(Map<String, String> connectionProperties)
+ public DBConnectionInfo(String dbName, Map<String, String>
connectionProperties)
{
+ this.dbName = dbName;
this.connectionProperties = connectionProperties;
}
@@ -44,4 +47,9 @@
{
return connectionProperties;
}
+
+ public String getDBName()
+ {
+ return dbName;
+ }
}
Modified:
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
===================================================================
---
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2011-07-01
08:50:58 UTC (rev 4570)
+++
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2011-07-01
08:57:52 UTC (rev 4571)
@@ -46,22 +46,24 @@
public class DBCreator
{
- private final static String DB_CONNECTION = "db-connection";
+ private final static String CONNECTION_PROPERTIES = "db-connection";
- private final static String DB_DRIVER = "driverClassName";
+ private final static String DRIVER_NAME = "driverClassName";
- private final static String DB_URL = "url";
+ private final static String SERVER_URL = "url";
- private final static String DB_USERNAME = "username";
+ private final static String USERNAME = "username";
- private final static String DB_PASSWORD = "password";
+ private final static String PASSWORD = "password";
- private final static String DB_ORCL_INTERNAL_LOGON = "internal_logon";
+ private final static String DB_CREATION_PROPERTIES = "db-creation";
- private final static String DB_CREATION = "db-creation";
-
private final static String DB_SCRIPT_PATH = "scriptPath";
+ private final static String DB_USERNAME = "username";
+
+ private final static String DB_PASSWORD = "password";
+
/**
* Database template.
*/
@@ -110,34 +112,52 @@
* @configurationManager
* configuration manager instance
*/
- public DBCreator(InitParams params, ConfigurationManager configurationManager) throws
ConfigurationException
+ public DBCreator(String serverUrl, Map<String, String> connectionProperties,
String scriptPath, String dbUserName,
+ String dbPassword, ConfigurationManager cm) throws ConfigurationException
{
+ this.serverUrl = serverUrl;
+ this.connectionProperties = connectionProperties;
+ this.dbUserName = dbUserName;
+ this.dbPassword = dbPassword;
+ this.dbScript = findScriptResource(scriptPath, cm);
+ }
+
+ /**
+ * DBCreator constructor.
+ *
+ * @param params
+ * Initializations parameters
+ * @configurationManager
+ * configuration manager instance
+ */
+ public DBCreator(InitParams params, ConfigurationManager cm) throws
ConfigurationException
+ {
if (params == null)
{
throw new ConfigurationException("Initializations parameters
expected");
}
- PropertiesParam prop = params.getPropertiesParam(DB_CONNECTION);
+ PropertiesParam prop = params.getPropertiesParam(CONNECTION_PROPERTIES);
if (prop != null)
{
- if (prop.getProperty(DB_DRIVER) == null)
+ if (prop.getProperty(DRIVER_NAME) == null)
{
throw new ConfigurationException("driverClassName expected in
db-connection properties section");
}
- serverUrl = prop.getProperty(DB_URL);
+ serverUrl = prop.getProperty(SERVER_URL);
if (serverUrl == null)
{
throw new ConfigurationException("url expected in db-connection
properties section");
}
- if (prop.getProperty(DB_USERNAME) == null)
+ if (prop.getProperty(USERNAME) == null)
{
throw new ConfigurationException("username expected in db-connection
properties section");
}
- if (prop.getProperty(DB_PASSWORD) == null)
+ if (prop.getProperty(PASSWORD) == null)
{
throw new ConfigurationException("password expected in db-connection
properties section");
}
@@ -148,7 +168,7 @@
while (pit.hasNext())
{
Property p = pit.next();
- if (!p.getName().equalsIgnoreCase(DB_URL))
+ if (!p.getName().equalsIgnoreCase(SERVER_URL))
{
connectionProperties.put(p.getName(), p.getValue());
}
@@ -159,29 +179,13 @@
throw new ConfigurationException("db-connection properties expected in
initializations parameters");
}
- prop = params.getPropertiesParam(DB_CREATION);
+ prop = params.getPropertiesParam(DB_CREATION_PROPERTIES);
if (prop != null)
{
String scriptPath = prop.getProperty(DB_SCRIPT_PATH);
if (scriptPath != null)
{
- String dbScript;
- try
- {
- dbScript =
readScriptResource(configurationManager.getInputStream(scriptPath));
- }
- catch (Exception e)
- {
- try
- {
- dbScript =
readScriptResource(PrivilegedFileHelper.fileInputStream(scriptPath));
- }
- catch (IOException ioe)
- {
- throw new ConfigurationException("Can't read script resource
" + scriptPath, e);
- }
- }
- this.dbScript = dbScript;
+ this.dbScript = findScriptResource(scriptPath, cm);
}
else
{
@@ -217,43 +221,13 @@
* @throws DBCreatorException
* if any error occurs
*/
- public DBConnectionInfo createDatabase(String dbName) throws DBCreatorException
+ public DBConnectionInfo createDatabase(final String dbName) throws DBCreatorException
{
- Connection conn = null;
+ Connection conn = openConnection();
try
{
- Class.forName(connectionProperties.get(DB_DRIVER));
+ String dbProductName = getDBProductName(conn);
- conn = SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<Connection>()
- {
- public Connection run() throws Exception
- {
- return DriverManager.getConnection(serverUrl,
connectionProperties.get(DB_USERNAME),
- connectionProperties.get(DB_PASSWORD));
- }
- });
- }
- catch (SQLException e)
- {
- throw new DBCreatorException("Can't establish the JDBC connection to
database " + serverUrl, e);
- }
- catch (ClassNotFoundException e)
- {
- throw new DBCreatorException("Can't load the JDBC driver " +
connectionProperties.get(DB_DRIVER), e);
- }
-
- String dbProductName;
- try
- {
- final Connection connection = conn;
- dbProductName = SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<String>()
- {
- public String run() throws Exception
- {
- return connection.getMetaData().getDatabaseProductName();
- }
- });
-
if (dbProductName.startsWith("Microsoft SQL Server") ||
dbProductName.startsWith("Adaptive Server Anywhere")
|| dbProductName.equals("Sybase SQL Server") ||
dbProductName.equals("Adaptive Server Enterprise"))
{
@@ -263,6 +237,8 @@
{
executeBatchMode(conn, dbName);
}
+
+ return constructDBConnectionInfo(dbName, dbProductName);
}
catch (SQLException e)
{
@@ -286,8 +262,6 @@
throw new DBCreatorException("Can't close connection", e);
}
}
-
- return constructDBConnectionInfo(dbName, dbProductName);
}
/**
@@ -296,49 +270,15 @@
* @param dbName
* new database name
* @throws DBCreatorException
- * if any error occurs
+ * if any error occurs or database is not available
*/
public DBConnectionInfo getDBConnectionInfo(String dbName) throws DBCreatorException
{
- Connection conn = null;
+ Connection conn = openConnection();
try
{
- Class.forName(connectionProperties.get(DB_DRIVER));
-
- conn = SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<Connection>()
- {
- public Connection run() throws Exception
- {
- return DriverManager.getConnection(serverUrl,
connectionProperties.get(DB_USERNAME),
- connectionProperties.get(DB_PASSWORD));
- }
- });
+ return constructDBConnectionInfo(dbName, getDBProductName(conn));
}
- catch (SQLException e)
- {
- throw new DBCreatorException("Can't establish the JDBC connection to
database " + serverUrl, e);
- }
- catch (ClassNotFoundException e)
- {
- throw new DBCreatorException("Can't load the JDBC driver " +
connectionProperties.get(DB_DRIVER), e);
- }
-
- String dbProductName;
- try
- {
- final Connection connection = conn;
- dbProductName = SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<String>()
- {
- public String run() throws Exception
- {
- return connection.getMetaData().getDatabaseProductName();
- }
- });
- }
- catch (SQLException e)
- {
- throw new DBCreatorException("Can't resolve database product name
", e);
- }
finally
{
try
@@ -350,8 +290,6 @@
throw new DBCreatorException("Can't close connection", e);
}
}
-
- return constructDBConnectionInfo(dbName, dbProductName);
}
/**
@@ -389,6 +327,10 @@
* database name
* @param dbProductName
* database product name
+ * @param serverUrl
+ * url to DB server
+ * @param connectionProperties
+ * connection properties
* @return DBConnectionInfo
*/
private DBConnectionInfo constructDBConnectionInfo(String dbName, String
dbProductName)
@@ -410,14 +352,15 @@
// clone connection properties
Map<String, String> connProperties = new HashMap<String, String>();
- for (Entry<String, String> entry : this.connectionProperties.entrySet())
+ for (Entry<String, String> entry : connectionProperties.entrySet())
{
connProperties.put(entry.getKey(), entry.getValue());
}
- connProperties.put(DB_URL, dbUrl);
+ // add url to database
+ connProperties.put(SERVER_URL, dbUrl);
- return new DBConnectionInfo(connProperties);
+ return new DBConnectionInfo(dbName, connProperties);
}
/**
@@ -452,7 +395,7 @@
/**
* Read SQL script from {@link InputStream}.
*/
- protected String readScriptResource(InputStream is) throws IOException
+ private String readResource(InputStream is) throws IOException
{
InputStreamReader isr = new InputStreamReader(is);
try
@@ -474,6 +417,37 @@
}
/**
+ * Find script resource.
+ *
+ * @param scriptPath
+ * path to the script
+ * @param cm
+ * the configuration manager will help to find script in jars
+ * @return
+ * script content
+ * @throws ConfigurationException
+ * if script not found
+ */
+ private String findScriptResource(String scriptPath, ConfigurationManager cm) throws
ConfigurationException
+ {
+ try
+ {
+ return readResource(cm.getInputStream(scriptPath));
+ }
+ catch (Exception e)
+ {
+ try
+ {
+ return readResource(PrivilegedFileHelper.fileInputStream(scriptPath));
+ }
+ catch (IOException ioe)
+ {
+ throw new ConfigurationException("Can't read script resource "
+ scriptPath, e);
+ }
+ }
+ }
+
+ /**
* Clean whitespace.
*/
private String cleanWhitespaces(String string)
@@ -492,4 +466,68 @@
}
return string;
}
+
+ /**
+ * Open connection to the DB.
+ *
+ * @param connectionProperties
+ * connection properties
+ * @return connection
+ * @throws DBCreatorException
+ * if can't establish connection to DB
+ */
+ private Connection openConnection() throws DBCreatorException
+ {
+ Connection conn = null;
+ try
+ {
+ Class.forName(connectionProperties.get(DRIVER_NAME));
+
+ conn = SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<Connection>()
+ {
+ public Connection run() throws Exception
+ {
+ return DriverManager.getConnection(serverUrl,
connectionProperties.get(USERNAME),
+ connectionProperties.get(PASSWORD));
+ }
+ });
+
+ return conn;
+ }
+ catch (SQLException e)
+ {
+ throw new DBCreatorException("Can't establish the JDBC connection to
database " + serverUrl, e);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new DBCreatorException("Can't load the JDBC driver " +
connectionProperties.get(DRIVER_NAME), e);
+ }
+ }
+
+ /**
+ * Get database product name.
+ *
+ * @param conn
+ * connection to database
+ * @return product name
+ * @throws DBCreatorException
+ * if can't resolve database product name
+ */
+ private String getDBProductName(final Connection conn) throws DBCreatorException
+ {
+ try
+ {
+ return SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<String>()
+ {
+ public String run() throws Exception
+ {
+ return conn.getMetaData().getDatabaseProductName();
+ }
+ });
+ }
+ catch (SQLException e)
+ {
+ throw new DBCreatorException("Can't resolve database product name
", e);
+ }
+ }
}
Modified:
core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java
===================================================================
---
core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java 2011-07-01
08:50:58 UTC (rev 4570)
+++
core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java 2011-07-01
08:57:52 UTC (rev 4571)
@@ -21,11 +21,13 @@
import junit.framework.TestCase;
import org.exoplatform.container.PortalContainer;
+import org.exoplatform.container.configuration.ConfigurationManager;
import org.exoplatform.services.database.creator.DBConnectionInfo;
import org.exoplatform.services.database.creator.DBCreator;
import org.exoplatform.services.naming.InitialContextInitializer;
import java.sql.Connection;
+import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
@@ -41,11 +43,12 @@
private InitialContextInitializer initContext;
+ private PortalContainer container;
+
@Override
public void setUp() throws Exception
{
- PortalContainer container = PortalContainer.getInstance();
-
+ container = PortalContainer.getInstance();
dbCreator = (DBCreator)container.getComponentInstanceOfType(DBCreator.class);
initContext =
(InitialContextInitializer)container.getComponentInstanceOfType(InitialContextInitializer.class);
}
@@ -61,9 +64,9 @@
Map<String, String> connProps1 = dbInfo1.getProperties();
assertEquals(connProps.get("driverClassName"),
connProps1.get("driverClassName"));
+ assertEquals(connProps.get("username"),
connProps1.get("username"));
+ assertEquals(connProps.get("url"), connProps1.get("url"));
assertEquals(connProps.get("password"),
connProps1.get("password"));
- assertEquals(connProps.get("url"), connProps1.get("url"));
- assertEquals(connProps.get("username"),
connProps1.get("username"));
Map<String, String> refAddr = dbInfo.getProperties();
@@ -77,6 +80,43 @@
assertNotNull(conn);
}
+ public void testDBCreateWithSpecificProperties() throws Exception
+ {
+ assertNotNull(dbCreator);
+
+ String serverUrl = "jdbc:hsqldb:file:target/temp/data/dbcreator_test";
+ Map<String, String> connectionProperties = new HashMap<String,
String>();
+ connectionProperties.put("driverClassName",
"org.hsqldb.jdbcDriver");
+ connectionProperties.put("username", "sa");
+ connectionProperties.put("password", "");
+
+ ConfigurationManager cm =
(ConfigurationManager)container.getComponentInstanceOfType(ConfigurationManager.class);
+ DBCreator dbCreator =
+ new DBCreator(serverUrl, connectionProperties,
"classpath:/dbcreator/test.sql", "sa", "", cm);
+
+ DBConnectionInfo dbInfo = dbCreator.createDatabase("testdb");
+ DBConnectionInfo dbInfo1 = dbCreator.getDBConnectionInfo("testdb");
+
+ Map<String, String> connProps = dbInfo.getProperties();
+ Map<String, String> connProps1 = dbInfo1.getProperties();
+
+ assertEquals(connProps.get("driverClassName"),
connProps1.get("driverClassName"));
+ assertEquals(connProps.get("username"),
connProps1.get("username"));
+ assertEquals(connProps.get("url"), connProps1.get("url"));
+ assertEquals(connProps.get("password"),
connProps1.get("password"));
+
+ Map<String, String> refAddr = dbInfo.getProperties();
+
+ initContext.bind("testjdbcjcr2", "javax.sql.DataSource",
"org.apache.commons.dbcp.BasicDataSourceFactory", null,
+ refAddr);
+
+ DataSource ds =
(DataSource)initContext.getInitialContext().lookup("testjdbcjcr2");
+ assertNotNull(ds);
+
+ Connection conn = ds.getConnection();
+ assertNotNull(conn);
+ }
+
public void testDBCreateMultiThread() throws Exception
{
DBCreateThread[] queue = new DBCreateThread[100];