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.

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