• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal;
18 
19 import com.android.ide.eclipse.adt.AdtPlugin;
20 import com.android.ide.eclipse.adt.Messages;
21 import com.android.ide.eclipse.adt.AdtPlugin.CheckSdkErrorHandler;
22 import com.android.sdklib.SdkConstants;
23 
24 import org.osgi.framework.Constants;
25 import org.osgi.framework.Version;
26 
27 import java.io.BufferedReader;
28 import java.io.FileNotFoundException;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 
34 /**
35  * Class handling the version check for the plugin vs. the SDK.<br>
36  * The plugin must be able to support all version of the SDK.
37  *
38  * <p/>An SDK can require a new version of the plugin.
39  * <p/>The SDK contains a file with the minimum version for the plugin. This file is inside the
40  * <code>tools/lib</code> directory, and is called <code>plugin.prop</code>.<br>
41  * Inside that text file, there is a line in the format "plugin.version=#.#.#". This is checked
42  * against the current plugin version.<br>
43  *
44  */
45 public final class VersionCheck {
46     /**
47      * Pattern to get the minimum plugin version supported by the SDK. This is read from
48      * the file <code>$SDK/tools/lib/plugin.prop</code>.
49      */
50     private final static Pattern sPluginVersionPattern = Pattern.compile(
51             "^plugin.version=(\\d+)\\.(\\d+)\\.(\\d+).*$"); //$NON-NLS-1$
52 
53     /**
54      * Checks the plugin and the SDK have compatible versions.
55      * @param osSdkPath The path to the SDK
56      * @return true if compatible.
57      */
checkVersion(String osSdkPath, CheckSdkErrorHandler errorHandler)58     public static boolean checkVersion(String osSdkPath, CheckSdkErrorHandler errorHandler) {
59         AdtPlugin plugin = AdtPlugin.getDefault();
60         String osLibs = osSdkPath + SdkConstants.OS_SDK_TOOLS_LIB_FOLDER;
61 
62         // get the plugin property file, and grab the minimum plugin version required
63         // to work with the sdk
64         int minMajorVersion = -1;
65         int minMinorVersion = -1;
66         int minMicroVersion = -1;
67         try {
68             FileReader reader = new FileReader(osLibs + SdkConstants.FN_PLUGIN_PROP);
69             BufferedReader bReader = new BufferedReader(reader);
70             String line;
71             while ((line = bReader.readLine()) != null) {
72                 Matcher m = sPluginVersionPattern.matcher(line);
73                 if (m.matches()) {
74                     minMajorVersion = Integer.parseInt(m.group(1));
75                     minMinorVersion = Integer.parseInt(m.group(2));
76                     minMicroVersion = Integer.parseInt(m.group(3));
77                     break;
78                 }
79             }
80         } catch (FileNotFoundException e) {
81             // the build id will be null, and this is handled by the builders.
82         } catch (IOException e) {
83             // the build id will be null, and this is handled by the builders.
84         }
85 
86         // Failed to get the min plugin version number?
87         if (minMajorVersion == -1 || minMinorVersion == -1 || minMicroVersion ==-1) {
88             return errorHandler.handleWarning(Messages.VersionCheck_Plugin_Version_Failed);
89         }
90 
91         // test the plugin number
92         String versionString = (String) plugin.getBundle().getHeaders().get(
93                 Constants.BUNDLE_VERSION);
94         Version version = new Version(versionString);
95 
96         boolean valid = true;
97         if (version.getMajor() < minMajorVersion) {
98             valid = false;
99         } else if (version.getMajor() == minMajorVersion) {
100             if (version.getMinor() < minMinorVersion) {
101                 valid = false;
102             } else if (version.getMinor() == minMinorVersion) {
103                 if (version.getMicro() < minMicroVersion) {
104                     valid = false;
105                 }
106             }
107         }
108 
109         if (valid == false) {
110             return errorHandler.handleWarning(
111                     String.format(Messages.VersionCheck_Plugin_Too_Old,
112                             minMajorVersion, minMinorVersion, minMicroVersion, versionString));
113         }
114 
115         return true; // no error!
116     }
117 }
118