|
| 1 | +package io.qase.commons.utils; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.net.InetAddress; |
| 5 | + |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | + |
| 14 | +import org.json.JSONObject; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class that collects and provides information about the host system and installed packages |
| 18 | + */ |
| 19 | +public class HostInfo { |
| 20 | + |
| 21 | + /** |
| 22 | + * Execute a command and return its output as a string |
| 23 | + * |
| 24 | + * @param command Command to execute |
| 25 | + * @param defaultValue Default value to return on error |
| 26 | + * @return Command output or default value |
| 27 | + */ |
| 28 | + private String execCommand(String command, String defaultValue) { |
| 29 | + StringBuilder output = new StringBuilder(); |
| 30 | + try { |
| 31 | + Process process; |
| 32 | + if (System.getProperty("os.name").toLowerCase().contains("windows")) { |
| 33 | + process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", command}); |
| 34 | + } else { |
| 35 | + process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command}); |
| 36 | + } |
| 37 | + |
| 38 | + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 39 | + String line; |
| 40 | + while ((line = reader.readLine()) != null) { |
| 41 | + output.append(line).append("\n"); |
| 42 | + } |
| 43 | + |
| 44 | + int exitCode = process.waitFor(); |
| 45 | + if (exitCode != 0) { |
| 46 | + System.err.println("Error executing command '" + command + "': Return code " + exitCode); |
| 47 | + return defaultValue; |
| 48 | + } |
| 49 | + |
| 50 | + return output.toString().trim(); |
| 51 | + } catch (IOException | InterruptedException e) { |
| 52 | + System.err.println("Exception executing command '" + command + "': " + e.getMessage()); |
| 53 | + return defaultValue; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get detailed OS information based on the platform |
| 59 | + * |
| 60 | + * @return Detailed OS information |
| 61 | + */ |
| 62 | + private String getDetailedOsInfo() { |
| 63 | + String system = System.getProperty("os.name").toLowerCase(); |
| 64 | + |
| 65 | + try { |
| 66 | + if (system.contains("windows")) { |
| 67 | + return execCommand("ver", ""); |
| 68 | + } else if (system.contains("mac")) { |
| 69 | + return execCommand("sw_vers -productVersion", ""); |
| 70 | + } else { |
| 71 | + // Linux and others |
| 72 | + try { |
| 73 | + File osReleaseFile = new File("/etc/os-release"); |
| 74 | + if (osReleaseFile.exists()) { |
| 75 | + String osRelease = new String(Files.readAllBytes(Paths.get("/etc/os-release"))); |
| 76 | + Pattern pattern = Pattern.compile("PRETTY_NAME=\"(.+)\""); |
| 77 | + Matcher matcher = pattern.matcher(osRelease); |
| 78 | + if (matcher.find()) { |
| 79 | + return matcher.group(1); |
| 80 | + } |
| 81 | + } |
| 82 | + } catch (IOException e) { |
| 83 | + // Fallback if /etc/os-release doesn't exist or can't be read |
| 84 | + } |
| 85 | + |
| 86 | + return System.getProperty("os.version"); |
| 87 | + } |
| 88 | + } catch (Exception e) { |
| 89 | + System.err.println("Error getting detailed OS info: " + e.getMessage()); |
| 90 | + return System.getProperty("os.version"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get information about the current host environment |
| 96 | + * |
| 97 | + * @param reporterVersion Reporter version to check |
| 98 | + * @return Map with host information |
| 99 | + */ |
| 100 | + public Map<String, String> getHostInfo(String reporterVersion) { |
| 101 | + Map<String, String> hostInfo = new HashMap<>(); |
| 102 | + |
| 103 | + try { |
| 104 | + // Get Java version |
| 105 | + String javaVersion = System.getProperty("java.version"); |
| 106 | + |
| 107 | + // Get Maven/Gradle version |
| 108 | + String buildToolVersion = ""; |
| 109 | + String mavenOutput = execCommand("mvn --version", ""); |
| 110 | + if (!mavenOutput.isEmpty()) { |
| 111 | + Pattern pattern = Pattern.compile("Apache Maven (\\S+)"); |
| 112 | + Matcher matcher = pattern.matcher(mavenOutput); |
| 113 | + if (matcher.find()) { |
| 114 | + buildToolVersion = matcher.group(1); |
| 115 | + } |
| 116 | + } else { |
| 117 | + String gradleOutput = execCommand("gradle --version", ""); |
| 118 | + if (!gradleOutput.isEmpty()) { |
| 119 | + Pattern pattern = Pattern.compile("Gradle (\\S+)"); |
| 120 | + Matcher matcher = pattern.matcher(gradleOutput); |
| 121 | + if (matcher.find()) { |
| 122 | + buildToolVersion = matcher.group(1); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + hostInfo.put("system", System.getProperty("os.name").toLowerCase()); |
| 128 | + hostInfo.put("machineName", InetAddress.getLocalHost().getHostName()); |
| 129 | + hostInfo.put("release", System.getProperty("os.version")); |
| 130 | + hostInfo.put("version", getDetailedOsInfo()); |
| 131 | + hostInfo.put("arch", System.getProperty("os.arch")); |
| 132 | + hostInfo.put("java", javaVersion); |
| 133 | + hostInfo.put("buildTool", buildToolVersion); |
| 134 | + hostInfo.put("reporter", reporterVersion); |
| 135 | + } catch (Exception e) { |
| 136 | + System.err.println("Error getting host info: " + e.getMessage()); |
| 137 | + |
| 138 | + try { |
| 139 | + hostInfo.put("system", System.getProperty("os.name").toLowerCase()); |
| 140 | + hostInfo.put("machineName", InetAddress.getLocalHost().getHostName()); |
| 141 | + hostInfo.put("release", System.getProperty("os.version")); |
| 142 | + hostInfo.put("version", ""); |
| 143 | + hostInfo.put("arch", System.getProperty("os.arch")); |
| 144 | + hostInfo.put("java", ""); |
| 145 | + hostInfo.put("buildTool", ""); |
| 146 | + hostInfo.put("reporter", ""); |
| 147 | + } catch (Exception ex) { |
| 148 | + System.err.println("Error creating fallback host info: " + ex.getMessage()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return hostInfo; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Convert the host info Map to a JSON string |
| 157 | + * |
| 158 | + * @param hostInfo The host info Map |
| 159 | + * @return A JSON string representation |
| 160 | + */ |
| 161 | + public String toJson(Map<String, String> hostInfo) { |
| 162 | + JSONObject json = new JSONObject(hostInfo); |
| 163 | + return json.toString(2); |
| 164 | + } |
| 165 | +} |
0 commit comments