PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Diagnostics/Manager.php

https://bitbucket.org/ktos/tinyshare
PHP | 225 lines | 90 code | 23 blank | 112 comment | 16 complexity | 0bbbb3a516a4bb4b72fac43107eea1e3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2011, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage Diagnostics
  31. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  32. * @license http://phpazure.codeplex.com/license
  33. * @version $Id: Storage.php 45989 2010-05-03 12:19:10Z unknown $
  34. */
  35. /**
  36. * @see Microsoft_AutoLoader
  37. */
  38. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  39. /**
  40. * @category Microsoft
  41. * @package Microsoft_WindowsAzure
  42. * @subpackage Diagnostics
  43. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  44. * @license http://phpazure.codeplex.com/license
  45. */
  46. class Microsoft_WindowsAzure_Diagnostics_Manager
  47. {
  48. /**
  49. * Blob storage client
  50. *
  51. * @var Microsoft_WindowsAzure_Storage_Blob
  52. */
  53. protected $_blobStorageClient = null;
  54. /**
  55. * Control container name
  56. *
  57. * @var string
  58. */
  59. protected $_controlContainer = '';
  60. /**
  61. * Create a new instance of Microsoft_WindowsAzure_Diagnostics_Manager
  62. *
  63. * @param Microsoft_WindowsAzure_Storage_Blob $blobStorageClient Blob storage client
  64. * @param string $controlContainer Control container name
  65. */
  66. public function __construct(Microsoft_WindowsAzure_Storage_Blob $blobStorageClient = null, $controlContainer = 'wad-control-container')
  67. {
  68. $this->_blobStorageClient = $blobStorageClient;
  69. $this->_controlContainer = $controlContainer;
  70. $this->_ensureStorageInitialized();
  71. }
  72. /**
  73. * Ensure storage has been initialized
  74. */
  75. protected function _ensureStorageInitialized()
  76. {
  77. if (!$this->_blobStorageClient->containerExists($this->_controlContainer)) {
  78. $this->_blobStorageClient->createContainer($this->_controlContainer);
  79. }
  80. }
  81. /**
  82. * Get default configuration values
  83. *
  84. * @return Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance
  85. */
  86. public function getDefaultConfiguration()
  87. {
  88. return new Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance();
  89. }
  90. /**
  91. * Checks if a configuration for a specific role instance exists.
  92. *
  93. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  94. * @return boolean
  95. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  96. */
  97. public function configurationForRoleInstanceExists($roleInstance = null)
  98. {
  99. if (is_null($roleInstance)) {
  100. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Role instance should be specified. Try reading $_SERVER[\'RdRoleId\'] for this information if the application is hosted on Windows Azure Fabric or Development Fabric.');
  101. }
  102. return $this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance);
  103. }
  104. /**
  105. * Checks if a configuration for current role instance exists. Only works on Development Fabric or Windows Azure Fabric.
  106. *
  107. * @return boolean
  108. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  109. */
  110. public function configurationForCurrentRoleInstanceExists()
  111. {
  112. if (!isset($_SERVER['RdRoleId'])) {
  113. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  114. }
  115. return $this->_blobStorageClient->blobExists($this->_controlContainer, $this->_getCurrentRoleInstanceId());
  116. }
  117. /**
  118. * Get configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
  119. *
  120. * @return Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance
  121. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  122. */
  123. public function getConfigurationForCurrentRoleInstance()
  124. {
  125. if (!isset($_SERVER['RdRoleId'])) {
  126. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  127. }
  128. return $this->getConfigurationForRoleInstance($this->_getCurrentRoleInstanceId());
  129. }
  130. /**
  131. * Get the current role instance ID. Only works on Development Fabric or Windows Azure Fabric.
  132. *
  133. * @return string
  134. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  135. */
  136. protected function _getCurrentRoleInstanceId()
  137. {
  138. if (!isset($_SERVER['RdRoleId'])) {
  139. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  140. }
  141. if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
  142. return $_SERVER['RdRoleId'];
  143. } else {
  144. $roleIdParts = explode('.', $_SERVER['RdRoleId']);
  145. return $roleIdParts[0] . '/' . $roleIdParts[2] . '/' . $_SERVER['RdRoleId'];
  146. }
  147. if (!isset($_SERVER['RoleDeploymentID']) && !isset($_SERVER['RoleInstanceID']) && !isset($_SERVER['RoleName'])) {
  148. throw new Exception('Server variables \'RoleDeploymentID\', \'RoleInstanceID\' and \'RoleName\' are unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  149. }
  150. if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
  151. return $_SERVER['RdRoleId'];
  152. } else {
  153. return $_SERVER['RoleDeploymentID'] . '/' . $_SERVER['RoleInstanceID'] . '/' . $_SERVER['RoleName'];
  154. }
  155. }
  156. /**
  157. * Set configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
  158. *
  159. * @param Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
  160. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  161. */
  162. public function setConfigurationForCurrentRoleInstance(Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
  163. {
  164. if (!isset($_SERVER['RdRoleId'])) {
  165. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  166. }
  167. $this->setConfigurationForRoleInstance($this->_getCurrentRoleInstanceId(), $configuration);
  168. }
  169. /**
  170. * Get configuration for a specific role instance
  171. *
  172. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  173. * @return Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance
  174. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  175. */
  176. public function getConfigurationForRoleInstance($roleInstance = null)
  177. {
  178. if (is_null($roleInstance)) {
  179. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Role instance should be specified. Try reading $_SERVER[\'RdRoleId\'] for this information if the application is hosted on Windows Azure Fabric or Development Fabric.');
  180. }
  181. if ($this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance)) {
  182. $configurationInstance = new Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance();
  183. $configurationInstance->loadXml( $this->_blobStorageClient->getBlobData($this->_controlContainer, $roleInstance) );
  184. return $configurationInstance;
  185. }
  186. return new Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance();
  187. }
  188. /**
  189. * Set configuration for a specific role instance
  190. *
  191. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  192. * @param Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
  193. * @throws Microsoft_WindowsAzure_Diagnostics_Exception
  194. */
  195. public function setConfigurationForRoleInstance($roleInstance = null, Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
  196. {
  197. if (is_null($roleInstance)) {
  198. throw new Microsoft_WindowsAzure_Diagnostics_Exception('Role instance should be specified. Try reading $_SERVER[\'RdRoleId\'] for this information if the application is hosted on Windows Azure Fabric or Development Fabric.');
  199. }
  200. $this->_blobStorageClient->putBlobData($this->_controlContainer, $roleInstance, $configuration->toXml(), array(), null, array('Content-Type' => 'text/xml'));
  201. }
  202. }