Home | History | Annotate | Download | only in internal
      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.SdkConstants;
     20 import com.android.ide.eclipse.adt.AdtPlugin;
     21 import com.android.ide.eclipse.adt.AdtPlugin.CheckSdkErrorHandler;
     22 import com.android.ide.eclipse.adt.AdtPlugin.CheckSdkErrorHandler.Solution;
     23 import com.android.ide.eclipse.adt.Messages;
     24 import com.android.sdklib.repository.FullRevision;
     25 import com.android.sdklib.repository.PkgProps;
     26 
     27 import org.osgi.framework.Constants;
     28 import org.osgi.framework.Version;
     29 
     30 import java.io.BufferedReader;
     31 import java.io.FileNotFoundException;
     32 import java.io.FileReader;
     33 import java.io.IOException;
     34 import java.util.regex.Matcher;
     35 import java.util.regex.Pattern;
     36 
     37 /**
     38  * Class handling the version check for the plugin vs. the SDK.<br>
     39  * The plugin must be able to support all version of the SDK.
     40  *
     41  * <p/>An SDK can require a new version of the plugin.
     42  * <p/>The SDK contains a file with the minimum version for the plugin. This file is inside the
     43  * <code>tools/lib</code> directory, and is called <code>plugin.prop</code>.<br>
     44  * Inside that text file, there is a line in the format "plugin.version=#.#.#". This is checked
     45  * against the current plugin version.<br>
     46  *
     47  */
     48 public final class VersionCheck {
     49     /**
     50      * The minimum version of the SDK Tools that this version of ADT requires.
     51      */
     52     private final static FullRevision MIN_TOOLS_REV = new FullRevision(22, 0, 0, 0);
     53 
     54     /**
     55      * Pattern to get the minimum plugin version supported by the SDK. This is read from
     56      * the file <code>$SDK/tools/lib/plugin.prop</code>.
     57      */
     58     private final static Pattern sPluginVersionPattern = Pattern.compile(
     59             "^plugin.version=(\\d+)\\.(\\d+)\\.(\\d+).*$"); //$NON-NLS-1$
     60     private final static Pattern sSourcePropPattern = Pattern.compile(
     61             "^" + PkgProps.PKG_REVISION + "=(.*)$"); //$NON-NLS-1$
     62 
     63     /**
     64      * Checks the plugin and the SDK have compatible versions.
     65      * @param osSdkPath The path to the SDK
     66      * @return true if compatible.
     67      */
     68     public static boolean checkVersion(String osSdkPath, CheckSdkErrorHandler errorHandler) {
     69         AdtPlugin plugin = AdtPlugin.getDefault();
     70         String osLibs = osSdkPath + SdkConstants.OS_SDK_TOOLS_LIB_FOLDER;
     71 
     72         // get the plugin property file, and grab the minimum plugin version required
     73         // to work with the sdk
     74         int minMajorVersion = -1;
     75         int minMinorVersion = -1;
     76         int minMicroVersion = -1;
     77         BufferedReader reader = null;
     78         try {
     79             reader = new BufferedReader(new FileReader(osLibs + SdkConstants.FN_PLUGIN_PROP));
     80             String line;
     81             while ((line = reader.readLine()) != null) {
     82                 Matcher m = sPluginVersionPattern.matcher(line);
     83                 if (m.matches()) {
     84                     minMajorVersion = Integer.parseInt(m.group(1));
     85                     minMinorVersion = Integer.parseInt(m.group(2));
     86                     minMicroVersion = Integer.parseInt(m.group(3));
     87                     break;
     88                 }
     89             }
     90         } catch (FileNotFoundException e) {
     91             // the build id will be null, and this is handled by the builders.
     92         } catch (IOException e) {
     93             // the build id will be null, and this is handled by the builders.
     94         } finally {
     95             if (reader != null) {
     96                 try {
     97                     reader.close();
     98                 } catch (IOException e) {
     99                 } finally {
    100                     reader = null;
    101                 }
    102             }
    103         }
    104 
    105         // Failed to get the min plugin version number?
    106         if (minMajorVersion == -1 || minMinorVersion == -1 || minMicroVersion ==-1) {
    107             return errorHandler.handleWarning(
    108                     Solution.OPEN_SDK_MANAGER,
    109                     Messages.VersionCheck_Plugin_Version_Failed);
    110         }
    111 
    112         // test the plugin number
    113         String versionString = (String) plugin.getBundle().getHeaders().get(
    114                 Constants.BUNDLE_VERSION);
    115         Version version = new Version(versionString);
    116 
    117         boolean valid = true;
    118         if (version.getMajor() < minMajorVersion) {
    119             valid = false;
    120         } else if (version.getMajor() == minMajorVersion) {
    121             if (version.getMinor() < minMinorVersion) {
    122                 valid = false;
    123             } else if (version.getMinor() == minMinorVersion) {
    124                 if (version.getMicro() < minMicroVersion) {
    125                     valid = false;
    126                 }
    127             }
    128         }
    129 
    130         if (valid == false) {
    131             return errorHandler.handleError(
    132                     Solution.OPEN_P2_UPDATE,
    133                     String.format(Messages.VersionCheck_Plugin_Too_Old,
    134                             minMajorVersion, minMinorVersion, minMicroVersion, versionString));
    135         }
    136 
    137         // now check whether the tools are new enough.
    138         String osTools = osSdkPath + SdkConstants.OS_SDK_TOOLS_FOLDER;
    139         FullRevision toolsRevision = new FullRevision(Integer.MAX_VALUE);
    140         try {
    141             reader = new BufferedReader(new FileReader(osTools + SdkConstants.FN_SOURCE_PROP));
    142             String line;
    143             while ((line = reader.readLine()) != null) {
    144                 Matcher m = sSourcePropPattern.matcher(line);
    145                 if (m.matches()) {
    146                     try {
    147                         toolsRevision = FullRevision.parseRevision(m.group(1));
    148                     } catch (NumberFormatException ignore) {}
    149                     break;
    150                 }
    151             }
    152         } catch (FileNotFoundException e) {
    153             // the build id will be null, and this is handled by the builders.
    154         } catch (IOException e) {
    155             // the build id will be null, and this is handled by the builders.
    156         } finally {
    157             if (reader != null) {
    158                 try {
    159                     reader.close();
    160                 } catch (IOException e) {
    161                 } finally {
    162                     reader = null;
    163                 }
    164             }
    165         }
    166 
    167         if (toolsRevision.compareTo(MIN_TOOLS_REV) < 0) {
    168             // this is a warning only as we need to parse the SDK to allow updating
    169             // of the tools!
    170             return errorHandler.handleWarning(
    171                     Solution.OPEN_SDK_MANAGER,
    172                     String.format(Messages.VersionCheck_Tools_Too_Old,
    173                             MIN_TOOLS_REV, toolsRevision));
    174         }
    175 
    176         return true; // no error!
    177     }
    178 }
    179