KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > ApplicationUpgradeHandler


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.util.Collection JavaDoc;
23
24 import org.apache.cayenne.conf.Configuration;
25
26 /**
27  * @since 1.1
28  * @author Andrus Adamchik
29  */

30 // TODO: andrus, 7/16/2006 - upgrade handler is rather primitive and relies on individual
31
// loaders to be able to read older versions and convert them to the latest one in memory.
32
// So this handler simply checks the version to trigger the update in the calling UI, and
33
// saves what's been already loaded.
34
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 JavaDoc supportedVersion();
43
44     abstract int checkForUpgrades(Configuration project, Collection JavaDoc appendMessages);
45
46     abstract void performUpgrade(ApplicationProject project) throws ProjectException;
47
48     int compareVersion(String JavaDoc 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 JavaDoc version) {
55         if (version == null || version.trim().length() == 0) {
56             return 0;
57         }
58
59         // leave the first dot, and treat remaining as a fraction
60
// remove all non digit chars
61
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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 JavaDoc _1_2_PACKAGE_PREFIX = "org.objectstyle.cayenne.";
80         final static String JavaDoc _2_0_PACKAGE_PREFIX = "org.apache.cayenne.";
81
82         String JavaDoc supportedVersion() {
83             return "2.0";
84         }
85     }
86
87     static class UpgradeHandler_1_1 extends ApplicationUpgradeHandler {
88
89         String JavaDoc 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 JavaDoc appendMessages) {
100             String JavaDoc loadedVersion = project.getProjectVersion();
101             int versionState = compareVersion(loadedVersion);
102             if (versionState < 0) {
103                 String JavaDoc 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 JavaDoc 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