Topia-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
June 2012
- 4 participants
- 21 discussions
r2571 - branches/topia-2.6-maintenance/topia-service-migration/src/main/java/org/nuiton/topia/migration
by tchemitï¼ users.nuiton.org 01 Jun '12
by tchemitï¼ users.nuiton.org 01 Jun '12
01 Jun '12
Author: tchemit
Date: 2012-06-01 11:29:47 +0200 (Fri, 01 Jun 2012)
New Revision: 2571
Url: http://nuiton.org/repositories/revision/topia/2571
Log:
fixes #2122: Introduce a simplier way of reference migration callback
Added:
branches/topia-2.6-maintenance/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClassNG.java
Copied: branches/topia-2.6-maintenance/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClassNG.java (from rev 2570, branches/topia-2.6-maintenance/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClass.java)
===================================================================
--- branches/topia-2.6-maintenance/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClassNG.java (rev 0)
+++ branches/topia-2.6-maintenance/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationCallbackByClassNG.java 2012-06-01 09:29:47 UTC (rev 2571)
@@ -0,0 +1,194 @@
+/*
+ * #%L
+ * ToPIA :: Service Migration
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2010 CodeLutin, Tony chemit
+ * %%
+ * 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.TopiaException;
+import org.nuiton.topia.framework.TopiaContextImplementor;
+import org.nuiton.util.Version;
+import org.nuiton.util.VersionUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.Set;
+import java.util.TreeMap;
+
+/**
+ * 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 MigrationCallBackForVersion}.
+ * <p/>
+ * Use the callback when you have a lot of version to migrate and the
+ * {@link TopiaMigrationCallbackByMethod} begins to be messy.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @version $Id$
+ * @since 2.9.11
+ */
+public abstract class TopiaMigrationCallbackByClassNG extends AbstractTopiaMigrationCallback {
+
+ /** Logger. */
+ private static final Log log =
+ LogFactory.getLog(TopiaMigrationCallbackByClassNG.class);
+
+ protected MigrationCallBackForVersionResolver callBackResolver;
+
+ protected TopiaMigrationCallbackByClassNG(MigrationCallBackForVersionResolver callBackResolver) {
+
+ this.callBackResolver = callBackResolver;
+ }
+
+ @Override
+ public Version[] getAvailableVersions() {
+ Set<Version> allVersions = callBackResolver.getAllVersions();
+ return allVersions.toArray(new Version[allVersions.size()]);
+ }
+
+ @Override
+ protected void migrateForVersion(Version version,
+ TopiaContextImplementor tx,
+ boolean showSql,
+ boolean showProgression) throws Exception {
+
+ MigrationCallBackForVersion migrator = callBackResolver.getCallBack(version);
+
+ migrator.setCallBack(this);
+
+ String[] queries = migrator.prepareMigration(tx, showSql, showProgression);
+
+ executeSQL(tx, showSql, showProgression, queries);
+
+ }
+
+ /**
+ * Call back for a given version.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.5
+ */
+ public abstract static class MigrationCallBackForVersion {
+
+ protected TopiaMigrationCallbackByClassNG callBack;
+
+ public abstract Version getVersion();
+
+ public void setCallBack(TopiaMigrationCallbackByClassNG callBack) {
+ 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;
+
+ public void executeSQL(TopiaContextImplementor tx,
+ String... sqls) throws TopiaException {
+ callBack.executeSQL(tx, sqls);
+ }
+
+ public void executeSQL(TopiaContextImplementor tx,
+ boolean showSql,
+ boolean showProgression,
+ String... sqls) throws TopiaException {
+ callBack.executeSQL(tx, showSql, showProgression, sqls);
+ }
+
+ }
+
+ /**
+ * Resolver to obtain the correct migration class for a given version.
+ *
+ * @since 2.6.11
+ */
+ public interface MigrationCallBackForVersionResolver {
+
+ /**
+ * Returns all detected versions.
+ *
+ * @return all detected versions.
+ */
+ Set<Version> getAllVersions();
+
+ /**
+ * for a given version, returns his migration callback.
+ *
+ * @param version the version to migrate
+ * @return the migration call for the given version, or {@code null}
+ * if no such migration callback exists for the version
+ */
+ MigrationCallBackForVersion getCallBack(Version version);
+ }
+
+ /**
+ * A simple call back resolver via a service loader.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.9.11
+ */
+ public static class MigrationCallBackForVersionResolverByServiceLoader implements MigrationCallBackForVersionResolver {
+
+ protected final Map<Version, MigrationCallBackForVersion> versionMigrationMapping;
+
+ public MigrationCallBackForVersionResolverByServiceLoader() {
+ this.versionMigrationMapping = new TreeMap<Version, MigrationCallBackForVersion>(
+ new VersionUtil.VersionComparator());
+ ServiceLoader<MigrationCallBackForVersion> load = ServiceLoader.load(MigrationCallBackForVersion.class);
+ for (MigrationCallBackForVersion callBackForVersion : load) {
+ Version version = callBackForVersion.getVersion();
+ if (log.isInfoEnabled()) {
+ log.info("Detects migration version " + version + " [" +
+ callBackForVersion + "]");
+ }
+ versionMigrationMapping.put(version, callBackForVersion);
+ }
+ }
+
+ @Override
+ public MigrationCallBackForVersion getCallBack(Version version) {
+ return versionMigrationMapping.get(version);
+ }
+
+ @Override
+ public Set<Version> getAllVersions() {
+ return versionMigrationMapping.keySet();
+ }
+ }
+}
\ No newline at end of file
1
0