1 package org.metricshub.winrm.wql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.nio.file.Path;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.TimeoutException;
27 import java.util.stream.Collectors;
28 import org.metricshub.winrm.Utils;
29 import org.metricshub.winrm.WinRMHttpProtocolEnum;
30 import org.metricshub.winrm.WmiHelper;
31 import org.metricshub.winrm.exceptions.WinRMException;
32 import org.metricshub.winrm.exceptions.WqlQuerySyntaxException;
33 import org.metricshub.winrm.service.WinRMEndpoint;
34 import org.metricshub.winrm.service.WinRMService;
35 import org.metricshub.winrm.service.client.auth.AuthenticationEnum;
36
37 public class WinRMWqlExecutor {
38
39 private final long executionTime;
40 private final List<String> headers;
41 private final List<List<String>> rows;
42
43
44
45
46
47
48
49
50 public WinRMWqlExecutor(final long executionTime, final List<String> headers, final List<List<String>> rows) {
51 this.executionTime = executionTime;
52 this.headers = headers;
53 this.rows = rows;
54 }
55
56
57
58
59
60 public long getExecutionTime() {
61 return executionTime;
62 }
63
64
65
66
67
68 public List<String> getHeaders() {
69 return headers;
70 }
71
72
73
74
75
76 public List<List<String>> getRows() {
77 return rows;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public static WinRMWqlExecutor executeWql(
101 final WinRMHttpProtocolEnum protocol,
102 final String hostname,
103 final Integer port,
104 final String username,
105 final char[] password,
106 final String namespace,
107 final String wqlQuery,
108 final long timeout,
109 final Path ticketCache,
110 final List<AuthenticationEnum> authentications
111 ) throws WinRMException, WqlQuerySyntaxException, TimeoutException {
112 Utils.checkNonNull(wqlQuery, "wqlQuery");
113 Utils.checkArgumentNotZeroOrNegative(timeout, "timeout");
114
115 final long start = Utils.getCurrentTimeMillis();
116
117 final WinRMEndpoint winRMEndpoint = new WinRMEndpoint(protocol, hostname, port, username, password, namespace);
118
119 try (
120 final WinRMService winRMService = WinRMService.createInstance(
121 winRMEndpoint,
122 timeout,
123 ticketCache,
124 authentications
125 )
126 ) {
127 final List<Map<String, Object>> result = winRMService.executeWql(wqlQuery, timeout);
128
129
130 final List<String> headers = WmiHelper.extractPropertiesFromResult(result, wqlQuery);
131
132 final List<List<String>> rows = result
133 .stream()
134 .map(row -> headers.stream().map(header -> (String) row.get(header)).collect(Collectors.toList()))
135 .collect(Collectors.toList());
136
137 return new WinRMWqlExecutor(Utils.getCurrentTimeMillis() - start, headers, rows);
138 }
139 }
140 }