View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
5    *               Eric Long <erilong@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 3 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, see
19   * <http://www.gnu.org/licenses/>.
20   */
21  
22  package org.jumpmind.symmetric;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Properties;
27  
28  import org.apache.commons.lang.StringUtils;
29  
30  /***
31   * Follow the Apache versioning scheme documented <a
32   * href="http://apr.apache.org/versioning.html">here</a>.
33   */
34  final public class Version {
35  
36      static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(Version.class);
37  
38      private static final int MAJOR_INDEX = 0;
39  
40      private static final int MINOR_INDEX = 1;
41  
42      private static final int PATCH_INDEX = 2;
43  
44      public static String version() {
45          InputStream is = Version.class
46                  .getResourceAsStream("/META-INF/maven/org.jumpmind.symmetric/symmetric/pom.properties");
47          if (is != null) {
48              Properties p = new Properties();
49              try {
50                  p.load(is);
51                  return p.getProperty("version");
52              } catch (IOException e) {
53                  log.warn(e, e);
54              }
55          }
56          return "development";
57      }
58  
59      public static int[] parseVersion(String version) {
60          version = version.replaceAll("[^0-9//.]", "");
61          int[] versions = new int[3];
62          if (!StringUtils.isEmpty(version)) {
63              String[] splitVersion = version.split("//.");
64              if (splitVersion.length >= 3) {
65                  versions[PATCH_INDEX] = parseVersionComponent(splitVersion[2]);
66              }
67              if (splitVersion.length >= 2) {
68                  versions[MINOR_INDEX] = parseVersionComponent(splitVersion[1]);
69              }
70              if (splitVersion.length >= 1) {
71                  versions[MAJOR_INDEX] = parseVersionComponent(splitVersion[0]);
72              }
73          }
74          return versions;
75      }
76  
77      private static int parseVersionComponent(String versionComponent) {
78          int version = 0;
79          try {
80              version = Integer.parseInt(versionComponent);
81          } catch (NumberFormatException e) {
82          }
83          return version;
84      }
85  
86      public static boolean isOlderMajorVersion(String version) {
87          return isOlderMajorVersion(parseVersion(version));
88      }
89  
90      public static boolean isOlderMajorVersion(int[] versions) {
91          int[] softwareVersion = parseVersion(version());
92          if (versions[MAJOR_INDEX] < softwareVersion[MAJOR_INDEX]) {
93              return true;
94          }
95          return false;
96      }
97  
98      public static boolean isOlderMinorVersion(String version) {
99          return isOlderMinorVersion(parseVersion(version));
100     }
101 
102     public static boolean isOlderMinorVersion(int[] versions) {
103         int[] softwareVersion = parseVersion(version());
104         if (versions[0] < softwareVersion[MAJOR_INDEX]) {
105             return true;
106         } else if (versions[MAJOR_INDEX] == softwareVersion[MAJOR_INDEX]
107                 && versions[MINOR_INDEX] < softwareVersion[MINOR_INDEX]) {
108             return true;
109         }
110         return false;
111     }
112 }