Manager.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_WindowsAzure
  17. * @subpackage Diagnostics
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  24. */
  25. require_once 'Zend/Service/WindowsAzure/Diagnostics/ConfigurationInstance.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_WindowsAzure
  29. * @subpackage Diagnostics
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_WindowsAzure_Diagnostics_Manager
  34. {
  35. /**
  36. * Blob storage client
  37. *
  38. * @var Zend_Service_WindowsAzure_Storage_Blob
  39. */
  40. protected $_blobStorageClient = null;
  41. /**
  42. * Control container name
  43. *
  44. * @var string
  45. */
  46. protected $_controlContainer = '';
  47. /**
  48. * Create a new instance of Zend_Service_WindowsAzure_Diagnostics_Manager
  49. *
  50. * @param Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient Blob storage client
  51. * @param string $controlContainer Control container name
  52. */
  53. public function __construct(Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient = null, $controlContainer = 'wad-control-container')
  54. {
  55. $this->_blobStorageClient = $blobStorageClient;
  56. $this->_controlContainer = $controlContainer;
  57. $this->_ensureStorageInitialized();
  58. }
  59. /**
  60. * Ensure storage has been initialized
  61. */
  62. protected function _ensureStorageInitialized()
  63. {
  64. if (!$this->_blobStorageClient->containerExists($this->_controlContainer)) {
  65. $this->_blobStorageClient->createContainer($this->_controlContainer);
  66. }
  67. }
  68. /**
  69. * Get default configuration values
  70. *
  71. * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  72. */
  73. public function getDefaultConfiguration()
  74. {
  75. return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
  76. }
  77. /**
  78. * Checks if a configuration for a specific role instance exists.
  79. *
  80. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  81. * @return boolean
  82. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  83. */
  84. public function configurationForRoleInstanceExists($roleInstance = null)
  85. {
  86. if (is_null($roleInstance)) {
  87. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  88. throw new Zend_Service_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.');
  89. }
  90. return $this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance);
  91. }
  92. /**
  93. * Checks if a configuration for current role instance exists. Only works on Development Fabric or Windows Azure Fabric.
  94. *
  95. * @return boolean
  96. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  97. */
  98. public function configurationForCurrentRoleInstanceExists()
  99. {
  100. if (!isset($_SERVER['RdRoleId'])) {
  101. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  102. throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  103. }
  104. return $this->_blobStorageClient->blobExists($this->_controlContainer, $this->_getCurrentRoleInstanceId());
  105. }
  106. /**
  107. * Get configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
  108. *
  109. * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  110. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  111. */
  112. public function getConfigurationForCurrentRoleInstance()
  113. {
  114. if (!isset($_SERVER['RdRoleId'])) {
  115. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  116. throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  117. }
  118. return $this->getConfigurationForRoleInstance($this->_getCurrentRoleInstanceId());
  119. }
  120. /**
  121. * Get the current role instance ID. Only works on Development Fabric or Windows Azure Fabric.
  122. *
  123. * @return string
  124. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  125. */
  126. protected function _getCurrentRoleInstanceId()
  127. {
  128. if (!isset($_SERVER['RdRoleId'])) {
  129. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  130. throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  131. }
  132. if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
  133. return $_SERVER['RdRoleId'];
  134. } else {
  135. $roleIdParts = explode('.', $_SERVER['RdRoleId']);
  136. return $roleIdParts[0] . '/' . $roleIdParts[2] . '/' . $_SERVER['RdRoleId'];
  137. }
  138. if (!isset($_SERVER['RoleDeploymentID']) && !isset($_SERVER['RoleInstanceID']) && !isset($_SERVER['RoleName'])) {
  139. throw new Exception('Server variables \'RoleDeploymentID\', \'RoleInstanceID\' and \'RoleName\' are 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. return $_SERVER['RoleDeploymentID'] . '/' . $_SERVER['RoleInstanceID'] . '/' . $_SERVER['RoleName'];
  145. }
  146. }
  147. /**
  148. * Set configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
  149. *
  150. * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
  151. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  152. */
  153. public function setConfigurationForCurrentRoleInstance(Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
  154. {
  155. if (!isset($_SERVER['RdRoleId'])) {
  156. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  157. throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
  158. }
  159. $this->setConfigurationForRoleInstance($this->_getCurrentRoleInstanceId(), $configuration);
  160. }
  161. /**
  162. * Get configuration for a specific role instance
  163. *
  164. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  165. * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  166. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  167. */
  168. public function getConfigurationForRoleInstance($roleInstance = null)
  169. {
  170. if (is_null($roleInstance)) {
  171. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  172. throw new Zend_Service_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.');
  173. }
  174. if ($this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance)) {
  175. $configurationInstance = new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
  176. $configurationInstance->loadXml( $this->_blobStorageClient->getBlobData($this->_controlContainer, $roleInstance) );
  177. return $configurationInstance;
  178. }
  179. return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
  180. }
  181. /**
  182. * Set configuration for a specific role instance
  183. *
  184. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  185. * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
  186. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  187. */
  188. public function setConfigurationForRoleInstance($roleInstance = null, Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
  189. {
  190. if (is_null($roleInstance)) {
  191. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  192. throw new Zend_Service_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.');
  193. }
  194. $this->_blobStorageClient->putBlobData($this->_controlContainer, $roleInstance, $configuration->toXml(), array(), null, array('Content-Type' => 'text/xml'));
  195. }
  196. }