Index: topia-service/src/java/org/codelutin/topia/migration/transformation/FinderMigration.java diff -u topia-service/src/java/org/codelutin/topia/migration/transformation/FinderMigration.java:1.1 topia-service/src/java/org/codelutin/topia/migration/transformation/FinderMigration.java:1.2 --- topia-service/src/java/org/codelutin/topia/migration/transformation/FinderMigration.java:1.1 Mon Apr 2 14:24:37 2007 +++ topia-service/src/java/org/codelutin/topia/migration/transformation/FinderMigration.java Thu Apr 26 13:03:56 2007 @@ -18,21 +18,6 @@ package org.codelutin.topia.migration.transformation; -import java.io.File; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.SortedSet; -import java.util.StringTokenizer; -import java.util.TreeSet; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import org.codelutin.topia.migration.common.ProxyClass; import org.codelutin.topia.migration.common.Version; @@ -43,259 +28,62 @@ * @author Chevallereau Benjamin * @author Eon Sébastien * @author Trève Vincent - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update : $Date: 2007/04/02 14:24:37 $ + * Last update : $Date: 2007/04/26 13:03:56 $ */ public class FinderMigration { /** * Prefixe du nom des classes de migration. */ - private static String prefix = "Migrate"; - - /** - * Ensemble des classes de migration - */ - private Map classes; + private static String PREFIX = "Migrate"; /** - * Initialisé. + * Logger (common-logging) */ - private boolean init; - + //private static Log logger = LogFactory.getLog(FinderMigration.class); + /** * Constructeur vide. - * */ public FinderMigration() { - classes = new HashMap(); - init = false; - } - - public FinderMigration(String _prefix) { - classes = new HashMap(); - FinderMigration.prefix = _prefix; - init = false; - } - - public void search() { - String classpath = System.getProperty("java.class.path"); - StringTokenizer chemins = new StringTokenizer(classpath, System - .getProperty("path.separator")); - while (chemins.hasMoreTokens()) { - searchClasses(chemins.nextToken(), true); - } - filterClasses(); - - init = true; - } - - public void search(String namePackage) { - char separator = File.separatorChar; - String path = namePackage.replace('.', separator); - String classpath = System.getProperty("java.class.path"); - StringTokenizer chemins = new StringTokenizer(classpath, System - .getProperty("path.separator")); - while (chemins.hasMoreTokens()) { - String pathTemp = new String(chemins.nextToken()); - File elementClasspath = new File(pathTemp); - if (elementClasspath.isDirectory()) { - if (pathTemp.charAt(pathTemp.length() - 1) != separator) { - pathTemp += separator; - } - pathTemp += path; - searchClasses(pathTemp, true); - } - } - filterClasses(); - - init = true; - } - - private void searchClasses(String path, boolean reccursive) { - File elementClasspath = new File(path); - if (elementClasspath.isDirectory()) { - - if (reccursive = true) { - File[] tab = elementClasspath.listFiles(); - for (int i = 0; i < tab.length; i++) { - searchClasses(tab[i].getAbsolutePath(), false); - } - } - - } else { - String name = elementClasspath.getName(); - if (name.endsWith(".class")) { - classes.put(name.substring(0, name.length() - 6), null); - } - } - } - - private void filterClasses() { - Set s = classes.keySet(); - Map classes_tmp = new HashMap(classes); - for (Iterator iter = s.iterator(); iter.hasNext();) { - String element = (String) iter.next(); - if (!(selectClasseByNameFile(element))) { - classes_tmp.remove(element); - } - } - - s = classes_tmp.keySet(); - for (Iterator iter = s.iterator(); iter.hasNext();) { - String element = (String) iter.next(); - classes_tmp.put(element, classes.get(element)); - } - - classes = classes_tmp; - } - - @SuppressWarnings("unchecked") - private boolean selectClasseByNameFile(String name) { - boolean selected = false; - - Pattern pattern = Pattern.compile(prefix - + "(.*)V([0-9]*(_[0-9]*)*)V([0-9](_*[0-9]*)*)"); - Matcher matcher = pattern.matcher(name); - - if (matcher.find()) { - selected = true; - - int inameclass = 1; - int ivdeb = 2; - int ivfin = 4; - - Version vdeb = new Version(); - Version vfin = new Version(); - String nameClass = new String(); - Map map = new HashMap(); - - if ((ivdeb <= matcher.groupCount()) - && (ivfin <= matcher.groupCount()) - && (inameclass <= matcher.groupCount())) { - vdeb.setVersion(String.valueOf(matcher.group(ivdeb).replace( - "_", "."))); - vfin.setVersion(String.valueOf(matcher.group(ivfin).replace( - "_", "."))); - nameClass = matcher.group(inameclass); - } - - map.put("begin", vdeb); - map.put("end", vfin); - map.put("class", nameClass); - - classes.put(name, map); - } - - return selected; - } - - /** - * @return the prefix - */ - public static String getPrefix() { - return prefix; } /** - * @param prefix - * the prefix to set + * Constructeur + * @param prefix nouveau prefix */ - public static void setPrefix(String prefix) { - FinderMigration.prefix = prefix; + public FinderMigration(String prefix) { + this(); + FinderMigration.PREFIX = prefix; } /** - * - * @param nameClass - * le nom absolu de la classe - * @return les classes de migration + * Use to get migration class for one version to next version + * @param klass class + * @param base base version + * @param next */ - public Set getMigrationClasses(String nameClass) { - String _nameClass = null; - String namePackage = new String(); - - StringTokenizer token = new StringTokenizer(nameClass, "."); - int nb = token.countTokens(); - int i = 1; - while (token.hasMoreTokens()) { - if (i < nb) { - namePackage += token.nextToken(); - if (i < nb - 1) { - namePackage += "."; - } - } else { - _nameClass = token.nextToken(); - } - i++; - } - - if (!init) { - search(namePackage); - init = true; - } + public MigrationClass getMigrationClass(ProxyClass klass, Version base, Version next) { - Set result = new HashSet(); - Set s = classes.keySet(); - for (Iterator iter = s.iterator(); iter.hasNext();) { - String element = (String) iter.next(); - Map m = classes.get(element); - - if (m.get("class").equals(_nameClass)) { - MigrationClass mi = new MigrationClass(namePackage +"."+ element, - namePackage +"."+ String.valueOf(m.get("class")), - (Version) m.get("begin"), (Version) m.get("end")); - result.add(mi); - } - } - return result; - } - - public List loadSortedListOfMigrationFor( - String cannonicalName) { - return sortMigrations(getMigrationClasses(cannonicalName)); - } - - public List loadSortedListOfMigrationFor(String canonicalName, Version minVersion){ - SetMigrationClasses = getMigrationClasses(canonicalName); - Set temp = new HashSet(); - for(MigrationClass m : MigrationClasses){ - if(m.getBegin().compareTo(minVersion)>=0){ - temp.add(m); - } - } - List result = sortMigrations(temp); + MigrationClass mcResult = null; - return result; - } - - // choisi dans la collection les modification a effectuer et les met dans - // l'ordre - private List sortMigrations( - Collection migrations) { - - // pour l'instant on trie juste les transformations selon leur n° de - // version de depart - SortedSet preresult = new TreeSet(); - List result = new LinkedList(); - preresult.addAll(migrations); - Iterator it = preresult.iterator(); - while (it.hasNext()) { - MigrationClass m = it.next(); - result.add(m); - } - return result; - } - - public MigrationClass getMigrationClass(ProxyClass klass, Version base, Version next) { - Set list = this.getMigrationClasses(klass.getCanonicalName()); - for(MigrationClass m : list){ - if(base.equals(m.getBegin()) && next.equals(m.getEnd())){ - return m; - } + String baseFullName = klass.getCanonicalName(); + String baseLittleName = baseFullName.substring(baseFullName.lastIndexOf('.')+1); + String basePackage = baseFullName.substring(0,baseFullName.lastIndexOf('.')); + String newClassName = basePackage + "." + PREFIX + baseLittleName + "V" + base.getVersion() + "V" + next.getVersion(); + + try { + Class.forName(newClassName); + + // pas d'exception, la classe existe + mcResult = new MigrationClass(newClassName,baseFullName,base,next); + } catch (ClassNotFoundException e) { + // no migration class + // but this is not an error } - return null; + + return mcResult; } - }