View Javadoc
1   package org.metricshub.wbem.client;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WBEM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2025 MetricsHub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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   * Functions to execute WBEM query.
35   *
36   */
37  public class WbemExecutor {
38  
39  	private WbemExecutor() {}
40  
41  	/**
42  	 * Execute a WBEM query on remote.
43  	 *
44  	 * @param url The remote URL.
45  	 * @param namespace The WBEM namespace.
46  	 * @param username The user name.
47  	 * @param password The Password.
48  	 * @param query The WQL Query to execute.
49  	 * @param timeout Timeout in milliseconds.
50  	 * @param arraySeparator The array separator value. default value '|'
51  	 * @return
52  	 * @throws WqlQuerySyntaxException On WQL syntax errors.
53  	 * @throws WBEMException On WBEM errors.
54  	 * @throws TimeoutException To notify userName of timeout.
55  	 * @throws InterruptedException
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  	 * Execute WBEM get associators on remote.
72  	 *
73  	 * @param url The remote URL.
74  	 * @param username The user name.
75  	 * @param password The Password.
76  	 * @param query The WQL Query to execute.
77  	 * @param objectPathAssociators The object path for ASSOCIATORS.
78  	 * @param timeout Timeout in milliseconds.
79  	 * @param arraySeparator The array separator value. default value '|'
80  	 * @return
81  	 * @throws WqlQuerySyntaxException On WQL syntax errors.
82  	 * @throws WBEMException On WBEM errors.
83  	 * @throws TimeoutException To notify userName of timeout.
84  	 * @throws InterruptedException
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 	 * Execute the WBEM method with timeout.
101 	 *
102 	 * @param url
103 	 * @param namespace
104 	 * @param username
105 	 * @param password
106 	 * @param query
107 	 * @param objectPathAssociators
108 	 * @param timeout
109 	 * @param arraySeparator
110 	 * @return
111 	 * @throws InterruptedException
112 	 * @throws TimeoutException
113 	 * @throws WBEMException
114 	 * @throws WqlQuerySyntaxException
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 			// else should be RunTimeException as matsyaWbemClient only thrown
160 			// WBEMException as checked exceptions.
161 			throw (RuntimeException) e.getCause();
162 		} finally {
163 			executor.shutdownNow();
164 		}
165 	}
166 }