(file) Return to CMPISCMOUtilities.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / ProviderManager2 / CMPI

  1 thilo.boehm 1.2 //%LICENSE////////////////////////////////////////////////////////////////
  2                 //
  3                 // Licensed to The Open Group (TOG) under one or more contributor license
  4                 // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5                 // this work for additional information regarding copyright ownership.
  6                 // Each contributor licenses this file to you under the OpenPegasus Open
  7                 // Source License; you may not use this file except in compliance with the
  8                 // License.
  9                 //
 10                 // Permission is hereby granted, free of charge, to any person obtaining a
 11                 // copy of this software and associated documentation files (the "Software"),
 12                 // to deal in the Software without restriction, including without limitation
 13                 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14                 // and/or sell copies of the Software, and to permit persons to whom the
 15                 // Software is furnished to do so, subject to the following conditions:
 16                 //
 17                 // The above copyright notice and this permission notice shall be included
 18                 // in all copies or substantial portions of the Software.
 19                 //
 20                 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21                 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 thilo.boehm 1.2 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23                 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24                 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25                 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26                 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27                 //
 28                 //////////////////////////////////////////////////////////////////////////
 29                 //
 30 karl        1.2.4.1 // This code implements part of PEP#348 - The CMPI infrastructure using SCMO
 31                     // (Single Chunk Memory Objects).
 32                     // The design document can be found on the OpenPegasus website openpegasus.org
 33                     // at https://collaboration.opengroup.org/pegasus/pp/documents/21210/PEP_348.pdf
 34                     //
 35 thilo.boehm 1.2     //%/////////////////////////////////////////////////////////////////////////////
 36                     
 37                     #include "CMPI_Version.h"
 38                     
 39                     #include "CMPI_String.h"
 40                     #include "CMPI_Value.h"
 41                     #include "CMPISCMOUtilities.h"
 42                     
 43                     #include <Pegasus/Common/Tracer.h>
 44                     #include <Pegasus/Common/CIMDateTimeInline.h>
 45                     #include <Pegasus/Common/StringConversion.h>
 46                     
 47                     PEGASUS_USING_STD;
 48                     PEGASUS_NAMESPACE_BEGIN
 49                     
 50                     CIMDateTimeRep* CMPISCMOUtilities::scmoDateTimeFromCMPI(CMPIDateTime* cmpidt)
 51                     {
 52                         CIMDateTimeRep* cimdt = 0;
 53                     
 54                         if (cmpidt && cmpidt->hdl)
 55                         {
 56 thilo.boehm 1.2             cimdt = ((CIMDateTime*)cmpidt->hdl)->_rep;
 57                         }
 58                         return cimdt;
 59                     }
 60                     
 61                     
 62                     // Function to copy all key bindings from one SCMOInstance to another
 63                     CMPIrc CMPISCMOUtilities::copySCMOKeyProperties( const SCMOInstance* sourcePath,
 64                                                                      SCMOInstance* targetPath )
 65                     {
 66                         PEG_METHOD_ENTER(
 67                             TRC_CMPIPROVIDERINTERFACE,
 68                             "CMPISCMOUtilities::copySCMOKeyProperties()");
 69                     
 70                         if ((0!=sourcePath) && (0!=targetPath))
 71                         {
 72                             SCMO_RC rc;
 73                             const char* keyName = 0;
 74                             const SCMBUnion* keyValue = 0;
 75                             CIMType keyType;
 76                     
 77 thilo.boehm 1.2             Uint32 numKeys = sourcePath->getKeyBindingCount();
 78                             for (Uint32 x=0; x < numKeys; x++)
 79                             {
 80                                 rc = sourcePath->getKeyBindingAt(
 81                                     x, &keyName, keyType, &keyValue);
 82                                 if (rc==SCMO_OK)
 83                                 {
 84                                     rc = targetPath->setKeyBinding(
 85                                         keyName, keyType, keyValue);
 86                                     if (keyType == CIMTYPE_STRING)
 87                                     {
 88                                         free((void*)keyValue);
 89                                     }
 90                                     if (rc != SCMO_OK)
 91                                     {
 92                                         PEG_TRACE_CSTRING(
 93                                             TRC_CMPIPROVIDERINTERFACE,
 94                                             Tracer::LEVEL2,
 95                                             "Failed to set keybinding");
 96                                         PEG_METHOD_EXIT();
 97                                         return CMPI_RC_ERR_FAILED;
 98 thilo.boehm 1.2                     }
 99                                 }
100                                 else
101                                 {
102                                     if (rc!=SCMO_NULL_VALUE)
103                                     {
104                                         PEG_TRACE_CSTRING(
105                                             TRC_CMPIPROVIDERINTERFACE,
106                                             Tracer::LEVEL2,
107                                             "Failed to retrieve keybinding");
108                                         PEG_METHOD_EXIT();
109                                         return CMPI_RC_ERR_FAILED;
110                                     }
111                                 }
112                             }
113                         }
114                         else
115                         {
116                             PEG_TRACE_CSTRING(
117                                 TRC_CMPIPROVIDERINTERFACE,
118                                 Tracer::LEVEL1,
119 thilo.boehm 1.2                 "Called with Nullpointer for source or target");
120                             PEG_METHOD_EXIT();
121                             return CMPI_RC_ERR_FAILED;
122                         }
123                     
124                         return CMPI_RC_OK;
125                     }
126                     
127                     // Function to convert a CIMInstance into an SCMOInstance
128                     // CAUTION: This function requires access to the CMPIClassCache, and
129                     //          therefore can only be called from within a CMPI provider !!!
130                     SCMOInstance* CMPISCMOUtilities::getSCMOFromCIMInstance(
131                         const CIMInstance& cimInst,
132                         const char* ns,
133                         const char* cls)
134                     {
135                         const CIMObjectPath& cimPath = cimInst.getPath();
136                     
137                         const CString nameSpace = cimPath.getNameSpace().getString().getCString();
138                         const CString className = cimPath.getClassName().getString().getCString();
139                     
140 thilo.boehm 1.2         if (!ns)
141                         {
142                             ns = (const char*)nameSpace;
143                         }
144                         if (!cls)
145                         {
146                             cls = (const char*)className;
147                         }
148                     
149                         SCMOInstance* scmoInst=0;
150                     
151                         SCMOClass* scmoClass = mbGetSCMOClass(ns,strlen(ns),cls,strlen(cls));
152                     
153                         if (0 != scmoClass)
154                         {
155                             scmoInst = new SCMOInstance(*scmoClass, cimInst);
156                         }
157                         else
158                         {
159                             SCMOClass localDirtySCMOClass(cls,ns);
160                             scmoInst = new SCMOInstance(localDirtySCMOClass, cimInst);
161 thilo.boehm 1.2             scmoInst->markAsCompromised();
162                         }
163                     
164                         return scmoInst;
165                     }
166                     
167                     // Function to convert a CIMObjectPath into an SCMOInstance
168                     // CAUTION: This function requires access to the CMPIClassCache, and
169                     //          therefore can only be called from within a CMPI provider !!!
170                     SCMOInstance* CMPISCMOUtilities::getSCMOFromCIMObjectPath(
171                         const CIMObjectPath& cimPath,
172                         const char* ns,
173                         const char* cls)
174                     {
175                         CString nameSpace = cimPath.getNameSpace().getString().getCString();
176                         CString className = cimPath.getClassName().getString().getCString();
177                         SCMOInstance* scmoRef;
178                     
179                         if (!ns)
180                         {
181                             ns = (const char*)nameSpace;
182 thilo.boehm 1.2         }
183                         if (!cls)
184                         {
185                             cls = (const char*)className;
186                         }
187                     
188                     
189                         SCMOClass* scmoClass = mbGetSCMOClass(ns,strlen(ns),cls,strlen(cls));
190                     
191                         if (0 != scmoClass)
192                         {
193                             scmoRef = new SCMOInstance(*scmoClass, cimPath);
194                         }
195                         else
196                         {
197                             SCMOClass localDirtySCMOClass(cls,ns);
198                             scmoRef = new SCMOInstance(localDirtySCMOClass, cimPath);
199                             scmoRef->markAsCompromised();
200                         }
201                     
202                     
203 thilo.boehm 1.2         return scmoRef;
204                     }
205                     
206                     // Converts SCMOUnion values to CMPIData for keys.
207                     // Includes special handling for the following types:
208                     //  Real32/64 -> Converted to CMPI_String
209                     //  DateTime  -> Converted to CMPI_String
210                     CMPIrc CMPISCMOUtilities::scmoValue2CMPIKeyData(
211                         const SCMBUnion* scmoValue,
212                         CMPIType type,
213                         CMPIData *data)
214                     {
215                         //Initialize CMPIData object
216                         data->type = type;
217                         data->value.uint64 = 0;
218                         data->state = CMPI_goodValue|CMPI_keyValue;
219                     
220                         //Check for NULL CIMValue
221                         if( scmoValue == 0 )
222                         {
223                             data->state |= CMPI_nullValue;
224 thilo.boehm 1.2             return CMPI_RC_OK;
225                         }
226                     
227                         switch (type)
228                         {
229                             case CMPI_uint8:
230                                 data->value.uint64=(CMPIUint64)scmoValue->simple.val.u8;
231                                 data->type=CMPI_keyInteger;
232                                 break;
233                             case CMPI_uint16:
234                                 data->value.uint64=(CMPIUint64)scmoValue->simple.val.u16;
235                                 data->type=CMPI_keyInteger;
236                                 break;
237                             case CMPI_uint32:
238                                 data->value.uint64=(CMPIUint64)scmoValue->simple.val.u32;
239                                 data->type=CMPI_keyInteger;
240                                 break;
241                             case CMPI_uint64:
242                                 data->value.uint64=scmoValue->simple.val.u64;
243                                 data->type=CMPI_keyInteger;
244                                 break;
245 thilo.boehm 1.2             case CMPI_sint8:
246                                 data->value.sint64=(CMPISint64)scmoValue->simple.val.s8;
247                                 data->type=CMPI_keyInteger;
248                                 break;
249                             case CMPI_sint16:
250                                 data->value.sint64=(CMPISint64)scmoValue->simple.val.s16;
251                                 data->type=CMPI_keyInteger;
252                                 break;
253                             case CMPI_sint32:
254                                 data->value.sint64=(CMPISint64)scmoValue->simple.val.s32;
255                                 data->type=CMPI_keyInteger;
256                                 break;
257                             case CMPI_sint64:
258                                 data->value.sint64=scmoValue->simple.val.s64;
259                                 data->type=CMPI_keyInteger;
260                                 break;
261                             case CMPI_char16:
262                                 data->value.uint64=(CMPIUint64)scmoValue->simple.val.c16;
263                                 data->type=CMPI_keyInteger;
264                                 break;
265                             case CMPI_boolean:
266 thilo.boehm 1.2                 data->value.boolean=scmoValue->simple.val.bin;
267                                 data->type=CMPI_keyBoolean;
268                                 break;
269                     
270                             case CMPI_real32:
271                                 {
272                                     char buffer[128];
273                                     Uint32 size=0;
274                                     Real32ToString(buffer, scmoValue->simple.val.r32, size);
275                                     data->value.string = reinterpret_cast<CMPIString*>(
276                                         new CMPI_Object(buffer));
277                                     data->type=CIMKeyBinding::STRING;
278                                     break;
279                                 }
280                     
281                             case CMPI_real64:
282                                 {
283                                     char buffer[128];
284                                     Uint32 size=0;
285                                     Real64ToString(buffer, scmoValue->simple.val.r64, size);
286                                     data->value.string = reinterpret_cast<CMPIString*>(
287 thilo.boehm 1.2                         new CMPI_Object(buffer));
288                                     data->type=CIMKeyBinding::STRING;
289                                     break;
290                                 }
291                     
292                             case CMPI_charsptr:
293                             case CMPI_chars:
294                             case CMPI_string:
295                                 {
296                                     if (scmoValue->extString.pchar)
297                                     {
298                                         data->value.string = reinterpret_cast<CMPIString*>(
299                                             new CMPI_Object(scmoValue->extString.pchar));
300                                         data->type = CMPI_string;
301                                     }
302                                     else
303                                     {
304                                         data->state|=CMPI_nullValue;
305                                     }
306                                     data->type=CMPI_keyString;
307                                     break;
308 thilo.boehm 1.2                 }
309                     
310                             case CMPI_dateTime:
311                                 {
312                                     char buffer[26];
313                                     _DateTimetoCStr(scmoValue->dateTimeValue, buffer);
314                                     data->value.string = reinterpret_cast<CMPIString*>(
315                                         new CMPI_Object(buffer));
316                                     data->type=CIMKeyBinding::STRING;
317                                     break;
318                                 }
319                     
320                             case CMPI_ref:
321                                 {
322                                     SCMOInstance* ref =
323                                         new SCMOInstance(*(scmoValue->extRefPtr));
324                                     data->value.ref = reinterpret_cast<CMPIObjectPath*>
325                                         (new CMPI_Object(
326                                             ref,
327                                             CMPI_Object::ObjectTypeObjectPath));
328                                 }
329 thilo.boehm 1.2                 break;
330                     
331                             default:
332                                 {
333                                     // Not supported for this CMPItype
334                                     data->state = CMPI_badValue;
335                                     return CMPI_RC_ERR_NOT_SUPPORTED;
336                                 }
337                         }
338                     
339                         if (!(type&CMPI_ENC))
340                         {
341                             // For non-encapsulated type simply verify that we have a valid value
342                             // otherwise set to CMPI_nullValue
343                             if (!scmoValue->simple.hasValue)
344                             {
345                                 data->value.uint64 = 0;
346                                 data->state = CMPI_nullValue;
347                             }
348                         }
349                     
350 thilo.boehm 1.2         return CMPI_RC_OK;
351                     }
352                     
353                     
354                     // Function to convert a SCMO Value into a CMPIData structure
355                     CMPIrc CMPISCMOUtilities::scmoValue2CMPIData(
356                         const SCMBUnion* scmoValue,
357                         CMPIType type,
358                         CMPIData *data,
359                         Uint32 arraySize)
360                     {
361                         //Initialize CMPIData object
362                         data->type = type;
363                         data->value.uint64 = 0;
364                         data->state = CMPI_goodValue;
365                     
366                         //Check for NULL CIMValue
367                         if( scmoValue == 0 )
368                         {
369                             data->state = CMPI_nullValue;
370                             return CMPI_RC_OK;
371 thilo.boehm 1.2         }
372                     
373                         if (type & CMPI_ARRAY)
374                         {
375                             // Get the type of the element of the CMPIArray
376                             CMPIType aType = type&~CMPI_ARRAY;
377                     
378                             //Allocate CMPIData array to hold the values
379                             CMPIData *arrayRoot = new CMPIData[arraySize+1];
380                     
381                             // Advance array pointer to first array element
382                             CMPIData *aData = arrayRoot;
383                             aData++;
384                     
385                             // Set the type, state and value of array elements
386                             for( Uint32 i=0; i<arraySize; i++ )
387                             {
388                                 CMPIrc rc = scmoValue2CMPIData(&(scmoValue[i]),aType,&(aData[i]));
389                                 if (rc != CMPI_RC_OK)
390                                 {
391                                     return rc;
392 thilo.boehm 1.2                 }
393                             }
394                     
395                             // Create array encapsulation object
396                             arrayRoot->type = aType;
397                             arrayRoot->value.sint32 = arraySize;
398                             CMPI_Array *arr = new CMPI_Array(arrayRoot);
399                     
400                             // Set the encapsulated array as data
401                             data->value.array =
402                                 reinterpret_cast<CMPIArray*>(new CMPI_Object(arr));
403                         }
404                         else
405                         {
406                             // Check for encpsulated type, which need special handling
407                             if (type&CMPI_ENC)
408                             {
409                                 switch (type)
410                                 {
411                                 case CMPI_chars:
412                                 case CMPI_string:
413 thilo.boehm 1.2                     {
414                                         if (scmoValue->extString.pchar)
415                                         {
416                                             data->value.string = reinterpret_cast<CMPIString*>(
417                                                 new CMPI_Object(scmoValue->extString.pchar));
418                                             data->type = CMPI_string;
419                                         }
420                                         else
421                                         {
422                                             data->state=CMPI_nullValue;
423                                         }
424                                         break;
425                                     }
426                     
427                                 case CMPI_dateTime:
428                                     {
429                                         CIMDateTime* cimdt =
430                                             new CIMDateTime(&scmoValue->dateTimeValue);
431                                         data->value.dateTime = reinterpret_cast<CMPIDateTime*>
432                                             (new CMPI_Object(cimdt));
433                                         break;
434 thilo.boehm 1.2                     }
435                     
436                                 case CMPI_ref:
437                                     {
438                                         SCMOInstance* ref =
439                                             new SCMOInstance(*(scmoValue->extRefPtr));
440                                         data->value.ref = reinterpret_cast<CMPIObjectPath*>
441                                             (new CMPI_Object(
442                                                 ref,
443                                                 CMPI_Object::ObjectTypeObjectPath));
444                                     }
445                                     break;
446                     
447                                 case CMPI_instance:
448                                     {
449                                         SCMOInstance* inst =
450                                             new SCMOInstance(*(scmoValue->extRefPtr));
451                                         data->value.inst = reinterpret_cast<CMPIInstance*>
452                                             (new CMPI_Object(
453                                                 inst,
454                                                 CMPI_Object::ObjectTypeInstance));
455 thilo.boehm 1.2                     }
456                                     break;
457                                 default:
458                                     {
459                                         // Not supported for this CMPItype
460                                         return CMPI_RC_ERR_NOT_SUPPORTED;
461                                     }
462                                 }
463                             }
464                             else
465                             {
466                                 // For non-encapsulated type simply copy the first 64bit
467                                 // of the SCMBUnion to CMPIValue
468                                 if (scmoValue->simple.hasValue)
469                                 {
470                                     data->value.uint64 = scmoValue->simple.val.u64;
471                                 }
472                                 else
473                                 {
474                                     data->value.uint64 = 0;
475                                     data->state = CMPI_nullValue;
476 thilo.boehm 1.2                 }
477                             }
478                         }
479                         return CMPI_RC_OK;
480                     }
481                     
482                     PEGASUS_NAMESPACE_END
483                     

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2