Ejemplo n.º 1
0
        public bool CheckPath(string path)
        {
            installed_version           = new AdoptiumReleaseVersion();
            installed_version.LocalPath = path;

            if (!File.Exists(System.IO.Path.Combine(path, "release")))
            {
                return(false);
            }


            bool found = false;
            bool seems_to_be_release_file = true;
            bool seems_to_be_adoptopenjdk = false;

            ////// make sure its openjdk
            ////// currently we rely on 'adopt' keyword in path ...
            ////if (path.IndexOf("adopt", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ////    path.IndexOf("openjdk", StringComparison.OrdinalIgnoreCase) >= 0)
            ////    seems_to_be_adoptopenjdk = true;

            ////// ... or 'ASSEMBLY_EXCEPTION' file with 'openjdk' keyword inside
            ////if (!seems_to_be_adoptopenjdk)
            ////{
            ////    string ASSEMBLY_EXCEPTION = Path.Combine(path, "ASSEMBLY_EXCEPTION");
            ////    if (File.Exists(ASSEMBLY_EXCEPTION))
            ////        if (File.ReadLines(ASSEMBLY_EXCEPTION).Any(line => line.IndexOf("openjdk", StringComparison.OrdinalIgnoreCase) >= 0 ))
            ////            seems_to_be_adoptopenjdk = true;
            ////}

            // we found the file - let's check its format
            if (seems_to_be_release_file)
            {
                Regex java_version_regex        = new Regex(@"JAVA_VERSION\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex semantic_version_regex    = new Regex(@"SEMANTIC_VERSION\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex full_version_string_regex = new Regex(@"FULL_VERSION\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex image_type_regex          = new Regex(@"IMAGE_TYPE\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex jvm_impl_regex            = new Regex(@"JVM_VARIANT\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex heap_size_regex           = new Regex(@"HEAP_SIZE\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex os_arch_regex             = new Regex(@"OS_ARCH\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex os_name_regex             = new Regex(@"OS_NAME\s*=\s*""(.+)""", RegexOptions.IgnoreCase);
                Regex source_regex = new Regex(@"SOURCE\s*=\s*""(.+)""", RegexOptions.IgnoreCase);

                bool semantic_parsed_successfully = false;
                bool full_version_processed       = false;

                installed_version.Arch = "";
                installed_version.JVMImplementation = "";
                installed_version.ImageType         = "";
                installed_version.Heap = "";

                StreamReader reader = File.OpenText(System.IO.Path.Combine(path, "release"));

                // initially, found = false
                // should change to true when at least something becomes available

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    // JAVA_VERSION
                    // least informative source
                    // we need it only if there's nothing else available
                    Match match_java_version = java_version_regex.Match(line);
                    if (match_java_version.Success)
                    {
                        // least informative => use only in case nothing is available yet
                        if (!found)
                        {
                            found = installed_version.TrySetVersionFromString(match_java_version.Groups[1].Value);
                        }
                    }

                    // FULL_VERSION (string -> installed_version_string)
                    // more informative (provided it is available)
                    // so try anyway, unless semantic has been parsed already
                    Match match_full_version = full_version_string_regex.Match(line);
                    if (match_full_version.Success)
                    {
                        // update found, unless semantic has been parsed already
                        if (!semantic_parsed_successfully)
                        {
                            bool result = installed_version.TrySetVersionFromString(match_full_version.Groups[1].Value);
                            // if JAVA_VERSION did find something, and this did not, we should use at least some available data
                            found = found || result;
                        }

                        // anyway, set VersionString (highest priority)
                        installed_version.VersionString = match_full_version.Groups[1].Value;
                        full_version_processed          = true;
                    }

                    // SEMANTIC_VERSION
                    // theoretically, the most informative source (provided it is available)
                    Match match_semantic_version = semantic_version_regex.Match(line);
                    if (match_semantic_version.Success)
                    {
                        bool result = installed_version.TrySetVersionFromString(match_semantic_version.Groups[1].Value);

                        // if SEMANTIC_VERSION is parsed correctly, prefer these data over all other
                        if (result)
                        {
                            semantic_parsed_successfully = true;
                            found = true;
                        }

                        // try to set VersionString (low priority)
                        if (!full_version_processed)
                        {
                            installed_version.VersionString = match_semantic_version.Groups[1].Value;
                        }
                    }


                    // IMAGE_TYPE
                    Match match_image_type = image_type_regex.Match(line);
                    if (match_image_type.Success)
                    {
                        installed_version.ImageType = match_image_type.Groups[1].Value.ToLowerInvariant();
                    }

                    // JVM_VARIANT
                    Match match_jvm_impl = jvm_impl_regex.Match(line);
                    if (match_jvm_impl.Success)
                    {
                        installed_version.JVMImplementation = match_jvm_impl.Groups[1].Value.ToLowerInvariant();
                    }

                    // HEAP_SIZE
                    Match match_heap_size = heap_size_regex.Match(line);
                    if (match_heap_size.Success)
                    {
                        installed_version.Heap = "normal";
                        if (match_heap_size.Groups[1].Value.ToLowerInvariant() == "large")
                        {
                            installed_version.Heap = "large";
                        }
                    }

                    // OS_ARCH
                    Match match_arch = os_arch_regex.Match(line);
                    if (match_arch.Success)
                    {
                        string os_arch_string = match_arch.Groups[1].Value;

                        if (os_arch_string.IndexOf("x86_64", StringComparison.OrdinalIgnoreCase) >= 0 ||
                            os_arch_string.IndexOf("amd64", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            installed_version.Arch = "x64";
                        }
                        else if (os_arch_string.IndexOf("i586", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            installed_version.Arch = "x32";
                        }
                    }

                    // OS_NAME
                    Match match_os_name = os_name_regex.Match(line);
                    if (match_os_name.Success)
                    {
                        installed_version.OS = match_os_name.Groups[1].Value.ToLowerInvariant();
                    }

                    // SOURCE (failsafe for installed_version_jvm_implementation)
                    Match match_source = source_regex.Match(line);
                    if (match_source.Success && String.IsNullOrEmpty(installed_version.JVMImplementation))
                    {
                        string source_string = match_source.Groups[1].Value;

                        if (source_string.IndexOf("openj9", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            installed_version.JVMImplementation = "openj9";
                        }
                        else if (source_string.IndexOf("hotspot", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            installed_version.JVMImplementation = "hotspot";
                        }
                    }
                }
            } // if (seems_to_be_adoptopenjdk)

            if (found)
            {
                // check for suggested version
                if (!String.IsNullOrEmpty(suggested_version_string))
                {
                    var version_elements = suggested_version_string.Split('.');
                    if (version_elements.Length == 4 &&
                        version_elements[0] == installed_version.Major.ToString() &&
                        version_elements[1] == installed_version.Minor.ToString() &&
                        version_elements[2] == installed_version.Security.ToString() &&
                        !String.IsNullOrEmpty(version_elements[3])
                        )
                    {
                        installed_version.MSIRevision = Convert.ToInt32(version_elements[3]);
                    }
                }

                // suggested Image Type should be prevalent
                if (!String.IsNullOrEmpty(suggested_image_type))
                {
                    installed_version.ImageType = suggested_image_type.ToLowerInvariant();
                }
                else
                {
                    // if we still do not know image type, let's try this:
                    if (String.IsNullOrEmpty(installed_version.ImageType))
                    {
                        installed_version.ImageType = "";
                        if (File.Exists(System.IO.Path.Combine(path, @"bin\javac.exe")))
                        {
                            installed_version.ImageType = "jdk";
                        }
                        else if (File.Exists(System.IO.Path.Combine(path, @"bin\java.exe")))
                        {
                            installed_version.ImageType = "jre";
                        }
                    }
                }

                // same for JVM Implementation
                if (!String.IsNullOrEmpty(suggested_jvm_implementation))
                {
                    installed_version.JVMImplementation = suggested_jvm_implementation.ToLowerInvariant();
                }
                else
                {
                    //one more check for robustness
                    if (installed_version.JVMImplementation == "")
                    {
                        installed_version.JVMImplementation = Directory.Exists(System.IO.Path.Combine(path, @"bin\j9vm")) ? "openj9" : "hotspot";
                    }
                }

                // same for arch:
                if (suggested_x64.HasValue && suggested_x64 != null)
                {
                    installed_version.Arch = suggested_x64 == true ? "x64" : "x32";
                }


                // if we still do not know heap size, let's try this:
                if (String.IsNullOrEmpty(installed_version.Heap))
                {
                    installed_version.Heap = "normal";
                    if (installed_version.JVMImplementation == "openj9" && Directory.Exists(System.IO.Path.Combine(path, @"bin\default")) &&
                        installed_version.Arch == "x64")    // because 'default' on openj9-x32 means normal heap, not large
                    {
                        installed_version.Heap = "large";
                    }
                }
            }

            installed_version.Found = found;
            return(found);
        }
Ejemplo n.º 2
0
        static public AdoptiumReleaseVersion GetLatestVersion(string version, string implementation, string desired_image_type, string desired_heap, out string error_message_out, string desired_arch = "x64", string desired_os = "windows")
        {
            string URL = baseURL + "assets/latest/" + version + "/" + implementation;

            AdoptiumReleaseVersion latest = new AdoptiumReleaseVersion();

            error_message_out = "";

            try
            {
                IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
                defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;

                HttpClientHandler hch = new HttpClientHandler();
                hch.Proxy = defaultWebProxy;

                //hch.Proxy = ProxyConfigurator.GetWebProxy;
                //hch.UseProxy = ProxyConfigurator.UseProxy;

                var httpClient = new HttpClient(hch);
                Debug.WriteLine($"Querying API: {URL}");

                httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
                var response = httpClient.GetStringAsync(new Uri(URL)).Result;

                JArray a = JArray.Parse(response);
                //MessageBox.Show(a.ToString());
                foreach (JObject o in a.Children <JObject>())
                {
                    string arch       = (string)o["binary"]["architecture"];
                    string image_type = (string)o["binary"]["image_type"];
                    string heap_size  = (string)o["binary"]["heap_size"];
                    string os         = (string)o["binary"]["os"];

                    if (
                        arch == desired_arch &&
                        image_type == desired_image_type &&
                        os == desired_os &&
                        heap_size == desired_heap
                        )

                    {
                        string version_major    = (string)o["version"]["major"];
                        string version_minor    = (string)o["version"]["minor"];
                        string version_security = (string)o["version"]["security"];
                        string version_build    = (string)o["version"]["build"];
                        string version_string   = (string)o["version"]["openjdk_version"];
                        string semantic_version = (string)o["version"]["semver"];
                        string version_release  = (string)o["release_name"];

                        string zip_url = (string)o["binary"]["package"]["link"];

                        string msi_url = (o["binary"]["installer"] != null && o["binary"]["installer"]["link"] != null) ?
                                         (string)o["binary"]["installer"]["link"] : null;

                        //MessageBox.Show(o.ToString());

                        latest.Major    = Convert.ToInt32(version_major);
                        latest.Minor    = Convert.ToInt32(version_minor);
                        latest.Security = Convert.ToInt32(version_security);

                        latest.Build = Convert.ToInt32(version_build);

                        if (o["version"]["patch"] != null)
                        {
                            latest.Patch = Convert.ToInt32(o["version"]["patch"]);
                        }

                        if (o["version"]["adopt_build_number"] != null)
                        {
                            latest.AdoptBuild = Convert.ToInt32(o["version"]["adopt_build_number"]);
                        }

                        latest.VersionString = version_string;
                        latest.ReleaseName   = version_release;
                        latest.SemVerAPI     = semantic_version;

                        latest.MSIURL    = msi_url;
                        latest.ZIPURL    = zip_url;
                        latest.ImageType = image_type;

                        // see https://github.com/AdoptOpenJDK/TSC/issues/185#issuecomment-724696068
                        latest.MSIRevision = Convert.ToInt32(semantic_version.Split('+').Last());

                        /*
                         * semver = major.minor.security + ((patch * 100) + build)
                         * MSI product version = major.minor.security.((patch * 100) + build)
                         * eg.jdk - 11.0.9.1 + 1
                         * => semver = 11.0.9 + 101
                         * => MSI product version = 11.0.9.101
                         */

                        latest.Found = true;
                        break;
                    }
                }

                if (!latest.Found)
                {
                    error_message_out = $"Nothing in API response ({version}/{implementation}) matches your set of release parameters ({desired_os}/{desired_arch}/{desired_image_type}/{desired_heap} heap).";
                }
            }
            catch (Exception ex)
            {
                var ie = ex;
                while (ie.InnerException != null)
                {
                    ie = ie.InnerException;
                }

                error_message_out += $"GetLatestVersion[{URL}]: {ex.Message}" + (ie.InnerException != null ? $" => {ie.Message}" : "");
                //if (latest.)
                Debug.WriteLine(error_message_out);
                //MessageBox.Show("There was an error: " + ex.Message, "Adoptium API Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return(latest);
        }