1 19 20 package org.apache.cayenne.project; 21 22 import java.util.Collection ; 23 24 import org.apache.cayenne.conf.Configuration; 25 26 30 abstract class ApplicationUpgradeHandler { 35 36 private static final ApplicationUpgradeHandler sharedInstance = new UpgradeHandler_2_0(); 37 38 static ApplicationUpgradeHandler sharedHandler() { 39 return sharedInstance; 40 } 41 42 abstract String supportedVersion(); 43 44 abstract int checkForUpgrades(Configuration project, Collection appendMessages); 45 46 abstract void performUpgrade(ApplicationProject project) throws ProjectException; 47 48 int compareVersion(String version) { 49 double supported = decodeVersion(supportedVersion()); 50 double newVersion = decodeVersion(version); 51 return supported < newVersion ? -1 : (supported == newVersion) ? 0 : 1; 52 } 53 54 static double decodeVersion(String version) { 55 if (version == null || version.trim().length() == 0) { 56 return 0; 57 } 58 59 StringBuffer buffer = new StringBuffer (version.length()); 62 boolean dotProcessed = false; 63 for (int i = 0; i < version.length(); i++) { 64 char nextChar = version.charAt(i); 65 if (nextChar == '.' && !dotProcessed) { 66 dotProcessed = true; 67 buffer.append('.'); 68 } 69 else if (Character.isDigit(nextChar)) { 70 buffer.append(nextChar); 71 } 72 } 73 74 return Double.parseDouble(buffer.toString()); 75 } 76 77 static class UpgradeHandler_2_0 extends UpgradeHandler_1_1 { 78 79 final static String _1_2_PACKAGE_PREFIX = "org.objectstyle.cayenne."; 80 final static String _2_0_PACKAGE_PREFIX = "org.apache.cayenne."; 81 82 String supportedVersion() { 83 return "2.0"; 84 } 85 } 86 87 static class UpgradeHandler_1_1 extends ApplicationUpgradeHandler { 88 89 String supportedVersion() { 90 return "1.1"; 91 } 92 93 void performUpgrade(ApplicationProject project) throws ProjectException { 94 project.setModified(true); 95 project.getConfiguration().setProjectVersion(supportedVersion()); 96 project.save(); 97 } 98 99 int checkForUpgrades(Configuration project, Collection appendMessages) { 100 String loadedVersion = project.getProjectVersion(); 101 int versionState = compareVersion(loadedVersion); 102 if (versionState < 0) { 103 String versionLabel = (loadedVersion != null) ? loadedVersion : "?"; 104 appendMessages.add("Newer Project Version Detected: \"" 105 + versionLabel 106 + "\""); 107 return Project.UPGRADE_STATUS_NEW; 108 } 109 else if (versionState > 0) { 110 String versionLabel = (loadedVersion != null) ? loadedVersion : "?"; 111 appendMessages.add("Older Project Version Detected: \"" 112 + versionLabel 113 + "\""); 114 return Project.UPGRADE_STATUS_OLD; 115 } 116 else { 117 return Project.UPGRADE_STATUS_CURRENT; 118 } 119 } 120 } 121 } 122 | Popular Tags |