OSDN Git Service

SDK: Use a different repository-N.xml for updating.
authorRaphael <raphael@google.com>
Tue, 4 Oct 2011 19:54:37 +0000 (12:54 -0700)
committerRaphael <raphael@google.com>
Tue, 4 Oct 2011 21:27:04 +0000 (14:27 -0700)
Change-Id: I566b6945b7d31c3bfe52b834014beb3f37a098d8

sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/SdkAddonSource.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/SdkRepoSource.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/SdkSource.java
sdkmanager/libs/sdklib/src/com/android/sdklib/repository/SdkRepoConstants.java

index b79cc90..42f7603 100755 (executable)
@@ -50,8 +50,8 @@ public class SdkAddonSource extends SdkSource {
     }\r
 \r
     @Override\r
-    protected String getUrlDefaultXmlFile() {\r
-        return SdkAddonConstants.URL_DEFAULT_FILENAME;\r
+    protected String[] getDefaultXmlFileUrls() {\r
+        return new String[] { SdkAddonConstants.URL_DEFAULT_FILENAME };\r
     }\r
 \r
     @Override\r
index d53bcc5..d35e365 100755 (executable)
@@ -64,8 +64,11 @@ public class SdkRepoSource extends SdkSource {
     }\r
 \r
     @Override\r
-    protected String getUrlDefaultXmlFile() {\r
-        return SdkRepoConstants.URL_DEFAULT_FILENAME;\r
+    protected String[] getDefaultXmlFileUrls() {\r
+        return new String[] {\r
+                SdkRepoConstants.URL_DEFAULT_FILENAME2,\r
+                SdkRepoConstants.URL_DEFAULT_FILENAME\r
+        };\r
     }\r
 \r
     @Override\r
index 5cc2cce..263bf15 100755 (executable)
@@ -86,7 +86,10 @@ public abstract class SdkSource implements IDescription, Comparable<SdkSource> {
         // looked for. This way it will be obvious to the user which\r
         // resource we are actually trying to fetch.\r
         if (url.endsWith("/")) {  //$NON-NLS-1$\r
-            url += getUrlDefaultXmlFile();\r
+            String[] names = getDefaultXmlFileUrls();\r
+            if (names.length > 0) {\r
+                url += names[0];\r
+            }\r
         }\r
 \r
         mUrl = url;\r
@@ -100,8 +103,13 @@ public abstract class SdkSource implements IDescription, Comparable<SdkSource> {
      */\r
     public abstract boolean isAddonSource();\r
 \r
-    /** Returns SdkRepoConstants.URL_DEFAULT_XML_FILE or SdkAddonConstants.URL_DEFAULT_XML_FILE */\r
-    protected abstract String getUrlDefaultXmlFile();\r
+    /**\r
+     * Returns the basename of the default URLs to try to download the\r
+     * XML manifest.\r
+     * E.g. this is typically SdkRepoConstants.URL_DEFAULT_XML_FILE\r
+     * or SdkAddonConstants.URL_DEFAULT_XML_FILE\r
+     */\r
+    protected abstract String[] getDefaultXmlFileUrls();\r
 \r
     /** Returns SdkRepoConstants.NS_LATEST_VERSION or SdkAddonConstants.NS_LATEST_VERSION. */\r
     protected abstract int getNsLatestVersion();\r
@@ -250,7 +258,7 @@ public abstract class SdkSource implements IDescription, Comparable<SdkSource> {
      */\r
     public void load(ITaskMonitor monitor, boolean forceHttp) {\r
 \r
-        monitor.setProgressMax(6);\r
+        monitor.setProgressMax(7);\r
 \r
         setDefaultDescription();\r
 \r
@@ -266,28 +274,77 @@ public abstract class SdkSource implements IDescription, Comparable<SdkSource> {
         Boolean[] validatorFound = new Boolean[] { Boolean.FALSE };\r
         String[] validationError = new String[] { null };\r
         Exception[] exception = new Exception[] { null };\r
-        InputStream xml = fetchUrl(url, monitor.createSubMonitor(1), exception);\r
         Document validatedDoc = null;\r
         boolean usingAlternateXml = false;\r
         boolean usingAlternateUrl = false;\r
         String validatedUri = null;\r
 \r
+        String[] defaultNames = getDefaultXmlFileUrls();\r
+        String firstDefaultName = defaultNames.length > 0 ? defaultNames[0] : "";\r
+\r
+        InputStream xml = fetchUrl(url, monitor.createSubMonitor(1), exception);\r
+        if (xml != null) {\r
+            int version = getXmlSchemaVersion(xml);\r
+            if (version == 0) {\r
+                xml = null;\r
+            }\r
+        }\r
+\r
+        // FIXME: this is a quick fix to support an alternate upgrade path.\r
+        // The whole logic below needs to be updated.\r
+        if (xml == null && defaultNames.length > 0) {\r
+            ITaskMonitor subMonitor = monitor.createSubMonitor(1);\r
+            subMonitor.setProgressMax(defaultNames.length);\r
+\r
+            String baseUrl = url;\r
+            if (!baseUrl.endsWith("/")) {\r
+                int pos = baseUrl.lastIndexOf('/');\r
+                if (pos > 0) {\r
+                    baseUrl = baseUrl.substring(0, pos + 1);\r
+                }\r
+            }\r
+\r
+            for(String name : defaultNames) {\r
+                String newUrl = baseUrl + name;\r
+                if (newUrl.equals(url)) {\r
+                    continue;\r
+                }\r
+                xml = fetchUrl(newUrl, subMonitor.createSubMonitor(1), exception);\r
+                if (xml != null) {\r
+                    int version = getXmlSchemaVersion(xml);\r
+                    if (version == 0) {\r
+                        xml = null;\r
+                    } else {\r
+                        url = newUrl;\r
+                        subMonitor.incProgress(\r
+                                subMonitor.getProgressMax() - subMonitor.getProgress());\r
+                        break;\r
+                    }\r
+                }\r
+            }\r
+        } else {\r
+            monitor.incProgress(1);\r
+        }\r
+\r
         // If the original URL can't be fetched\r
         // and the URL doesn't explicitly end with our filename\r
         // and it wasn't an HTTP authentication operation canceled by the user\r
         // then make another tentative after changing the URL.\r
         if (xml == null\r
-                && !url.endsWith(getUrlDefaultXmlFile())\r
+                && !url.endsWith(firstDefaultName)\r
                 && !(exception[0] instanceof CanceledByUserException)) {\r
             if (!url.endsWith("/")) {       //$NON-NLS-1$\r
                 url += "/";                 //$NON-NLS-1$\r
             }\r
-            url += getUrlDefaultXmlFile();\r
+            url += firstDefaultName;\r
 \r
             xml = fetchUrl(url, monitor.createSubMonitor(1), exception);\r
             usingAlternateUrl = true;\r
+        } else {\r
+            monitor.incProgress(1);\r
         }\r
 \r
+        // FIXME this needs to revisited.\r
         if (xml != null) {\r
             monitor.setDescription("Validate XML: %1$s", url);\r
 \r
@@ -348,11 +405,11 @@ public abstract class SdkSource implements IDescription, Comparable<SdkSource> {
                     // If we haven't already tried the alternate URL, let's do it now.\r
                     // We don't capture any fetch exception that happen during the second\r
                     // fetch in order to avoid hidding any previous fetch errors.\r
-                    if (!url.endsWith(getUrlDefaultXmlFile())) {\r
+                    if (!url.endsWith(firstDefaultName)) {\r
                         if (!url.endsWith("/")) {       //$NON-NLS-1$\r
                             url += "/";                 //$NON-NLS-1$\r
                         }\r
-                        url += getUrlDefaultXmlFile();\r
+                        url += firstDefaultName;\r
 \r
                         xml = fetchUrl(url, subMonitor.createSubMonitor(1), null /* outException */);\r
                         subMonitor.incProgress(1);\r
index b7701fe..bbd5f7a 100755 (executable)
@@ -26,6 +26,10 @@ import java.io.InputStream;
  */\r
 public class SdkRepoConstants extends RepoConstants {\r
 \r
+    /** The latest version of the sdk-repository XML Schema.\r
+     *  Valid version numbers are between 1 and this number, included. */\r
+    public static final int NS_LATEST_VERSION = 5;\r
+\r
     /** The URL of the official Google sdk-repository site. */\r
     public static final String URL_GOOGLE_SDK_SITE =\r
         "https://dl-ssl.google.com/android/repository/";                        //$NON-NLS-1$\r
@@ -34,6 +38,13 @@ public class SdkRepoConstants extends RepoConstants {
      * sdk-repository XML if the URL doesn't match an existing resource. */\r
     public static final String URL_DEFAULT_FILENAME = "repository.xml";         //$NON-NLS-1$\r
 \r
+    /** The pattern name looked by {@link SdkSource} when trying to load\r
+     * the latest sdk-repository XML that is specific to the current XSD\r
+     * schema revision.\r
+     */\r
+    public static final String URL_DEFAULT_FILENAME2 =\r
+        String.format("repository-%d.xml", NS_LATEST_VERSION);                  //$NON-NLS-1$\r
+\r
     /** The base of our sdk-repository XML namespace. */\r
     private static final String NS_BASE =\r
         "http://schemas.android.com/sdk/android/repository/";                   //$NON-NLS-1$\r
@@ -44,10 +55,6 @@ public class SdkRepoConstants extends RepoConstants {
      */\r
     public static final String NS_PATTERN = NS_BASE + "([1-9][0-9]*)";          //$NON-NLS-1$\r
 \r
-    /** The latest version of the sdk-repository XML Schema.\r
-     *  Valid version numbers are between 1 and this number, included. */\r
-    public static final int NS_LATEST_VERSION = 5;\r
-\r
     /** The XML namespace of the latest sdk-repository XML. */\r
     public static final String NS_URI = getSchemaUri(NS_LATEST_VERSION);\r
 \r