r2472 - in trunk: topia-persistence/src/main/java/org/nuiton/topia topia-persistence/src/main/java/org/nuiton/topia/framework topia-persistence-hibernate/src/main/java/org/nuiton/topia topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework topia-persist
Author: athimel Date: 2012-05-10 18:29:00 +0200 (Thu, 10 May 2012) New Revision: 2472 Url: http://nuiton.org/repositories/revision/topia/2472 Log: Reimplement TopiaConnectionProvider in t-p-hibernate Create TopiaSpecificUtil for all non-JPA standard operations Move isSchemaExist from TopiaUtil to TopiaContext Added: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate/ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate/TopiaHibernateConnectionProvider.java trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernateSpecificUtil.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java Removed: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernatePersistenceProvider.java trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-it.properties trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-legacy.properties trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-mapping.properties trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPersistenceProvider.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -123,6 +123,15 @@ throws TopiaNotFoundException; /** + * Detect if the table is created on storage for a given persistent class. + * + * @param clazz the researched class + * @return true if the table exists on storage + * @throws TopiaException if the context is not initialized + */ + boolean isSchemaExist(Class<?> clazz) throws TopiaException; + + /** * Permet de créer le schema de la base de données. * * @throws TopiaException if any exception Deleted: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -1,328 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2011 CodeLutin - * %% - * This program 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 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.topia.framework; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -//import org.hibernate.cfg.Environment; -//import org.hibernate.connection.ConnectionProvider; -//import org.hibernate.connection.ConnectionProviderFactory; -//import org.hibernate.util.PropertiesHelper; -//import org.hibernate.util.ReflectHelper; - -import javax.persistence.PersistenceException; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -/** - * Customized connection provider. - * <p/> - * This provider fix the following bug : - * http://nuiton.org/issues/show/561 - * <p/> - * To use this connection provider, add this property to topia configuration - * <p/> - * <pre> - * config.setProperty(Environment.CONNECTION_PROVIDER, TopiaConnectionProvider.class.getName()); - * </pre> - * <p/> - * or in a properties file : - * <p/> - * <pre> - * hibernate.connection.provider_class=org.nuiton.topia.framework.TopiaConnectionProvider - * </pre> - * - * @author tchemit <chemit@codelutin.com> - * @since 2.5.3 - */ -public class TopiaConnectionProvider { -// implements ConnectionProvider { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(TopiaConnectionProvider.class); - - /** - * JDBC url of connection. - * <p/> - * This is a mandatory hibernate configuration vi the property - * {@link Environment#URL}. - */ - private String url; - - /** All grabbed connection properties */ - private Properties connectionProps; - - /** - * Sql isolation level to use in connection. - * <p/> - * Can be configured by hibernate property {@link Environment#ISOLATION_LEVELS}. - * - * @see Connection#getTransactionIsolation() - */ - private Integer isolation; - - /** - * auto commit connection state. - * <p/> - * Can be configured by hibernate property {@link Environment#AUTOCOMMIT}. - * - * @see Connection#getAutoCommit() - */ - private boolean autocommit; - - /** - * Size of connection pool. - * <p/> - * By default use {@code 20}, can be specify by using the hibernate - * configuration property {@link Environment#POOL_SIZE}. - */ - private int poolSize; - - /** Our pool of connections which are not closed and availables. */ - private final List<Connection> pool; - - public TopiaConnectionProvider() { - pool = new ArrayList<Connection>(); - } - -// @Override - public void configure(Properties props) throws PersistenceException { -// String driverClass = props.getProperty(Environment.DRIVER); -// -// poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20 -// if (log.isDebugEnabled()) { -// log.debug("Connection pool size: " + poolSize); -// } -// -// autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props); -// if (log.isDebugEnabled()) -// log.debug("autocommit mode: " + autocommit); -// -// isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props); -// if (isolation != null) { -// if (log.isDebugEnabled()) { -// log.debug("JDBC isolation level: " + -// Environment.isolationLevelToString(isolation)); -// } -// } -// -// if (driverClass == null) { -// -// if (log.isWarnEnabled()) { -// log.warn("no JDBC Driver class was specified by property " + -// Environment.DRIVER); -// } -// } else { -// try { -// // trying via forName() first to be as close to DriverManager's semantics -// Class.forName(driverClass); -// } catch (ClassNotFoundException cnfe) { -// try { -// ReflectHelper.classForName(driverClass); -// } catch (ClassNotFoundException e) { -// String msg = "JDBC Driver class not found: " + driverClass; -// log.error(msg, e); -// throw new PersistenceException(msg, e); -// } -// } -// } -// -// url = props.getProperty(Environment.URL); -// if (url == null) { -// String msg = "JDBC URL was not specified by property " + -// Environment.URL; -// if (log.isErrorEnabled()) { -// log.error(msg); -// } -// throw new PersistenceException(msg); -// } -// -// connectionProps = -// ConnectionProviderFactory.getConnectionProperties(props); -// -// if (log.isDebugEnabled()) { -// log.debug("using driver: " + driverClass + " at URL: " + url); -// } -// // if debug level is enabled, then log the password, otherwise mask it -// if (log.isTraceEnabled()) { -// log.debug("connection properties: " + connectionProps); -// } else if (log.isDebugEnabled()) { -// log.debug("connection properties: " + -// PropertiesHelper.maskOut(connectionProps, "password")); -// } - } - -// @Override - public Connection getConnection() throws SQLException { - - Connection connection = null; - - synchronized (pool) { - - // try to use a connection from the pool (if any) - - while (!pool.isEmpty() && connection == null) { - int last = pool.size() - 1; - if (log.isTraceEnabled()) { - log.trace("using pooled JDBC connection, pool size: " + - last); - } - - connection = pool.remove(last); - if (connection.isClosed()) { - - // this connection is closed!, don't use it - connection = null; - - if (log.isDebugEnabled()) { - log.debug("Remove already closed connection from pool " + - connection); - } - } - } - } - - if (connection == null) { - - // the pool was empty, creates a new connection - - if (log.isDebugEnabled()) { - log.debug("opening new JDBC connection to " + url); - } - connection = DriverManager.getConnection(url, connectionProps); - } - - // configure connection - - if (isolation != null) { - connection.setTransactionIsolation(isolation); - } - if (connection.getAutoCommit() != autocommit) { - connection.setAutoCommit(autocommit); - } - - return connection; - } - -// @Override - public void closeConnection(Connection conn) throws SQLException { - - // if connection is already closed, nothing has to be done - // we can't keep this connection (and can not be push in pool) - - if (conn.isClosed()) { - - if (log.isDebugEnabled()) { - log.debug("Connection [" + conn + - "] alreay closed!, will not use it any longer "); - } - return; - } - - // connection was not closed, can push it in the pool (if pool is not - // full) - - synchronized (pool) { - int currentSize = pool.size(); - if (currentSize < getPoolSize()) { - if (log.isTraceEnabled()) { - log.trace("returning connection to pool, pool size: " + - (currentSize + 1)); - } - pool.add(conn); - return; - } - } - - // pool was full, must release the connection which will be loose - - if (log.isDebugEnabled()) { - log.debug("closing JDBC connection"); - } - - conn.close(); - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - close(); - } - -// @Override - public void close() { - - if (log.isDebugEnabled()) { - log.debug("cleaning up connection pool: " + url); - } - - for (Connection connection : pool) { - try { - connection.close(); - } catch (SQLException sqle) { - if (log.isWarnEnabled()) { - log.warn("problem closing pooled connection", sqle); - } - } - } - pool.clear(); - - } - -// @Override - public boolean supportsAggressiveRelease() { - return false; - } - - public String getUrl() { - return url; - } - - public Properties getConnectionProps() { - return connectionProps; - } - - public Integer getIsolation() { - return isolation; - } - - public List<Connection> getPool() { - return pool; - } - - public int getPoolSize() { - return poolSize; - } - - public boolean isAutocommit() { - return autocommit; - } - -} Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -29,18 +29,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -//import org.hibernate.EntityMode; -//import org.hibernate.ReplicationMode; -//import org.hibernate.event.PostDeleteEventListener; -//import org.hibernate.event.PostInsertEventListener; -//import org.hibernate.event.PostLoadEventListener; -//import org.hibernate.event.PostUpdateEventListener; -//import org.hibernate.event.PreDeleteEventListener; -//import org.hibernate.event.PreInsertEventListener; -//import org.hibernate.event.PreLoadEventListener; -//import org.hibernate.event.PreUpdateEventListener; -//import org.hibernate.tool.hbm2ddl.SchemaExport; -//import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaException; @@ -87,6 +75,19 @@ import static org.nuiton.i18n.I18n._; +//import org.hibernate.EntityMode; +//import org.hibernate.ReplicationMode; +//import org.hibernate.event.PostDeleteEventListener; +//import org.hibernate.event.PostInsertEventListener; +//import org.hibernate.event.PostLoadEventListener; +//import org.hibernate.event.PostUpdateEventListener; +//import org.hibernate.event.PreDeleteEventListener; +//import org.hibernate.event.PreInsertEventListener; +//import org.hibernate.event.PreLoadEventListener; +//import org.hibernate.event.PreUpdateEventListener; +//import org.hibernate.tool.hbm2ddl.SchemaExport; +//import org.hibernate.tool.hbm2ddl.SchemaUpdate; + /** * Le TopiaContextImpl est le point d'entrée pour accéder aux données. Il est * configuré par un fichier de propriétés. @@ -216,6 +217,12 @@ */ protected Set<Class<?>> persistenceClasses; + /** + * Provides all the support for non-JPA operations. The instance will be + * shared with the parent context. + */ + protected TopiaSpecificUtil specificUtil; + /** Default constructor, To be used for tests only. */ protected TopiaContextImpl() { } @@ -232,7 +239,7 @@ this.config = config; services = loadServices(config); preInitServices(services); - getJPAConfiguration(); // Force configuration loading + getJPAConfiguration(); // Force mapping loading postInitServices(services); } @@ -324,6 +331,29 @@ } @Override + public TopiaSpecificUtil getSpecificUtil() throws TopiaException { + TopiaSpecificUtil result = specificUtil; + if (getParentContext() != null) { + result = getParentContext().getSpecificUtil(); + } + if (result == null) { + throw new TopiaException("No TopiaSpecificUtil found."); + } + return result; + } + + @Override + public void registerSpecificUtil(TopiaSpecificUtil specificUtil) { + if (getParentContext() == null) { + this.specificUtil = specificUtil; + } else { + if (log.isWarnEnabled()) { + log.warn("Try to set a specific util to non root context"); + } + } + } + + @Override public Set<Class<?>> getPersistenceClasses() { if (getParentContext() != null) { return getParentContext().getPersistenceClasses(); @@ -390,6 +420,7 @@ } jpaConfiguration.put("topia.entities", persistenceClasses); // TODO AThimel 10/05/2012 Push topia.entities to constants + jpaConfiguration.put("topia.context", this); // TODO AThimel 10/05/2012 Push topia.context to constants // FIXME AThimel 10/05/2012 Don't push all the configuration, maybe only a part of it ? for (Map.Entry<Object, Object> entry : getConfig().entrySet()) { @@ -1112,56 +1143,55 @@ throws TopiaException { checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", "replicateEntity")); - boolean result = TopiaUtil.isSchemaExist(this, clazz.getName()); + + boolean result = getSpecificUtil().isSchemaExist(clazz); + return result; } @Override public void createSchema() throws TopiaException { -// try { -// boolean showSchema = false; -// if (log.isDebugEnabled()) { -// showSchema = true; -// } -// getFiresSupport().firePreCreateSchema(this); -// new SchemaExport(getHibernateConfiguration()).create(showSchema, -// true); -// getFiresSupport().firePostCreateSchema(this); -// } catch (PersistenceException eee) { -// throw new TopiaException( -// _("topia.persistence.error.create.schema", -// eee.getMessage()), eee); -// } + try { + boolean showSchema = false; + if (log.isDebugEnabled()) { + showSchema = true; + } + getFiresSupport().firePreCreateSchema(this); + getSpecificUtil().createSchema(showSchema); + getFiresSupport().firePostCreateSchema(this); + } catch (PersistenceException eee) { + throw new TopiaException( + _("topia.persistence.error.create.schema", + eee.getMessage()), eee); + } } @Override public void showCreateSchema() throws TopiaException { -// try { -// new SchemaExport(getHibernateConfiguration()). -// execute(true, false, false, true); -// } catch (PersistenceException eee) { -// throw new TopiaException( -// _("topia.persistence.error.create.schema", -// eee.getMessage()), eee); -// } + try { + getSpecificUtil().showCreateSchema(); + } catch (PersistenceException eee) { + throw new TopiaException( + _("topia.persistence.error.create.schema", + eee.getMessage()), eee); + } } @Override public void updateSchema() throws TopiaException { -// try { -// boolean showSchema = false; -// if (log.isDebugEnabled()) { -// showSchema = true; -// } -// getFiresSupport().firePreUpdateSchema(this); -// new SchemaUpdate(getHibernateConfiguration()).execute(showSchema, -// true); -// getFiresSupport().firePostUpdateSchema(this); -// } catch (PersistenceException eee) { -// throw new TopiaException( -// _("topia.persistence.error.update.schema", -// eee.getMessage()), eee); -// } + try { + boolean showSchema = false; + if (log.isDebugEnabled()) { + showSchema = true; + } + getFiresSupport().firePreUpdateSchema(this); + getSpecificUtil().updateSchema(showSchema); + getFiresSupport().firePostUpdateSchema(this); + } catch (PersistenceException eee) { + throw new TopiaException( + _("topia.persistence.error.update.schema", + eee.getMessage()), eee); + } } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -95,15 +95,6 @@ void setUseFlushMode(boolean useFlushMode); /** - * Detect if the table is created on storage for a given persistant class. - * - * @param clazz the researched class - * @return Returns the hibernate. - * @throws TopiaException si aucune transaction n'est ouverte - */ - boolean isSchemaExist(Class<?> clazz) throws TopiaException; - - /** * Get DAO for specified class. If Specialized DAO exists then it returned * otherwize TopiaDAO<entityClass> is returned * @@ -136,5 +127,23 @@ Set<Class<?>> getPersistenceClasses(); + /** + * Get the registered the TopiaSpecificUtil. It may be null, meaning that + * the chosen JPA implementation does not support specific operations. + * + * @return the specificUtil instance registered + * @see TopiaSpecificUtil + */ + TopiaSpecificUtil getSpecificUtil() throws TopiaException; + + /** + * Used to register the TopiaSpecificUtil. The registered instance will be + * used to achieve any operation that does not fit the JPA standard. + * + * @param specificUtil the instance to register + * @see TopiaSpecificUtil + */ + void registerSpecificUtil(TopiaSpecificUtil specificUtil); + } //TopiaContextImplementor Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPersistenceProvider.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPersistenceProvider.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPersistenceProvider.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -1,17 +1,12 @@ package org.nuiton.topia.framework; +import org.apache.commons.lang3.tuple.Pair; import org.nuiton.topia.TopiaRuntimeException; import javax.persistence.EntityManagerFactory; import javax.persistence.spi.PersistenceProvider; import javax.persistence.spi.PersistenceUnitInfo; import javax.persistence.spi.ProviderUtil; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -22,12 +17,14 @@ public static final String TOPIA_PERSISTENCE_UNIT_NAME = "topia"; - public abstract EntityManagerFactory configureSpecific(String emName, Map<String, Object> map, Set<Class<?>> entities); + private static final String MISSING_PROPERTY = "Unable to create EntityManagerFactory, you have to provide property '%s'"; + public abstract Pair<EntityManagerFactory, TopiaSpecificUtil> configureSpecific(String emName, Map<String, Object> map, Set<Class<?>> entities); + @Override public EntityManagerFactory createEntityManagerFactory(String emName, Map map) { - EntityManagerFactory result = null; + EntityManagerFactory entityManagerFactory = null; if (TOPIA_PERSISTENCE_UNIT_NAME.equals(emName)) { // Use PersistenceProvider of the underlying JPA library @@ -35,12 +32,25 @@ Set<Class<?>> entityClasses = (Set<Class<?>>) map.get("topia.entities"); if (entityClasses == null) { - throw new TopiaRuntimeException("topia.entities list must be provided"); + String message = String.format(MISSING_PROPERTY, "topia.entities"); + throw new TopiaRuntimeException(message); } - result = configureSpecific(TOPIA_PERSISTENCE_UNIT_NAME, properties, entityClasses); + TopiaContextImplementor topiaContext = (TopiaContextImplementor)map.get("topia.context"); + if (topiaContext == null) { + String message = String.format(MISSING_PROPERTY, "topia.context"); + throw new TopiaRuntimeException(message); + } + + Pair<EntityManagerFactory, TopiaSpecificUtil> emfUtilPair = + configureSpecific(TOPIA_PERSISTENCE_UNIT_NAME, properties, entityClasses); + + entityManagerFactory = emfUtilPair.getLeft(); + + TopiaSpecificUtil specificUtil = emfUtilPair.getRight(); + topiaContext.registerSpecificUtil(specificUtil); } - return result; + return entityManagerFactory; } @Override Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -0,0 +1,51 @@ +package org.nuiton.topia.framework; + + +import org.nuiton.topia.TopiaContext; + +/** + * Provides all non-JPA standard methods (specific to an implementation) needed + * by ToPIA + * + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public interface TopiaSpecificUtil { + + /** + * Implementation-dependent method to detect if the table is created on + * storage for a given persistent class. This is used within the + * TopiaContext + * + * @param clazz the researched class + * @return true if the table exists on storage + * @see TopiaContext#isSchemaExist(Class) + */ + boolean isSchemaExist(Class<?> clazz); + + /** + * Implementation-dependent method to create the schema on storage. This is + * used within the TopiaContext + * + * @param showSchema indicates if schema must be displayed + * @see TopiaContext#createSchema() + */ + void createSchema(boolean showSchema); + + /** + * Implementation-dependent method to display the SQL queries for the + * schema creation on storage. This is used within the TopiaContext + * + * @see TopiaContext#showCreateSchema() + */ + void showCreateSchema(); + + /** + * Implementation-dependent method to update the schema on storage. This is + * used within the TopiaContext + * + * @param showSchema indicates if schema must be displayed + * @see TopiaContext#updateSchema() + */ + void updateSchema(boolean showSchema); + +} Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -151,76 +151,6 @@ return buffer.substring(2); } - /** - * Test si une entite donnee correspondant a une configuration existe en - * base. - * - * @param tx la session topia - * @param entityName le nom de l'entite a tester - * @return <tt>true</tt> si le schema de la table existe - * @since 2.6.4 - */ - public static boolean isSchemaExist(TopiaContext tx, - String entityName) { - - boolean exist = false; - -// try { -// Configuration configuration = -// ((TopiaContextImplementor) tx).getHibernateConfiguration(); -// PersistentClass classMapping = -// configuration.getClassMapping(entityName); -// if (classMapping == null) { -// if (log.isInfoEnabled()) { -// Iterator<?> itr = configuration.getClassMappings(); -// while (itr.hasNext()) { -// log.info("available mapping " + itr.next()); -// } -// } -// throw new IllegalArgumentException( -// "could not find entity with name " + entityName); -// } -// Table testTable = classMapping.getTable(); -// -// if (testTable == null) { -// throw new IllegalArgumentException( -// "could not find entity with name " + entityName); -// } -// ConnectionProvider connectionProvider = -// ConnectionProviderFactory.newConnectionProvider( -// configuration.getProperties()); -// -// Dialect dialect = Dialect.getDialect(configuration.getProperties()); -// -// Connection connection = null; -// try { -// connection = connectionProvider.getConnection(); -// -// DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); -// -// TableMetadata tmd = meta.getTableMetadata( -// testTable.getName(), testTable.getSchema(), -// testTable.getCatalog(), testTable.isQuoted()); -// -// if (tmd != null) { -// //table exist -// exist = true; -// } -// } finally { -// if (connection != null) { -// connection.close(); -// } -// } -// -// } catch (SQLException e) { -// log.error("Cant connect to database", e); -// } catch (TopiaNotFoundException e) { -// log.error("Cant connect to database", e); -// } - - return exist; - } - // /** // * Test si une entite donnee correspondant a une configuration existe en // * base. @@ -285,18 +215,18 @@ // // return exist; // } - - /** - * Test if the db associated to the given {@code configuration} contaisn any of - * the dealed entities. - * - * @param configuration hibernate db configuration - * @return {@code true} if there is no schema for any of the dealed entities, - * {@code false} otherwise. - * @since 2.5.3 - */ - public static boolean isSchemaEmpty(/*Configuration configuration*/) { - +// +// /** +// * Test if the db associated to the given {@code configuration} contaisn any of +// * the dealed entities. +// * +// * @param configuration hibernate db configuration +// * @return {@code true} if there is no schema for any of the dealed entities, +// * {@code false} otherwise. +// * @since 2.5.3 +// */ +// public static boolean isSchemaEmpty(Configuration configuration) { +// // try { // // ConnectionProvider connectionProvider = @@ -351,10 +281,10 @@ // } catch (SQLException e) { // log.error("Cant connect to database", e); // } +// +// return true; +// } - return true; - } - /** * Return hibernate schema name * Copied: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate/TopiaHibernateConnectionProvider.java (from rev 2470, trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java) =================================================================== --- trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate/TopiaHibernateConnectionProvider.java (rev 0) +++ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate/TopiaHibernateConnectionProvider.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -0,0 +1,343 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.topia.framework.hibernate; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; +import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.cfg.Environment; +import org.hibernate.service.spi.Configurable; +import org.hibernate.service.spi.Stoppable; + +import javax.persistence.PersistenceException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Customized connection provider. + * <p/> + * This provider fix the following bug : + * http://nuiton.org/issues/show/561 + * <p/> + * To use this connection provider, add this property to topia configuration + * <p/> + * <pre> + * config.setProperty(AvailableSettings.CONNECTION_PROVIDER, TopiaHibernateConnectionProvider.class.getName()); + * </pre> + * <p/> + * or in a properties file : + * <p/> + * <pre> + * hibernate.connection.provider_class=org.nuiton.topia.framework.hibernate.TopiaHibernateConnectionProvider + * </pre> + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.3 + */ +public class TopiaHibernateConnectionProvider implements ConnectionProvider, Configurable, Stoppable { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(TopiaHibernateConnectionProvider.class); + + /** + * JDBC url of connection. + * <p/> + * This is a mandatory hibernate configuration vi the property + * {@link Environment#URL}. + */ + private String url; + + /** All grabbed connection properties */ + private Properties connectionProps; + + /** + * Sql isolation level to use in connection. + * <p/> + * Can be configured by hibernate property {@link Environment#ISOLATION_LEVELS}. + * + * @see Connection#getTransactionIsolation() + */ + private Integer isolation; + + /** + * auto commit connection state. + * <p/> + * Can be configured by hibernate property {@link Environment#AUTOCOMMIT}. + * + * @see Connection#getAutoCommit() + */ + private boolean autocommit; + + /** + * Size of connection pool. + * <p/> + * By default use {@code 20}, can be specify by using the hibernate + * configuration property {@link Environment#POOL_SIZE}. + */ + private int poolSize; + + /** Our pool of connections which are not closed and availables. */ + private final List<Connection> pool; + + public TopiaHibernateConnectionProvider() { + pool = new ArrayList<Connection>(); + } + + @Override + public void configure(Map map) { + Map<String, String> configurationValues = (Map<String, String>)map; + + String driverClass = configurationValues.get(Environment.DRIVER); + + poolSize = ConfigurationHelper.getInt(AvailableSettings.POOL_SIZE, configurationValues, 20); // default pool size 20 + if (log.isDebugEnabled()) { + log.debug("Connection pool size: " + poolSize); + } + + autocommit = ConfigurationHelper.getBoolean( AvailableSettings.AUTOCOMMIT, configurationValues ); + if (log.isDebugEnabled()) + log.debug("autocommit mode: " + autocommit); + + isolation = ConfigurationHelper.getInteger( AvailableSettings.ISOLATION, configurationValues ); + if (isolation != null) { + if (log.isDebugEnabled()) { + log.debug("JDBC isolation level: " + + Environment.isolationLevelToString(isolation)); + } + } + + if (driverClass == null) { + + if (log.isWarnEnabled()) { + log.warn("no JDBC Driver class was specified by property " + + Environment.DRIVER); + } + } else { + try { + // trying via forName() first to be as close to DriverManager's semantics + Class.forName(driverClass); + } catch (ClassNotFoundException cnfe) { + try { + ReflectHelper.classForName(driverClass); + } catch (ClassNotFoundException e) { + String msg = "JDBC Driver class not found: " + driverClass; + log.error(msg, e); + throw new PersistenceException(msg, e); + } + } + } + + url = configurationValues.get(Environment.URL); + if (url == null) { + String msg = "JDBC URL was not specified by property " + + Environment.URL; + if (log.isErrorEnabled()) { + log.error(msg); + } + throw new PersistenceException(msg); + } + + connectionProps = + ConnectionProviderInitiator.getConnectionProperties(configurationValues); + + if (log.isDebugEnabled()) { + log.debug("using driver: " + driverClass + " at URL: " + url); + } + // if debug level is enabled, then log the password, otherwise mask it + if (log.isTraceEnabled()) { + log.debug("connection properties: " + connectionProps); + } else if (log.isDebugEnabled()) { + log.debug("connection properties: " + + ConfigurationHelper.maskOut(connectionProps, "password")); + } + } + + @Override + public Connection getConnection() throws SQLException { + + Connection connection = null; + + synchronized (pool) { + + // try to use a connection from the pool (if any) + + while (!pool.isEmpty() && connection == null) { + int last = pool.size() - 1; + if (log.isTraceEnabled()) { + log.trace("using pooled JDBC connection, pool size: " + + last); + } + + connection = pool.remove(last); + if (connection.isClosed()) { + + // this connection is closed!, don't use it + connection = null; + + if (log.isDebugEnabled()) { + log.debug("Remove already closed connection from pool " + + connection); + } + } + } + } + + if (connection == null) { + + // the pool was empty, creates a new connection + + if (log.isDebugEnabled()) { + log.debug("opening new JDBC connection to " + url); + } + connection = DriverManager.getConnection(url, connectionProps); + } + + // configure connection + + if (isolation != null) { + connection.setTransactionIsolation(isolation); + } + if (connection.getAutoCommit() != autocommit) { + connection.setAutoCommit(autocommit); + } + + return connection; + } + + @Override + public void closeConnection(Connection conn) throws SQLException { + + // if connection is already closed, nothing has to be done + // we can't keep this connection (and can not be push in pool) + + if (conn.isClosed()) { + + if (log.isDebugEnabled()) { + log.debug("Connection [" + conn + + "] alreay closed!, will not use it any longer "); + } + return; + } + + // connection was not closed, can push it in the pool (if pool is not + // full) + + synchronized (pool) { + int currentSize = pool.size(); + if (currentSize < getPoolSize()) { + if (log.isTraceEnabled()) { + log.trace("returning connection to pool, pool size: " + + (currentSize + 1)); + } + pool.add(conn); + return; + } + } + + // pool was full, must release the connection which will be loose + + if (log.isDebugEnabled()) { + log.debug("closing JDBC connection"); + } + + conn.close(); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + stop(); + } + + @Override + public void stop() { + + if (log.isDebugEnabled()) { + log.debug("cleaning up connection pool: " + url); + } + + for (Connection connection : pool) { + try { + connection.close(); + } catch (SQLException sqle) { + if (log.isWarnEnabled()) { + log.warn("problem closing pooled connection", sqle); + } + } + } + pool.clear(); + + } + + @Override + public boolean supportsAggressiveRelease() { + return false; + } + + public String getUrl() { + return url; + } + + public Properties getConnectionProps() { + return connectionProps; + } + + public Integer getIsolation() { + return isolation; + } + + public List<Connection> getPool() { + return pool; + } + + public int getPoolSize() { + return poolSize; + } + + public boolean isAutocommit() { + return autocommit; + } + + @Override + public boolean isUnwrappableAs(Class unwrapType) { + return false; + } + + @Override + public <T> T unwrap(Class<T> unwrapType) { + return null; + } + +} Property changes on: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/framework/hibernate/TopiaHibernateConnectionProvider.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernatePersistenceProvider.java =================================================================== --- trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernatePersistenceProvider.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernatePersistenceProvider.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -1,15 +1,16 @@ package org.nuiton.topia.persistence.hibernate; +import org.apache.commons.lang3.tuple.Pair; import org.hibernate.ejb.Ejb3Configuration; import org.hibernate.ejb.packaging.NamedInputStream; import org.hibernate.ejb.packaging.PersistenceMetadata; import org.nuiton.topia.framework.TopiaPersistenceProvider; +import org.nuiton.topia.framework.TopiaSpecificUtil; import javax.persistence.EntityManagerFactory; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -20,7 +21,7 @@ public class TopiaHibernatePersistenceProvider extends TopiaPersistenceProvider { @Override - public EntityManagerFactory configureSpecific(String entityManagerName, Map<String, Object> properties, Set<Class<?>> entities) { + public Pair<EntityManagerFactory, TopiaSpecificUtil> configureSpecific(String entityManagerName, Map<String, Object> properties, Set<Class<?>> entities) { Map<String, Object> hibernateProperties = new HashMap<String, Object>(properties); // On convertit tout ce qui est en topia.connection.* vers hibernate.connection.* @@ -55,10 +56,17 @@ metadata.setHbmfiles(entitiesHbmFiles); Ejb3Configuration configured = cfg.configure(metadata, hibernateProperties); - EntityManagerFactory result = null; + EntityManagerFactory entityManagerFactory = null; if (configured != null) { - result = configured.buildEntityManagerFactory(); + entityManagerFactory = configured.buildEntityManagerFactory(); } + + TopiaSpecificUtil specificUtil = + new TopiaHibernateSpecificUtil(configured.getHibernateConfiguration()); + + Pair<EntityManagerFactory, TopiaSpecificUtil> result = + Pair.of(entityManagerFactory, specificUtil); return result; } + } Added: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernateSpecificUtil.java =================================================================== --- trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernateSpecificUtil.java (rev 0) +++ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/hibernate/TopiaHibernateSpecificUtil.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -0,0 +1,119 @@ +package org.nuiton.topia.persistence.hibernate; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; +import org.hibernate.dialect.Dialect; +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Table; +import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; +import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.tool.hbm2ddl.DatabaseMetadata; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.hibernate.tool.hbm2ddl.TableMetadata; +import org.nuiton.topia.TopiaNotFoundException; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaSpecificUtil; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Iterator; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class TopiaHibernateSpecificUtil implements TopiaSpecificUtil { + + private static final Log log = LogFactory.getLog(TopiaHibernateSpecificUtil.class); + + protected Configuration configuration; + + public TopiaHibernateSpecificUtil(Configuration configuration) { + this.configuration = configuration; + } + + @Override + public void createSchema(boolean showSchema) { + SchemaExport schemaExport = new SchemaExport(configuration); + schemaExport.create(showSchema, true); + } + + @Override + public void showCreateSchema() { + new SchemaExport(configuration).execute(true, false, false, true); + } + + @Override + public void updateSchema(boolean showSchema) { + new SchemaUpdate(configuration).execute(showSchema, true); + } + + @Override + public boolean isSchemaExist(Class<?> clazz) { + String entityName = clazz.getName(); + + boolean result = false; + try { + PersistentClass classMapping = + configuration.getClassMapping(entityName); + if (classMapping == null) { + if (log.isInfoEnabled()) { + Iterator<?> itr = configuration.getClassMappings(); + while (itr.hasNext()) { + log.info("available mapping " + itr.next()); + } + } + throw new IllegalArgumentException( + "could not find entity with name " + entityName); + } + Table testTable = classMapping.getTable(); + + if (testTable == null) { + throw new IllegalArgumentException( + "could not find entity with name " + entityName); + } +// ConnectionProvider connectionProvider = +// ConnectionProviderFactory.newConnectionProvider( +// configuration.getProperties()); + + Dialect dialect = Dialect.getDialect(configuration.getProperties()); + + Connection connection = null; + try { +// connection = connectionProvider.getConnection(); + + // FIXME AThimel 10/05/2012 It would be better to re-use the ConnectionProvider but I can't find how to do it for the moment + String url = configuration.getProperty(AvailableSettings.URL); + String username = configuration.getProperty(AvailableSettings.USER); + String password = configuration.getProperty(AvailableSettings.PASS); + connection = DriverManager.getConnection(url, username, password); + + DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); + + TableMetadata tmd = meta.getTableMetadata( + testTable.getName(), testTable.getSchema(), + testTable.getCatalog(), testTable.isQuoted()); + + if (tmd != null) { + //table exist + result = true; + } + } finally { + if (connection != null) { + connection.close(); + } + } + + } catch (SQLException e) { + log.error("Cant connect to database", e); +// } catch (TopiaNotFoundException e) { +// log.error("Cant connect to database", e); + } + + return result; + } +} Modified: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderHardCoded.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -27,15 +27,21 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.service.spi.Configurable; +import org.hibernate.service.spi.Stoppable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; +import java.util.Map; import java.util.Properties; /** @@ -44,7 +50,7 @@ * @author tchemit <chemit@codelutin.com> * @since 2.5.3 */ -public class TopiaConnectionProviderHardCoded implements ConnectionProvider { +public class TopiaConnectionProviderHardCoded implements ConnectionProvider, Configurable, Stoppable { private String url; @@ -64,23 +70,19 @@ private static final Log log = LogFactory.getLog(TopiaConnectionProviderHardCoded.class); - public TopiaConnectionProviderHardCoded() { - System.out.println("created"); - } - public void configure(Properties props) throws HibernateException { - String driverClass = props.getProperty(Environment.DRIVER); + @Override + public void configure(Map map) throws HibernateException { + Map<String, String> props = (Map<String, String>)map; + String driverClass = props.get(Environment.DRIVER); -// poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20 - poolSize = 20; //default pool size 20 + poolSize = ConfigurationHelper.getInt(AvailableSettings.POOL_SIZE, props, 20); // default pool size 20 log.info("Using Hibernate built-in connection pool (not for production use!)"); log.info("Hibernate connection pool size: " + poolSize); -// autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props); - autocommit = true; + autocommit = ConfigurationHelper.getBoolean( AvailableSettings.AUTOCOMMIT, props ); log.info("autocommit mode: " + autocommit); -// isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props); - isolation = 0; + isolation = ConfigurationHelper.getInteger( AvailableSettings.ISOLATION, props ); if (isolation != null) log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation)); @@ -91,13 +93,13 @@ // trying via forName() first to be as close to DriverManager's semantics Class.forName(driverClass); } catch (ClassNotFoundException cnfe) { -// try { -// ReflectHelper.classForName(driverClass); -// } catch (ClassNotFoundException e) { -// String msg = "JDBC Driver class not found: " + driverClass; -// log.error(msg, e); -// throw new HibernateException(msg, e); -// } + try { + ReflectHelper.classForName(driverClass); + } catch (ClassNotFoundException e) { + String msg = "JDBC Driver class not found: " + driverClass; + log.error(msg, e); + throw new HibernateException(msg, e); + } } } @@ -105,17 +107,10 @@ // the real directory where db is and then make sure hibernate always // use the connection provider... String directory = - props.getProperty(TopiaConnectionProviderTest.TEST_URL); + props.get(TopiaConnectionProviderTest.TEST_URL); url = directory; -// url = props.getProperty(Environment.URL); -// if (url == null) { -// String msg = "JDBC URL was not specified by property " + Environment.URL; -// log.error(msg); -// throw new HibernateException(msg); -// } - connectionProps = ConnectionProviderInitiator.getConnectionProperties(props); log.info("using driver: " + driverClass + " at URL: " + url); @@ -123,7 +118,7 @@ if (log.isDebugEnabled()) { log.info("connection properties: " + connectionProps); } else if (log.isInfoEnabled()) { -// log.info("connection properties: " + PropertiesHelper.maskOut(connectionProps, "password")); + log.info("connection properties: " + ConfigurationHelper.maskOut(connectionProps, "password")); } } @@ -186,10 +181,11 @@ @Override protected void finalize() throws Throwable { super.finalize(); - close(); + stop(); } - public void close() { + @Override + public void stop() { log.info("cleaning up connection pool: " + url); Modified: trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java =================================================================== --- trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-hibernate/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -24,8 +24,9 @@ */ package org.nuiton.topia.framework; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.nuiton.topia.TopiaContext; @@ -45,7 +46,7 @@ import static org.junit.Assert.assertNotNull; /** - * To test the {@link TopiaConnectionProvider} and make sure all connections + * To test the {@link org.nuiton.topia.framework.hibernate.TopiaHibernateConnectionProvider} and make sure all connections * are done from here... * * @author tchemit <chemit@codelutin.com> @@ -53,8 +54,8 @@ */ public class TopiaConnectionProviderTest { -// private static final Log log = -// LogFactory.getLog(TopiaConnectionProviderTest.class); + private static final Log log = + LogFactory.getLog(TopiaConnectionProviderTest.class); public static final String TEST_URL = "testURL"; @@ -87,32 +88,11 @@ }; @Test - @Ignore // FIXME AThimel 09/05/2012 Remove @Ignore public void testWithHardcoded() throws Exception { -// Properties dbProperties = TestHelper.loadHibernateConfiguration( -// "/TopiaConnectionProviderHardcoded.properties"); -// -// File directory = new File(TestHelper.getDbName(testBasedir, "testWithHardcoded")); - String dbPath = (String) db.getDbConfiguration().get("dbPath"); String dbPathFake = (String) db.getDbConfiguration().get("dbPathFake"); -// new File(directory, "real" + File.separator + "db").getAbsolutePath(); -// Assert.assertFalse(new File(dbPath).getParentFile().exists()); - -// String dbPathFake = new File(directory, "fake" + File.separator + "db").getAbsolutePath(); - -// Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); - -// // give the path where connection provider will create db -// dbProperties.setProperty(TEST_URL, "jdbc:h2:file:" + dbPath); -// -// // give a fake db path (we will make sure it is never create after hibernate usage). -// dbProperties.setProperty(Environment.URL, "jdbc:h2:file:" + dbPathFake); -// -// root = TopiaContextFactory.getContext(dbProperties); - Locale.setDefault(Locale.FRANCE); doStuffOnDb(); Modified: trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-it.properties =================================================================== --- trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-it.properties 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-it.properties 2012-05-10 16:29:00 UTC (rev 2472) @@ -6,4 +6,7 @@ topia.connection.username=sa topia.connection.password= -topia.connection.driver_class=org.h2.Driver \ No newline at end of file +topia.connection.driver_class=org.h2.Driver + +#Not necessary, but useful +hibernate.connection.provider_class=org.nuiton.topia.framework.hibernate.TopiaHibernateConnectionProvider Modified: trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-legacy.properties =================================================================== --- trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-legacy.properties 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-legacy.properties 2012-05-10 16:29:00 UTC (rev 2472) @@ -6,4 +6,7 @@ topia.connection.username=sa topia.connection.password= -topia.connection.driver_class=org.h2.Driver \ No newline at end of file +topia.connection.driver_class=org.h2.Driver + +#Not necessary, but useful +hibernate.connection.provider_class=org.nuiton.topia.framework.hibernate.TopiaHibernateConnectionProvider Modified: trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-mapping.properties =================================================================== --- trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-mapping.properties 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-hibernate/src/test/resources/TopiaContext-mapping.properties 2012-05-10 16:29:00 UTC (rev 2472) @@ -6,4 +6,7 @@ topia.connection.username=sa topia.connection.password= -topia.connection.driver_class=org.h2.Driver \ No newline at end of file +topia.connection.driver_class=org.h2.Driver + +#Not necessary, but useful +hibernate.connection.provider_class=org.nuiton.topia.framework.hibernate.TopiaHibernateConnectionProvider Modified: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java =================================================================== --- trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/TopiaContextFactoryTest.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -76,7 +76,7 @@ String databaseName = "h2data-testGetContextByPropertie"; File dbDirectory = new File(testBasedir, databaseName); String url = "jdbc:h2:file:" + dbDirectory; - properties.setProperty("hibernate.connection.url", url); + properties.setProperty(TopiaContextFactory.CONFIG_URL, url); TopiaContextImpl test = new TopiaContextImpl(properties); TopiaContextFactory.contextCache.put(properties, test); Modified: trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java =================================================================== --- trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java 2012-05-10 10:37:36 UTC (rev 2471) +++ trunk/topia-persistence-tck/src/main/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java 2012-05-10 16:29:00 UTC (rev 2472) @@ -94,20 +94,25 @@ @Test(expected = IllegalArgumentException.class) public void testIsSchemaExistFailed() throws Exception { - TopiaUtil.isSchemaExist(db.getRootCtxt(), "fake"); + TopiaContext rootContext = db.getRootCtxt(); + rootContext.beginTransaction(); + rootContext.isSchemaExist(String.class); } @Test public void testIsSchemaExist() throws Exception { TopiaContext rootContext = db.getRootCtxt(); - boolean actual = TopiaUtil.isSchemaExist(rootContext, - PersonImpl.class.getName()); + // XXX AThimel 10/05/2012 This cannot be done with ToPIA 3 because of + // independence regarding to the JPA implem, which changes the way EntityManager is created +// boolean actual = rootContext.isSchemaExist(PersonImpl.class); + boolean actual = false; assertFalse(actual); + TopiaContext tx = rootContext.beginTransaction(); tx.createSchema(); - actual = TopiaUtil.isSchemaExist(rootContext, PersonImpl.class.getName()); + actual = rootContext.isSchemaExist(PersonImpl.class); assertTrue(actual);
participants (1)
-
athimel@users.nuiton.org