[exo-jcr-commits] exo-jcr SVN: r5426 - in core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database: jdbc and 1 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Jan 6 04:43:11 EST 2012
Author: tolusha
Date: 2012-01-06 04:43:10 -0500 (Fri, 06 Jan 2012)
New Revision: 5426
Added:
core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectConstants.java
core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectDetecter.java
core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/JDBCUtils.java
Removed:
core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/ExceptionManagementHelper.java
Modified:
core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/jdbc/DBSchemaCreator.java
Log:
EXOJCR-968: Move DBInitalizer and DBCleaner common code into separated class
Modified: core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
===================================================================
--- core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2012-01-06 09:42:23 UTC (rev 5425)
+++ core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2012-01-06 09:43:10 UTC (rev 5426)
@@ -19,18 +19,18 @@
package org.exoplatform.services.database.creator;
import org.exoplatform.commons.utils.ClassLoading;
-import org.exoplatform.commons.utils.PrivilegedFileHelper;
+import org.exoplatform.commons.utils.IOUtil;
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.container.configuration.ConfigurationException;
import org.exoplatform.container.configuration.ConfigurationManager;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.PropertiesParam;
import org.exoplatform.container.xml.Property;
-import org.exoplatform.services.database.utils.ExceptionManagementHelper;
+import org.exoplatform.services.database.utils.DialectConstants;
+import org.exoplatform.services.database.utils.DialectDetecter;
+import org.exoplatform.services.database.utils.JDBCUtils;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.security.PrivilegedExceptionAction;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -121,7 +121,7 @@
this.connectionProperties = connectionProperties;
this.dbUserName = dbUserName;
this.dbPassword = dbPassword;
- this.dbScript = findScriptResource(scriptPath, cm);
+ this.dbScript = readScript(scriptPath, cm);
}
/**
@@ -187,7 +187,7 @@
String scriptPath = prop.getProperty(DB_SCRIPT_PATH);
if (scriptPath != null)
{
- this.dbScript = findScriptResource(scriptPath, cm);
+ this.dbScript = readScript(scriptPath, cm);
}
else
{
@@ -228,23 +228,23 @@
Connection conn = openConnection();
try
{
- String dbProductName = getDBProductName(conn);
+ String dialect = DialectDetecter.detect(conn.getMetaData());
- if (dbProductName.startsWith("Microsoft SQL Server") || dbProductName.startsWith("Adaptive Server Anywhere")
- || dbProductName.equals("Sybase SQL Server") || dbProductName.equals("Adaptive Server Enterprise"))
+ if (dialect.equalsIgnoreCase(DialectConstants.DB_DIALECT_MSSQL)
+ || dialect.equalsIgnoreCase(DialectConstants.DB_DIALECT_SYBASE))
{
- executeAutoCommitMode(conn, dbName);
+ executeInAutoCommitMode(conn, dbName);
}
else
{
- executeBatchMode(conn, dbName);
+ executeInBatchMode(conn, dbName);
}
- return constructDBConnectionInfo(dbName, dbProductName);
+ return constructDBConnectionInfo(dbName, dialect);
}
catch (SQLException e)
{
- throw new DBCreatorException("Can't execute SQL script : " + ExceptionManagementHelper.getFullSQLExceptionMessage(e));
+ throw new DBCreatorException("Can't execute SQL script : " + JDBCUtils.getFullMessage(e));
}
finally
{
@@ -272,8 +272,12 @@
Connection conn = openConnection();
try
{
- return constructDBConnectionInfo(dbName, getDBProductName(conn));
+ return constructDBConnectionInfo(dbName, DialectDetecter.detect(conn.getMetaData()));
}
+ catch (SQLException e)
+ {
+ throw new DBCreatorException("Can not get database connection information", e);
+ }
finally
{
try
@@ -297,7 +301,7 @@
* @throws SQLException
* if any errors occurs
*/
- private void executeBatchMode(Connection conn, String dbName) throws SQLException
+ private void executeInBatchMode(Connection conn, String dbName) throws SQLException
{
Statement statement = conn.createStatement();
for (String scr : dbScript.split(";"))
@@ -306,7 +310,7 @@
scr = scr.replace(USERNAME_TEMPLATE, dbUserName);
scr = scr.replace(PASSWORD_TEMPLATE, dbPassword);
- String s = cleanWhitespaces(scr.trim());
+ String s = JDBCUtils.cleanWhitespaces(scr.trim());
if (s.length() > 0)
{
statement.addBatch(s);
@@ -320,26 +324,27 @@
*
* @param dbName
* database name
- * @param dbProductName
- * database product name
+ * @param dialect
+ * dialect
* @param serverUrl
* url to DB server
* @param connectionProperties
* connection properties
* @return DBConnectionInfo
*/
- private DBConnectionInfo constructDBConnectionInfo(String dbName, String dbProductName)
+ private DBConnectionInfo constructDBConnectionInfo(String dbName, String dialect)
{
StringBuilder dbUrl = new StringBuilder(serverUrl);
- if (dbProductName.startsWith("Microsoft SQL Server"))
+ if (dialect.equalsIgnoreCase(DialectConstants.DB_DIALECT_MSSQL))
{
dbUrl.append(serverUrl.endsWith(";") ? "" : ";");
dbUrl.append("databaseName=");
dbUrl.append(dbName);
dbUrl.append(";");
}
- else if (dbProductName.equals("Oracle"))
+ else if (dialect.equalsIgnoreCase(DialectConstants.DB_DIALECT_ORACLE)
+ || dialect.equalsIgnoreCase(DialectConstants.DB_DIALECT_ORACLEOCI))
{
// do nothing
}
@@ -375,7 +380,7 @@
* @throws SQLException
* if any errors occurs
*/
- private void executeAutoCommitMode(Connection conn, String dbName) throws SQLException
+ private void executeInAutoCommitMode(Connection conn, String dbName) throws SQLException
{
conn.setAutoCommit(true);
for (String scr : dbScript.split(";"))
@@ -384,7 +389,7 @@
scr = scr.replace(USERNAME_TEMPLATE, dbUserName);
scr = scr.replace(PASSWORD_TEMPLATE, dbPassword);
- String s = cleanWhitespaces(scr.trim());
+ String s = JDBCUtils.cleanWhitespaces(scr.trim());
if (s.length() > 0)
{
conn.createStatement().executeUpdate(s);
@@ -393,31 +398,7 @@
}
/**
- * Read SQL script from {@link InputStream}.
- */
- private String readResource(InputStream is) throws IOException
- {
- InputStreamReader isr = new InputStreamReader(is);
- try
- {
- StringBuilder sbuff = new StringBuilder();
- char[] buff = new char[is.available()];
- int r = 0;
- while ((r = isr.read(buff)) > 0)
- {
- sbuff.append(buff, 0, r);
- }
-
- return sbuff.toString();
- }
- finally
- {
- is.close();
- }
- }
-
- /**
- * Find script resource.
+ * Read script resource.
*
* @param scriptPath
* path to the script
@@ -428,46 +409,26 @@
* @throws ConfigurationException
* if script not found
*/
- private String findScriptResource(String scriptPath, ConfigurationManager cm) throws ConfigurationException
+ private String readScript(String scriptPath, ConfigurationManager cm) throws ConfigurationException
{
try
{
- return readResource(cm.getInputStream(scriptPath));
+ return IOUtil.getStreamContentAsString(cm.getInputStream(scriptPath));
}
catch (Exception e)
{
try
{
- return readResource(PrivilegedFileHelper.fileInputStream(scriptPath));
+ return IOUtil.getFileContentAsString(scriptPath);
}
catch (IOException ioe)
{
- throw new ConfigurationException("Can't read script resource " + scriptPath, e);
+ throw new ConfigurationException("Can't read script at " + scriptPath, e);
}
}
}
/**
- * Clean whitespace.
- */
- private String cleanWhitespaces(String string)
- {
- if (string != null)
- {
- char[] cc = string.toCharArray();
- for (int ci = cc.length - 1; ci > 0; ci--)
- {
- if (Character.isWhitespace(cc[ci]))
- {
- cc[ci] = ' ';
- }
- }
- return new String(cc);
- }
- return string;
- }
-
- /**
* Open connection to the DB.
*
* @param connectionProperties
@@ -503,31 +464,4 @@
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/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/jdbc/DBSchemaCreator.java
===================================================================
--- core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/jdbc/DBSchemaCreator.java 2012-01-06 09:42:23 UTC (rev 5425)
+++ core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/jdbc/DBSchemaCreator.java 2012-01-06 09:43:10 UTC (rev 5426)
@@ -19,7 +19,7 @@
package org.exoplatform.services.database.jdbc;
import org.exoplatform.container.component.ComponentPlugin;
-import org.exoplatform.services.database.utils.ExceptionManagementHelper;
+import org.exoplatform.services.database.utils.JDBCUtils;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.naming.InitialContextInitializer;
@@ -46,11 +46,6 @@
public class DBSchemaCreator
{
-
- static public String SQL_DELIMITER_COMMENT_PREFIX = "/*$DELIMITER:";
-
- static public String SQL_DELIMITER = ";";
-
static private String SQL_ALREADYEXISTS = ".*((already exist)|(duplicate key)| (already used)|(ORA-00955))+.*";
private final Pattern pattern;
@@ -80,36 +75,12 @@
String sql = "";
try
{
- String[] scripts = null;
- if (script.startsWith(SQL_DELIMITER_COMMENT_PREFIX))
- {
- // read custom prefix
- try
- {
- String s = script.substring(SQL_DELIMITER_COMMENT_PREFIX.length());
- int endOfDelimIndex = s.indexOf("*/");
- String delim = s.substring(0, endOfDelimIndex).trim();
- s = s.substring(endOfDelimIndex + 2).trim();
- scripts = s.split(delim);
- }
- catch (IndexOutOfBoundsException e)
- {
- log.warn("Error of parse SQL-script file. Invalid DELIMITER configuration. Valid format is '"
- + SQL_DELIMITER_COMMENT_PREFIX
- + "XXX*/' at begin of the SQL-script file, where XXX - DELIMITER string."
- + " Spaces will be trimed. ", e);
- log.info("Using DELIMITER:[" + SQL_DELIMITER + "]");
- scripts = script.split(SQL_DELIMITER);
- }
- }
- else
- {
- scripts = script.split(SQL_DELIMITER);
- }
+ String[] scripts = JDBCUtils.splitWithSQLDelimiter(script);
for (String scr : scripts)
{
- String s = cleanWhitespaces(scr.trim());
+ String s = JDBCUtils.cleanWhitespaces(scr.trim());
+
if (s.length() < 1)
continue;
sql = s;
@@ -139,7 +110,7 @@
catch (SQLException e)
{
log.error("Could not create db schema of DataSource: '" + dsName + "'. Reason: " + e.getMessage() + "; "
- + ExceptionManagementHelper.getFullSQLExceptionMessage(e) + ". Last command: " + sql, e);
+ + JDBCUtils.getFullMessage(e) + ". Last command: " + sql, e);
}
finally
{
@@ -185,18 +156,4 @@
{
return new DBSchemaCreator(dsName, script);
}
-
- static public String cleanWhitespaces(String string)
- {
- if (string == null || string.length() < 1)
- return string;
- char[] cc = string.toCharArray();
- for (int ci = cc.length - 1; ci > 0; ci--)
- {
- if (Character.isWhitespace(cc[ci]))
- cc[ci] = ' ';
- }
- return new String(cc);
- }
-
}
Added: core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectConstants.java
===================================================================
--- core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectConstants.java (rev 0)
+++ core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectConstants.java 2012-01-06 09:43:10 UTC (rev 5426)
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2012 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.database.utils;
+
+/**
+ * @author <a href="abazko at exoplatform.com">Anatoliy Bazko</a>
+ * @version $Id: DialectConstants.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class DialectConstants
+{
+ /**
+ * DB_DIALECT_AUTO.
+ */
+ public final static String DB_DIALECT_AUTO = "Auto".intern();
+
+ /**
+ * DB_DIALECT_GENERIC.
+ */
+ public final static String DB_DIALECT_GENERIC = "Generic".intern();
+
+ /**
+ * DB_DIALECT_ORACLE.
+ */
+ public final static String DB_DIALECT_ORACLE = "Oracle".intern();
+
+ /**
+ * DB_DIALECT_ORACLEOCI.
+ */
+ public final static String DB_DIALECT_ORACLEOCI = "Oracle-OCI".intern();
+
+ /**
+ * DB_DIALECT_PGSQL.
+ */
+ public final static String DB_DIALECT_PGSQL = "PgSQL".intern();
+
+ /**
+ * DB_DIALECT_MYSQL.
+ */
+ public final static String DB_DIALECT_MYSQL = "MySQL".intern();
+
+ /**
+ * DB_DIALECT_MYSQL_UTF8.
+ */
+ public final static String DB_DIALECT_MYSQL_UTF8 = "MySQL-UTF8".intern();
+
+ /**
+ * DB_DIALECT_MYSQL_MYISAM.
+ */
+ public final static String DB_DIALECT_MYSQL_MYISAM = "MySQL-MyISAM".intern();
+
+ /**
+ * DB_DIALECT_MYSQL_MYISAM_UTF8.
+ */
+ public final static String DB_DIALECT_MYSQL_MYISAM_UTF8 = "MySQL-MyISAM-UTF8".intern();
+
+ /**
+ * DB_DIALECT_HSQLDB.
+ */
+ public final static String DB_DIALECT_HSQLDB = "HSQLDB".intern();
+
+ /**
+ * DB_DIALECT_DB2.
+ */
+ public final static String DB_DIALECT_DB2 = "DB2".intern();
+
+ /**
+ * DB_DIALECT_DB2V8.
+ */
+ public final static String DB_DIALECT_DB2V8 = "DB2V8".intern();
+
+ /**
+ * DB_DIALECT_MSSQL.
+ */
+ public final static String DB_DIALECT_MSSQL = "MSSQL".intern();
+
+ /**
+ * DB_DIALECT_SYBASE.
+ */
+ public final static String DB_DIALECT_SYBASE = "Sybase".intern();
+
+ /**
+ * DB_DIALECT_DERBY.
+ */
+ public final static String DB_DIALECT_DERBY = "Derby".intern();
+
+ /**
+ * DB_DIALECT_INGRES.
+ */
+ public final static String DB_DIALECT_INGRES = "Ingres".intern();
+
+ /**
+ * DB_DIALECT_H2.
+ */
+ public final static String DB_DIALECT_H2 = "H2".intern();
+
+ /**
+ * DB_DIALECTS.
+ */
+ public final static String[] DB_DIALECTS = {DB_DIALECT_GENERIC, DB_DIALECT_ORACLE, DB_DIALECT_ORACLEOCI,
+ DB_DIALECT_PGSQL, DB_DIALECT_MYSQL, DB_DIALECT_HSQLDB, DB_DIALECT_DB2, DB_DIALECT_DB2V8, DB_DIALECT_MSSQL,
+ DB_DIALECT_SYBASE, DB_DIALECT_DERBY, DB_DIALECT_MYSQL_UTF8, DB_DIALECT_INGRES, DB_DIALECT_H2,
+ DB_DIALECT_MYSQL_MYISAM, DB_DIALECT_MYSQL_MYISAM_UTF8};
+
+}
Added: core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectDetecter.java
===================================================================
--- core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectDetecter.java (rev 0)
+++ core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/DialectDetecter.java 2012-01-06 09:43:10 UTC (rev 5426)
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.database.utils;
+
+import org.exoplatform.commons.utils.SecurityHelper;
+
+import java.security.PrivilegedExceptionAction;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+
+/**
+ * JDBC dialect detecter based on database metadata and vendor product name.
+ *
+ * @author <a href="mailto:peter.nedonosko at exoplatform.com">Peter Nedonosko</a>
+ * @version $Id:DialectDetecter.java 1111 2010-01-01 00:00:01Z pnedonosko $
+ */
+public class DialectDetecter
+{
+
+ /**
+ * Detect databse dialect using JDBC metadata. Based on code of
+ * http://svn.jboss.org/repos/hibernate/core/trunk/core/src/main/java/org/hibernate/
+ * dialect/resolver/StandardDialectResolver.java
+ *
+ * @param jdbcConn Connection
+ * @return String
+ * @throws SQLException if error occurs
+ */
+ public static String detect(final DatabaseMetaData metaData) throws SQLException
+ {
+ final String databaseName =
+ SecurityHelper.doPrivilegedSQLExceptionAction(new PrivilegedExceptionAction<String>()
+ {
+ public String run() throws Exception
+ {
+ return metaData.getDatabaseProductName();
+ }
+ });
+
+ if ("HSQL Database Engine".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_HSQLDB;
+ }
+
+ if ("H2".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_H2;
+ }
+
+ if ("MySQL".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_MYSQL;
+ }
+
+ if ("PostgreSQL".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_PGSQL;
+ }
+
+ if ("Apache Derby".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_DERBY;
+ }
+
+ if ("ingres".equalsIgnoreCase(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_INGRES;
+ }
+
+ if (databaseName.startsWith("Microsoft SQL Server"))
+ {
+ return DialectConstants.DB_DIALECT_MSSQL;
+ }
+
+ if ("Sybase SQL Server".equals(databaseName) || "Adaptive Server Enterprise".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_SYBASE;
+ }
+
+ if (databaseName.startsWith("Adaptive Server Anywhere"))
+ {
+ return DialectConstants.DB_DIALECT_SYBASE;
+ }
+
+ if (databaseName.startsWith("DB2/"))
+ {
+ return DialectConstants.DB_DIALECT_DB2;
+ }
+
+ if ("Oracle".equals(databaseName))
+ {
+ return DialectConstants.DB_DIALECT_ORACLE;
+ }
+
+ return DialectConstants.DB_DIALECT_GENERIC;
+ }
+}
Deleted: core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/ExceptionManagementHelper.java
===================================================================
--- core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/ExceptionManagementHelper.java 2012-01-06 09:42:23 UTC (rev 5425)
+++ core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/ExceptionManagementHelper.java 2012-01-06 09:43:10 UTC (rev 5426)
@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2003-2011 eXo Platform SAS.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Affero General Public License
- * as published by the Free Software Foundation; either version 3
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see<http://www.gnu.org/licenses/>.
- */
-package org.exoplatform.services.database.utils;
-
-import java.sql.SQLException;
-
-/**
- * Created by The eXo Platform SAS.
- *
- * <br/>Date: 2011
- *
- * @author <a href="mailto:alex.reshetnyak at exoplatform.com.ua">Alex Reshetnyak</a>
- * @version $Id: ExceptionManagementHelper.java 111 2011-11-11 11:11:11Z rainf0x $
- */
-public class ExceptionManagementHelper
-{
- /**
- * Prepare message from SQLException.
- *
- * @param e
- * SQLException
- * @return String
- * The message form SQLException
- */
- public static String getFullSQLExceptionMessage(SQLException e)
- {
- SQLException next = e.getNextException();
- StringBuilder errorTrace = new StringBuilder();
-
- while (next != null)
- {
- errorTrace.append(next.getMessage());
- errorTrace.append("; ");
- next = next.getNextException();
- }
-
- Throwable cause = e.getCause();
-
- return errorTrace + (cause != null ? " (Cause: " + cause.getMessage() + ")" : "");
- }
-}
Added: core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/JDBCUtils.java
===================================================================
--- core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/JDBCUtils.java (rev 0)
+++ core/branches/2.5.x/exo.core.component.database/src/main/java/org/exoplatform/services/database/utils/JDBCUtils.java 2012-01-06 09:43:10 UTC (rev 5426)
@@ -0,0 +1,193 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.database.utils;
+
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * This class provides JDBC tools
+ *
+ * @author <a href="mailto:nfilotto at exoplatform.com">Nicolas Filotto</a>
+ * @version $Id$
+ */
+/**
+ * @author <a href="abazko at exoplatform.com">Anatoliy Bazko</a>
+ * @version $Id: JDBCUtils.java 34360 2009-07-22 23:58:59Z tolusha $
+ *
+ */
+public class JDBCUtils
+{
+ private static final Log LOG = ExoLogger.getLogger("exo.core.component.database.JDBCUtils");
+
+ /**
+ * Default SQL delimiter.
+ */
+ public static final String SQL_DELIMITER = ";";
+
+ /**
+ * SQL delimiter comment prefix.
+ */
+ public static final String SQL_DELIMITER_COMMENT_PREFIX = "/*$DELIMITER:";
+
+ public static final String SQL_DELIMITER_COMMENT_SUFFIX = "*/";
+
+ private JDBCUtils()
+ {
+ }
+
+ /**
+ * Indicates whether or not a given table exists
+ *
+ * @param tableName
+ * the name of the table to check
+ * @param con
+ * the connection to use
+ * @return <code>true</code> if it exists, <code>false</code> otherwise
+ */
+ public static boolean tableExists(String tableName, Connection con)
+ {
+ Statement stmt = null;
+ ResultSet trs = null;
+ try
+ {
+ stmt = con.createStatement();
+ trs = stmt.executeQuery("SELECT count(*) FROM " + tableName);
+ return trs.next();
+ }
+ catch (SQLException e)
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("SQLException occurs while checking the table " + tableName, e);
+ }
+ return false;
+ }
+ finally
+ {
+ if (trs != null)
+ {
+ try
+ {
+ trs.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the ResultSet: " + e);
+ }
+ }
+ if (stmt != null)
+ {
+ try
+ {
+ stmt.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the Statement: " + e);
+ }
+ }
+ }
+ }
+
+ /**
+ * Retrieves the full message from SQLException.
+ *
+ * @param exception
+ * SQLException which will be parsed
+ */
+ public static String getFullMessage(SQLException exception)
+ {
+ StringBuilder errorTrace = new StringBuilder(exception.getMessage());
+
+ SQLException next = exception.getNextException();
+ while (next != null)
+ {
+ errorTrace.append("; ");
+ errorTrace.append(next.getMessage());
+
+ next = next.getNextException();
+ }
+
+ Throwable cause = exception.getCause();
+
+ return errorTrace + (cause != null ? " (Cause: " + cause.getMessage() + ")" : "");
+ }
+
+ /**
+ * Replace whitespace characters with space character.
+ */
+ public static String cleanWhitespaces(String string)
+ {
+ if (string != null)
+ {
+ char[] cc = string.toCharArray();
+ for (int ci = cc.length - 1; ci > 0; ci--)
+ {
+ if (Character.isWhitespace(cc[ci]))
+ {
+ cc[ci] = ' ';
+ }
+ }
+ return new String(cc);
+ }
+ return string;
+ }
+
+ /**
+ * Split string resource with SQL Delimiter. Delimiter can be taken from resource
+ * at the begining of the first line. It surrounded with {@link #SQL_DELIMITER_COMMENT_PREFIX}
+ * and {@link #SQL_DELIMITER_COMMENT_SUFFIX}. Otherwise the default delimiter will
+ * be used {@link #SQL_DELIMITER}.
+ */
+ public static String[] splitWithSQLDelimiter(String resource)
+ {
+ if (resource.startsWith(SQL_DELIMITER_COMMENT_PREFIX))
+ {
+ try
+ {
+ String scripts = resource.substring(SQL_DELIMITER_COMMENT_PREFIX.length());
+
+ int endOfDelimIndex = scripts.indexOf(SQL_DELIMITER_COMMENT_SUFFIX);
+ String delim = scripts.substring(0, endOfDelimIndex).trim();
+
+ scripts = scripts.substring(endOfDelimIndex + 2).trim();
+ return scripts.split(delim);
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ LOG.warn("Error of parse SQL-script file. Invalid DELIMITER configuration. Valid format is '"
+ + SQL_DELIMITER_COMMENT_PREFIX + "XXX*/' at begin of the SQL-script file, where XXX - DELIMITER string."
+ + " Spaces will be trimed. ", e);
+ LOG.info("Using DELIMITER:[" + SQL_DELIMITER + "]");
+
+ return resource.split(SQL_DELIMITER);
+ }
+ }
+ else
+ {
+ return resource.split(SQL_DELIMITER);
+ }
+ }
+}
More information about the exo-jcr-commits
mailing list