(file) Return to WMIInstanceProvider.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / WMIMapper / WMIProvider

   1 karl  1.12 //%2006////////////////////////////////////////////////////////////////////////
   2 kumpf 1.1  //
   3 karl  1.6  // 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.6  // 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.7  // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
  10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
  11 karl  1.12 // 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  //              Mateus Baur (HPB)
  36            //              Jair Santos, Hewlett-Packard Company (jair.santos@hp.com)
  37 kumpf 1.5  //              Terry Martin, Hewlett-Packard Company (terry.martin@hp.com)
  38 kumpf 1.1  //
  39            //%/////////////////////////////////////////////////////////////////////////////
  40            
  41            /////////////////////////////////////////////////////////////////////////////
  42            // WMIInstanceProvider::
  43            //
  44            // ///////////////////////////////////////////////////////////////////////////
  45            
  46            #include "StdAfx.h"
  47            
  48            #include "WMICollector.h"
  49            #include "WMIBaseProvider.h"
  50            #include "WMIClassProvider.h"
  51            #include "WMIInstanceProvider.h"
  52 kumpf 1.2  #include "WMIObjectPath.h"
  53 kumpf 1.1  
  54            #include "WMIProperty.h"
  55            #include "WMIString.h"
  56            #include "WMIValue.h"
  57            #include "WMIQualifier.h"
  58            #include "WMIQualifierSet.h"
  59            #include "WMIType.h"
  60            #include "WMIException.h"
  61 kumpf 1.2  #include <mbstring.h>
  62 kumpf 1.1  
  63            //using namespace std;
  64            
  65            /////////////////////////////////////////////////////////////////////////////
  66            // WMIInstanceProvider::
  67            //
  68            // ///////////////////////////////////////////////////////////////////////////
  69            PEGASUS_NAMESPACE_BEGIN
  70            
  71            //////////////////////////////////////////////////////////////////////
  72            // Construction/Destruction
  73            //////////////////////////////////////////////////////////////////////
  74            WMIInstanceProvider::WMIInstanceProvider(void)
  75            {
  76 kumpf 1.5  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::constructor()");
  77            	
  78                _collector = NULL;
  79 kumpf 1.1  	m_bInitialized = false;
  80 kumpf 1.5  
  81            	PEG_METHOD_EXIT();
  82 kumpf 1.1  }
  83            
  84            WMIInstanceProvider::~WMIInstanceProvider(void)
  85            {
  86 kumpf 1.5  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::destructor()");
  87            
  88 kumpf 1.1  	cleanup();
  89 kumpf 1.5  
  90            	PEG_METHOD_EXIT();
  91 kumpf 1.1  }
  92            
  93            /////////////////////////////////////////////////////////////////////////////
  94            // WMIInstanceProvider::getInstance
  95            //
  96            // ///////////////////////////////////////////////////////////////////////////
  97            CIMInstance WMIInstanceProvider::getInstance(
  98                    const String& nameSpace,
  99 kumpf 1.2          const String& userName,
 100                    const String& password,
 101 kumpf 1.1          const CIMObjectPath& instanceName,
 102                    Boolean localOnly,
 103                    Boolean includeQualifiers,
 104                    Boolean includeClassOrigin ,
 105                    const CIMPropertyList& propertyList)
 106            {
 107 kumpf 1.2  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::getInstance()");
 108 kumpf 1.1  
 109 kumpf 1.2  	CComPtr<IWbemClassObject> pInstance;
 110 kumpf 1.3  	String sClassName = instanceName.getClassName().getString();
 111 kumpf 1.2  	String sInstanceName = getObjectName(instanceName);
 112 kumpf 1.1  	CIMInstance cimInstance(sClassName);
 113            
 114 kumpf 1.2  	setup(nameSpace, userName, password);
 115 kumpf 1.1  
 116 marek 1.13 	PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 117 kumpf 1.1  		"getInstance - localOnly %x, includeQualifiers %x, includeClassOrigin %x", 
 118 kumpf 1.2  		localOnly, 
 119            		includeQualifiers, 
 120 marek 1.13 		includeClassOrigin));
 121 kumpf 1.2  
 122 marek 1.13 	PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 123 kumpf 1.3  		"getInstance - classname - %s, namespace - %s, instancename - %s",  
 124 marek 1.13 		sClassName, nameSpace ,sInstanceName));
 125 kumpf 1.1  
 126            	if (!m_bInitialized)
 127            	{
 128 marek 1.13 		PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 129 kumpf 1.5  			"WMIInstanceProvider::getInstance - m_bInitilized= %x, throw CIM_ERR_FAILED exception",  
 130 marek 1.13 			m_bInitialized));
 131 kumpf 1.5  
 132 kumpf 1.1  		throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Collector initialation failed.");
 133            	}
 134            
 135            	// retrieve instance object
 136            	if (!(_collector->getObject(&pInstance, sInstanceName)))
 137 kumpf 1.3  	{
 138            		if (pInstance)
 139            			pInstance.Release();
 140            
 141 kumpf 1.1  		throw CIMException(CIM_ERR_NOT_FOUND);
 142            	}
 143            	else if (!(_collector->isInstance(pInstance)))
 144            	{
 145 kumpf 1.3  		if (pInstance)
 146            			pInstance.Release();
 147            		
 148 kumpf 1.1  		throw CIMException(CIM_ERR_INVALID_PARAMETER);
 149            	}
 150            
 151            	// Get the instance object.
 152 kumpf 1.2  	if (!_collector->getCIMInstance(pInstance, 
 153            		                            cimInstance,
 154            									localOnly, 
 155            									includeQualifiers,
 156            									includeClassOrigin, 
 157            									propertyList, 
 158            									TRUE)) // need key properties here
 159 kumpf 1.1  	{
 160 kumpf 1.3  		if (pInstance)
 161            			pInstance.Release();
 162            
 163 kumpf 1.1  		throw CIMException(CIM_ERR_NOT_FOUND);
 164            	}
 165            
 166 kumpf 1.3  	if (pInstance)
 167            		pInstance.Release();
 168            
 169 kumpf 1.1  	PEG_METHOD_EXIT();
 170            
 171            	return cimInstance;
 172            }
 173            
 174            /////////////////////////////////////////////////////////////////////////////
 175            // WMIInstanceProvider::enumerateInstances
 176            //
 177            // ///////////////////////////////////////////////////////////////////////////
 178            Array<CIMInstance> WMIInstanceProvider::enumerateInstances(
 179                    const String& nameSpace,
 180 kumpf 1.2          const String& userName,
 181                    const String& password,
 182 kumpf 1.1          const String& className,
 183                    Boolean deepInheritance,
 184                    Boolean localOnly,
 185                    Boolean includeQualifiers,
 186                    Boolean includeClassOrigin,
 187                    const CIMPropertyList& propertyList)
 188            {
 189            	HRESULT hr;
 190            	long lCount = 0;
 191            	DWORD dwReturned;
 192            
 193            	CComPtr<IEnumWbemClassObject>	pInstEnum;
 194            	CComPtr<IWbemClassObject>		pInstance;
 195            
 196            	Array<CIMInstance> namedInstances;
 197            
 198            	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::enumerateInstances()");
 199            
 200 kumpf 1.2  	setup(nameSpace, userName, password);
 201 kumpf 1.1  
 202 marek 1.13 	PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 203 kumpf 1.1  		"enumerateInstances - deepInheritance %x, localOnly %x, includeQualifiers %x, includeClassOrigin %x", 
 204 marek 1.13 		deepInheritance, localOnly, includeQualifiers, includeClassOrigin));
 205 kumpf 1.1  	
 206            	if (!m_bInitialized)
 207            	{
 208 marek 1.13 		PEG_TRACE_CSTRING(TRC_WMIPROVIDER, Tracer::LEVEL3,
 209 kumpf 1.5  			"enumerateInstances - m_bInitialized is false; throw exception"); 
 210            
 211 kumpf 1.1  		throw CIMException(CIM_ERR_FAILED);
 212            	}
 213            
 214            	// retrieve instance enumeration object
 215            	if (!(_collector->getInstanceEnum(&pInstEnum, className, deepInheritance)))
 216            	{
 217 kumpf 1.3  		if (pInstEnum)
 218            			pInstEnum.Release();
 219            
 220 kumpf 1.2  		throw CIMException(CIM_ERR_FAILED);
 221 kumpf 1.1  	}
 222            
 223 kumpf 1.2  	// set proxy security on pInstEnum
 224            	bool bSecurity = _collector->setProxySecurity(pInstEnum);
 225 kumpf 1.1  
 226            	// Get the instances and append them to the array
 227            	hr = pInstEnum->Next(WBEM_INFINITE, 1, &pInstance, &dwReturned);
 228            
 229            	while (SUCCEEDED(hr) && (1 == dwReturned))
 230            	{
 231 mateus.baur 1.10 		//get class from the returned instance 
 232                  		//it will avoid "type mismatch" exceptions
 233                  		//when deepInheritance is true and instances
 234                  		//of subclasses are returned
 235                  		CComVariant vTmpClassName;
 236                  		String strTmpClassName;
 237                  		if (pInstance->Get(L"__CLASS", 0, &vTmpClassName, NULL, NULL) == S_OK)
 238                  		{
 239                  			strTmpClassName = WMIString(vTmpClassName);
 240                  		}
 241                  
 242                  		CIMInstance tempInst(strTmpClassName);
 243 kumpf       1.2  
 244                  		if (_collector->getCIMInstance(pInstance, 
 245                  			                           tempInst,
 246                  									   localOnly, 
 247                  									   includeQualifiers,
 248                  									   includeClassOrigin, 
 249                  									   propertyList, 
 250                  									   TRUE))
 251 kumpf       1.1  		{
 252                  			lCount++;
 253                  
 254 kumpf       1.2  			//build instance path
 255                  			CComVariant v;
 256                  			hr = pInstance->Get(L"__PATH", 
 257                  				                0,
 258                  								&v,
 259                  								NULL,
 260                  								NULL);
 261                  
 262                  			WMIObjectPath tempRef(v.bstrVal);
 263 kumpf       1.1  			tempInst.setPath(tempRef);
 264 kumpf       1.3  			namedInstances.append(CIMInstance(tempInst));
 265 kumpf       1.5  			v.Clear();
 266 kumpf       1.3  		}
 267 kumpf       1.1  
 268 kumpf       1.3  		if (pInstance)
 269                  			pInstance.Release();
 270 kumpf       1.1  
 271                  		hr = pInstEnum->Next(WBEM_INFINITE, 1, &pInstance, &dwReturned);
 272                  	}
 273                  
 274 kumpf       1.3  	if (pInstEnum)
 275                  		pInstEnum.Release();
 276                  
 277 marek       1.13 	PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 278                  		"WMIInstanceProvider::enumerateInstances() - Instance count is %d", lCount)); 
 279 kumpf       1.1  
 280                  	if (lCount == 0)
 281                  	{
 282 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 283                  			"WMIInstanceProvider::enumerateInstances() - hResult value is %x", hr));
 284 kumpf       1.1  	}
 285                  
 286                  	PEG_METHOD_EXIT();
 287                  
 288                  	return namedInstances;
 289                  }
 290                  	
 291                  /////////////////////////////////////////////////////////////////////////////
 292                  // WMIInstanceProvider::enumerateInstanceNames
 293                  //
 294                  // ///////////////////////////////////////////////////////////////////////////
 295                  Array<CIMObjectPath> WMIInstanceProvider::enumerateInstanceNames(
 296                  		const String& nameSpace,
 297 kumpf       1.2          const String& userName,
 298                          const String& password,
 299 kumpf       1.1  		const String& className)
 300                  {
 301                  	HRESULT hr;
 302                  	long lCount = 0;
 303                  	DWORD dwReturned;
 304                  
 305                  	CComPtr<IEnumWbemClassObject>	pInstEnum;
 306                  	CComPtr<IWbemClassObject>		pInstance;
 307                  
 308                  	Array<CIMObjectPath> instanceNames;
 309                  
 310                  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::enumerateInstanceNames()");
 311                  
 312 kumpf       1.2  	setup(nameSpace, userName, password);
 313 kumpf       1.1  
 314                  	if (!m_bInitialized)
 315                  	{
 316 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 317 kumpf       1.5  			"WMIInstanceProvider::enumerateInstanceNames - m_bInitilized= %x, throw CIM_ERR_FAILED exception",  
 318 marek       1.13 			m_bInitialized));
 319 kumpf       1.5  
 320 kumpf       1.1  		throw CIMException(CIM_ERR_FAILED);
 321                  	}
 322                  
 323                  	// retrieve the instance enumeration object
 324 mateus.baur 1.9  	if (!(_collector->getInstanceEnum(&pInstEnum, className, TRUE)))
 325 kumpf       1.1  	{
 326 kumpf       1.3  		if (pInstEnum)
 327                  			pInstEnum.Release();
 328                  
 329 kumpf       1.2  		throw CIMException(CIM_ERR_FAILED);
 330 kumpf       1.1  	}
 331                  
 332 kumpf       1.2  	// set proxy security on pInstEnum
 333                  	bool bSecurity = _collector->setProxySecurity(pInstEnum);
 334 kumpf       1.1  
 335                  	// Get the names of the instances and send to handler
 336                  	hr = pInstEnum->Next(WBEM_INFINITE, 1, &pInstance, &dwReturned);
 337                  
 338                  	while (SUCCEEDED(hr) && (1 == dwReturned))
 339                  	{
 340                  		CIMInstance tempInst(className);
 341 kumpf       1.2  
 342                  		if (_collector->getCIMInstance(pInstance, 
 343                  			                           tempInst, 
 344                  									   FALSE, 
 345                  									   FALSE,
 346                  									   FALSE))
 347 kumpf       1.1  		{
 348                  			lCount++;
 349                  
 350 kumpf       1.2  			//build instance path
 351                  			CComVariant v;
 352                  			hr = pInstance->Get(L"__PATH", 0, &v, NULL, NULL);
 353                  			
 354                  			WMIObjectPath tempRef(v.bstrVal);
 355 kumpf       1.1  			instanceNames.append(tempRef);
 356 kumpf       1.5  			v.Clear();
 357 kumpf       1.1  		}
 358                  
 359 kumpf       1.3  		if (pInstance)
 360                  			pInstance.Release();
 361                  
 362 kumpf       1.1  		hr = pInstEnum->Next(WBEM_INFINITE, 1, &pInstance, &dwReturned);
 363                  	}
 364                  
 365 kumpf       1.3  	if (pInstEnum)
 366                  		pInstEnum.Release();
 367                  
 368 marek       1.13 	PEG_TRACE((TRC_WMIPROVIDER, 
 369 kumpf       1.2  		          Tracer::LEVEL3,
 370                  				  "WMIInstanceProvider::enumerateInstanceNames() - Instance count is %d", 
 371 marek       1.13 				  lCount));
 372 kumpf       1.1  
 373                  	if (lCount == 0)
 374                  	{
 375 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, 
 376 kumpf       1.2  			          Tracer::LEVEL3,
 377                  					  "WMIInstanceProvider::enumerateInstanceNames() - hResult value is %x", 
 378 marek       1.13 					  hr));
 379 kumpf       1.1  	}
 380 kumpf       1.3  
 381 kumpf       1.1  	PEG_METHOD_EXIT();
 382                  
 383                  	return instanceNames;
 384                  }
 385                  
 386                  /////////////////////////////////////////////////////////////////////////////
 387                  // WMIInstanceProvider::getProperty
 388                  //
 389                  // ///////////////////////////////////////////////////////////////////////////
 390                  CIMValue WMIInstanceProvider::getProperty(
 391 kumpf       1.2  		const String& nameSpace,
 392                          const String& userName,
 393                          const String& password,
 394                  		const CIMObjectPath& instanceName,
 395                  		const String& propertyName)
 396 kumpf       1.1  {
 397                  
 398                  	CIMInstance cimInstance;
 399                  	Array<CIMName> propertyNames;
 400                  
 401                  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::getProperty()");
 402                  
 403 kumpf       1.2  	setup(nameSpace,userName,password);
 404 kumpf       1.1  
 405                  	if (!m_bInitialized)
 406                  	{
 407 kumpf       1.3  		throw CIMException(CIM_ERR_FAILED, "[getProperty] m_bInitialized");
 408 kumpf       1.1  	}
 409                  
 410                  	CIMName propName = propertyName;
 411                  
 412                  	propertyNames.append(propName);
 413                  
 414                  	CIMPropertyList propertyList = CIMPropertyList(propertyNames);
 415                  
 416                  	// get the relevant CIMInstance object
 417 kumpf       1.2  	cimInstance = getCIMInstance(nameSpace, 
 418                  		                         userName, 
 419                  								 password, 
 420                  								 instanceName, 
 421                  								 propertyList);
 422 kumpf       1.1  
 423                  	// now fetch the property
 424                  	Uint32 pos = cimInstance.findProperty(propName);
 425                  
 426                  	if (PEG_NOT_FOUND == pos)
 427                  	{
 428 kumpf       1.3  		throw CIMException(CIM_ERR_NO_SUCH_PROPERTY, "[getProperty] findproperty");
 429 kumpf       1.1  	}
 430                  
 431                  	CIMProperty property = cimInstance.getProperty(pos);
 432                  
 433                  	// and return the value
 434                  	CIMValue value = property.getValue();
 435                  
 436                  	PEG_METHOD_EXIT();
 437                  
 438                  	return value;
 439                  }
 440                  
 441                  /////////////////////////////////////////////////////////////////////////////
 442                  // WMIInstanceProvider::setProperty
 443                  //
 444                  // ///////////////////////////////////////////////////////////////////////////
 445                  void WMIInstanceProvider::setProperty(
 446 kumpf       1.2  		const String& nameSpace,
 447                          const String& userName,
 448                          const String& password,
 449                  		const CIMObjectPath& instanceName,
 450                  		const String& propertyName,
 451 kumpf       1.1  		const CIMValue& newValue)
 452                  {
 453                  
 454 kumpf       1.2  	CComPtr<IWbemServices>			pServices;
 455 kumpf       1.1  	CComPtr<IWbemClassObject>		pInstance;
 456                  	CComVariant						vValue;
 457                  	CComBSTR						bsPropName;
 458 kumpf       1.2  	HRESULT							hr;
 459                  	String							sInstanceName;
 460 kumpf       1.1  
 461                  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::setProperty()");
 462                  
 463 kumpf       1.2  	sInstanceName = getObjectName(instanceName);	
 464 kumpf       1.1  
 465 kumpf       1.2  	setup(nameSpace, userName, password);
 466 kumpf       1.1  
 467 marek       1.13 	PEG_TRACE((TRC_WMIPROVIDER, 
 468 kumpf       1.2  		          Tracer::LEVEL3,
 469                  				  "setProperty() - setting property %s in %s", 
 470 kumpf       1.3  				  propertyName, 
 471 marek       1.13 				  sInstanceName));
 472 kumpf       1.1  
 473                  	if (!m_bInitialized)
 474                  	{
 475 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 476 kumpf       1.5  			"WMIInstanceProvider::setProperty - m_bInitilized= %x, throw CIM_ERR_FAILED exception",  
 477 marek       1.13 			m_bInitialized));
 478 kumpf       1.5  
 479 kumpf       1.1  		throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Collector initialization failed.");
 480                  	}
 481                  
 482                  	// retrieve instance object
 483                  	if (!(_collector->getObject(&pInstance, sInstanceName)))
 484                  	{
 485 kumpf       1.3  		if (pInstance)
 486                  			pInstance.Release();
 487                  
 488 kumpf       1.1  		throw CIMException(CIM_ERR_NOT_FOUND);
 489                  	}
 490 kumpf       1.3  	//else if ((!(_collector->isInstance(pInstance))) || (prop == ""))
 491                  	else if ((!(_collector->isInstance(pInstance))) || (propertyName.size() == 0))
 492 kumpf       1.1  	{
 493 kumpf       1.3  		if (pInstance)
 494                  			pInstance.Release();
 495                  
 496 kumpf       1.1  		throw CIMException(CIM_ERR_INVALID_PARAMETER);
 497                  	}
 498                  
 499 kumpf       1.2  	//check if property exists
 500                  	CIMInstance cimInstance;
 501                  	Array<CIMName> propertyNames;
 502                  	CIMName propName = propertyName;
 503                  	
 504                  	propertyNames.append(propName);
 505                  	CIMPropertyList propertyList = CIMPropertyList(propertyNames);	
 506                  	
 507                  	cimInstance = getCIMInstance(nameSpace, 
 508                  		                         userName, 
 509                  								 password, 
 510                  								 instanceName, 
 511                  								 propertyList);
 512                  
 513                  	Uint32 pos = cimInstance.findProperty(propName);
 514                  
 515                  	if (PEG_NOT_FOUND == pos)
 516                  	{
 517                  		throw CIMException(CIM_ERR_NO_SUCH_PROPERTY);
 518                  	}
 519                  
 520 kumpf       1.1  	// check the existing value and type
 521 kumpf       1.3  	//bsPropName = prop.Bstr();
 522                  	bsPropName = propertyName.getCString();
 523 kumpf       1.1  
 524 kumpf       1.2  	//convert property value from CIMValue to VARIANT
 525                  	WMIValue(newValue).getAsVariant(&vValue);
 526                  
 527                  	//update property value
 528 kumpf       1.1  	hr = pInstance->Put(bsPropName, 0, &vValue, 0);
 529 kumpf       1.5  	vValue.Clear();
 530 kumpf       1.1  
 531                  	if (FAILED(hr))
 532                  	{
 533 kumpf       1.5  		if (pInstance)
 534                  			pInstance.Release();
 535                  
 536                          switch(hr)
 537 kumpf       1.2  		{
 538 kumpf       1.3  			case WBEM_E_TYPE_MISMATCH:
 539                  				throw CIMException(CIM_ERR_TYPE_MISMATCH);
 540                  			default:
 541 marek       1.13 				PEG_TRACE((TRC_WMIPROVIDER, 
 542 kumpf       1.3  							  Tracer::LEVEL3,
 543                  							  "setProperty() - Put failed, hr = %x", 
 544 marek       1.13 							  hr));
 545 kumpf       1.1  
 546 kumpf       1.3  				throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "WMI Put property failed.");				
 547 kumpf       1.2  		}
 548                  		
 549 kumpf       1.1  	}
 550 kumpf       1.2  	
 551                  	//update instance
 552                  	_collector->Connect(&pServices);
 553                  
 554                  	hr = pServices->PutInstance(pInstance, 
 555                  		                        WBEM_FLAG_UPDATE_ONLY, 
 556                  								NULL, 
 557                  								NULL);
 558 kumpf       1.3  	
 559                  	if (pInstance)
 560                  		pInstance.Release();
 561                  	
 562                  	if (pServices)
 563                  		pServices.Release();
 564 kumpf       1.2  
 565                  	if (FAILED(hr))
 566                  	{
 567 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, 
 568 kumpf       1.2  			          Tracer::LEVEL3,
 569                  					  "setProperty() - PutInstance failed, hr = %x", 
 570 marek       1.13 					  hr));
 571 kumpf       1.1  
 572 kumpf       1.2  		throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "WMI Put instance failed.");
 573                  	}
 574                  	
 575 kumpf       1.1  	PEG_METHOD_EXIT();
 576                  
 577 kumpf       1.2  	return;
 578 kumpf       1.1  }
 579                  
 580                  /////////////////////////////////////////////////////////////////////////////
 581                  // WMIInstanceProvider::modifyInstance
 582                  //
 583                  // ///////////////////////////////////////////////////////////////////////////
 584                  void WMIInstanceProvider::modifyInstance(
 585 kumpf       1.2  	const String& nameSpace,
 586                      const String& userName,
 587                      const String& password,
 588 kumpf       1.1  	const CIMInstance& modifiedInstance,
 589                  	Boolean includeQualifiers,
 590 kumpf       1.2  	const CIMPropertyList& propertylist)
 591 kumpf       1.1  {
 592 kumpf       1.2  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIClassProvider::modifyInstance()");
 593                  
 594                  	HRESULT hr;
 595                      CComPtr<IWbemClassObject> pClass;
 596                      CComPtr<IWbemClassObject> pInstance;
 597                  
 598                  	setup(nameSpace, userName, password);
 599                  
 600 marek       1.13 	PEG_TRACE((TRC_WMIPROVIDER, 
 601 kumpf       1.2  		          Tracer::LEVEL3,
 602                  				  "ModifyInstance() - nameSpace %s, userName %s",
 603                  				  nameSpace.getCString(),
 604 marek       1.13 				  userName.getCString()));
 605 kumpf       1.2  
 606                  	if (!m_bInitialized)
 607                  	{
 608 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3,
 609 kumpf       1.5  			"WMIInstanceProvider::ModifyInstance - m_bInitilized= %x, throw CIM_ERR_FAILED exception",  
 610 marek       1.13 			m_bInitialized));
 611 kumpf       1.5  
 612 kumpf       1.2  		throw CIMException(CIM_ERR_FAILED);
 613                  	}
 614                  
 615                  	// Check if the instance's class is valid.
 616                  	String className = modifiedInstance.getClassName().getString();
 617                  	
 618 kumpf       1.3  	if (!(_collector->getObject(&pClass, className)))
 619 kumpf       1.2  	{
 620 kumpf       1.3  		if (pClass)
 621                  			pClass.Release();
 622 kumpf       1.2  
 623 kumpf       1.3  		throw CIMException(CIM_ERR_INVALID_CLASS);
 624 kumpf       1.2  	}
 625 kumpf       1.3  	else if (_collector->isInstance(pClass))
 626 kumpf       1.2  	{
 627 kumpf       1.3  		if (pClass)
 628                  			pClass.Release();
 629                  
 630                  		throw CIMException(CIM_ERR_INVALID_PARAMETER);
 631 kumpf       1.2  	}
 632 kumpf       1.3  
 633                  	if (pClass)
 634                  		pClass.Release();
 635 kumpf       1.2  	
 636                  	// Get the instance path
 637                  	CIMObjectPath objPath = modifiedInstance.getPath();
 638                  	
 639                  	// Get the name of the instance
 640                  	String instanceName = getObjectName(objPath);  // TERRY: WAS: objPath.toString();
 641                  	
 642                  	// Check if the instance exists
 643                  	if (!(_collector->getObject(&pInstance, instanceName)))
 644                  	{
 645 kumpf       1.3  		if (pInstance)
 646                  			pInstance.Release();
 647                  
 648 kumpf       1.2  		throw CIMException(CIM_ERR_NOT_FOUND);
 649                  	}
 650                  	else if (!(_collector->isInstance(pInstance)))
 651                  	{
 652 kumpf       1.3  		if (pInstance)
 653                  			pInstance.Release();
 654                  
 655 kumpf       1.2  		throw CIMException(CIM_ERR_INVALID_PARAMETER);
 656                  	}
 657                  
 658                  	// Set the properties that are into propertylist
 659                  	Array<CIMName> listNames;
 660                  	listNames = propertylist.getPropertyNameArray();
 661                  	
 662                  	bool foundInArray;
 663                  	bool bPropertySet = false;
 664                  
 665                  	for(Uint32 i = 0; i < modifiedInstance.getPropertyCount(); i++)
 666                  	{
 667                  		CComVariant v;
 668                  		CIMProperty property = modifiedInstance.getProperty(i).clone();
 669 kumpf       1.3  		String sPropName = property.getName().getString();
 670 kumpf       1.2  
 671                  		// change only the properties defined into the array
 672                  		// if the array is null, change all properties
 673                  		if (propertylist.isNull())
 674                  		{
 675                  			foundInArray = true;
 676                  		}
 677                  		else
 678                  		{
 679                  			foundInArray = false;
 680                  			for (Uint32 j = 0; (j < listNames.size()) && !foundInArray; j++)
 681 kumpf       1.3  				//if (listNames[j].getString() == str)
 682                  				if (String::equalNoCase(listNames[j].getString(), sPropName))
 683 kumpf       1.2  					foundInArray = true;
 684                  		}
 685                  
 686                  		if (foundInArray)
 687                  		{
 688                  			WMIValue propertyValue = property.getValue();
 689                  
 690                  			try
 691                  			{
 692                  				propertyValue.getAsVariant(&v);
 693                  			}
 694 kumpf       1.11 			catch (CIMException&)
 695 kumpf       1.2  			{
 696 kumpf       1.3  				if (pInstance)
 697                  					pInstance.Release();
 698                  
 699 kumpf       1.5  				v.Clear();
 700 kumpf       1.3  
 701 kumpf       1.11 				throw;
 702 kumpf       1.2  			}
 703                  			
 704 kumpf       1.3  			CComBSTR bs = sPropName.getCString();
 705                  			hr = pInstance->Put(bs, 0, &v, 0);
 706 kumpf       1.5  			v.Clear();
 707 kumpf       1.2  		
 708                  			// If we fail to set one property, we must assure 
 709                  			// that the others will be processed
 710                  			if(SUCCEEDED(hr))
 711                  			{
 712                  				// Mark that at least one property was set
 713                  				bPropertySet = true;
 714                  			}
 715                  		}
 716                  	}		
 717                  
 718                  	// Check if at least one property was set
 719                  	// otherwise throw invalid parameter error
 720                  	if(!bPropertySet)
 721                  	{
 722 marek       1.13 		PEG_TRACE((TRC_WMIPROVIDER, 
 723 kumpf       1.2  			          Tracer::LEVEL3,
 724                  					  "modifyInstance() - Put Failed hr=0x%x.", 
 725 marek       1.13 					  hr));
 726 kumpf       1.2  
 727                  		throw CIMException(CIM_ERR_FAILED);
 728                  	}
 729                  
 730                  	// Connect to the server
 731                  	CComPtr<IWbemServices> pServices;
 732                  	bool bConnected = _collector->Connect(&pServices);
 733                  
 734                  	if (!bConnected)
 735                  	{
 736                  		throw CIMException(CIM_ERR_ACCESS_DENIED);
 737                  	}
 738                  
 739                  	// Write the instance to WMI. 
 740                  	hr = pServices->PutInstance(pInstance, 
 741                  		                        WBEM_FLAG_UPDATE_ONLY, 
 742                  								NULL, 
 743                  								NULL);
 744                  
 745 kumpf       1.3  	if (pInstance)
 746                  		pInstance.Release();
 747                  	
 748                  	if (pServices)
 749                  		pServices.Release();
 750                  	
 751 kumpf       1.2  	if(FAILED(hr))
 752                  	{
 753                  		switch(hr)
 754                  		{
 755                  			case E_ACCESSDENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
 756                  			case WBEM_E_ACCESS_DENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
 757                  			case WBEM_E_NOT_FOUND: throw CIMException(CIM_ERR_NOT_FOUND);	break;
 758                  			case WBEM_E_INVALID_CLASS: throw CIMException(CIM_ERR_INVALID_CLASS); break;
 759                  			case WBEM_E_INVALID_OBJECT: throw CIMException(CIM_ERR_INVALID_PARAMETER); break;
 760                  			default: throw CIMException(CIM_ERR_FAILED);
 761                  		}
 762                  	}
 763                  
 764                  	PEG_METHOD_EXIT();
 765                  
 766                  	return;
 767 kumpf       1.1  }
 768                  
 769                  /////////////////////////////////////////////////////////////////////////////
 770                  // WMIInstanceProvider::createInstance
 771                  //
 772                  // ///////////////////////////////////////////////////////////////////////////
 773                  CIMObjectPath WMIInstanceProvider::createInstance(
 774 kumpf       1.2  		const String& nameSpace,
 775                          const String& userName,
 776                          const String& password,
 777                  		const CIMInstance& newInstance)
 778 kumpf       1.1  {
 779 kumpf       1.2  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::createInstance()");
 780                  
 781                  	HRESULT hr;	
 782                  	CComPtr<IWbemClassObject>	pClass;
 783                      CComPtr<IWbemClassObject>	pNewInstance;
 784 kumpf       1.3  	CComBSTR bs;
 785                  	
 786 kumpf       1.2  	setup(nameSpace, userName, password);
 787                  
 788 marek       1.13 	PEG_TRACE((TRC_WMIPROVIDER, 
 789 kumpf       1.2  		          Tracer::LEVEL3,
 790                  				  "createInstance() - nameSpace %s, userName %s",
 791                  				  nameSpace.getCString(),
 792 marek       1.13 				  userName.getCString()));
 793 kumpf       1.2  
 794                  	if (!m_bInitialized)
 795                  	{
 796                  		throw CIMException(CIM_ERR_FAILED);
 797                  	}
 798                  
 799                  	// Get the class definition.
 800                  	String className = newInstance.getClassName().getString();
 801                  	
 802 kumpf       1.3  	if (!(_collector->getObject(&pClass, className)))
 803 kumpf       1.2  	{
 804 kumpf       1.3  		if (pClass)
 805                  			pClass.Release();
 806                  
 807                  		throw CIMException(CIM_ERR_INVALID_CLASS);
 808 kumpf       1.2  	}
 809 kumpf       1.3  	else if (_collector->isInstance(pClass))
 810 kumpf       1.2  	{
 811 kumpf       1.3  		if (pClass)
 812                  			pClass.Release();
 813                  
 814                  		throw CIMException(CIM_ERR_INVALID_PARAMETER);
 815 kumpf       1.2  	}
 816 kumpf       1.3  	
 817 kumpf       1.2      // Create a new instance.
 818                      hr = pClass->SpawnInstance(0, &pNewInstance);
 819 kumpf       1.3  	
 820                  	if (pClass)
 821                  		pClass.Release();
 822 kumpf       1.2  
 823                  	if(FAILED(hr))
 824                  	{
 825                  		throw CIMException(CIM_ERR_FAILED);
 826                  	}
 827                  
 828                  	// Set the properties
 829                  	for(Uint32 i = 0; i < newInstance.getPropertyCount(); i++)
 830                  	{
 831                  		CComVariant v;
 832                  
 833                  		CIMProperty property = newInstance.getProperty(i).clone();
 834                  		CIMValue propertyValue = property.getValue();
 835 kumpf       1.3  		
 836 kumpf       1.2  		try
 837                  		{
 838                  			WMIValue(propertyValue).getAsVariant(&v);
 839                  		}
 840 kumpf       1.11 		catch (CIMException&)
 841 kumpf       1.2  		{
 842 kumpf       1.3  			if (pNewInstance)
 843                  				pNewInstance.Release();
 844                  
 845 kumpf       1.5  			v.Clear();
 846 kumpf       1.3  
 847 kumpf       1.11 			throw;
 848 kumpf       1.2  		}
 849 kumpf       1.3  		
 850                  		bs.Empty();
 851                  		bs = property.getName().getString().getCString();
 852                  		hr = pNewInstance->Put(bs, 0, &v, 0);
 853 kumpf       1.5  		v.Clear();
 854 kumpf       1.2  
 855                  		if(FAILED(hr))
 856                  		{
 857 kumpf       1.3  			if (pNewInstance)
 858                  				pNewInstance.Release();
 859                  
 860 kumpf       1.2  			throw CIMException(CIM_ERR_FAILED);
 861                  		}
 862                  	}		
 863                  	
 864                  	// Connect to the server
 865                  	CComPtr<IWbemServices>	pServices;
 866                  	bool bConnected = _collector->Connect(&pServices);
 867                  
 868                  	if (!bConnected)
 869                  	{
 870                  		throw CIMException(CIM_ERR_ACCESS_DENIED);
 871                  	}
 872                  	
 873                  	// Write the instance to WMI. 
 874                  	CComPtr<IWbemCallResult> pResult;
 875                  
 876                  	hr = pServices->PutInstance(pNewInstance, 
 877 mateus.baur 1.9  		                        WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_CREATE_ONLY | WBEM_FLAG_USE_AMENDED_QUALIFIERS, 
 878 kumpf       1.2  								NULL, 
 879                  								&pResult);
 880                  
 881 kumpf       1.3  	if (pNewInstance)
 882                  		pNewInstance.Release();
 883                  
 884                  	if (pServices)
 885                  		pServices.Release();
 886                  
 887 mateus.baur 1.9  
 888                  	// set proxy security on pResult
 889                  	bool bSecurity = _collector->setProxySecurity(pResult);
 890                  	
 891                  
 892                  	//check for error	
 893                  	pResult->GetCallStatus(WBEM_INFINITE, &hr);
 894 kumpf       1.2  	if(FAILED(hr))
 895                  	{
 896                  		switch(hr)
 897                  		{
 898                  			case E_ACCESSDENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
 899                  			case WBEM_E_ACCESS_DENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
 900                  			case WBEM_E_ALREADY_EXISTS: throw CIMException(CIM_ERR_ALREADY_EXISTS);	break;
 901                  			case WBEM_E_INVALID_CLASS: throw CIMException(CIM_ERR_INVALID_CLASS); break;
 902 kumpf       1.3  			// Terry: Added this check for WBEM_E_NOT_FOUND:
 903                              case WBEM_E_NOT_FOUND: throw CIMException(CIM_ERR_NOT_FOUND); break;
 904                  			default: throw CIMException(CIM_ERR_FAILED);
 905 kumpf       1.2  		}
 906                  	}
 907                  
 908 mateus.baur 1.9  
 909 kumpf       1.2  	// Mount the path to return
 910                  
 911                  	// Prepend namespace to path
 912                  	String sPath = "//";
 913                  	
 914                  	// Append the host name if it is local
 915                  	if(_collector->isLocalNamespace())
 916                  	{
 917                  		sPath.append(getHostName());
 918                  		sPath.append("/");
 919                  	}
 920                  
 921                  	// Append the namespace, colon 
 922                  	sPath.append(nameSpace);
 923                  	sPath.append(":");
 924                  	
 925                  	// Get the key bindings
 926                  	CComBSTR bsKeyBindings;
 927                  	hr = pResult->GetResultString(WBEM_INFINITE, &bsKeyBindings);
 928 kumpf       1.3  
 929                  	if (pResult)
 930                  		pResult.Release();
 931 kumpf       1.2  	
 932                  	if(FAILED(hr))
 933                  	{
 934                  		switch(hr)
 935                  		{
 936                  			case E_ACCESSDENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
 937                  			case WBEM_E_ACCESS_DENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
 938                              ////////////////////////////////////////////////////////////////////////////
 939                              // TERRY ADDED:
 940                              case WBEM_E_INVALID_OPERATION:
 941                                  // GetResultString() may fail on Win2K - get the keys from the CIM instance:
 942                                  if (!GetKeyBindingsFromCIMInstance(newInstance, &bsKeyBindings))
 943                                      throw CIMException(CIM_ERR_NOT_SUPPORTED);
 944                                  break;
 945                              ////////////////////////////////////////////////////////////////////////////
 946                  			default: throw CIMException(CIM_ERR_FAILED);
 947                  		}
 948                  	}
 949                  	
 950                  	// Append the key bindings to the path  
 951 kumpf       1.3  	bs.Empty();
 952                  	bs = sPath.getCString();
 953                  	bs.Append(bsKeyBindings);
 954                  	bsKeyBindings.Empty();
 955 kumpf       1.2  	
 956                  	// Return the CIMObjectPath
 957                  
 958                  	PEG_METHOD_EXIT();
 959                  
 960 kumpf       1.3  	CMyString s; s = bs;
 961                  	return CIMObjectPath(String((LPCTSTR)s));
 962 kumpf       1.2   }
 963 kumpf       1.1  
 964                  /////////////////////////////////////////////////////////////////////////////
 965                  // WMIInstanceProvider::deleteInstance
 966                  //
 967                  // ///////////////////////////////////////////////////////////////////////////
 968                  void WMIInstanceProvider::deleteInstance(
 969 kumpf       1.2  	const String& nameSpace,
 970                      const String& userName,
 971                      const String& password,
 972                  	const CIMObjectPath& instanceName)
 973                  {
 974                  	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::deleteInstance()");
 975                  
 976                  	CComPtr<IWbemServices> pServices;
 977                  
 978                  	//Connect to namespace
 979                  	setup(nameSpace, userName, password);
 980                  
 981 kumpf       1.3  	if (!_collector->Connect(&pServices)) 
 982                  	{
 983                  		if (pServices) 
 984                  			pServices.Release();
 985 kumpf       1.2  
 986 kumpf       1.3  		throw CIMException(CIM_ERR_ACCESS_DENIED);
 987                  	}
 988 kumpf       1.2  	
 989                  	//Convert the parameters to make the WMI call
 990 kumpf       1.3  	CComBSTR bsInstanceName = getObjectName(instanceName).getCString();
 991 kumpf       1.2  	LONG lFlags = 0L;
 992                  
 993                  	//Perform the WMI operation
 994                  	HRESULT hr = pServices->DeleteInstance(bsInstanceName,
 995                  										   lFlags,
 996                  										   NULL,
 997                  										   NULL);
 998 kumpf       1.3  	if (pServices) 
 999                  		pServices.Release();
1000                  
1001 kumpf       1.2  	//Handle the WMI operation result
1002                  	if (FAILED(hr))
1003                  	{
1004                  		switch (hr)
1005                  		{
1006                  			case WBEM_E_ACCESS_DENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break;
1007                  			case WBEM_E_FAILED: throw CIMException(CIM_ERR_FAILED); break;
1008                  			case WBEM_E_INVALID_PARAMETER: throw CIMException(CIM_ERR_FAILED, "WMI Invalid Parameter"); break;
1009                  			case WBEM_E_INVALID_CLASS: throw CIMException(CIM_ERR_INVALID_CLASS); break;
1010                  			case WBEM_E_NOT_FOUND: throw CIMException(CIM_ERR_NOT_FOUND); break;
1011                  			default: throw CIMException(CIM_ERR_FAILED);
1012                  		}
1013                  	}
1014                  
1015                  	PEG_METHOD_EXIT();
1016                  
1017                  	return;
1018                  }
1019                  
1020                  /////////////////////////////////////////////////////////////////////////////
1021                  // WMIInstanceProvider::getHostName
1022 kumpf       1.2  //
1023                  // ///////////////////////////////////////////////////////////////////////////
1024                  String WMIInstanceProvider::getHostName()
1025                  {
1026                  	DWORD nSize = 255;
1027                      char hostName[256];
1028                  
1029                      // Get the computer name
1030                  	if(GetComputerName(hostName, &nSize))
1031                  		return String(hostName);
1032                  	
1033                  	throw CIMException(CIM_ERR_FAILED);
1034                  }
1035                  
1036                  //////////////////////////////////////////////////////////////////////
1037                  // TERRY: ADDED: helper function, maybe should go in a utilities or base class?
1038                  //////////////////////////////////////////////////////////////////////
1039                  bool WMIInstanceProvider::GetKeyBindingsFromCIMInstance(const CIMInstance& newInstance, 
1040                                                                          BSTR* pbsKeyBindings)
1041 kumpf       1.1  {
1042 kumpf       1.2      // might check for any NULL keys, just returning success always for now:
1043                      bool bSuccess = true;
1044                  
1045                      String relPath = getObjectName(newInstance.getPath());
1046                      *pbsKeyBindings = SysAllocString((const OLECHAR*)relPath.getChar16Data());
1047                  
1048                      return bSuccess;
1049 kumpf       1.1  }
1050                  
1051                  PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2