1 karl 1.9 //%2006////////////////////////////////////////////////////////////////////////
|
2 kumpf 1.1 //
|
3 karl 1.5 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.4 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.5 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.6 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.9 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 kumpf 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Barbara Packard (barbara_packard@hp.com)
33 //
|
34 kumpf 1.2 // Modified By: Adriano Zanuz (adriano.zanuz@hp.com)
|
35 kumpf 1.3 // Jair Santos, Hewlett-Packard Company (jair.santos@hp.com)
|
36 mateus.baur 1.8 // Mateus Baur, Hewlett-Packard Company (jair.santos@hp.com)
|
37 kumpf 1.1 //
38 //%/////////////////////////////////////////////////////////////////////////////
39 // WMIBaseProvider.cpp: implementation of the WMIBaseProvider class.
40 //
41 //////////////////////////////////////////////////////////////////////
42
43 #include "stdafx.h"
44
45 #include "WMICollector.h"
46 #include "WMIBaseProvider.h"
47 #include "WMIClassProvider.h"
48 #include "WMIInstanceProvider.h"
49 #include "WMIQueryProvider.h"
50
51 #include "WMIProperty.h"
52 #include "WMIString.h"
53 #include "WMIValue.h"
54 #include "WMIQualifier.h"
55 #include "WMIQualifierSet.h"
56 #include "WMIType.h"
57 #include "WMIException.h"
58 kumpf 1.1
59 PEGASUS_NAMESPACE_BEGIN
60
61 //////////////////////////////////////////////////////////////////////
62 // Construction/Destruction
63 //////////////////////////////////////////////////////////////////////
64
65 WMIBaseProvider::WMIBaseProvider()
66 {
67 }
68
69 WMIBaseProvider::~WMIBaseProvider()
70 {
71 }
72
73 /////////////////////////////////////////////////////////////////////////////
74 // WMIBaseProvider::initialize
75 //
76 // ///////////////////////////////////////////////////////////////////////////
|
77 kumpf 1.2 void WMIBaseProvider::initialize(bool bLocal)
|
78 kumpf 1.1 {
79 PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIBaseProvider::initialize()");
80
|
81 kumpf 1.2 initCollector(bLocal);
|
82 kumpf 1.1
83 PEG_METHOD_EXIT();
84 }
85
86 /////////////////////////////////////////////////////////////////////////////
87 // WMIBaseProvider::terminate
88 //
89 // ///////////////////////////////////////////////////////////////////////////
90 void WMIBaseProvider::terminate(void)
91 {
92 PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIBaseProvider::terminate()");
93
94 cleanup();
95
96 PEG_METHOD_EXIT();
97 }
98
99
100 /////////////////////////////////////////////////////////////////////////////
101 // WMIBaseProvider::setup
102 //
|
103 kumpf 1.2 /////////////////////////////////////////////////////////////////////////////
104 void WMIBaseProvider::setup(const String & nameSpace,
105 const String & userName,
106 const String & password)
|
107 kumpf 1.1 {
108 m_sNamespace = nameSpace;
|
109 kumpf 1.2 m_sUserName = userName;
110 m_sPassword = password;
|
111 kumpf 1.1
112 if (!m_bInitialized)
113 {
114 initCollector();
115 }
116
117 if (m_bInitialized)
118 {
119 _collector->setNamespace(m_sNamespace);
|
120 kumpf 1.2
121 if (m_sUserName != String::EMPTY)
122 _collector->setUserName(m_sUserName);
123
124 if (m_sPassword != String::EMPTY)
125 _collector->setPassword(m_sPassword);
|
126 kumpf 1.1 }
127 }
128
129 /////////////////////////////////////////////////////////////////////////////
130 // WMIBaseProvider::initCollector
131 //
132 // ///////////////////////////////////////////////////////////////////////////
|
133 kumpf 1.2 void WMIBaseProvider::initCollector(bool bLocal)
|
134 kumpf 1.1 {
135
|
136 kumpf 1.2 if (!m_bInitialized)
|
137 kumpf 1.1 {
|
138 kumpf 1.2 _collector = new WMICollector(bLocal);
|
139 kumpf 1.1 m_bInitialized = _collector->setup();
140 }
141
142 }
143
144 /////////////////////////////////////////////////////////////////////////////
145 // WMIBaseProvider::cleanup
146 //
147 // ///////////////////////////////////////////////////////////////////////////
148 void WMIBaseProvider::cleanup()
149 {
150 if (m_bInitialized)
151 {
152 _collector->terminate();
153 delete _collector;
154 _collector = NULL;
155 m_bInitialized = false;
156 }
157 }
158
159 /////////////////////////////////////////////////////////////////////////////
160 kumpf 1.1 // WMIBaseProvider::getCIMInstance - retrieves a CIMInstance object
161 //
162 // ///////////////////////////////////////////////////////////////////////////
|
163 kumpf 1.2 CIMInstance WMIBaseProvider::getCIMInstance(const String& nameSpace,
164 const String& userName,
165 const String& password,
|
166 kumpf 1.1 const CIMObjectPath &instanceName,
167 const CIMPropertyList &propertyList)
168 {
169
170 CIMInstance cimInstance;
171 CIMStatusCode errorCode = CIM_ERR_SUCCESS;
172 String errorDescription;
|
173 kumpf 1.3 WMIInstanceProvider provider;
|
174 kumpf 1.1
175 PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIBaseProvider::getCIMInstance()");
176
177 try
|
178 mateus.baur 1.7 {
179 // This fix uses the current boolean value stored in collector
180 // to initialize it.
181 provider.initialize(_collector->isLocalConnection());
|
182 kumpf 1.1
|
183 kumpf 1.2 cimInstance = provider.getInstance(nameSpace,
184 userName,
185 password,
186 instanceName,
187 false,
188 false,
189 false,
190 propertyList);
|
191 kumpf 1.1 provider.terminate();
192 }
193 catch(CIMException& exception)
194 {
|
195 kumpf 1.3 provider.terminate();
|
196 kumpf 1.1 errorCode = exception.getCode();
197 errorDescription = exception.getMessage();
198 throw PEGASUS_CIM_EXCEPTION(errorCode, errorDescription);
199 }
200 catch(Exception& exception)
201 {
|
202 kumpf 1.3 provider.terminate();
|
203 kumpf 1.1 errorCode = CIM_ERR_FAILED;
204 errorDescription = exception.getMessage();
205 throw PEGASUS_CIM_EXCEPTION(errorCode, errorDescription);
206 }
207 catch(...)
208 {
|
209 kumpf 1.3 provider.terminate();
|
210 kumpf 1.1 throw CIMException(CIM_ERR_FAILED);
211 }
212
213 PEG_METHOD_EXIT();
214
215 return cimInstance;
216 }
217
218
219 /////////////////////////////////////////////////////////////////////////////
220 // WMIBaseProvider::getCIMClass - retrieves a CIMClass object
221 //
222 // ///////////////////////////////////////////////////////////////////////////
|
223 kumpf 1.2 CIMClass WMIBaseProvider::getCIMClass(const String& nameSpace,
224 const String& userName,
225 const String& password,
226 const String& className,
227 const CIMPropertyList &propertyList)
|
228 kumpf 1.1 {
229 CIMClass cimClass;
230 CIMStatusCode errorCode = CIM_ERR_SUCCESS;
231 String errorDescription;
|
232 kumpf 1.3 WMIClassProvider provider;
|
233 kumpf 1.1
234 PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIBaseProvider::getCIMClass()");
235
236 try
237 {
|
238 mateus.baur 1.7 // This fix uses the current boolean value stored in collector
239 // to initialize it.
240 provider.initialize(_collector->isLocalConnection());
|
241 kumpf 1.1
|
242 kumpf 1.2 cimClass = provider.getClass(nameSpace, userName, password, className, false, true, true, propertyList);
|
243 kumpf 1.1
244 provider.terminate();
245 }
246 catch(CIMException& exception)
247 {
|
248 kumpf 1.3 provider.terminate();
|
249 kumpf 1.1 errorCode = exception.getCode();
250 errorDescription = exception.getMessage();
251 throw PEGASUS_CIM_EXCEPTION(errorCode, errorDescription);
252 }
253 catch(Exception& exception)
254 {
|
255 kumpf 1.3 provider.terminate();
|
256 kumpf 1.1 errorCode = CIM_ERR_FAILED;
257 errorDescription = exception.getMessage();
258 throw PEGASUS_CIM_EXCEPTION(errorCode, errorDescription);
259 }
260 catch(...)
261 {
|
262 kumpf 1.3 provider.terminate();
|
263 kumpf 1.1 throw CIMException(CIM_ERR_FAILED);
264 }
265
266 PEG_METHOD_EXIT();
267
268 return cimClass;
269 }
270
271 /////////////////////////////////////////////////////////////////////////////
272 // WMIBaseProvider::execCIMQuery - retrieves a query result
273 //
274 // ///////////////////////////////////////////////////////////////////////////
275 Array<CIMObject> WMIBaseProvider::execCIMQuery(
276 const String& nameSpace,
|
277 kumpf 1.2 const String& userName,
278 const String& password,
|
279 kumpf 1.1 const String& queryLanguage,
280 const String& query,
281 const CIMPropertyList& propertyList,
282 Boolean includeQualifiers,
283 Boolean includeClassOrigin)
284 {
285 Array<CIMObject> objects;
286
287 CIMInstance cimInstance;
288 CIMStatusCode errorCode = CIM_ERR_SUCCESS;
289 String errorDescription;
|
290 kumpf 1.3 WMIQueryProvider provider;
|
291 kumpf 1.1
292 PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIBaseProvider::execCIMQuery()");
293
294 try
295 {
|
296 mateus.baur 1.7 // This fix uses the current boolean value stored in collector
297 // to initialize it.
298 provider.initialize(_collector->isLocalConnection());
|
299 kumpf 1.1
300 objects = provider.execQuery(nameSpace,
|
301 kumpf 1.2 userName,
302 password,
|
303 kumpf 1.1 queryLanguage,
304 query,
305 propertyList,
306 includeQualifiers,
307 includeClassOrigin);
|
308 kumpf 1.3
|
309 kumpf 1.1 provider.terminate();
310 }
311 catch(CIMException& exception)
312 {
|
313 kumpf 1.3 provider.terminate();
|
314 kumpf 1.1 errorCode = exception.getCode();
315 errorDescription = exception.getMessage();
316 throw PEGASUS_CIM_EXCEPTION(errorCode, errorDescription);
317 }
318 catch(Exception& exception)
319 {
|
320 kumpf 1.3 provider.terminate();
|
321 kumpf 1.1 errorCode = CIM_ERR_FAILED;
322 errorDescription = exception.getMessage();
323 throw PEGASUS_CIM_EXCEPTION(errorCode, errorDescription);
324 }
325 catch(...)
326 {
|
327 kumpf 1.3 provider.terminate();
|
328 kumpf 1.1 throw CIMException(CIM_ERR_FAILED);
329 }
330
331 PEG_METHOD_EXIT();
332
333 return objects;
334 }
335
336
337 //////////////////////////////////////////////////////////////////////////////
338 // WMIBaseProvider::getQueryString - builds the query string from the
339 // input parameters for Associator and Reference commands
340 //
341 // ///////////////////////////////////////////////////////////////////////////
342 String WMIBaseProvider::getQueryString(const CIMObjectPath &objectName,
343 const String &sQueryCommand,
344 const String &assocClass,
345 const String &resultClass,
346 const String &role,
347 const String &resultRole)
348 {
349 kumpf 1.1 bool hasWHERE = false;
350 bool isInst;
351
352 //first we need to get the object name
|
353 kumpf 1.3 String sObjName = getObjectName(objectName);
|
354 kumpf 1.1
355 // check if is an instance name
356 Uint32 pos = sObjName.find(qString(Q_PERIOD));
357 isInst = (PEG_NOT_FOUND != pos);
358
|
359 kumpf 1.3 CMyString sQuery;
360 sQuery.Format(CMyString(sQueryCommand), 128, CMyString(sObjName));
|
361 kumpf 1.1
362 //set up any optional parameters
|
363 kumpf 1.3 if (!((0 == assocClass.size()) && (0 == resultClass.size()) &&
364 (0 == role.size()) && (0 == resultRole.size())))
|
365 kumpf 1.1 {
366 // we have optional parameters, append the appropriate ones
367 sQuery += qChar(Q_WHERE);
368 hasWHERE = true;
369
370 if (0 != assocClass.size())
371 {
372 sQuery += qChar(Q_ASSOC_CLS);
|
373 kumpf 1.3 sQuery += assocClass;
|
374 kumpf 1.1 }
375
376 if (0 != resultClass.size())
377 {
378 sQuery += qChar(Q_RESULT_CLASS);
|
379 kumpf 1.3 sQuery += resultClass;
|
380 kumpf 1.1 }
381
382 if (0 != role.size())
383 {
384 sQuery += qChar(Q_ROLE);
|
385 kumpf 1.3 sQuery += role;
|
386 kumpf 1.1 }
387
388 if (0 != resultRole.size())
389 {
390 sQuery += qChar(Q_RESULT_ROLE);
|
391 kumpf 1.3 sQuery += resultRole;
|
392 kumpf 1.1 }
393 }
394
395 // check if an instance
396 if (!isInst)
397 {
398 // have a class, add "SchemaOnly"
399 if (!hasWHERE)
400 {
401 sQuery += qChar(Q_WHERE);
402 }
403
404 sQuery += qChar(Q_SCHEMA);
405 }
406
|
407 marek 1.10 PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
408 "WMIBaseProvider::getQueryString() - Query is %s", (LPCTSTR)sQuery));
|
409 kumpf 1.1
|
410 kumpf 1.3 String s = (LPCTSTR)sQuery;
|
411 kumpf 1.1 return s;
412 }
413
414 //////////////////////////////////////////////////////////////////////////////
415 // WMIBaseProvider::getObjectName - extracts the String object name from
416 // CIMObjectPath
417 // removes namespace
|
418 mateus.baur 1.7 //
419 // Possible input Object Path formats:
420 // 1. Fully-qualified path
421 // example: \\hostname:port\root\cimv2:ClassName.Key1="Value",Key2="Value"
422 //
423 // 2. No hostname & port (implies current host)
424 // example: root\cimv2:ClassName.Key1="Value",Key2="Value"
425 //
426 // 3. No namespace (implies current namespace):
427 // example: ClassName.Key1="Value",Key2="Value"
428 //
429 // 4. Reference instance
430 // example: ClassName.Key1=R"root\cimv2:RefClass.Key="RefValue""
431 //
432 // In all cases, this method needs to return only the class name and keys from
433 // the input object path (need to strip any hostname, port, and namespace).
434 // For example, the return for cases #1-3, above, should be:
435 // ClassName.Key1="Value",Key2="Value"
436 //
437 // Also, for "reference" keys, the reference indicator (R) needs to be removed.
438 // Therefore, the output from case #4, above, would be:
439 mateus.baur 1.7 // ClassName.Key1="root\cimv2:RefClass.Key="RefValue""
440 //
|
441 kumpf 1.1 // ///////////////////////////////////////////////////////////////////////////
442 String WMIBaseProvider::getObjectName( const CIMObjectPath& objectName)
443 {
444 String sObjName;
|
445 mateus.baur 1.7 String sObjNameLower;
|
446 kumpf 1.1 bool bHaveReference = false;
|
447 kumpf 1.3
|
448 kumpf 1.1 PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIBaseProvider::getObjectName()");
449
|
450 mateus.baur 1.7 sObjName = objectName.toString();
451 sObjNameLower = sObjName;
452 sObjNameLower.toLower();
|
453 mateus.baur 1.8
|
454 marek 1.10 PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
|
455 mateus.baur 1.8 "WMIBaseProvider::getObjectName() - ObjectName: %s",
|
456 marek 1.10 sObjName.getCString()));
|
457 mateus.baur 1.7
|
458 mateus.baur 1.8 Uint32 pos;
|
459 kumpf 1.1
|
460 mateus.baur 1.8 // 1. if Object name initiates with a hostname then remove it
461 if ((sObjName.subString(0, 4) != "root") &&
462 (sObjNameLower.subString(0, 2) != "//") &&
463 (sObjNameLower.subString(0, 2) != "\\\\"))
464 {
465 pos = sObjNameLower.find("root");
|
466 mateus.baur 1.7
|
467 mateus.baur 1.8 if (sObjNameLower.find("=") > pos) {
468
469 if (PEG_NOT_FOUND != pos)
470 {
471 sObjName.remove(0, pos);
472 sObjNameLower.remove(0, pos);
473 }
474 }
475 }
476
477 //2. Remove the machine name and port if it exists
478 if ((sObjNameLower.subString(0, 2) == "//") ||
479 (sObjNameLower.subString(0, 2) == "\\\\"))
|
480 mateus.baur 1.7 {
481 pos = sObjNameLower.find("root");
|
482 kumpf 1.2
483 if (PEG_NOT_FOUND != pos)
484 {
|
485 mateus.baur 1.7 sObjName.remove(0, pos);
486 sObjNameLower.remove(0, pos);
487 }
488
|
489 mateus.baur 1.8 //3. After ensuring that all stuff before root was removed,
|
490 mateus.baur 1.7 // get the class/instance name.
491 pos = sObjName.find(qString(Q_COLON));
492
493 if (PEG_NOT_FOUND != pos)
494 {
495 sObjName.remove(0, pos + 1);
|
496 kumpf 1.2 }
497 }
|
498 mateus.baur 1.7 else
499 {
500 // get the class/instance name.
501 if (sObjNameLower.subString(0, 4) == "root")
502 {
503 pos = sObjName.find(qString(Q_COLON));
504
505 if (PEG_NOT_FOUND != pos)
506 {
507 sObjName.remove(0, pos + 1);
508 }
509 }
|
510 kumpf 1.1 }
511
|
512 mateus.baur 1.8 //4. Check if has =R".." for a reference instance and
|
513 kumpf 1.1 // if so, remove the R
|
514 kumpf 1.2 //Uint32 pos = sObjName.find(qString(Q_REF_KEY));
|
515 kumpf 1.1 pos = sObjName.find(qString(Q_REF_KEY));
516 bHaveReference = (PEG_NOT_FOUND != pos);
517
518 if (bHaveReference)
519 {
520 while (PEG_NOT_FOUND != pos)
521 {
|
522 kumpf 1.2 sObjName.remove(pos + 1, 1); //removing R"
|
523 kumpf 1.1 pos = sObjName.find(qString(Q_REF_KEY));
524 }
525 }
526
|
527 marek 1.10 PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
|
528 mateus.baur 1.8 "WMIBaseProvider::getObjectName() - ObjectName: %s",
|
529 marek 1.10 sObjName.getCString()));
|
530 mateus.baur 1.8
|
531 kumpf 1.1 PEG_METHOD_EXIT();
532
533 return sObjName;
534 }
535
536 PEGASUS_NAMESPACE_END
|