Manager.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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-2010 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_Storage_Blob
  24. */
  25. require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_Diagnostics_Exception
  28. */
  29. require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
  30. /**
  31. * @see Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  32. */
  33. require_once 'Zend/Service/WindowsAzure/Diagnostics/ConfigurationInstance.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Service_WindowsAzure
  37. * @subpackage Diagnostics
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Service_WindowsAzure_Diagnostics_Manager
  42. {
  43. /**
  44. * Blob storage client
  45. *
  46. * @var Zend_Service_WindowsAzure_Storage_Blob
  47. */
  48. protected $_blobStorageClient = null;
  49. /**
  50. * Control container name
  51. *
  52. * @var string
  53. */
  54. protected $_controlContainer = '';
  55. /**
  56. * Create a new instance of Zend_Service_WindowsAzure_Diagnostics_Manager
  57. *
  58. * @param Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient Blob storage client
  59. * @param string $controlContainer Control container name
  60. */
  61. public function __construct(Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient = null, $controlContainer = 'wad-control-container')
  62. {
  63. $this->_blobStorageClient = $blobStorageClient;
  64. $this->_controlContainer = $controlContainer;
  65. $this->_ensureStorageInitialized();
  66. }
  67. /**
  68. * Ensure storage has been initialized
  69. */
  70. protected function _ensureStorageInitialized()
  71. {
  72. if (!$this->_blobStorageClient->containerExists($this->_controlContainer)) {
  73. $this->_blobStorageClient->createContainer($this->_controlContainer);
  74. }
  75. }
  76. /**
  77. * Get default configuration values
  78. *
  79. * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  80. */
  81. public function getDefaultConfiguration()
  82. {
  83. return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
  84. }
  85. /**
  86. * Checks if a configuration for a specific role instance exists.
  87. *
  88. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  89. * @return boolean
  90. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  91. */
  92. public function configurationForRoleInstanceExists($roleInstance = null)
  93. {
  94. if (is_null($roleInstance)) {
  95. 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.');
  96. }
  97. return $this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance);
  98. }
  99. /**
  100. * Checks if a configuration for current role instance exists. Only works on Development Fabric or Windows Azure Fabric.
  101. *
  102. * @return boolean
  103. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  104. */
  105. public function configurationForCurrentRoleInstanceExists()
  106. {
  107. if (!isset($_SERVER['RdRoleId'])) {
  108. 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.');
  109. }
  110. return $this->_blobStorageClient->blobExists($this->_controlContainer, $_SERVER['RdRoleId']);
  111. }
  112. /**
  113. * Get configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
  114. *
  115. * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  116. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  117. */
  118. public function getConfigurationForCurrentRoleInstance()
  119. {
  120. if (!isset($_SERVER['RdRoleId'])) {
  121. 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.');
  122. }
  123. return $this->getConfigurationForRoleInstance($_SERVER['RdRoleId']);
  124. }
  125. /**
  126. * Set configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
  127. *
  128. * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
  129. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  130. */
  131. public function setConfigurationForCurrentRoleInstance(Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
  132. {
  133. if (!isset($_SERVER['RdRoleId'])) {
  134. 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.');
  135. }
  136. $this->setConfigurationForRoleInstance($_SERVER['RdRoleId'], $configuration);
  137. }
  138. /**
  139. * Get configuration for a specific role instance
  140. *
  141. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  142. * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
  143. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  144. */
  145. public function getConfigurationForRoleInstance($roleInstance = null)
  146. {
  147. if (is_null($roleInstance)) {
  148. 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.');
  149. }
  150. if ($this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance)) {
  151. $configurationInstance = new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
  152. $configurationInstance->loadXml( $this->_blobStorageClient->getBlobData($this->_controlContainer, $roleInstance) );
  153. return $configurationInstance;
  154. }
  155. return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
  156. }
  157. /**
  158. * Set configuration for a specific role instance
  159. *
  160. * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
  161. * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
  162. * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
  163. */
  164. public function setConfigurationForRoleInstance($roleInstance = null, Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
  165. {
  166. if (is_null($roleInstance)) {
  167. 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.');
  168. }
  169. $this->_blobStorageClient->putBlobData($this->_controlContainer, $roleInstance, $configuration->toXml(), array(), null, array('Content-Type' => 'text/xml'));
  170. }
  171. }