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.

436 lines
8.4 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. appladmin.cpp
  5. Abstract:
  6. This file contains implementation of:
  7. CAppPoolMethod, CWebAppMethod
  8. Author:
  9. ???
  10. Revision History:
  11. Mohit Srivastava 21-Jan-01
  12. --*/
  13. #include "iisprov.h"
  14. #include "appladmin.h"
  15. #include "MultiSzHelper.h"
  16. #include "iiswmimsg.h"
  17. //
  18. // CApplAdmin
  19. //
  20. CAppPoolMethod::CAppPoolMethod()
  21. {
  22. HRESULT hr = CoCreateInstance(
  23. CLSID_WamAdmin,
  24. NULL,
  25. CLSCTX_ALL,
  26. IID_IIISApplicationAdmin,
  27. (void**)&m_spAppAdmin);
  28. THROW_ON_ERROR(hr);
  29. }
  30. CAppPoolMethod::~CAppPoolMethod()
  31. {
  32. }
  33. void CAppPoolMethod::GetCurrentMode(
  34. VARIANT* io_pvtServerMode)
  35. /*++
  36. Synopsis:
  37. This method, unlike the others, is actually on the IIsWebService node.
  38. Arguments: [io_pvtServerMode] -
  39. --*/
  40. {
  41. DBG_ASSERT(io_pvtServerMode != NULL);
  42. DWORD dwServerMode = 0;
  43. VariantInit(io_pvtServerMode);
  44. HRESULT hr = m_spAppAdmin->GetProcessMode(&dwServerMode);
  45. THROW_ON_ERROR(hr);
  46. io_pvtServerMode->vt = VT_I4;
  47. io_pvtServerMode->lVal = dwServerMode;
  48. }
  49. void CAppPoolMethod::Start(
  50. LPCWSTR i_wszMbPath)
  51. {
  52. DBG_ASSERT(i_wszMbPath != NULL);
  53. SetState(i_wszMbPath, MD_APPPOOL_COMMAND_START);
  54. }
  55. void CAppPoolMethod::Stop(
  56. LPCWSTR i_wszMbPath)
  57. {
  58. DBG_ASSERT(i_wszMbPath != NULL);
  59. SetState(i_wszMbPath, MD_APPPOOL_COMMAND_STOP);
  60. }
  61. void CAppPoolMethod::RecycleAppPool(
  62. LPCWSTR i_wszMbPath)
  63. {
  64. DBG_ASSERT(i_wszMbPath != NULL);
  65. LPCWSTR wszAppPool = NULL;
  66. GetPtrToAppPool(i_wszMbPath, &wszAppPool);
  67. HRESULT hr = m_spAppAdmin->RecycleApplicationPool(wszAppPool);
  68. THROW_ON_ERROR(hr);
  69. }
  70. void CAppPoolMethod::EnumAppsInPool(
  71. LPCWSTR i_wszMbPath,
  72. VARIANT* io_pvtApps)
  73. /*++
  74. Synopsis:
  75. Arguments: [i_wszMbPath] -
  76. [io_pvtApps] - Will be an array of strings
  77. --*/
  78. {
  79. DBG_ASSERT(i_wszMbPath != NULL);
  80. DBG_ASSERT(io_pvtApps != NULL);
  81. CComBSTR sbstrApps = NULL;
  82. VariantInit(io_pvtApps);
  83. LPCWSTR wszAppPool = NULL;
  84. GetPtrToAppPool(i_wszMbPath, &wszAppPool);
  85. HRESULT hr = m_spAppAdmin->EnumerateApplicationsInPool(
  86. wszAppPool,
  87. &sbstrApps);
  88. THROW_ON_ERROR(hr);
  89. CMultiSz MultiSz;
  90. hr = MultiSz.ToWmiForm(
  91. sbstrApps,
  92. io_pvtApps);
  93. THROW_ON_ERROR(hr);
  94. }
  95. void CAppPoolMethod::DeleteAppPool(
  96. LPCWSTR i_wszMbPath)
  97. {
  98. DBG_ASSERT(i_wszMbPath);
  99. LPCWSTR wszAppPool = NULL;
  100. GetPtrToAppPool(i_wszMbPath, &wszAppPool);
  101. HRESULT hr = m_spAppAdmin->DeleteApplicationPool(wszAppPool);
  102. THROW_ON_ERROR(hr);
  103. }
  104. //
  105. // CAppPoolMethod - private methods
  106. //
  107. void CAppPoolMethod::GetPtrToAppPool(
  108. LPCWSTR i_wszMbPath,
  109. LPCWSTR* o_pwszAppPool)
  110. /*++
  111. Synopsis:
  112. Arguments: [i_wszMbPath] -
  113. [o_wszAppPool] - This is a ptr to i_wszMbPath. Does not need to be
  114. freed by caller.
  115. --*/
  116. {
  117. DBG_ASSERT(i_wszMbPath);
  118. DBG_ASSERT(o_pwszAppPool);
  119. DBG_ASSERT(*o_pwszAppPool == NULL);
  120. DBG_ASSERT(i_wszMbPath[0] == L'/');
  121. DBG_ASSERT(i_wszMbPath[1] != L'\0');
  122. LPWSTR wszAppPool = (LPWSTR)wcsrchr(i_wszMbPath, L'/');
  123. *wszAppPool = L'\0';
  124. if(_wcsicmp(i_wszMbPath, L"/LM/w3svc/AppPools") != 0)
  125. {
  126. *wszAppPool = L'/';
  127. CIIsProvException e;
  128. e.SetMC(WBEM_E_FAILED, IISWMI_INVALID_APPPOOL_CONTAINER, i_wszMbPath);
  129. throw e;
  130. }
  131. *wszAppPool = L'/';
  132. //
  133. // Set out params on success
  134. //
  135. *o_pwszAppPool = wszAppPool + 1;
  136. }
  137. void CAppPoolMethod::SetState(
  138. LPCWSTR i_wszMbPath,
  139. DWORD dwState)
  140. {
  141. METADATA_HANDLE hObjHandle = NULL;
  142. DWORD dwBufferSize = sizeof(DWORD);
  143. METADATA_RECORD mdrMDData;
  144. LPBYTE pBuffer = (LPBYTE)&dwState;
  145. CMetabase metabase;
  146. CComPtr<IMSAdminBase2> spIABase = (IMSAdminBase2*)metabase;
  147. HRESULT hr = spIABase->OpenKey(
  148. METADATA_MASTER_ROOT_HANDLE,
  149. i_wszMbPath,
  150. METADATA_PERMISSION_WRITE,
  151. DEFAULT_TIMEOUT_VALUE, // 30 seconds
  152. &hObjHandle
  153. );
  154. THROW_ON_ERROR(hr);
  155. MD_SET_DATA_RECORD(&mdrMDData,
  156. MD_APPPOOL_COMMAND,
  157. METADATA_VOLATILE,
  158. IIS_MD_UT_SERVER,
  159. DWORD_METADATA,
  160. dwBufferSize,
  161. pBuffer);
  162. hr = spIABase->SetData(
  163. hObjHandle,
  164. L"",
  165. &mdrMDData
  166. );
  167. spIABase->CloseKey(hObjHandle);
  168. THROW_ON_ERROR(hr);
  169. }
  170. //
  171. // CWebAppMethod
  172. //
  173. CWebAppMethod::CWebAppMethod()
  174. {
  175. HRESULT hr = CoCreateInstance(
  176. CLSID_WamAdmin,
  177. NULL,
  178. CLSCTX_ALL,
  179. IID_IIISApplicationAdmin,
  180. (void**)&m_pAppAdmin
  181. );
  182. hr = CoCreateInstance(
  183. CLSID_WamAdmin,
  184. NULL,
  185. CLSCTX_ALL,
  186. IID_IWamAdmin2,
  187. (void**)&m_pWamAdmin2
  188. );
  189. THROW_ON_ERROR(hr);
  190. }
  191. CWebAppMethod::~CWebAppMethod()
  192. {
  193. if(m_pAppAdmin)
  194. m_pAppAdmin->Release();
  195. if(m_pWamAdmin2)
  196. m_pWamAdmin2->Release();
  197. }
  198. HRESULT CWebAppMethod::AppCreate(
  199. LPCWSTR szMetaBasePath,
  200. bool bInProcFlag,
  201. LPCWSTR szAppPoolName,
  202. bool bCreatePool
  203. )
  204. {
  205. HRESULT hr;
  206. LPWSTR szActualName;
  207. BOOL bActualCreation = FALSE;
  208. if (szAppPoolName) {
  209. szActualName = (LPWSTR)szAppPoolName;
  210. }
  211. else {
  212. szActualName = NULL;
  213. }
  214. if (bCreatePool != true) {
  215. bActualCreation = FALSE;
  216. }
  217. else {
  218. bActualCreation = TRUE;
  219. }
  220. hr = m_pAppAdmin->CreateApplication(
  221. szMetaBasePath,
  222. bInProcFlag ? 0 : 2, // 0 for InProc, 2 for Pooled Proc
  223. szActualName,
  224. bActualCreation // Don't create - DefaultAppPool should already be there
  225. );
  226. return hr;
  227. }
  228. HRESULT CWebAppMethod::AppCreate2(
  229. LPCWSTR szMetaBasePath,
  230. long lAppMode,
  231. LPCWSTR szAppPoolName,
  232. bool bCreatePool
  233. )
  234. {
  235. HRESULT hr;
  236. LPWSTR szActualName;
  237. BOOL bActualCreation = FALSE;
  238. if (szAppPoolName) {
  239. szActualName = (LPWSTR)szAppPoolName;
  240. }
  241. else {
  242. szActualName = NULL;
  243. }
  244. if (bCreatePool != true) {
  245. bActualCreation = FALSE;
  246. }
  247. else {
  248. bActualCreation = TRUE;
  249. }
  250. hr = m_pAppAdmin->CreateApplication(
  251. szMetaBasePath,
  252. lAppMode,
  253. szActualName,
  254. bActualCreation // Don't create - DefaultAppPool should already be there
  255. );
  256. return hr;
  257. }
  258. HRESULT CWebAppMethod::AppDelete(
  259. LPCWSTR szMetaBasePath,
  260. bool bRecursive
  261. )
  262. {
  263. HRESULT hr;
  264. hr = m_pAppAdmin->DeleteApplication(
  265. szMetaBasePath,
  266. bRecursive ? TRUE : FALSE // Don't mix bool w/ BOOL
  267. );
  268. return hr;
  269. }
  270. HRESULT CWebAppMethod::AppUnLoad(
  271. LPCWSTR szMetaBasePath,
  272. bool bRecursive
  273. )
  274. {
  275. HRESULT hr;
  276. hr = m_pWamAdmin2->AppUnLoad(
  277. szMetaBasePath,
  278. bRecursive ? TRUE : FALSE // Don't mix bool w/ BOOL
  279. );
  280. return hr;
  281. }
  282. HRESULT CWebAppMethod::AppDisable(
  283. LPCWSTR szMetaBasePath,
  284. bool bRecursive
  285. )
  286. {
  287. HRESULT hr;
  288. hr = m_pWamAdmin2->AppDeleteRecoverable(
  289. szMetaBasePath,
  290. bRecursive ? TRUE : FALSE // Don't mix bool w/ BOOL
  291. );
  292. return hr;
  293. }
  294. HRESULT CWebAppMethod::AppEnable(
  295. LPCWSTR szMetaBasePath,
  296. bool bRecursive
  297. )
  298. {
  299. HRESULT hr;
  300. hr = m_pWamAdmin2->AppRecover(
  301. szMetaBasePath,
  302. bRecursive ? TRUE : FALSE // Don't mix bool w/ BOOL
  303. );
  304. return hr;
  305. }
  306. HRESULT CWebAppMethod::AppGetStatus(
  307. LPCWSTR szMetaBasePath,
  308. DWORD* pdwStatus
  309. )
  310. {
  311. HRESULT hr;
  312. hr = m_pWamAdmin2->AppGetStatus(
  313. szMetaBasePath,
  314. pdwStatus);
  315. return hr;
  316. }
  317. HRESULT CWebAppMethod::AspAppRestart(
  318. LPCWSTR a_szMetaBasePath
  319. )
  320. {
  321. HRESULT hr = S_OK;
  322. DWORD dwState = 0;
  323. METADATA_HANDLE t_hKey = NULL;
  324. CMetabase t_mb;
  325. // open key
  326. t_hKey = t_mb.OpenKey(a_szMetaBasePath, true);
  327. // check app
  328. hr = t_mb.WebAppCheck(t_hKey);
  329. THROW_ON_ERROR(hr);
  330. // get state
  331. hr = t_mb.WebAppGetStatus(t_hKey, &dwState);
  332. THROW_ON_ERROR(hr);
  333. // change state value
  334. dwState = dwState ? 0 : 1;
  335. hr = t_mb.WebAppSetStatus(t_hKey, dwState);
  336. THROW_ON_ERROR(hr);
  337. // re-set back state value
  338. dwState = dwState ? 0 : 1;
  339. hr = t_mb.WebAppSetStatus(t_hKey, dwState);
  340. THROW_ON_ERROR(hr);
  341. t_mb.CloseKey(t_hKey);
  342. return hr;
  343. }