Skip to main content

elemental_core/runtime/providers/
pm.rs

1#[cfg(any(target_os = "linux", target_os = "macos"))]
2use std::path::Path;
3use std::path::PathBuf;
4
5use async_trait::async_trait;
6
7use super::super::provider::RuntimeProvider;
8use crate::runtime::discovery::find_runtime_homes;
9#[cfg(any(target_os = "linux", target_os = "macos"))]
10use crate::runtime::discovery::find_runtime_homes_with_prefixes;
11
12const PLATFORM_SCAN_DEPTH: usize = 1;
13
14#[derive(Default)]
15pub struct PackageManagerProvider;
16
17#[async_trait]
18impl RuntimeProvider for PackageManagerProvider {
19    async fn list(&self) -> Vec<PathBuf> {
20        Self::get_platform_java_distribution().await
21    }
22
23    fn name(&self) -> &'static str {
24        "PackageManager"
25    }
26}
27
28impl PackageManagerProvider {
29    #[cfg(windows)]
30    async fn get_platform_java_distribution() -> Vec<PathBuf> {
31        const VENDOR_DIRECTORIES: &[&str] = &[
32            "Java",
33            "BellSoft",
34            "AdoptOpenJDK",
35            "Zulu",
36            "Microsoft",
37            "Eclipse Foundation",
38            "Eclipse Adoptium",
39            "Semeru",
40            "Amazon Corretto",
41            "SapMachine",
42        ];
43
44        let mut javas = Vec::new();
45        for variable_name in ["ProgramFiles", "ProgramFiles(x86)"] {
46            let Some(base_path) = std::env::var_os(variable_name).map(PathBuf::from) else {
47                continue;
48            };
49            for vendor_directory in VENDOR_DIRECTORIES {
50                javas.extend(
51                    find_runtime_homes(&base_path.join(vendor_directory), PLATFORM_SCAN_DEPTH)
52                        .await,
53                );
54            }
55        }
56        javas
57    }
58
59    #[cfg(target_os = "linux")]
60    async fn get_platform_java_distribution() -> Vec<PathBuf> {
61        let mut javas = Vec::new();
62
63        for directory in [
64            "/usr/java",
65            "/usr/lib/jvm",
66            "/usr/lib32/jvm",
67            "/usr/lib64/jvm",
68            "/opt/jdk",
69            "/opt/jdks",
70            "/opt/ibm",
71            "/app/jdk",
72        ] {
73            javas.extend(find_runtime_homes(Path::new(directory), PLATFORM_SCAN_DEPTH).await);
74        }
75
76        javas.extend(
77            find_runtime_homes_with_prefixes(
78                Path::new("/usr/lib64"),
79                PLATFORM_SCAN_DEPTH,
80                &["openjdk-", "openj9-"],
81            )
82            .await,
83        );
84        javas.extend(
85            find_runtime_homes_with_prefixes(
86                Path::new("/usr/lib"),
87                PLATFORM_SCAN_DEPTH,
88                &["java", "openjdk-", "openj9-"],
89            )
90            .await,
91        );
92        javas.extend(
93            find_runtime_homes_with_prefixes(
94                Path::new("/opt"),
95                PLATFORM_SCAN_DEPTH,
96                &["openjdk-", "openj9-"],
97            )
98            .await,
99        );
100
101        if let Some(snap_root) = std::env::var_os("SNAP").map(PathBuf::from) {
102            for directory in ["usr/lib/jvm", "app/jdk"] {
103                javas.extend(
104                    find_runtime_homes(&snap_root.join(directory), PLATFORM_SCAN_DEPTH).await,
105                );
106            }
107        }
108
109        javas
110    }
111
112    #[cfg(target_os = "macos")]
113    async fn get_platform_java_distribution() -> Vec<PathBuf> {
114        let mut javas = Vec::new();
115
116        for directory in [
117            "/Library/Java/JavaVirtualMachines",
118            "/System/Library/Java/JavaVirtualMachines",
119        ] {
120            javas.extend(find_runtime_homes(Path::new(directory), 3).await);
121        }
122        for directory in ["/opt/homebrew/opt", "/usr/local/opt"] {
123            javas.extend(
124                find_runtime_homes_with_prefixes(
125                    Path::new(directory),
126                    PLATFORM_SCAN_DEPTH,
127                    &["openjdk", "java"],
128                )
129                .await,
130            );
131        }
132
133        for directory in [
134            "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home",
135            "/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/MacOS/itms/java",
136        ] {
137            javas.extend(find_runtime_homes(Path::new(directory), 0).await);
138        }
139
140        javas
141    }
142
143    #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
144    async fn get_platform_java_distribution() -> Vec<PathBuf> {
145        Vec::new()
146    }
147}