Author: tchemit Date: 2010-12-26 16:25:30 +0100 (Sun, 26 Dec 2010) New Revision: 2168 Url: http://nuiton.org/repositories/revision/topia/2168 Log: use last release of nuiton-i18n, nuiton-utils, eugene Evolution #1168: Implements a second way of migrate versions (one class by version) Added: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClass.java trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByMethod.java Modified: trunk/pom.xml trunk/topia-persistence/src/main/resources/META-INF/services/org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaModelPropertiesProviderTest.java trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallback.java trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2010-12-12 12:39:34 UTC (rev 2167) +++ trunk/pom.xml 2010-12-26 15:25:30 UTC (rev 2168) @@ -133,81 +133,6 @@ <scope>test</scope> </dependency> - <!--dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.8.2</version> - <scope>test</scope> - </dependency--> - - <!-- All theses dependancies are no more used - Keep them for topia-soa and topia-service-XXX - --> - - <!--dependency> - <groupId>org.apache.xmlrpc</groupId> - <artifactId>xmlrpc-server</artifactId> - <version>${xmlrpcVersion}</version> - <scope>compile</scope> - - </dependency> - <dependency> - <groupId>org.apache.xmlrpc</groupId> - <artifactId>xmlrpc-client</artifactId> - <version>${xmlrpcVersion}</version> - <scope>compile</scope> - </dependency--> - - <!-- Dependencies for class generation --> - <!--dependency> - <groupId>org.nuiton.thirdparty</groupId> - <artifactId>asm</artifactId> - <version>1.5.4-snapshot</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>xom</groupId> - <artifactId>xom</artifactId> - <version>1.1</version> - </dependency--> - - <!--dependency> - <groupId>org.codehaus.xfire</groupId> - <artifactId>xfire-java5</artifactId> - <version>1.2.6</version> - <scope>compile</scope> - <exclusions--> - <!-- Fix org.apache.ant conflict) --> - <!--exclusion> - <groupId>ant</groupId> - <artifactId>ant</artifactId> - </exclusion> - </exclusions> - </dependency> - - <dependency> - <groupId>commons-httpclient</groupId> - <artifactId>commons-httpclient</artifactId> - <version>3.1</version> - <scope>runtime</scope> - </dependency--> - - - <!-- branch 5.1 : ne fonctionne pas en 6+ --> - <!--dependency> - <groupId>jetty</groupId> - <artifactId>jetty</artifactId> - <version>5.1.10</version> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>org.apache.lucene</groupId> - <artifactId>lucene-core</artifactId> - <version>3.0.1</version> - <scope>compile</scope> - </dependency--> - </dependencies> </dependencyManagement> @@ -341,10 +266,10 @@ <projectId>topia</projectId> <!-- libs version --> - <eugeneVersion>2.3-SNAPSHOT</eugeneVersion> - <nuitonUtilsVersion>1.5.2-SNAPSHOT</nuitonUtilsVersion> + <eugeneVersion>2.3</eugeneVersion> + <nuitonUtilsVersion>1.5.2</nuitonUtilsVersion> <processorPluginVersion>1.0.4</processorPluginVersion> - <nuitonI18nVersion>2.0</nuitonI18nVersion> + <nuitonI18nVersion>2.0.1</nuitonI18nVersion> <xmlrpcVersion>3.1.2</xmlrpcVersion> <hibernateVersion>3.3.2.GA</hibernateVersion> Property changes on: trunk/topia-persistence/src/main/resources/META-INF/services/org.nuiton.eugene.ModelPropertiesUtil$ModelPropertiesProvider ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaModelPropertiesProviderTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaModelPropertiesProviderTest.java 2010-12-12 12:39:34 UTC (rev 2167) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaModelPropertiesProviderTest.java 2010-12-26 15:25:30 UTC (rev 2168) @@ -1,3 +1,27 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 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.generator; import org.junit.Assert; Property changes on: trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaModelPropertiesProviderTest.java ___________________________________________________________________ Modified: svn:keywords - Author Date Id Revision + Author Date Id Revision HeadURL Copied: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java (from rev 2167, trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallback.java) =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java (rev 0) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/AbstractTopiaMigrationCallback.java 2010-12-26 15:25:30 UTC (rev 2168) @@ -0,0 +1,225 @@ +/* + * #%L + * ToPIA :: Service Migration + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 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.migration; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.jdbc.Work; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.util.StringUtil; +import org.nuiton.util.Version; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +import static org.nuiton.i18n.I18n._; + +/** + * Abstract migration callback. + * + * @author tchemit <chemit@codelutin.com> + * @version $Id$ + * @since 2.5 + */ +public abstract class AbstractTopiaMigrationCallback { + + /** Logger */ + static private Log log = LogFactory.getLog(AbstractTopiaMigrationCallback.class); + + /** @return the available versions from the call back */ + public abstract Version[] getAvailableVersions(); + + /** @return the current application version (says the version to use) */ + public abstract Version getApplicationVersion(); + + /** + * Hook to ask user if migration can be performed. + * + * @param dbVersion the actual db version + * @param versions the versions to update + * @return {@code false} if migration is canceled, {@code true} otherwise. + */ + public abstract boolean askUser(Version dbVersion, + List<Version> versions); + + protected abstract void migrateForVersion(Version version, + TopiaContextImplementor tx, + boolean showSql, + boolean showProgression) throws Exception; + + /** + * Tentative de migration depuis la version de la base version la version + * souhaitee. + * <p/> + * On applique toutes les migrations de version indiquee dans le parametre + * <code>version</code>. + * <p/> + * Pour chaque version, on cherche la methode migrateTo_XXX ou XXX est la + * version transforme en identifiant java via la methode + * {@link Version#getValidName()} et on l'execute. + * <p/> + * Note: pour chaque version a appliquer, on ouvre une nouvelle transaction. + * + * @param ctxt topia context de la transaction en cours + * @param dbVersion database version + * @param showSql drapeau pour afficher les requete sql + * @param showProgression drapeau pour afficher la progression + * @param versions all versions knwon by service @return migration a + * ggrement + * @return {@code true} si la migration est accepté, {@code false} autrement. + */ + public boolean doMigration(TopiaContext ctxt, + Version dbVersion, + boolean showSql, + boolean showProgression, + List<Version> versions) { + + boolean doMigrate = askUser(dbVersion, versions); + if (!doMigrate) { + // l'utilisateur a refuse la migration + return false; + } + TopiaContextImplementor tx; + + for (Version v : versions) { + // ouverture d'une connexion direct JDBC sur la base + try { + + tx = (TopiaContextImplementor) ctxt.beginTransaction(); + + try { + + log.info(_("topia.migration.start.migrate", v)); + + migrateForVersion(v, tx, showSql, showProgression); + + // commit des modifs + tx.commitTransaction(); + + } catch (Exception eee) { + // en cas d'erreur + log.error("Migration impossible de la base", eee); + // rollback du travail en cours + tx.rollbackTransaction(); + // propagation de l'erreur + throw eee; + } finally { + // close database connexion + if (tx != null) { + tx.closeContext(); + } + } + + } catch (Exception eee) { + log.error("Error lors de la tentative de migration", eee); + doMigrate = false; + // toute erreur arrête la mgration + break; + } + } + return doMigrate; + } + + public void executeSQL(TopiaContextImplementor tx, String... sqls) + throws TopiaException { + executeSQL(tx, false, false, sqls); + } + + /** + * Executes the given {@code sqls} requests. + * + * @param tx the session + * @param showSql flag to see sql requests + * @param showProgression flag to see progession on console + * @param sqls requests to execute + * @throws TopiaException if any pb + * @since 2.3.0 + */ + public void executeSQL(TopiaContextImplementor tx, + final boolean showSql, + final boolean showProgression, + final String... sqls) throws TopiaException { + + if (log.isInfoEnabled()) { + + log.info(_("topia.migration.start.sqls", sqls.length)); + } + if (showSql) { + StringBuilder buffer = new StringBuilder(); + for (String s : sqls) { + buffer.append(s).append("\n"); + } + log.info("SQL TO EXECUTE :\n" + + "--------------------------------------------------------------------------------\n" + + "--------------------------------------------------------------------------------\n" + + buffer.toString() + + "--------------------------------------------------------------------------------\n" + + "--------------------------------------------------------------------------------\n" + ); + } + tx.getHibernate().doWork(new Work() { + + @Override + public void execute(Connection connection) throws SQLException { + int index = 0; + int max = sqls.length; + for (String sql : sqls) { + long t0 = System.nanoTime(); + if (log.isInfoEnabled()) { + String message = ""; + + if (showProgression) { + message = _("topia.migration.start.sql", ++index, max); + } + if (showSql) { + message += "\n" + sql; + } + if (showProgression || showSql) { + + log.info(message); + } + } + PreparedStatement sta = connection.prepareStatement(sql); + try { + sta.executeUpdate(); + } finally { + sta.close(); + } + if (log.isDebugEnabled()) { + String message; + message = _("topia.migration.end.sql", ++index, max, StringUtil.convertTime(System.nanoTime() - t0)); + log.debug(message); + } + } + } + }); + + } + +} \ No newline at end of file Modified: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallback.java =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallback.java 2010-12-12 12:39:34 UTC (rev 2167) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallback.java 2010-12-26 15:25:30 UTC (rev 2168) @@ -25,23 +25,11 @@ package org.nuiton.topia.migration; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.jdbc.Work; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaContextImplementor; -import org.nuiton.util.StringUtil; import org.nuiton.util.Version; import java.lang.reflect.Method; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.List; -import static org.nuiton.i18n.I18n._; - /** * Default migration call back to use. * <p/> @@ -50,109 +38,13 @@ * @author tchemit <chemit@codelutin.com> * @version $Id$ * @since 2.3.4 + * @deprecated since 2.5, use now the {@link TopiaMigrationCallbackByClass} or {@link TopiaMigrationCallbackByMethod}. + * <b>will not be replaced and remove before version {@code 2.6}</b>. */ -public abstract class TopiaMigrationCallback { +@Deprecated +public abstract class TopiaMigrationCallback extends TopiaMigrationCallbackByMethod { - /** Logger */ - static private Log log = LogFactory.getLog(TopiaMigrationCallback.class); - - /** @return the available versions from the call back */ - public abstract Version[] getAvailableVersions(); - - /** @return the current application version (says the version to use) */ - public abstract Version getApplicationVersion(); - - /** - * Hook to ask user if migration can be performed. - * - * @param dbVersion the actual db version - * @param versions the versions to update - * @return {@code false} if migration is canceled, {@code true} otherwise. - */ - public abstract boolean askUser(Version dbVersion, - List<Version> versions); - - /** - * Tentative de migration depuis la version de la base version la version - * souhaitee. - * <p/> - * On applique toutes les migrations de version indiquee dans le parametre - * <code>version</code>. - * <p/> - * Pour chaque version, on cherche la methode migrateTo_XXX ou XXX est la - * version transforme en identifiant java via la methode - * {@link Version#getValidName()} et on l'execute. - * <p/> - * Note: pour chaque version a appliquer, on ouvre une nouvelle transaction. - * - * @param ctxt topia context de la transaction en cours - * @param dbVersion database version - * @param showSql drapeau pour afficher les requete sql - * @param showProgression drapeau pour afficher la progression - * @param versions all versions knwon by service @return migration a - * ggrement - * @return {@code true} si la migration est accepté, {@code false} autrement. - */ - public boolean doMigration(TopiaContext ctxt, - Version dbVersion, - boolean showSql, - boolean showProgression, - List<Version> versions) { - - boolean doMigrate = askUser(dbVersion, versions); - if (!doMigrate) { - // l'utilisateur a refuse la migration - return false; - } - TopiaContextImplementor tx; - - for (Version v : versions) { - // ouverture d'une connexion direct JDBC sur la base - try { - - tx = (TopiaContextImplementor) ctxt.beginTransaction(); - - try { - - Method m = getMigrationMethod(v); - - m.setAccessible(true); - - log.info(_("topia.migration.start.migrate", v)); - - if (log.isDebugEnabled()) { - log.debug("launch method " + m.getName()); - } - - m.invoke(this, tx, showSql, showProgression); - - // commit des modifs - tx.commitTransaction(); - - } catch (Exception eee) { - // en cas d'erreur - log.error("Migration impossible de la base", eee); - // rollback du travail en cours - tx.rollbackTransaction(); - // propagation de l'erreur - throw eee; - } finally { - // close database connexion - if (tx != null) { - tx.closeContext(); - } - } - - } catch (Exception eee) { - log.error("Error lors de la tentative de migration", eee); - doMigrate = false; - // toute erreur arrête la mgration - break; - } - } - return doMigrate; - } - + @Deprecated protected Method getMigrationMethod(Version version) throws NoSuchMethodException { String methodName = "migrateTo_" + version.getValidName(); @@ -164,80 +56,4 @@ ); return m; } - - public void executeSQL(TopiaContextImplementor tx, String... sqls) - throws TopiaException { - executeSQL(tx, false, false, sqls); - } - - /** - * Executes the given {@code sqls} requests. - * - * @param tx the session - * @param showSql flag to see sql requests - * @param showProgression flag to see progession on console - * @param sqls requests to execute - * @throws TopiaException if any pb - * @since 2.3.0 - */ - public void executeSQL(TopiaContextImplementor tx, - final boolean showSql, - final boolean showProgression, - final String... sqls) throws TopiaException { - - if (log.isInfoEnabled()) { - - log.info(_("topia.migration.start.sqls", sqls.length)); - } - if (showSql) { - StringBuilder buffer = new StringBuilder(); - for (String s : sqls) { - buffer.append(s).append("\n"); - } - log.info("SQL TO EXECUTE :\n" + - "--------------------------------------------------------------------------------\n" + - "--------------------------------------------------------------------------------\n" + - buffer.toString() + - "--------------------------------------------------------------------------------\n" + - "--------------------------------------------------------------------------------\n" - ); - } - tx.getHibernate().doWork(new Work() { - - @Override - public void execute(Connection connection) throws SQLException { - int index = 0; - int max = sqls.length; - for (String sql : sqls) { - long t0 = System.nanoTime(); - if (log.isInfoEnabled()) { - String message = ""; - - if (showProgression) { - message = _("topia.migration.start.sql", ++index, max); - } - if (showSql) { - message += "\n" + sql; - } - if (showProgression || showSql) { - - log.info(message); - } - } - PreparedStatement sta = connection.prepareStatement(sql); - try { - sta.executeUpdate(); - } finally { - sta.close(); - } - if (log.isDebugEnabled()) { - String message; - message = _("topia.migration.end.sql", ++index, max, StringUtil.convertTime(System.nanoTime() - t0)); - log.debug(message); - } - } - } - }); - - } } \ No newline at end of file Added: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClass.java =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClass.java (rev 0) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClass.java 2010-12-26 15:25:30 UTC (rev 2168) @@ -0,0 +1,119 @@ +/* + * #%L + * ToPIA :: Service Migration + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 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.migration; + +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.util.ObjectUtil; +import org.nuiton.util.Version; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * Migration callback which use a different class for each version to migrate. + * <p/> + * You must fill in the constructor the mapping for each version of + * {@link #getAvailableVersions()} a matching migrator for version which + * extends {@link AbstractMigrationCallBackForVersion}. + * <p/> + * Use the callback when you have a lot of version to migrate and the + * {@link TopiaMigrationCallbackByMethod} begins to be messy. + * + * @author tchemit <chemit@codelutin.com> + * @version $Id$ + * @since 2.5 + */ +public abstract class TopiaMigrationCallbackByClass extends AbstractTopiaMigrationCallback { + + protected Map<Version, Class<? extends AbstractMigrationCallBackForVersion>> versionMigrationMapping; + + protected TopiaMigrationCallbackByClass(Map<Version, Class<? extends AbstractMigrationCallBackForVersion>> versionMigrationMapping) { + + this.versionMigrationMapping = versionMigrationMapping; + + // check for each version of migration we have a migrator + for (Version version : getAvailableVersions()) { + if (!this.versionMigrationMapping.containsKey(version)) { + throw new IllegalStateException("It misses a migration class for version " + version); + } + } + } + + @Override + protected void migrateForVersion(Version version, + TopiaContextImplementor tx, + boolean showSql, + boolean showProgression) throws Exception { + Class<? extends AbstractMigrationCallBackForVersion> migratorClass = versionMigrationMapping.get(version); + + AbstractMigrationCallBackForVersion migrator = ObjectUtil.newInstance(migratorClass, Arrays.asList(this), true); + + String[] queries = migrator.prepareMigration(tx, showSql, showProgression); + + executeSQL(tx, showSql, showProgression, queries); + + } + + /** + * La classe de base pour chaque migration de version. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5 + */ + public abstract static class AbstractMigrationCallBackForVersion { + + protected final Version version; + + protected final TopiaMigrationCallbackByClass callBack; + + public AbstractMigrationCallBackForVersion(Version version, + TopiaMigrationCallbackByClass callBack) { + this.version = version; + this.callBack = callBack; + + } + + protected String[] prepareMigration(TopiaContextImplementor tx, + boolean showSql, + boolean showProgression) throws TopiaException { + + List<String> queries = new ArrayList<String>(); + + prepareMigrationScript(tx, queries, showSql, showProgression); + + return queries.toArray(new String[queries.size()]); + } + + protected abstract void prepareMigrationScript(TopiaContextImplementor tx, + List<String> queries, + boolean showSql, + boolean showProgression) throws TopiaException; + + + } +} \ No newline at end of file Property changes on: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClass.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByMethod.java (from rev 2167, trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallback.java) =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByMethod.java (rev 0) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByMethod.java 2010-12-26 15:25:30 UTC (rev 2168) @@ -0,0 +1,77 @@ +/* + * #%L + * ToPIA :: Service Migration + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 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.migration; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.util.Version; + +import java.lang.reflect.Method; + +/** + * Migration callback base on methods. + * <p/> + * The callback defines for each version of {@link #getAvailableVersions()} + * a method named {@code migrate_on_XXX} where {@code XXX} is the version with + * all dots replaces by underscores. + * <p/> + * Replace deprecated implementation {@code TopiaMigrationCallBack}. + * + * @author tchemit <chemit@codelutin.com> + * @version $Id$ + * @since 2.5 + */ +public abstract class TopiaMigrationCallbackByMethod extends AbstractTopiaMigrationCallback { + + /** Logger */ + static private Log log = LogFactory.getLog(TopiaMigrationCallbackByMethod.class); + + @Override + protected void migrateForVersion(Version version, + TopiaContextImplementor tx, + boolean showSql, + boolean showProgression) throws Exception { + + String methodName = "migrateTo_" + version.getValidName(); + + Method m = getClass().getMethod(methodName, + TopiaContextImplementor.class, + boolean.class, + boolean.class + ); + + m.setAccessible(true); + + if (log.isDebugEnabled()) { + log.debug("launch method " + m.getName()); + } + + m.invoke(this, tx, showSql, showProgression); + + } + + +} \ No newline at end of file Modified: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java 2010-12-12 12:39:34 UTC (rev 2167) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java 2010-12-26 15:25:30 UTC (rev 2168) @@ -54,7 +54,7 @@ import static org.nuiton.i18n.I18n._; /** - * Le moteur de migration proposé par topia. Il est basé sur un {@link TopiaMigrationCallback} + * Le moteur de migration proposé par topia. Il est basé sur un {@link AbstractTopiaMigrationCallback} * qui donne la version de l'application, les version de mises à jour disponibles. * <p/> * Le call back offre aussi les commandes sql à passer pour chaque version de mise à jour. @@ -92,7 +92,7 @@ protected boolean migrateOnInit; /** CallbackHandler list (initialise en pre-init) */ - protected TopiaMigrationCallback callback; + protected AbstractTopiaMigrationCallback callback; /** topia root context (initialise en pre-init) */ protected TopiaContextImplementor rootContext; @@ -215,7 +215,7 @@ // enregistrement du callback try { Class<?> clazz = Class.forName(callbackStr); - callback = (TopiaMigrationCallback) clazz.newInstance(); + callback = (AbstractTopiaMigrationCallback) clazz.newInstance(); } catch (Exception e) { log.error("Could not instanciate CallbackHandler [" + callbackStr + "]", e); }