Source code of Windows XP (NT5)
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.

238 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. metafact.cpp
  5. Abstract:
  6. The CMetabaseFactory class.
  7. Author:
  8. Magnus Hedlund (MagnusH) --
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "oleutil.h"
  13. #include "metautil.h"
  14. #include "metafact.h"
  15. CMetabaseFactory::CMetabaseFactory ( ) :
  16. m_wszServerName ( NULL ),
  17. m_pMetabase ( NULL )
  18. {
  19. }
  20. CMetabaseFactory::~CMetabaseFactory ()
  21. {
  22. if ( m_wszServerName ) {
  23. delete m_wszServerName;
  24. m_wszServerName = NULL;
  25. }
  26. DestroyMetabaseObject ( );
  27. }
  28. //$-------------------------------------------------------------------
  29. //
  30. // CMetabaseFactory::DestroyMetabaseObject
  31. //
  32. // Description:
  33. //
  34. // Destroys the current metabase object. This includes calling
  35. // the terminate routine on the metabase.
  36. //
  37. //--------------------------------------------------------------------
  38. void CMetabaseFactory::DestroyMetabaseObject ( )
  39. {
  40. TraceQuietEnter ( "CMetabaseFactory::DestroyMetabaseObject" );
  41. if ( m_pMetabase ) {
  42. _VERIFY ( m_pMetabase->Release () == 0 );
  43. m_pMetabase = NULL;
  44. }
  45. }
  46. //$-------------------------------------------------------------------
  47. //
  48. // CMetabaseFactory::GetMetabaseObject
  49. //
  50. // Description:
  51. //
  52. // Returns an interface to the metabase. If the metabase object
  53. // hasn't been created on the same machine, it is created.
  54. //
  55. // Parameters:
  56. //
  57. // wszServer - remote machine to create object on, or NULL for local machine.
  58. // ppMetabaseResult - returns the interface pointer if successful.
  59. // client has the responsibility to Release this pointer.
  60. //
  61. // Returns:
  62. //
  63. // hresult.
  64. //
  65. //--------------------------------------------------------------------
  66. HRESULT CMetabaseFactory::GetMetabaseObject ( LPCWSTR wszServer, IMSAdminBase ** ppMetabaseResult )
  67. {
  68. TraceFunctEnter ( "CMetabaseFactory::GetMetabaseObject" );
  69. // Validate parameters:
  70. _ASSERT ( ppMetabaseResult != NULL );
  71. _ASSERT ( IS_VALID_OUT_PARAM ( ppMetabaseResult ) );
  72. if ( ppMetabaseResult == NULL ) {
  73. FatalTrace ( (LPARAM) this, "Bad Return Pointer" );
  74. TraceFunctLeave ();
  75. return E_POINTER;
  76. }
  77. // Variables:
  78. HRESULT hr = NOERROR;
  79. CComPtr<IMSAdminBase> pNewMetabase;
  80. // Zero the out parameter:
  81. *ppMetabaseResult = NULL;
  82. // A server name of "" should be NULL:
  83. if ( wszServer && (*wszServer == NULL || !lstrcmpi(wszServer, _T("localhost")) ) ) {
  84. wszServer = NULL;
  85. }
  86. if ( IsCachedMetabase ( wszServer ) ) {
  87. // We've already got this metabase pointer. Just return it.
  88. DebugTrace ( (LPARAM) this, "Returning Cached metabase" );
  89. _ASSERT ( m_pMetabase );
  90. *ppMetabaseResult = m_pMetabase;
  91. m_pMetabase->AddRef ();
  92. hr = NOERROR;
  93. goto Exit;
  94. }
  95. // We have to create the metabase object:
  96. StateTrace ( (LPARAM) this, "Creating new metabase" );
  97. // Destroy the old metabase object:
  98. DestroyMetabaseObject ( );
  99. // Setup the server name field:
  100. if ( !SetServerName ( wszServer ) ) {
  101. hr = E_OUTOFMEMORY;
  102. goto Exit;
  103. }
  104. // m_wszServer = NULL is valid, it means the local machine.
  105. hr = CreateMetabaseObject ( m_wszServerName, &pNewMetabase );
  106. if ( FAILED(hr) ) {
  107. goto Exit;
  108. }
  109. // Save the metabase pointer:
  110. m_pMetabase = pNewMetabase;
  111. pNewMetabase->AddRef ();
  112. // Return the interface pointer:
  113. *ppMetabaseResult = pNewMetabase;
  114. pNewMetabase->AddRef ();
  115. Exit:
  116. TraceFunctLeave ();
  117. return hr;
  118. // pMetabase will be released automatically.
  119. }
  120. //$-------------------------------------------------------------------
  121. //
  122. // CMetabaseFactory::IsCachedMetabase
  123. //
  124. // Description:
  125. //
  126. // Returns TRUE if we have a metabase for the given server
  127. //
  128. // Parameters:
  129. //
  130. // wszServer - remote machine or NULL
  131. //
  132. // Returns:
  133. //
  134. // TRUE if we have a pointer already, FALSE otherwise.
  135. //
  136. //--------------------------------------------------------------------
  137. BOOL CMetabaseFactory::IsCachedMetabase ( LPCWSTR wszServer )
  138. {
  139. if ( !m_pMetabase ) {
  140. // We don't even have a cached metabase object.
  141. return FALSE;
  142. }
  143. if (
  144. // Both are the local machine OR
  145. ( m_wszServerName == NULL && wszServer == NULL ) ||
  146. // Both are the same remote machine
  147. ( m_wszServerName && wszServer && !lstrcmpi ( m_wszServerName, wszServer ) ) ) {
  148. // It's a match
  149. return TRUE;
  150. }
  151. // No dice...
  152. return FALSE;
  153. }
  154. //$-------------------------------------------------------------------
  155. //
  156. // CMetabaseFactory::SetServerName
  157. //
  158. // Description:
  159. //
  160. // Sets the m_wszServerName string.
  161. //
  162. // Parameters:
  163. //
  164. // wszServer - The new servername. Can be NULL.
  165. //
  166. // Returns:
  167. //
  168. // FALSE on failure due to lack of memory.
  169. //
  170. //--------------------------------------------------------------------
  171. BOOL CMetabaseFactory::SetServerName ( LPCWSTR wszServer )
  172. {
  173. TraceQuietEnter ( "CMetabaseFactory::SetServerName" );
  174. delete m_wszServerName;
  175. m_wszServerName = NULL;
  176. if ( wszServer != NULL ) {
  177. _ASSERT ( IS_VALID_STRING ( wszServer ) );
  178. m_wszServerName = new WCHAR [ lstrlen ( wszServer ) + 1 ];
  179. if ( m_wszServerName == NULL ) {
  180. FatalTrace ( (LPARAM) this, "Out of Memory" );
  181. return FALSE;
  182. }
  183. lstrcpy ( m_wszServerName, wszServer );
  184. }
  185. return TRUE;
  186. }