1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.windowsutils;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.util.StringTokenizer;
22
23 import com.gridsystems.utils.SystemUtils;
24
25
26
27
28
29
30
31 public final class WindowsUtils {
32
33
34
35
36 private WindowsUtils() {
37 }
38
39 static {
40 if (SystemUtils.isWindows()) {
41 try {
42 SystemUtils.extractDynamicLibrary("WindowsUtils", WindowsUtils.class);
43 } catch (Exception ex) {
44 System.err.println("Cannot load WindowsUtils dynamic library. Reason: \n");
45 ex.printStackTrace();
46 }
47 }
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public static void createWindowsService(String servicename,
68 String executable, String user, String pass) throws Exception {
69 if (!SystemUtils.isWindows()) {
70 throw new Exception("SystemUtils.createWindowsService() -> "
71 + "This method is not supported in systems that are not Windows");
72 }
73
74 if ((servicename == null) || (servicename.equals(""))) {
75 throw new Exception(
76 "SystemUtils.createWindowsService() -> Service name is empty");
77 }
78
79 if ((executable == null) || (executable.equals(""))) {
80 throw new Exception(
81 "SystemUtils.createWindowsService() -> Executable name is empty");
82 }
83
84 java.io.File f = new java.io.File(executable);
85
86 if (!f.exists()) {
87 throw new Exception(
88 "SystemUtils.createWindowsService() -> Executable file not exists.("
89 + executable + ")");
90 }
91
92 if ((user != null) && (user.length() > 0)) {
93 if (!user.startsWith(".\\")) {
94 user = ".\\" + user;
95 }
96 } else {
97
98 user = null;
99 pass = null;
100 }
101
102 String error =
103 nativeCreateWindowsService(servicename, executable, user, pass);
104
105 if (error != null) {
106 throw new Exception(
107 "RegistryKey.createWindowsService() -> Error:" + error);
108 }
109
110
111
112 executable = executable.replaceAll("/", "\\\\");
113 error = registerServiceMessages(servicename, executable);
114
115 if (error != null) {
116 throw new Exception(
117 "RegistryKey.registeringMessages() -> Error:" + error);
118 }
119 }
120
121
122
123
124
125
126 public static void removeWindowsService(String servicename) throws Exception {
127 if (!SystemUtils.isWindows()) {
128 throw new Exception("SystemUtils.removeWindowsService() -> "
129 + "This method is not supported in systems that are not Windows");
130 }
131
132 if ((servicename == null) || servicename.equals("")) {
133 throw new Exception(
134 "SystemUtils.createWindowsService() -> Service name is empty");
135 }
136
137 String error = nativeRemoveWindowsService(servicename);
138
139 if (error != null) {
140 throw new Exception(
141 "SystemUtils.removeWindowsService() -> Error:" + error);
142 }
143
144
145
146 unregisterServiceMessages(servicename);
147 }
148
149
150
151
152
153
154
155
156
157
158 public static void addUserPrivilege(String hostName, String user,
159 String privilege) throws Exception {
160
161 if (!SystemUtils.isWindows()) {
162 throw new Exception("SystemUtils.addUserPrivilege() -> "
163 + "This method is not supported in systems that are not Windows");
164 }
165
166 if ((user == null) || (user.equals(""))) {
167 throw new Exception("SystemUtils.addUserPrivilege() -> No user provided");
168 }
169
170 if ((privilege == null) || (privilege.equals(""))) {
171 throw new Exception(
172 "SystemUtils.addUserPrivilege() -> No privilege provided");
173 }
174
175 String error = nativeAddUserPrivilege(hostName, user, privilege);
176
177 if (error != null) {
178 throw new Exception("SystemUtils.addUserPrivilege() -> Error:" + error);
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 public static boolean userHasPrivilege(String user, String privilege)
194 throws Exception {
195
196 if (!SystemUtils.isWindows()) {
197 throw new Exception(
198 "SystemUtils.userHasPrivilege() -> This method is not supported"
199 + " in systems that are not Windows");
200 }
201
202 if (user == null || user.equals("")) {
203 throw new Exception("SystemUtils.userHasPrivilege() -> No user provided");
204 }
205
206 if (privilege == null || privilege.equals("")) {
207 throw new Exception(
208 "SystemUtils.userHasPrivilege() -> No privilege provided");
209 }
210
211 return nativeUserHasPrivilege(user, privilege);
212 }
213
214
215
216
217
218
219
220
221 public static boolean checkWindowsUser(String user) throws Exception {
222 boolean existUser = false;
223
224 ByteArrayOutputStream baos = new ByteArrayOutputStream();
225 final String cmd = "net users";
226 Integer exitCode = SystemUtils.execAndSaveOutput(cmd, null, null, baos);
227 if (exitCode == null) {
228 throw new Exception("Unknown error executing " + cmd + ": " + baos.toString());
229 }
230
231
232
233 String allUsers = baos.toString();
234 StringTokenizer st = new StringTokenizer(allUsers, "\n");
235
236 for (int i = 0; i <= 3; i++) {
237 st.nextToken();
238 }
239 int last = st.countTokens();
240
241 String lineOfNTUsers = null;
242 String currentNTUser = null;
243 int lon;
244 for (int i = 2; i < last; i++) {
245
246
247
248
249
250
251 lineOfNTUsers = st.nextToken();
252 lon = lineOfNTUsers.length();
253 for (int j = 25; j <= lon; j = j + 25) {
254 currentNTUser = lineOfNTUsers.substring(j - 25, j).trim();
255 if (user.equals(currentNTUser)) {
256 existUser = true;
257 }
258 }
259 }
260 return existUser;
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 private static native String nativeCreateWindowsService(String servicename,
277 String executable, String usr, String pass);
278
279
280
281
282
283
284
285
286 private static native String nativeRemoveWindowsService(String servicename);
287
288
289
290
291
292
293
294
295
296
297
298 private static native String nativeAddUserPrivilege(String hostName,
299 String user, String privilege);
300
301
302
303
304
305
306
307
308
309
310 private static native boolean nativeUserHasPrivilege(String userName,
311 String privilege);
312
313
314
315
316
317
318
319
320
321
322 private static native String registerServiceMessages(
323 String serviceName, String path);
324
325
326
327
328
329
330
331
332 private static native String unregisterServiceMessages(
333 String serviceName);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 public static native boolean startService(String serviceName,
349 String[] arguments, boolean errorIfRunning);
350
351
352
353
354
355
356
357
358
359
360
361
362 public static native boolean stopService(String serviceName,
363 boolean errorIfStopped);
364
365
366
367
368
369
370
371
372
373
374
375
376 public static native boolean serviceExists(String serviceName)
377 throws Exception;
378
379
380
381
382
383
384
385
386
387 public static native boolean checkAdministrator() throws Exception;
388
389
390
391
392
393
394
395
396
397
398
399 public static native String getServiceStartUser(String serviceName)
400 throws Exception;
401 }