Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.3 KiB

  1. //
  2. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  3. //
  4. #include <precomp.h>
  5. // Initialize the statics
  6. const __int64 CWbemCache :: MAX_CACHE_AGE = 60*60*20; // In seconds, 4 Hours
  7. const DWORD CWbemCache :: MAX_CACHE_SIZE = 500;
  8. DWORD CWbemCache:: dwWBEMCacheCount = 0;
  9. DWORD CEnumInfo:: dwCEnumInfoCount = 0;
  10. DWORD CWbemClass:: dwCWbemClassCount = 0;
  11. //***************************************************************************
  12. //
  13. // CWbemCache::CWbemCache
  14. //
  15. // Purpose : Constructor. Creates an empty cache
  16. //
  17. // Parameters:
  18. //***************************************************************************
  19. CWbemCache :: CWbemCache()
  20. {
  21. dwWBEMCacheCount ++;
  22. }
  23. //***************************************************************************
  24. //
  25. // CWbemCache::GetClass
  26. //
  27. // Purpose : Retreives the CWbemClass object, if present in the cache. Otherwise returns NULL
  28. //
  29. // Parameters:
  30. // lpszClassName : The WBEM name of the Class to be retreived.
  31. // ppWbemClass : The address of the pointer where the CWbemClass object will be placed
  32. //
  33. // Return value:
  34. // The COM value representing the return status. The user should release the WBEM cclass
  35. // when done.
  36. //
  37. //***************************************************************************
  38. HRESULT CWbemCache :: GetClass(LPCWSTR lpszWbemClassName, CWbemClass **ppWbemClass )
  39. {
  40. #ifdef NO_WBEM_CACHE
  41. return E_FAIL;
  42. #else
  43. if(*ppWbemClass = (CWbemClass *)m_objectTree.GetElement(lpszWbemClassName))
  44. {
  45. // Get the current time
  46. FILETIME fileTime;
  47. GetSystemTimeAsFileTime(&fileTime);
  48. LARGE_INTEGER currentTime;
  49. memcpy((LPVOID)&currentTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  50. // The QuadPart is in number of 100s of NanoSeconds.
  51. // Delete the object if is too old, and return failure
  52. // timeElapsed is the amount of time in seconds
  53. __int64 timeElapsed = ( currentTime.QuadPart - (*ppWbemClass)->GetCreationTime());
  54. timeElapsed = timeElapsed/(__int64)10000000;
  55. if( timeElapsed > MAX_CACHE_AGE ) // in Seconds
  56. {
  57. (*ppWbemClass)->Release();
  58. *ppWbemClass = NULL;
  59. m_objectTree.DeleteElement(lpszWbemClassName);
  60. g_pLogObject->WriteW( L"CWbemCache :: GetClass() Deleted senile class : %s\r\n", lpszWbemClassName);
  61. return E_FAIL;
  62. }
  63. // Set its last accessed time
  64. (*ppWbemClass)->SetLastAccessTime(currentTime.QuadPart);
  65. return S_OK;
  66. }
  67. return E_FAIL;
  68. #endif
  69. }
  70. //***************************************************************************
  71. //
  72. // CWbemCache::AddClass
  73. //
  74. // Purpose : Adds the CWbemClass object to the cache
  75. //
  76. // Parameters:
  77. // ppWbemClass : The CWbemClass pointer of the object to be added
  78. //
  79. // Return value:
  80. // The COM value representing the return status.
  81. // when done.
  82. //
  83. //***************************************************************************
  84. HRESULT CWbemCache :: AddClass(CWbemClass *pWbemClass )
  85. {
  86. #ifdef NO_WBEM_CACHE
  87. return E_FAIL;
  88. #else
  89. // Delete an element from the tree if its size has reached a limit of 100 nodes
  90. if(m_objectTree.GetNumberOfElements() >= MAX_CACHE_SIZE)
  91. {
  92. if(!m_objectTree.DeleteLeastRecentlyAccessedElement())
  93. return E_FAIL;
  94. g_pLogObject->WriteW( L"CWbemCache :: AddClass() Deleted LRU class from cache\r\n");
  95. }
  96. // Add the new element
  97. if(m_objectTree.AddElement(pWbemClass->GetName(), pWbemClass))
  98. {
  99. g_pLogObject->WriteW( L"CWbemCache :: AddClass() Added a class %s to cache\r\n", pWbemClass->GetName());
  100. return S_OK;
  101. }
  102. return E_FAIL;
  103. #endif
  104. }
  105. //***************************************************************************
  106. //
  107. // CWbemCache::GetEnumInfo
  108. //
  109. // Purpose : Retreives the CEnumInfo object, if present in the cache. Otherwise returns NULL
  110. //
  111. // Parameters:
  112. // lpszClassName : The WBEM name of the Class to be retreived.
  113. // ppEnumInfo : The address of the pointer where the CEnumInfo object will be placed
  114. //
  115. // Return value:
  116. // The COM value representing the return status. The user should release the EnumInfo object
  117. // when done.
  118. //
  119. //***************************************************************************
  120. HRESULT CWbemCache :: GetEnumInfo(LPCWSTR lpszWbemClassName, CEnumInfo **ppEnumInfo )
  121. {
  122. #ifdef NO_WBEM_CACHE
  123. return E_FAIL;
  124. #else
  125. if(*ppEnumInfo = (CEnumInfo *)m_EnumTree.GetElement(lpszWbemClassName))
  126. {
  127. // Get the current time
  128. FILETIME fileTime;
  129. GetSystemTimeAsFileTime(&fileTime);
  130. LARGE_INTEGER currentTime;
  131. memcpy((LPVOID)&currentTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  132. // The QuadPart is in number of 100s of NanoSeconds.
  133. // Delete the object if is too old, and return failure
  134. // timeElapsed is the amount of time in seconds
  135. __int64 timeElapsed = ( currentTime.QuadPart - (*ppEnumInfo)->GetCreationTime());
  136. timeElapsed = timeElapsed/(__int64)10000000;
  137. if( timeElapsed > MAX_CACHE_AGE ) // in Seconds
  138. {
  139. (*ppEnumInfo)->Release();
  140. *ppEnumInfo = NULL;
  141. m_EnumTree.DeleteElement(lpszWbemClassName);
  142. g_pLogObject->WriteW( L"CEnumCache :: GetClass() Deleted senile EnumInfo : %s\r\n", lpszWbemClassName);
  143. return E_FAIL;
  144. }
  145. // Set its last accessed time
  146. (*ppEnumInfo)->SetLastAccessTime(currentTime.QuadPart);
  147. return S_OK;
  148. }
  149. return E_FAIL;
  150. #endif
  151. }
  152. //***************************************************************************
  153. //
  154. // CWbemCache::AddEnumInfo
  155. //
  156. // Purpose : Adds the CEnumInfo object to the cache
  157. //
  158. // Parameters:
  159. // ppEnumInfo : The CEnumInfo pointer of the object to be added
  160. //
  161. // Return value:
  162. // The COM value representing the return status.
  163. // when done.
  164. //
  165. //***************************************************************************
  166. HRESULT CWbemCache :: AddEnumInfo(CEnumInfo *pEnumInfo )
  167. {
  168. #ifdef NO_WBEM_CACHE
  169. return E_FAIL;
  170. #else
  171. // Delete an element from the tree if its size has reached a limit of 100 nodes
  172. if(m_EnumTree.GetNumberOfElements() >= MAX_CACHE_SIZE)
  173. {
  174. if(!m_EnumTree.DeleteLeastRecentlyAccessedElement())
  175. return E_FAIL;
  176. g_pLogObject->WriteW( L"CEnumCache :: AddClass() Deleted LRU class from cache\r\n");
  177. }
  178. // Add the new element
  179. if(m_EnumTree.AddElement(pEnumInfo->GetName(), pEnumInfo))
  180. {
  181. g_pLogObject->WriteW( L"CEnumCache :: AddClass() Added a EnumInfo %s to cache\r\n", pEnumInfo->GetName());
  182. return S_OK;
  183. }
  184. return E_FAIL;
  185. #endif
  186. }