1 package org.metricshub.wbem.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.net.URL;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import org.metricshub.wbem.client.exceptions.WqlQuerySyntaxException;
31 import org.metricshub.wbem.javax.wbem.WBEMException;
32
33
34
35
36
37 public class WbemExecutor {
38
39 private WbemExecutor() {}
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static WbemQueryResult executeWql(
58 final URL url,
59 final String namespace,
60 final String username,
61 final char[] password,
62 final String query,
63 int timeout,
64 final String arraySeparator
65 )
66 throws WqlQuerySyntaxException, WBEMException, TimeoutException, InterruptedException {
67 return executeMethod(url, namespace, username, password, query, null, timeout, arraySeparator);
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public static WbemQueryResult getAssociators(
87 final URL url,
88 final String username,
89 final char[] password,
90 final String query,
91 final String objectPathAssociators,
92 int timeout,
93 final String arraySeparator
94 )
95 throws WqlQuerySyntaxException, WBEMException, TimeoutException, InterruptedException {
96 return executeMethod(url, null, username, password, query, objectPathAssociators, timeout, arraySeparator);
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 private static WbemQueryResult executeMethod(
117 final URL url,
118 final String namespace,
119 final String username,
120 final char[] password,
121 final String query,
122 final String objectPathAssociators,
123 int timeout,
124 final String arraySeparator
125 )
126 throws InterruptedException, TimeoutException, WBEMException, WqlQuerySyntaxException {
127 Utils.checkNonNull(url, "url");
128 Utils.checkNonNull(username, "username");
129 Utils.checkNonNull(password, "password");
130
131 final WqlQuery wqlQuery = WqlQuery.parseQuery(query);
132
133 final ExecutorService executor = Executors.newSingleThreadExecutor();
134
135 final Future<WbemQueryResult> future = executor.submit(
136 () -> {
137 try (final WbemClient matsyaWbemClient = new WbemClient()) {
138 matsyaWbemClient.connect(url, username, password, timeout);
139
140 return objectPathAssociators == null
141 ? matsyaWbemClient.executeWql(wqlQuery, namespace, arraySeparator)
142 : matsyaWbemClient.getAssociators(wqlQuery, objectPathAssociators, arraySeparator);
143 }
144 }
145 );
146
147 try {
148 return future.get(timeout, TimeUnit.MILLISECONDS);
149 } catch (InterruptedException e) {
150 Thread.currentThread().interrupt();
151 throw (InterruptedException) e;
152 } catch (TimeoutException e) {
153 future.cancel(true);
154 throw e;
155 } catch (ExecutionException e) {
156 if (e.getCause() instanceof WBEMException) {
157 throw (WBEMException) e.getCause();
158 }
159
160
161 throw (RuntimeException) e.getCause();
162 } finally {
163 executor.shutdownNow();
164 }
165 }
166 }