Author: tolusha
Date: 2010-11-25 02:55:08 -0500 (Thu, 25 Nov 2010)
New Revision: 3547
Modified:
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-929: get database connection info without db creation
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 2010-11-25
07:29:52 UTC (rev 3546)
+++
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2010-11-25
07:55:08 UTC (rev 3547)
@@ -261,22 +261,70 @@
}
}
- // try to solve database url connection depending on specific database
- String dbUrl = serverUrl;
- if (dbProductName.startsWith("Microsoft SQL Server"))
+ return constructDBConnectionInfo(dbName, dbProductName);
+ }
+
+ /**
+ * Get database connection info.
+ *
+ * @param dbName
+ * new database name
+ * @throws DBCreatorException
+ * if any error occurs
+ */
+ public DBConnectionInfo getDBConnectionInfo(String dbName) throws DBCreatorException
+ {
+ Connection conn = null;
+ try
{
- dbUrl = dbUrl + (dbUrl.endsWith(";") ? "" : ";") +
"databaseName=" + dbName + ";";
+ Class.forName(driver);
+
+ conn = SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Connection>()
+ {
+ public Connection run() throws Exception
+ {
+ return DriverManager.getConnection(serverUrl, adminName, adminPwd);
+ }
+ });
}
- else if (dbProductName.equals("Oracle"))
+ catch (SQLException e)
{
- // do nothing
+ throw new DBCreatorException("Can't establish the JDBC connection to
database " + serverUrl, e);
}
- else
+ catch (ClassNotFoundException e)
{
- dbUrl = dbUrl + (dbUrl.endsWith("/") ? "" : "/") +
dbName;
+ throw new DBCreatorException("Can't load the JDBC driver " +
driver, e);
}
- return new DBConnectionInfo(driver, dbUrl, dbUserName, dbPassword);
+ String dbProductName;
+ try
+ {
+ final Connection connection = conn;
+ dbProductName = SecurityHelper.doPriviledgedSQLExceptionAction(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
+ {
+ conn.close();
+ }
+ catch (SQLException e)
+ {
+ throw new DBCreatorException("Can't close connection", e);
+ }
+ }
+
+ return constructDBConnectionInfo(dbName, dbProductName);
}
/**
@@ -308,6 +356,34 @@
}
/**
+ * Construct database url connection depending on specific database.
+ *
+ * @param dbName
+ * database name
+ * @param dbProductName
+ * database product name
+ * @return DBConnectionInfo
+ */
+ private DBConnectionInfo constructDBConnectionInfo(String dbName, String
dbProductName)
+ {
+ String dbUrl = serverUrl;
+ if (dbProductName.startsWith("Microsoft SQL Server"))
+ {
+ dbUrl = dbUrl + (dbUrl.endsWith(";") ? "" : ";") +
"databaseName=" + dbName + ";";
+ }
+ else if (dbProductName.equals("Oracle"))
+ {
+ // do nothing
+ }
+ else
+ {
+ dbUrl = dbUrl + (dbUrl.endsWith("/") ? "" : "/") +
dbName;
+ }
+
+ return new DBConnectionInfo(driver, dbUrl, dbUserName, dbPassword);
+ }
+
+ /**
* Executes DDL script with autocommit mode set true. Actually need for MSSQL and
Sybase database servers.
* After execution "create database" command newly created database not
available for "use" command and
* therefore you can't create user inside.
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 2010-11-25
07:29:52 UTC (rev 3546)
+++
core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java 2010-11-25
07:55:08 UTC (rev 3547)
@@ -56,7 +56,13 @@
assertNotNull(dbCreator);
DBConnectionInfo dbInfo = dbCreator.createDatabase("testdb");
+ DBConnectionInfo dbInfo1 = dbCreator.getDBConnectionInfo("testdb");
+ assertEquals(dbInfo.getDriver(), dbInfo1.getDriver());
+ assertEquals(dbInfo.getPassword(), dbInfo1.getPassword());
+ assertEquals(dbInfo.getUrl(), dbInfo1.getUrl());
+ assertEquals(dbInfo.getUsername(), dbInfo1.getUsername());
+
Map<String, String> refAddr = new HashMap<String, String>();
refAddr.put("driverClassName", dbInfo.getDriver());
refAddr.put("url", dbInfo.getUrl());
Show replies by date