[exo-jcr-commits] exo-jcr SVN: r1769 - core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/impl.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Feb 12 04:21:57 EST 2010


Author: sergiykarpenko
Date: 2010-02-12 04:21:56 -0500 (Fri, 12 Feb 2010)
New Revision: 1769

Modified:
   core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/impl/HibernateServiceImpl.java
Log:
EXOJCR-485: auto detection dialect implemented

Modified: core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/impl/HibernateServiceImpl.java
===================================================================
--- core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/impl/HibernateServiceImpl.java	2010-02-12 08:39:36 UTC (rev 1768)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/impl/HibernateServiceImpl.java	2010-02-12 09:21:56 UTC (rev 1769)
@@ -34,15 +34,24 @@
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.dialect.Dialect;
+import org.hibernate.dialect.resolver.DialectFactory;
 import org.hibernate.tool.hbm2ddl.SchemaUpdate;
 
 import java.io.Serializable;
 import java.net.URL;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Properties;
 
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
 /**
  * Created by The eXo Platform SAS .
  * 
@@ -52,6 +61,7 @@
  */
 public class HibernateServiceImpl implements HibernateService, ComponentRequestLifecycle
 {
+
    public static final String AUTO_DIALECT = "AUTO";
 
    private ThreadLocal<Session> threadLocal_;
@@ -69,7 +79,6 @@
    {
       threadLocal_ = new ThreadLocal<Session>();
       PropertiesParam param = initParams.getPropertiesParam("hibernate.properties");
-
       HibernateSettingsFactory settingsFactory = new HibernateSettingsFactory(new ExoCacheProvider(cacheService));
       conf_ = new HibernateConfigurationImpl(settingsFactory);
       Iterator properties = param.getPropertyIterator();
@@ -77,42 +86,135 @@
       {
          Property p = (Property)properties.next();
 
+         //
          String name = p.getName();
          String value = p.getValue();
 
          // Julien: Don't remove that unless you know what you are doing
-         if (name.equals("hibernate.dialect"))
+         if (name.equals("hibernate.dialect") && !value.equalsIgnoreCase(AUTO_DIALECT))
          {
+            Package pkg = Dialect.class.getPackage();
+            String dialect = value.substring(22);
+            value = pkg.getName() + "." + dialect; // 22 is the length of
+            // "org.hibernate.dialect"
+            log_.info("Using dialect " + dialect);
+         }
 
-            if (!value.equalsIgnoreCase(AUTO_DIALECT))
+         //
+         conf_.setProperty(name, value);
+      }
+
+      // Replace the potential "java.io.tmpdir" variable in the connection URL
+      String connectionURL = conf_.getProperty("hibernate.connection.url");
+      if (connectionURL != null)
+      {
+         connectionURL = connectionURL.replace("${java.io.tmpdir}", System.getProperty("java.io.tmpdir"));
+         conf_.setProperty("hibernate.connection.url", connectionURL);
+      }
+
+      // Auto-detect dialect if "hibernate.dialect" is set as AUTO or is not set.
+
+      String dialect = conf_.getProperty("hibernate.dialect");
+
+      if (dialect != null && dialect.equalsIgnoreCase(AUTO_DIALECT))
+      {
+         // detect dialect and replace parameter
+         Connection connection = null;
+
+         try
+         {
+            // check is there is datasource
+            String dataSourceName = conf_.getProperty("hibernate.connection.datasource");
+            if (dataSourceName != null)
             {
+               //detect with datasource
+               DataSource dataSource;
+               try
+               {
+                  dataSource = (DataSource)new InitialContext().lookup(dataSourceName);
+                  if (dataSource == null)
+                  {
+                     log_.error("DataSource is configured but not finded.", new Exception());
+                  }
 
-               // TODO throw exception if dialect is incorrect
-               Package pkg = Dialect.class.getPackage();
-               String dialect = value.substring(22);
-               value = pkg.getName() + "." + dialect; // 22 is the length of
-               // "org.hibernate.dialect"
-               log_.info("Using dialect " + dialect);
-               conf_.setProperty(name, value);
+                  connection = dataSource.getConnection();
+
+                  Dialect d = DialectFactory.buildDialect(new Properties(), connection);
+                  conf_.setProperty("hibernate.dialect", d.getClass().getName());
+
+               }
+               catch (NamingException e)
+               {
+                  log_.error(e.getMessage(), e);
+               }
+
             }
             else
             {
-               log_.info("Dialect will be automaticaly detected by Hibernate.");
+
+               String url = conf_.getProperty("hibernate.connection.url");
+               if (url != null)
+               {
+                  //detect with url               
+                  //get driver class
+
+                  try
+                  {
+                     Class.forName(conf_.getProperty("hibernate.connection.driver_class")).newInstance();
+                  }
+                  catch (InstantiationException e)
+                  {
+                     log_.error(e.getMessage(), e);
+                  }
+                  catch (IllegalAccessException e)
+                  {
+                     log_.error(e.getMessage(), e);
+                  }
+                  catch (ClassNotFoundException e)
+                  {
+                     log_.error(e.getMessage(), e);
+                  }
+
+                  String dbUserName = conf_.getProperty("hibernate.connection.username");
+                  String dbPassword = conf_.getProperty("hibernate.connection.password");
+
+                  connection =
+                     dbUserName != null ? DriverManager.getConnection(url, dbUserName, dbPassword) : DriverManager
+                        .getConnection(url);
+
+                  Dialect d = DialectFactory.buildDialect(new Properties(), connection);
+                  conf_.setProperty("hibernate.dialect", d.getClass().getName());
+
+               }
+               else
+               {
+                  Exception e = new Exception("Any data source is not configured!");
+                  log_.error(e.getMessage(), e);
+               }
             }
+
          }
-         else
+         catch (SQLException e)
          {
-            conf_.setProperty(name, value);
+            log_.error(e.getMessage(), e);
          }
+         finally
+         {
+            if (connection != null)
+            {
+               try
+               {
+                  connection.close();
+               }
+               catch (SQLException e)
+               {
+                  log_.error(e.getMessage(), e);
+               }
+            }
+         }
+
       }
 
-      // Replace the potential "java.io.tmpdir" variable in the connection URL
-      String connectionURL = conf_.getProperty("hibernate.connection.url");
-      if (connectionURL != null)
-      {
-         connectionURL = connectionURL.replace("${java.io.tmpdir}", System.getProperty("java.io.tmpdir"));
-         conf_.setProperty("hibernate.connection.url", connectionURL);
-      }
    }
 
    public void addPlugin(ComponentPlugin plugin)



More information about the exo-jcr-commits mailing list