/
TestService.java
189 lines (167 loc) · 5.97 KB
/
TestService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.wmi.test;
import org.jkiss.wmi.service.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* WMI Service tester
*/
public class TestService {
private WMIService service;
private boolean finished = false;
private WMIService nsService;
public TestService()
{
}
public static void main(String[] args)
{
new TestService()
.test();
}
void test()
{
try {
{
Thread testThread = new Thread() {
@Override
public void run()
{
try {
service = WMIService.connect(null, "localhost", null, null, null, "root");
ObjectCollectorSink classesSink = new ObjectCollectorSink();
service.enumClasses(null, classesSink, 0);
classesSink.waitForFinish();
Thread.sleep(10000);
} catch (WMIException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
};
testThread.start();
Thread.sleep(1000);
}
{
Thread testThread2 = new Thread() {
@Override
public void run()
{
try {
testNamespace();
} catch (WMIException e) {
e.printStackTrace();
}
}
};
testThread2.start();
testThread2.join();
}
} catch (InterruptedException e) {
// do nothing
} finally {
service.close();
}
System.gc();
System.out.println("DONE");
System.exit(0);
}
private void testNamespace()
throws WMIException
{
//WMIService.initializeThread();
ObjectCollectorSink classesSink;
nsService = service.openNamespace("cimv2");
ObjectCollectorSink tmpSink = new ObjectCollectorSink();
nsService.executeQuery("SELECT * FROM Win32_Process", tmpSink, WMIConstants.WBEM_FLAG_SEND_STATUS);
tmpSink.waitForFinish();
for (WMIObject o : tmpSink.objectList) {
System.out.println("=============");
// for (WMIObjectAttribute attr : o.getAttributes(WMIConstants.WBEM_FLAG_ALWAYS)) {
// System.out.println(attr.toString());
// }
System.out.println("Caption=" + o.getValue("Caption"));
System.out.println("CommandLine=" + o.getValue("CommandLine"));
System.out.println("CreationClassName=" + o.getValue("CreationClassName"));
System.out.println("CreationDate=" + o.getValue("CreationDate"));
}
}
private static void printObject(WMIObject object)
{
try {
System.out.println("====== " + object.getObjectText());
} catch (WMIException e) {
e.printStackTrace();
}
}
private static void examineObject(WMIObject object) throws WMIException
{
final String objectText = object.getObjectText();
//final Object name = object.getValue("Name");
for (WMIObjectAttribute prop : object.getAttributes(WMIConstants.WBEM_FLAG_ALWAYS)) {
Object propValue = prop.getValue();
if (propValue instanceof Object[]) {
//System.out.print("\t" + prop.getName() + "= { ");
Object[] array = (Object[])propValue;
for (int i = 0; i < array.length; i++) {
//if (i > 0) System.out.print(", ");
//System.out.print("'" + array[i] + "'");
}
//System.out.println(" }");
} else if (propValue instanceof byte[]) {
//System.out.println("\t" + prop.getName() + "= { byte array } " + ((byte[])propValue).length);
} else {
//System.out.println("\t" + prop.getName() + "=" + propValue);
}
}
}
private class ObjectCollectorSink implements WMIObjectSink
{
private final List<WMIObject> objectList;
private boolean finished = false;
public ObjectCollectorSink()
{
this.objectList = new ArrayList<>();
}
@Override
public void indicate(WMIObject[] objects)
{
Collections.addAll(objectList, objects);
}
@Override
public void setStatus(WMIObjectSinkStatus status, int result, String param, WMIObject errorObject)
{
if (status == WMIObjectSinkStatus.complete) {
finished = true;
}
}
public void waitForFinish()
{
try {
while (!finished) {
Thread.sleep(100);
}
//service.cancelAsyncOperation(wmiObjectSink);
}
catch (InterruptedException e) {
// do nothing
}
}
}
}